3 var num_to_create = 400000;
5 var net = require('net');
12 function createentry(i) {
20 //console.log("var x is "+x);
22 //console.log("i now: "+i+" i>>8 "+i+" i>>16 "+(i>>16));
29 //console.log("created "+a+"."+b+"."+c+" from "+i);
33 function createaspath(i) {
36 var ret = new Array();
38 for(var t=0; t<n; t++) {
40 as = 1024 + (i%30000);
46 console.log("startup....");
50 var data = new Array();
52 console.log("start construction");
53 for(var t=0; t<num_to_create; t++) {
54 var thisdata = new Array();
55 thisdata[0] = createentry(t);
56 //console.log("create entry from "+thisdata[0]+" with " + t);
57 thisdata[1] = createaspath(t);
58 // we construct the update messages while we do this
61 thisdata[2] = constructUpdateMessage(thisdata);
65 console.log("finish construction");
68 //console.log("data: " + data.toString());
69 //console.log("Done!: " + data.length);
71 function parseBuffer(b, c) {
72 var len = b.readUInt16BE(16);
73 var type = b.readUInt8(18);
75 console.log("got input: " + len + ", type: " + type);
78 var vers = b.readUInt8(19);
79 var as = b.readUInt16BE(20);
80 var ht = b.readUInt16BE(22);
81 var ot1 = b.readUInt8(24);
82 var ot2 = b.readUInt8(25);
83 var ot3 = b.readUInt8(26);
84 var ot4 = b.readUInt8(27);
85 var opl = b.readUInt8(28);
86 console.log("got open type, vers: "+vers+", as: " + as);
87 console.log("ht: " + ht + ", id: "+ot1+"."+ot2+"."+ot3+"."+ot4+", opl: "+opl);
90 console.log("sending our open type");
91 var out = new Buffer(29);
94 out.fill(0xff, 0, 16);
95 out.writeUInt16BE(29, 16);
96 out.writeUInt8(1, 18);
97 out.writeUInt8(4, 19);
98 out.writeUInt16BE(myas, 20);
99 out.writeUInt16BE(90, 22);
100 out.writeUInt8(10, 24);
101 out.writeUInt8(99, 25);
102 out.writeUInt8(99, 26);
103 out.writeUInt8(1,27);
104 out.writeUInt8(0,28);
107 } else if(type == 4) {
108 console.log("writing keepalive - exact as sent");
109 if(updateSent ==0) beginUpdateSend(c);
111 } else if(type == 2) {
112 console.log("got update...");
113 if(updateSent ==0) beginUpdateSend(c);
115 console.log("sending end...");
122 // this function gets prefix t from data[] and then
123 // creates an update message for it
124 function constructUpdateMessage(localdata) {
125 //console.log("Construction update for "+t);
128 //console.log("localdata0: " + localdata[0]);
129 //console.log("localdata1: " + localdata[1]);
130 //console.log("localdata0 - : " + typeof localdata[1]);
131 //console.log("localdata1 - : " + typeof localdata[1]);
133 // first the header components
136 // next the length component
139 // next the n unfeasible
142 // next, path attr length
146 // now we begin the path attrs
147 // first origin - simple
150 // next as path - hard, flag + type + len + aspath segment
153 // as path segment size = 1 (type), + 1 (len) + as's*2
154 var aspathlen = ((localdata[1].length+1)*2)+1+1;
155 aspathn += aspathlen;
157 // now next hop attrs = flag (1) + type (1) + len (1) + octets (4);
161 // now nlri = prefix len (1) + prefix fixed in our case (3)
167 //console.log("size: " + bsize + ", an: " + aspathn + " al:" + aspathlen);
168 var buf = new Buffer(bsize);
171 // now lets create the buffer
172 buf.fill(0xff, bp, bp+16);
174 buf.writeUInt16BE(bsize, bp);
176 buf.writeUInt8(2, bp);
178 buf.writeUInt16BE(0, bp);
180 buf.writeUInt16BE(aspathn, bp);
185 buf.writeUInt8(0x40, bp);
187 buf.writeUInt8(1, bp);
189 buf.writeUInt8(1, bp);
191 buf.writeUInt8(0, bp);
195 buf.writeUInt8(0x40, bp);
197 buf.writeUInt8(2, bp);
199 buf.writeUInt8(aspathlen, bp);
201 buf.writeUInt8(2, bp);
203 buf.writeUInt8(localdata[1].length+1, bp);
205 //console.log("writing in my aspath: "+myas);
206 buf.writeUInt16BE(myas, bp);
208 localdata[1].forEach(function (ed) {
209 //console.log("writing in aspath: "+ed);
210 buf.writeUInt16BE(ed, bp);
215 buf.writeUInt8(0x40, bp);
217 buf.writeUInt8(3, bp);
219 buf.writeUInt8(4, bp);
221 myip.split(".").forEach(function (ed) {
222 //console.log("writing in next hop info: " + ed);
223 buf.writeUInt8(parseInt(ed), bp);
228 buf.writeUInt8(24, bp);
230 localdata[0].split(".").forEach(function(ed){
231 //console.log("Writing in nlri: "+ed);
232 buf.writeUInt8(parseInt(ed), bp);
239 // start sending updates messages
240 function beginUpdateSend(c) {
243 data.forEach(function(led) {
247 console.log("finished publishing - "+n);
250 function serverconnection(c) {
254 c.on("end", function() {
255 console.log("Server disconnected");
258 c.on("data", function(buffer) {
259 parseBuffer(buffer, c);
262 console.log("Service connected from: " + c.remoteAddress);
264 //c.write("hello\r\n");
267 console.log("Prefixes created, starting server");
269 var server = net.createServer(serverconnection);
271 server.listen(179, function() {
272 console.log("Server bound");