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