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