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