-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.cpp
More file actions
435 lines (388 loc) · 11.4 KB
/
Message.cpp
File metadata and controls
435 lines (388 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <sstream>
#include <iostream>
#include <string>
#include <memory>
#include <string.h>
#include <boost/lexical_cast.hpp>
#include "Socket.h"
#include "Exceptions.h"
#include "Message.h"
#include "usage.h"
using namespace std;
namespace cs6390
{
//test case to ensure we don't receive an invalid message type
int Message::test_catch(short port)
{
Socket sock("localhost", port);
sock.sendToSocket("XXXX"); //can't instantiate Message...
Message *m = sock.getMessage();
if(m != NULL) {
cerr << "getMessage() should have failed" << endl;
return -1;
}
return 0;
}
//basic test case used by all classes
// receives a message then sends it to cout
int Message::test(Socket &sock)
{
Message *m = sock.getMessage();
if(m != NULL) {
cout << m; //should throw() if not a child class...
}
else {
cerr << "getMessage() failed" << endl;
return -1;
}
return 0;
}
//uses the child class's serialize() funtion to output a human readble
//representations of the message
ostream& operator<< (ostream& out, const Message *m)
{
out << m->serialize(true);
return out;
}
LSA::LSA()
: Message("LSA"), AS(99), routerID(99), neighborAS(99), neighborRouterID(99) { }
//LSA contructor, only to be used by the main() for router.cpp
//most of the command line parsing for router.cpp happens in this function
LSA::LSA(int argc, char ** argv)
: Message("LSA")
{
try { AS = boost::lexical_cast<uint32_t>(argv[1]); }
catch ( ... ) { router_usage("First argument must be an integer"); }
if(AS < 0 || AS > 9)
router_usage("AS # must be between 0 and 9 and must match a configured AS");
try { routerID = boost::lexical_cast<uint32_t>(argv[2]); }
catch ( ... ) { router_usage("Second argument must be an integer"); }
if(routerID < 0 || routerID > 9)
router_usage("routerID # must be between 0 and 9");
try { neighborAS = boost::lexical_cast<uint32_t>(argv[4]); }
catch ( ... ) { router_usage("Fourth argument must be an integer"); }
try { neighborRouterID = boost::lexical_cast<uint32_t>(argv[5]); }
catch ( ... ) { router_usage("Fifth argument must be an integer"); }
for(int32_t i=6; i < argc; i ++)
{
uint32_t tmp;
try { tmp = boost::lexical_cast<uint32_t>(argv[i]); }
catch ( ... ) { router_usage("net arguments must be integer"); }
if(tmp > 99)
router_usage("networks must be less than 99");
if( linkStates.find(tmp) != linkStates.end() )
router_usage("Don't specify duplicate networks");
linkStates[tmp]=1;
}
}
//add a link to linkStates
int LSA::addLink(uint32_t net, uint32_t metric)
{
if ( linkStates.find(net) != linkStates.end() )
return 1;
linkStates[net]=metric;
return 0;
}
//set link state of a single link
int LSA::setLinkMetric(uint32_t net, uint32_t metric)
{
LinkMap::iterator it = linkStates.find(net);
if ( it == linkStates.end())
return 1;
it->second=metric;
return 0;
}
//set link states of all links
int LSA::setLinkMetric(uint32_t metric)
{
for(LinkMap::iterator it = linkStates.begin(); it != linkStates.end(); it++)
it->second=metric;
return 0;
}
//constructor from a string of vectors
LSA::LSA(vector<string> &v)
{
if(v.size() < 4) //check for number of parameters
THROW_DES("too few parameters for LSA");
else if(v.size() % 2 != 0) //LSA will always have odd number of members
THROW_DES("odd number of parameters");
type = v[0];
if(type != "LSA") //check type
THROW_DES("wrong message type");
//convert numeric types
routerID = boost::lexical_cast<uint32_t>(v[1]);
neighborAS = boost::lexical_cast<uint32_t>(v[2]);
neighborRouterID = boost::lexical_cast<uint32_t>(v[3]);
for(uint32_t i=4; i < v.size(); i+=2)
{
linkStates[boost::lexical_cast<uint32_t>(v[i])] = boost::lexical_cast<uint32_t>(v[i+1]);
}
}
//serialize LSA (either for sending or in human readable form)
string LSA::serialize(bool readable) const
{
stringstream ss;
ss << type << " ";
if(readable) ss << endl << " routerID: ";
ss << routerID << " ";
if(readable) ss << endl << " neighborAS: ";
ss << neighborAS << " ";
if(readable) ss << endl << " neighborRouterID: ";
ss << neighborRouterID;
if(readable) ss << endl << " linkStates: " << endl;
for(LinkMap::const_iterator it = linkStates.begin(); it != linkStates.end(); it++) {
if(readable) ss << "net: ";
ss << " " << it->first << " ";
if(readable) ss << "(";
ss << it-> second;
if(readable) ss << ")" << endl;
}
return ss.str();
}
//used in Message_test.cpp
// sends a simple LSA then receives it back
// requires a simple echo server listening on <port>
int LSA::test(short port)
{
Socket sock("localhost", port);
{
//use default constructor
LSA out;
//add a couple of links
out.addLink(1,1);
out.addLink(5,1);
out.addLink(9,99);
//send the message
sock.sendMessage(out);
}
return Message::test(sock);
}
//every other message type has similar member functions...
//not going to comment each one...
RREQ::RREQ(vector<string> &v)
{
if(v.size() != 3)
THROW_DES("wrong number of parameters for RREQ");
type = v[0];
source = boost::lexical_cast<uint32_t>(v[1]);
dest = boost::lexical_cast<uint32_t>(v[2]);
}
string RREQ::serialize(bool readable) const
{
ostringstream oss;
oss << type << " ";
if(readable) oss << "source: ";
oss << source << " ";
if(readable) oss << "dest: ";
oss << dest;
if(readable) oss << endl;
return oss.str();
}
int RREQ::test(short port)
{
Socket sock("localhost", port);
{
RREQ out(1, 2);
sock.sendMessage(out);
}
return Message::test(sock);
}
BGP::BGP(vector<string> &v)
{
if(v.size() < 3)
THROW_DES("too few parameters for BGP");
type = v[0];
AS = boost::lexical_cast<uint32_t>(v[1]);
AS_hops = boost::lexical_cast<uint32_t>(v[2]);
for(uint32_t i=3; i < v.size(); i++)
nets.push_back( boost::lexical_cast<uint32_t>(v[i]) );
}
string BGP::serialize(bool readable) const
{
ostringstream oss;
oss << type << " ";
if(readable) oss << "AS: ";
oss << AS << " ";
if(readable) oss << "AS_hops: ";
oss << AS_hops;
if(readable) oss << endl << "networks: ";
for(vector<uint32_t>::const_iterator it = nets.begin(); it != nets.end(); it++)
oss << " " << *it;
if(readable) oss <<endl;
return oss.str();
}
int BGP::test(short port)
{
Socket sock("localhost", port);
{
BGP out;
out.nets.push_back(1);
out.nets.push_back(2);
out.nets.push_back(3);
sock.sendMessage(out);
}
return Message::test(sock);
}
RRES::RRES(vector<string> &v)
{
if(v.size() < 2)
THROW_DES("too few parameters for RRES");
type = v[0];
for(uint32_t i=1; i < v.size(); i++)
routers.push_back( boost::lexical_cast<uint32_t>(v[i]) );
}
string RRES::serialize(bool readable) const
{
ostringstream oss;
oss << type;
if(readable) oss << " routers:";
for(vector<uint32_t>::const_iterator it = routers.begin(); it != routers.end(); it++)
oss << " " << *it;
if(readable) oss << endl;
return oss.str();
}
int RRES::test(short port)
{
Socket sock("localhost", port);
{
RRES out;
out.routers.push_back(1);
out.routers.push_back(2);
out.routers.push_back(3);
sock.sendMessage(out);
}
return Message::test(sock);
}
IRRQ::IRRQ(vector<string> &v)
{
if(v.size() != 3)
THROW_DES("too few parameters for IRRQ");
type = v[0];
AS = boost::lexical_cast<uint32_t>(v[1]);
dest_net = boost::lexical_cast<uint32_t>(v[2]);
}
string IRRQ::serialize(bool readable) const
{
ostringstream oss;
oss << type << " ";
if(readable)
oss << "AS: ";
oss << AS << " ";
if(readable)
oss << "dest_net: ";
oss << dest_net;
if(readable)
oss << endl;
return oss.str();
}
int IRRQ::test(short port)
{
Socket sock("localhost", port);
{
IRRQ out(1, 77);
sock.sendMessage(out);
}
return Message::test(sock);
}
IRRS::IRRS(vector<string> &v)
{
if( v.size() < 3)
THROW_DES("too few parameters for IRRS");
type = v[0];
if(v[v.size() -1] == "BLK") {
blank=true;
for(vector<string>::const_iterator it = v.begin() +1 ; *it != "BLK" ; it++) {
if(it->substr(0,2) != "AS")
THROW_DES(string("WTF") + *it);
ASlist.push_back(
make_pair(
boost::lexical_cast<uint32_t>(it->substr(2, string::npos)),
vector<uint32_t>()
) );
}
}
else {
blank=false;
vector<string>::const_iterator it1,it2;
for(it1=v.begin() + 1; it1 != v.end(); it1++)
{
if(it1->substr(0,2) != "AS")
THROW_DES(string("WTF") + *it1);
uint32_t as=boost::lexical_cast<uint32_t>(it1->substr(2, string::npos));
vector<uint32_t> intv;
for(it2=it1 + 1; it2 != v.end(); it2++) {
intv.push_back( boost::lexical_cast<uint32_t>(*it2) );
if( (it2 + 1) == v.end() || (it2+1)->substr(0,2) == "AS")
break;
}
ASlist.push_back( make_pair(as, intv) );
it1=it2;
}
}
}
string IRRS::serialize(bool readable) const
{
ostringstream oss;
oss << type;
if(readable)
oss << endl;
for(list<ASroute>::const_iterator it = ASlist.begin(); it != ASlist.end(); it++ )
{
oss << " " << "AS" << it->first;
if(!blank) {
if(readable)
oss << " routers:";
for(vector<uint32_t>::const_iterator vit = it->second.begin(); vit != it->second.end(); vit++)
oss << " " << *vit;
}
if(readable)
oss << endl;
}
if(blank) oss << " BLK";
if(readable) oss << endl;
return oss.str();
}
int IRRS::test_route(short port)
{
Socket sock("localhost", port);
{
IRRS out;
vector<uint32_t> v;
v.push_back(1); v.push_back(2); v.push_back(3);
out.ASlist.push_back( make_pair( 1, v) );
v.clear(); v.push_back(4); v.push_back(5); v.push_back(6);
out.ASlist.push_back( make_pair( 5, v) );
v.clear(); v.push_back(7); v.push_back(8); v.push_back(9);
out.ASlist.push_back( make_pair( 9, v) );
out.blank=false;
sock.sendMessage(out);
}
return Message::test(sock);
}
int IRRS::test_blk(short port)
{
Socket sock("localhost", port);
{
IRRS out;
out.ASlist.push_back( make_pair( 1, vector<uint32_t>() ) );
out.ASlist.push_back( make_pair( 5, vector<uint32_t>() ) );
out.ASlist.push_back( make_pair( 9, vector<uint32_t>() ) );
out.blank=true;
sock.sendMessage(out);
}
return Message::test(sock);
}
int IRRS::test(short port)
{
int retval=0;
if(test_route(port) != 0) {
cerr << "route test failed" << endl;
retval=1;
}
if(test_blk(port) != 0) {
cerr << "BLK test failed" << endl;
retval=1;
}
return retval;
}
} //cs6390