4 var net = require('net');
9 function createentry(i) {
17 //console.log("var x is "+x);
19 //console.log("i now: "+i+" i>>8 "+i+" i>>16 "+(i>>16));
26 //console.log("created "+a+"."+b+"."+c+" from "+i);
30 function createaspath(i) {
33 var ret = new Array();
35 for(var t=0; t<n; t++) {
37 as = 1024 + (i%30000);
43 console.log("startup....");
46 var num_to_create = 2000;
48 var data = new Array();
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
59 thisdata[2] = constructUpdateMessage(thisdata);
63 console.log("finish construction");
66 //console.log("data: " + data.toString());
67 //console.log("Done!: " + data.length);
69 function parseBuffer(b, c) {
70 var len = b.readUInt16BE(16);
71 var type = b.readUInt8(18);
73 console.log("got input: " + len + ", type: " + type);
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);
88 console.log("sending our open type");
89 var out = new Buffer(29);
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);
105 } else if(type == 4) {
106 console.log("writing keepalive - exact as sent");
108 } else if(type == 2) {
109 console.log("got update...");
112 console.log("sending end...");
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);
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]);
130 // first the header components
133 // next the length component
136 // next the n unfeasible
139 // next, path attr length
143 // now we begin the path attrs
144 // first origin - simple
147 // next as path - hard, flag + type + len + aspath segment
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;
154 // now next hop attrs = flag (1) + type (1) + len (1) + octets (4);
158 // now nlri = prefix len (1) + prefix fixed in our case (3)
164 //console.log("size: " + bsize + ", an: " + aspathn + " al:" + aspathlen);
165 var buf = new Buffer(bsize);
168 // now lets create the buffer
169 buf.fill(0xff, bp, bp+16);
171 buf.writeUInt16BE(bsize, bp);
173 buf.writeUInt8(2, bp);
175 buf.writeUInt16BE(0, bp);
177 buf.writeUInt16BE(aspathn, bp);
182 buf.writeUInt8(0x40, bp);
184 buf.writeUInt8(1, bp);
186 buf.writeUInt8(1, bp);
188 buf.writeUInt8(0, bp);
192 buf.writeUInt8(0x40, bp);
194 buf.writeUInt8(2, bp);
196 buf.writeUInt8(aspathlen, bp);
198 buf.writeUInt8(2, bp);
200 buf.writeUInt8(localdata[1].length+1, bp);
202 //console.log("writing in my aspath: "+myas);
203 buf.writeUInt16BE(myas, bp);
205 localdata[1].forEach(function (ed) {
206 //console.log("writing in aspath: "+ed);
207 buf.writeUInt16BE(ed, bp);
212 buf.writeUInt8(0x40, bp);
214 buf.writeUInt8(3, bp);
216 buf.writeUInt8(4, bp);
218 myip.split(".").forEach(function (ed) {
219 //console.log("writing in next hop info: " + ed);
220 buf.writeUInt8(parseInt(ed), bp);
225 buf.writeUInt8(24, bp);
227 localdata[0].split(".").forEach(function(ed){
228 //console.log("Writing in nlri: "+ed);
229 buf.writeUInt8(parseInt(ed), bp);
236 // start sending updates messages
237 function beginUpdateSend(c) {
239 data.forEach(function(led) {
243 console.log("finished publishing - "+n);
246 function serverconnection(c) {
250 c.on("end", function() {
251 console.log("Server disconnected");
254 c.on("data", function(buffer) {
255 parseBuffer(buffer, c);
258 console.log("Service connected from: " + c.remoteAddress);
260 //c.write("hello\r\n");
263 console.log("Prefixes created, starting server");
265 var server = net.createServer(serverconnection);
267 server.listen(179, function() {
268 console.log("Server bound");