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