bgpfake updates
[random_node_code.git] / bgpfake / bf.js
1 var myas=1234;
2 var myip="10.99.99.1";
3 var num_to_create = 400000;
4
5 var net = require('net');
6
7 var updateSent = 0;
8
9 var scon;
10
11
12 function createentry(i) {
13         // split into octets
14         var a = 0;
15         var b = 0;
16         var c = 0;
17         
18         var x = 45<<16;
19
20         //console.log("var x is "+x);
21         i = i+x;
22         //console.log("i now: "+i+" i>>8 "+i+" i>>16 "+(i>>16));
23         
24         c = i&255;
25         b = (i>>8)&255;
26         a = (i>>16)&255;
27         
28         
29         //console.log("created "+a+"."+b+"."+c+" from "+i);
30         return a+"."+b+"."+c;
31 }
32
33 function createaspath(i) {
34         var n=(i%5)+2;
35         var as = 1024;
36         var ret = new Array();
37
38         for(var t=0; t<n; t++) {
39                 i = i << 1;
40                 as = 1024 + (i%30000);
41                 ret[t] = as;
42         }
43         return ret;
44 }
45
46 console.log("startup....");
47
48
49
50 var data = new Array();
51
52 console.log("start construction");
53 for(var t=0; t<num_to_create; t++) {
54         var thisdata = new Array();
55         thisdata[0] = createentry(t);
56         //console.log("create entry from "+thisdata[0]+" with " + t);
57         thisdata[1] = createaspath(t);
58         // we construct the update messages while we do this
59
60         // ok, that was dumb
61         thisdata[2] = constructUpdateMessage(thisdata);
62         data[t] = thisdata;
63
64 }
65 console.log("finish construction");
66
67
68 //console.log("data: " + data.toString());
69 //console.log("Done!: " + data.length);
70
71 function parseBuffer(b, c) {
72         var len = b.readUInt16BE(16);
73         var type = b.readUInt8(18);
74
75         console.log("got input: " + len + ", type: " + type);
76
77         if(type == 1) {
78                 var vers = b.readUInt8(19);
79                 var as = b.readUInt16BE(20);
80                 var ht = b.readUInt16BE(22);
81                 var ot1 = b.readUInt8(24);
82                 var ot2 = b.readUInt8(25);
83                 var ot3 = b.readUInt8(26);
84                 var ot4 = b.readUInt8(27);
85                 var opl = b.readUInt8(28);
86                 console.log("got open type, vers: "+vers+", as: " + as);
87                 console.log("ht: " + ht + ", id: "+ot1+"."+ot2+"."+ot3+"."+ot4+", opl: "+opl);
88
89
90                 console.log("sending our open type");
91                 var out = new Buffer(29);
92
93
94                 out.fill(0xff, 0, 16);
95                 out.writeUInt16BE(29, 16);
96                 out.writeUInt8(1, 18);
97                 out.writeUInt8(4, 19);
98                 out.writeUInt16BE(myas, 20);
99                 out.writeUInt16BE(90, 22);
100                 out.writeUInt8(10, 24);
101                 out.writeUInt8(99, 25);
102                 out.writeUInt8(99, 26);
103                 out.writeUInt8(1,27);
104                 out.writeUInt8(0,28);
105
106                 c.write(out);
107         } else if(type == 4) {
108                 console.log("writing keepalive - exact as sent");
109                 if(updateSent ==0) beginUpdateSend(c);
110                 c.write(b);
111         } else if(type == 2) {
112                 console.log("got update...");
113                 if(updateSent ==0) beginUpdateSend(c);
114         } else {
115                 console.log("sending end...");
116                 c.end();
117         }
118
119         
120 }
121
122 // this function gets prefix t from data[] and then
123 // creates an update message for it
124 function constructUpdateMessage(localdata) {
125         //console.log("Construction update for "+t);
126         var bsize = 0;
127
128         //console.log("localdata0: " + localdata[0]);
129         //console.log("localdata1: " + localdata[1]);
130         //console.log("localdata0 - : " + typeof localdata[1]);
131         //console.log("localdata1 - : " + typeof localdata[1]);
132
133         // first the header components
134         bsize += 16;
135
136         // next the length component
137         bsize += 2;
138
139         // next the n unfeasible
140         bsize += 2;
141
142         // next, path attr length
143         bsize += 2;
144
145
146         // now we begin the path attrs
147         // first origin - simple
148         var aspathn = 4;
149
150         // next as path - hard, flag + type + len + aspath segment
151         aspathn += 3;
152
153         // as path segment size = 1 (type), + 1 (len) + as's*2
154         var aspathlen = ((localdata[1].length+1)*2)+1+1;
155         aspathn += aspathlen;
156         
157         // now next hop attrs = flag (1) + type (1) + len (1) + octets (4);
158         aspathn += 7;
159         bsize += aspathn;
160
161         // now nlri = prefix len (1) + prefix fixed in our case (3)
162         bsize += 4;
163
164         // fudge
165         bsize+=1;
166
167         //console.log("size: " + bsize + ", an: " + aspathn + " al:" + aspathlen);
168         var buf = new Buffer(bsize);
169         var bp = 0;
170
171         // now lets create the buffer
172         buf.fill(0xff, bp, bp+16);
173         bp+=16;
174         buf.writeUInt16BE(bsize, bp);
175         bp+=2;
176         buf.writeUInt8(2, bp);
177         bp++;
178         buf.writeUInt16BE(0, bp);
179         bp+=2;
180         buf.writeUInt16BE(aspathn, bp);
181         bp+=2;
182
183         // path attr
184         // origin
185         buf.writeUInt8(0x40, bp);
186         bp++;
187         buf.writeUInt8(1, bp);
188         bp++;
189         buf.writeUInt8(1, bp);
190         bp++;
191         buf.writeUInt8(0, bp);
192         bp++;
193
194         // as path
195         buf.writeUInt8(0x40, bp);
196         bp++;
197         buf.writeUInt8(2, bp);
198         bp++;
199         buf.writeUInt8(aspathlen, bp);
200         bp++;
201         buf.writeUInt8(2, bp);
202         bp++;
203         buf.writeUInt8(localdata[1].length+1, bp);
204         bp++;
205         //console.log("writing in my aspath: "+myas);
206         buf.writeUInt16BE(myas, bp);
207         bp+=2;
208         localdata[1].forEach(function (ed) {
209                 //console.log("writing in aspath: "+ed);
210                 buf.writeUInt16BE(ed, bp);
211                 bp+=2;
212         });
213
214         // next hop
215         buf.writeUInt8(0x40, bp);
216         bp++;
217         buf.writeUInt8(3, bp);
218         bp++;
219         buf.writeUInt8(4, bp);
220         bp++;
221         myip.split(".").forEach(function (ed) {
222                 //console.log("writing in next hop info: " + ed);
223                 buf.writeUInt8(parseInt(ed), bp);
224                 bp++;
225         }); 
226
227         // last, nlri
228         buf.writeUInt8(24, bp);
229         bp++;
230         localdata[0].split(".").forEach(function(ed){
231                 //console.log("Writing in nlri: "+ed);
232                 buf.writeUInt8(parseInt(ed), bp);
233                 bp++;
234         });
235
236         return buf;
237 }
238
239 // start sending updates messages
240 function beginUpdateSend(c) {
241         updateSent = 1;
242         var n = 0;
243         data.forEach(function(led) {
244                 c.write(led[2]);
245                 n++;
246         });
247         console.log("finished publishing - "+n);
248 }
249
250 function serverconnection(c) {
251
252         scon = c;
253
254         c.on("end", function() {
255                 console.log("Server disconnected");
256         });
257
258         c.on("data", function(buffer) {
259                 parseBuffer(buffer, c);
260         });
261
262         console.log("Service connected from: " + c.remoteAddress);
263
264         //c.write("hello\r\n");
265 }
266
267 console.log("Prefixes created, starting server");
268
269 var server = net.createServer(serverconnection);
270
271 server.listen(179, function() {
272         console.log("Server bound");
273 });