3 var number_of_route_adverts = 6;
4 var routes_per_advert = 4;
6 var net = require('net');
13 function createentry(i) {
21 //console.log("var x is "+x);
23 //console.log("i now: "+i+" i>>8 "+i+" i>>16 "+(i>>16));
30 //console.log("created "+a+"."+b+"."+c+" from "+i);
34 function createaspath(i) {
37 var ret = new Array();
39 for(var t=0; t<n; t++) {
41 as = 1024 + (i%30000);
47 console.log("startup....");
51 var data = new Array();
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
65 if(b>0) thisdata[2] += constructUpdateMessage(thisdata, true, routes_per_advert);
66 else thisdata[2] = constructUpdateMessage(thisdata, false, routes_per_advert);
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());
75 console.log("finish construction");
78 //console.log("data: " + data.toString());
80 //console.log("Done!: " + data.length);
82 function parseBuffer(b, c) {
83 var len = b.readUInt16BE(16);
84 var type = b.readUInt8(18);
86 //console.log("got input: " + len + ", type: " + type);
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);
101 console.log("sending our open type");
102 var out = new Buffer(29);
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);
118 } else if(type == 4) {
119 console.log("writing keepalive - exact as sent");
121 //if(updateSent ==0) beginUpdateSend(c);
122 } else if(type == 2) {
123 console.log("got update...");
124 if(updateSent ==0) beginUpdateSend(c);
126 console.log("sending end...");
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);
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]);
144 // first the header components
147 // next the length component
150 // next the n unfeasible
153 // next, path attr length
157 // now we begin the path attrs
158 // first origin - simple
161 // next as path - hard, flag + type + len + aspath segment
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;
168 // now next hop attrs = flag (1) + type (1) + len (1) + octets (4);
172 // now nlri = prefix len (1) + prefix fixed in our case (3)
178 // now figure out t_bsize;
179 var t_bsize = bsize + (4*(n_up-1));
181 //console.log("size: " + bsize + ", an: " + aspathn + " al:" + aspathlen);
182 var buf = new Buffer(bsize);
185 // now lets create the buffer
186 buf.fill(0xff, bp, bp+16);
188 buf.writeUInt16BE(t_bsize, bp);
190 buf.writeUInt8(2, bp);
192 buf.writeUInt16BE(0, bp);
194 buf.writeUInt16BE(aspathn, bp);
199 buf.writeUInt8(0x40, bp);
201 buf.writeUInt8(1, bp);
203 buf.writeUInt8(1, bp);
205 buf.writeUInt8(0, bp);
209 buf.writeUInt8(0x40, bp);
211 buf.writeUInt8(2, bp);
213 buf.writeUInt8(aspathlen, bp);
215 buf.writeUInt8(2, bp);
217 buf.writeUInt8(localdata[1].length+1, bp);
219 //console.log("writing in my aspath: "+myas);
220 buf.writeUInt16BE(myas, bp);
222 localdata[1].forEach(function (ed) {
223 //console.log("writing in aspath: "+ed);
224 buf.writeUInt16BE(ed, bp);
229 buf.writeUInt8(0x40, bp);
231 buf.writeUInt8(3, bp);
233 buf.writeUInt8(4, bp);
235 myip.split(".").forEach(function (ed) {
236 //console.log("writing in next hop info: " + ed);
237 buf.writeUInt8(parseInt(ed), bp);
243 console.log("constructing new buffer for only nlri");
247 buf.writeUInt8(24, bp);
249 localdata[0].split(".").forEach(function(ed){
250 console.log("Writing in nlri: "+ed);
251 buf.writeUInt8(parseInt(ed), bp);
255 console.log("buf is:");
257 console.log(buf.length);
262 // start sending updates messages
263 function beginUpdateSend(c) {
266 data.forEach(function(led) {
270 console.log("finished publishing - "+n);
273 function serverconnection(c) {
277 c.on("end", function() {
278 console.log("Server disconnected");
281 c.on("data", function(buffer) {
282 parseBuffer(buffer, c);
285 console.log("Service connected from: " + c.remoteAddress);
287 //c.write("hello\r\n");
290 console.log("Prefixes created, starting server");
292 var server = net.createServer(serverconnection);
294 server.listen(179, function() {
295 console.log("Server bound");