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));
29 //console.log("i is now: "+i);
31 // if we're above 256....
34 //console.log("i is now: "+i);
38 // if we're still above 256....
41 //console.log("i is now: "+i);
46 //console.log("created "+a+"."+b+"."+c+" from "+i);
50 function createaspath(i) {
53 var ret = new Array();
55 for(var t=0; t<n; t++) {
57 as = 1024 + (i%30000);
63 console.log("startup....");
66 var num_to_create = 2000;
68 var data = new Array();
70 console.log("start construction");
71 for(var t=0; t<num_to_create; t++) {
72 var thisdata = new Array();
73 thisdata[0] = createentry(t);
74 //console.log("create entry from "+thisdata[0]+" with " + t);
75 thisdata[1] = createaspath(t);
76 // we construct the update messages while we do this
79 thisdata[2] = constructUpdateMessage(thisdata);
83 console.log("finish construction");
86 //console.log("data: " + data.toString());
87 //console.log("Done!: " + data.length);
89 function parseBuffer(b, c) {
90 var len = b.readUInt16BE(16);
91 var type = b.readUInt8(18);
93 console.log("got input: " + len + ", type: " + type);
96 var vers = b.readUInt8(19);
97 var as = b.readUInt16BE(20);
98 var ht = b.readUInt16BE(22);
99 var ot1 = b.readUInt8(24);
100 var ot2 = b.readUInt8(25);
101 var ot3 = b.readUInt8(26);
102 var ot4 = b.readUInt8(27);
103 var opl = b.readUInt8(28);
104 console.log("got open type, vers: "+vers+", as: " + as);
105 console.log("ht: " + ht + ", id: "+ot1+"."+ot2+"."+ot3+"."+ot4+", opl: "+opl);
108 console.log("sending our open type");
109 var out = new Buffer(29);
112 out.fill(0xff, 0, 16);
113 out.writeUInt16BE(29, 16);
114 out.writeUInt8(1, 18);
115 out.writeUInt8(4, 19);
116 out.writeUInt16BE(myas, 20);
117 out.writeUInt16BE(90, 22);
118 out.writeUInt8(10, 24);
119 out.writeUInt8(99, 25);
120 out.writeUInt8(99, 26);
121 out.writeUInt8(1,27);
122 out.writeUInt8(0,28);
125 } else if(type == 4) {
126 console.log("writing keepalive - exact as sent");
128 } else if(type == 2) {
129 console.log("got update...");
132 console.log("sending end...");
139 // this function gets prefix t from data[] and then
140 // creates an update message for it
141 function constructUpdateMessage(localdata) {
142 //console.log("Construction update for "+t);
145 //console.log("localdata0: " + localdata[0]);
146 //console.log("localdata1: " + localdata[1]);
147 //console.log("localdata0 - : " + typeof localdata[1]);
148 //console.log("localdata1 - : " + typeof localdata[1]);
150 // first the header components
153 // next the length component
156 // next the n unfeasible
159 // next, path attr length
163 // now we begin the path attrs
164 // first origin - simple
167 // next as path - hard, flag + type + len + aspath segment
170 // as path segment size = 1 (type), + 1 (len) + as's*2
171 var aspathlen = ((localdata[1].length+1)*2)+1+1;
172 aspathn += aspathlen;
174 // now next hop attrs = flag (1) + type (1) + len (1) + octets (4);
178 // now nlri = prefix len (1) + prefix fixed in our case (3)
184 //console.log("size: " + bsize + ", an: " + aspathn + " al:" + aspathlen);
185 var buf = new Buffer(bsize);
188 // now lets create the buffer
189 buf.fill(0xff, bp, bp+16);
191 buf.writeUInt16BE(bsize, bp);
193 buf.writeUInt8(2, bp);
195 buf.writeUInt16BE(0, bp);
197 buf.writeUInt16BE(aspathn, bp);
202 buf.writeUInt8(0x40, bp);
204 buf.writeUInt8(1, bp);
206 buf.writeUInt8(1, bp);
208 buf.writeUInt8(0, bp);
212 buf.writeUInt8(0x40, bp);
214 buf.writeUInt8(2, bp);
216 buf.writeUInt8(aspathlen, bp);
218 buf.writeUInt8(2, bp);
220 buf.writeUInt8(localdata[1].length+1, bp);
222 //console.log("writing in my aspath: "+myas);
223 buf.writeUInt16BE(myas, bp);
225 localdata[1].forEach(function (ed) {
226 //console.log("writing in aspath: "+ed);
227 buf.writeUInt16BE(ed, bp);
232 buf.writeUInt8(0x40, bp);
234 buf.writeUInt8(3, bp);
236 buf.writeUInt8(4, bp);
238 myip.split(".").forEach(function (ed) {
239 //console.log("writing in next hop info: " + ed);
240 buf.writeUInt8(parseInt(ed), bp);
245 buf.writeUInt8(24, bp);
247 localdata[0].split(".").forEach(function(ed){
248 //console.log("Writing in nlri: "+ed);
249 buf.writeUInt8(parseInt(ed), bp);
256 // start sending updates messages
257 function beginUpdateSend(c) {
259 data.forEach(function(led) {
263 console.log("finished publishing - "+n);
266 function serverconnection(c) {
270 c.on("end", function() {
271 console.log("Server disconnected");
274 c.on("data", function(buffer) {
275 parseBuffer(buffer, c);
278 console.log("Service connected from: " + c.remoteAddress);
280 //c.write("hello\r\n");
283 console.log("Prefixes created, starting server");
285 var server = net.createServer(serverconnection);
287 server.listen(179, function() {
288 console.log("Server bound");