-
Notifications
You must be signed in to change notification settings - Fork 74
/
peer.cc
2017 lines (1787 loc) · 52.4 KB
/
peer.cc
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
// Copyright (c) 2001-2009 XORP, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2, June
// 1991 as published by the Free Software Foundation. Redistribution
// and/or modification of this program under the terms of any other
// version of the GNU General Public License is not permitted.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU General Public License, Version 2, a copy of which can be
// found in the XORP LICENSE.gpl file.
//
// XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net
// #define DEBUG_LOGGING
// #define DEBUG_PRINT_FUNCTION_NAME
#include "bgp/bgp_module.h"
#include "libxorp/debug.h"
#include "libxorp/xlog.h"
#include "libxorp/eventloop.hh"
#include "libxorp/ipv4net.hh"
#include "libxorp/utils.hh"
#include "libproto/packet.hh"
#include "libxipc/xrl_std_router.hh"
#include "xrl/interfaces/test_peer_xif.hh"
#include "bgp/local_data.hh"
#include "bgp/packet.hh"
#include "peer.hh"
#include "bgppp.hh"
Peer::~Peer()
{
debug_msg("XXX Deleting peer %p\n", this);
}
Peer::Peer(EventLoop *eventloop,
XrlStdRouter *xrlrouter,
const string& peername,
const uint32_t genid,
const string& target_hostname,
const string& target_port)
: _eventloop(eventloop),
_xrlrouter(xrlrouter),
_peername(peername),
_genid(genid),
_target_hostname(target_hostname),
_target_port(target_port),
_up(true),
_busy(0),
_connected(false),
_session(false),
_passive(false),
_established(false),
_keepalive(false),
_as(AsNum::AS_INVALID), // XXX
_holdtime(0),
_ipv6(false),
_last_recv(0),
_last_sent(0)
{
debug_msg("XXX Creating peer %p\n", this);
Iptuple iptuple;
_localdata = new LocalData(*eventloop);
_localdata->set_use_4byte_asnums(false);
_localdata->set_as(AsNum(0));
_peerdata = new BGPPeerData(*_localdata, iptuple, AsNum(0), IPv4(),0);
_peerdata->compute_peer_type();
_peerdata->set_multiprotocol<IPv6>(SAFI_UNICAST, BGPPeerData::NEGOTIATED);
_peerdata->set_multiprotocol<IPv6>(SAFI_UNICAST, BGPPeerData::SENT);
}
Peer::Peer(const Peer& rhs)
: _as(AsNum::AS_INVALID) // XXX
{
debug_msg("Copy constructor\n");
copy(rhs);
}
Peer&
Peer::operator=(const Peer& rhs)
{
debug_msg("= operator\n");
if(&rhs == this)
return *this;
copy(rhs);
return *this;
}
void
Peer::copy(const Peer& rhs)
{
debug_msg("peername: %s\n", rhs._peername.c_str());
_eventloop = rhs._eventloop;
_xrlrouter = rhs._xrlrouter;
_peername = rhs._peername;
_genid = rhs._genid;
_target_hostname = rhs._target_hostname;
_target_port = rhs._target_port;
_up = rhs._up;
_busy = rhs._busy;
_session = rhs._session;
_passive = rhs._passive;
_keepalive = rhs._keepalive;
_established = rhs._established;
_as = rhs._as;
_holdtime = rhs._holdtime;
_ipv6 = rhs._ipv6;
_id = rhs._id;
_last_recv = rhs._last_recv;
_last_sent = rhs._last_sent;
_peerdata = rhs._peerdata;
_localdata = rhs._localdata;
}
Peer::PeerState
Peer::is_this_you(const string& peer_name) const
{
debug_msg("is this you: %s %s\n", _peername.c_str(), peer_name.c_str());
if(_up && peer_name == _peername) {
debug_msg("Its me\n");
return Peer::YES_ITS_ME;
}
/*
** If this peer has been down for ten minutes its a candidate to
** be deleted. There should be no more activity on this peer.
*/
if(!_up) {
TimeVal now;
_eventloop->current_time(now);
if(now - _shutdown_time > TimeVal(60 * 10, 0))
return Peer::PLEASE_DELETE_ME;
}
return Peer::NO_ITS_NOT_ME;
}
Peer::PeerState
Peer::is_this_you(const string& peer_name, const uint32_t genid) const
{
debug_msg("is this you: %s %s %u\n", _peername.c_str(), peer_name.c_str(),
XORP_UINT_CAST(genid));
if(_up && peer_name == _peername && genid == _genid)
return Peer::YES_ITS_ME;
if(!_up)
return is_this_you("");
return Peer::NO_ITS_NOT_ME;
}
void
Peer::shutdown()
{
XLOG_ASSERT(_up); // We should only ever be shutdown once.
_up = false;
_eventloop->current_time(_shutdown_time);
/*
** The corresponding test peer may have a tcp connection with a
** bgp process. Attempt a disconnect.
*/
_busy++;
XrlTestPeerV0p1Client test_peer(_xrlrouter);
if(!test_peer.send_reset(_peername.c_str(),
callback(this, &Peer::xrl_callback, "reset")))
XLOG_FATAL("reset failed");
}
bool
Peer::up() const
{
return _up;
}
void
Peer::status(string& status)
{
status = _peername + " ";
status += _established ? "established" : "";
status += " ";
status += "sent: ";
uint32_t sent = _trie_sent.update_count();
if (0 == _last_sent) {
status += c_format("%u", sent) + " ";
} else {
status += c_format("%u(%u)", sent, sent - _last_sent) + " ";
}
status += "received: ";
uint32_t recv = _trie_recv.update_count();
if (0 == _last_recv) {
status += c_format("%u", recv) + " ";
} else {
status += c_format("%u(%u)", recv, recv - _last_recv) + " ";
}
}
bool
Peer::pending()
{
debug_msg("pending? : %s\n",
bool_c_str(_session && !_established && !_passive));
// if(_xrlrouter->pending())
// return true;
if(_busy > 0)
return true;
// printf("%s: session: %d established: %d passive %d\n",
// _peername.c_str(), _session, _established, _passive);
if(_session && !_established && !_passive)
return true;
return false;
}
void
Peer::listen(const string& /*line*/, const vector<string>& /*words*/)
throw(InvalidString)
{
/* Connect the test peer to the target BGP */
debug_msg("About to listen on: %s\n", _peername.c_str());
_busy += 4;
XrlTestPeerV0p1Client test_peer(_xrlrouter);
if(!test_peer.send_register(_peername.c_str(), _xrlrouter->name(), _genid,
callback(this, &Peer::xrl_callback, "register")))
XLOG_FATAL("send_register failed");
if(!test_peer.send_packetisation(_peername.c_str(), "bgp",
callback(this, &Peer::xrl_callback,
"packetisation")))
XLOG_FATAL("send_packetisation");
if(!test_peer.send_use_4byte_asnums(_peername.c_str(), _localdata->use_4byte_asnums(),
callback(this, &Peer::xrl_callback,
"use_4byte_asnums")))
XLOG_FATAL("send_use_4byte_asnums");
if(!test_peer.send_listen(_peername.c_str(),
_target_hostname, atoi(_target_port.c_str()),
callback(this, &Peer::xrl_callback, "listen")))
XLOG_FATAL("send_listen failed");
}
void
Peer::connect(const string& /*line*/, const vector<string>& /*words*/)
throw(InvalidString)
{
/* Connect the test peer to the target BGP */
debug_msg("About to connect to: %s\n", _peername.c_str());
_busy += 4;
XrlTestPeerV0p1Client test_peer(_xrlrouter);
if(!test_peer.send_register(_peername.c_str(), _xrlrouter->name(), _genid,
callback(this, &Peer::xrl_callback, "register")))
XLOG_FATAL("send_register failed");
if(!test_peer.send_packetisation(_peername.c_str(), "bgp",
callback(this, &Peer::xrl_callback, "packetisation")))
XLOG_FATAL("send_packetisation failed");
if(!test_peer.send_use_4byte_asnums(_peername.c_str(), _localdata->use_4byte_asnums(),
callback(this, &Peer::xrl_callback,
"use_4byte_asnums")))
XLOG_FATAL("send_use_4byte_asnums");
if(!test_peer.send_connect(_peername.c_str(),
_target_hostname, atoi(_target_port.c_str()),
callback(this, &Peer::xrl_callback_connected,
"connected")))
XLOG_FATAL("send_connect failed");
}
void
Peer::disconnect(const string& /*line*/, const vector<string>& /*words*/)
throw(InvalidString)
{
/* Disconnect the test peer from the target BGP */
debug_msg("About to disconnect from: %s\n", _peername.c_str());
_busy++;
XrlTestPeerV0p1Client test_peer(_xrlrouter);
_session = false;
_established = false;
_last_sent = _trie_sent.update_count();
_last_recv = _trie_recv.update_count();
if(!test_peer.send_disconnect(_peername.c_str(),
callback(this, &Peer::xrl_callback, "disconnect")))
XLOG_FATAL("send_disconnect failed");
}
/*
** Form of command:
** peer1 establish active <true/false> AS <asnum>
**
*/
void
Peer::establish(const string& line, const vector<string>& words)
throw(InvalidString)
{
debug_msg("About to establish a connection %s\n", _peername.c_str());
bool active = true;
_session = true;
/*
** Parse the command line.
** The arguments for this command come in pairs:
** name value
*/
string as = "";
size_t size = words.size();
if(0 != (size % 2))
xorp_throw(InvalidString,
c_format("Incorrect number of arguments:\n[%s]",
line.c_str()));
for(size_t i = 2; i < words.size(); i += 2) {
debug_msg("name: %s value: %s\n",
words[i].c_str(),
words[i + 1].c_str());
if("active" == words[i]) {
string aarg = words[i+1];
if("true" == aarg)
active = true;
else if("false" == aarg)
active = false;
else
xorp_throw(InvalidString,
c_format("Illegal argument to active: <%s>\n[%s]",
aarg.c_str(), line.c_str()));
} else if("AS" == words[i]) {
as = words[i + 1];
} else if("keepalive" == words[i]) {
string kaarg = words[i+1];
if("true" == kaarg)
_keepalive = true;
else if("false" == kaarg)
_keepalive = false;
else
xorp_throw(InvalidString,
c_format("Illegal argument to keepalive: <%s>\n[%s]",
kaarg.c_str(), line.c_str()));
} else if("holdtime" == words[i]) {
_holdtime = atoi(words[i + 1].c_str());
} else if("id" == words[i]) {
_id = IPv4(words[i + 1].c_str());
} else if("ipv6" == words[i]) {
string ipv6arg = words[i+1];
if("true" == ipv6arg)
_ipv6 = true;
else if("false" == ipv6arg)
_ipv6 = false;
else
xorp_throw(InvalidString,
c_format("Illegal argument to ipv6: <%s>\n[%s]",
ipv6arg.c_str(), line.c_str()));
} else if("use_4byte_asnums" == words[i]) {
string use4bytearg = words[i+1];
if("true" == use4bytearg) {
_localdata->set_use_4byte_asnums(true);
_peerdata->set_use_4byte_asnums(true);
} else if("false" == use4bytearg) {
_localdata->set_use_4byte_asnums(false);
_peerdata->set_use_4byte_asnums(true);
} else
xorp_throw(InvalidString,
c_format("Illegal argument to use_4byte_asnums: <%s>\n[%s]",
use4bytearg.c_str(), line.c_str()));
} else
xorp_throw(InvalidString,
c_format("Illegal argument to establish: <%s>\n[%s]",
words[i].c_str(), line.c_str()));
}
if("" == as)
xorp_throw(InvalidString,
c_format("No AS number specified:\n[%s]", line.c_str()));
_as = AsNum(as);
if(!active) {
_passive = true;
listen(line, words);
return;
}
connect(line, words);
send_open();
}
void
Peer::send(const string& line, const vector<string>& words)
throw(InvalidString)
{
size_t size = words.size();
if(size < 3)
xorp_throw(InvalidString,
c_format("Insufficient number of arguments:\n[%s]",
line.c_str()));
const char PACKET[] = "packet";
const char DUMP[] = "dump";
if(PACKET == words[2]) {
send_packet(line, words);
} else if(DUMP == words[2]) {
send_dump(line, words);
} else {
xorp_throw(InvalidString,
c_format(
"Second argument should be %s or %s not <%s>\n[%s]",
PACKET, DUMP, words[2].c_str(), line.c_str()));
}
}
/**
* A corruption entry.
*
* The offset at which to corrupt a packet and the value with which to corrupt.
*/
class Corrupt {
public:
Corrupt(uint32_t offset, uint8_t val) : _offset(offset), _val(val)
{}
uint32_t offset() const { return _offset; }
uint32_t val() const { return _val; }
private:
uint32_t _offset;
uint8_t _val;
};
/*
** Form of command:
** peer1 send packet update ...
** peer1 send packet notify ...
** peer1 send packet open ...
** peer1 send packet keepalive
** peer1 send packet corrupt offset byte offset byte ... update
** notify
** open
** keepalive
**
** See the packet method for the rest of the commands
*/
void
Peer::send_packet(const string& line, const vector<string>& words)
throw(InvalidString)
{
size_t size = words.size();
uint32_t word = 3;
list<Corrupt> _corrupt;
if ("corrupt" == words[3]) {
word += 1;
// Pairs of offset byte should be in the stream until we reach
// packet.
while (word + 2 < size) {
if (!xorp_isdigit(words[word][0])) {
xorp_throw(InvalidString,
c_format("Should be an offset not %s\n[%s]",
words[word].c_str(), line.c_str()));
}
if (!xorp_isdigit(words[word + 1][0])) {
xorp_throw(InvalidString,
c_format("Should be a value not %s\n[%s]",
words[word + 1].c_str(), line.c_str()));
}
_corrupt.push_back(Corrupt(atoi(words[word].c_str()),
atoi(words[word+1].c_str())));
word += 2;
if (!xorp_isdigit(words[word][0]))
break;
}
}
const BGPPacket *pkt = Peer::packet(line, words, word);
size_t len = BGPPacket::MAXPACKETSIZE;
uint8_t buf[BGPPacket::MAXPACKETSIZE];
XLOG_ASSERT(pkt->encode(buf, len, _peerdata));
printf("packet: %s\n", pkt->str().c_str());
delete pkt;
if (!_corrupt.empty()) {
list<Corrupt>::const_iterator i;
for (i = _corrupt.begin(); i != _corrupt.end(); i++) {
if (i->offset() >= len)
xorp_throw(InvalidString,
c_format("Offset %u larger than packet %u\n[%s]",
i->offset(), XORP_UINT_CAST(len),
line.c_str()));
buf[i->offset()] = i->val();
}
}
/*
** If this is an update message, save it in the sent trie.
*/
const char update[] = "update";
if(update == words[3]) {
TimeVal tv;
_eventloop->current_time(tv);
try {
_trie_sent.process_update_packet(tv, buf, len, _peerdata);
} catch(CorruptMessage& c) {
/*
** A corrupt message is being sent so catch the decode exception.
*/
XLOG_WARNING("BAD Message: %s", c.why().c_str());
}
}
_busy++;
send_message(buf, len, callback(this, &Peer::xrl_callback, "send packet"));
}
struct mrt_header {
uint32_t time;
uint16_t type;
uint16_t subtype;
uint32_t length;
};
struct mrt_update {
uint16_t source_as;
uint16_t dest_as;
uint16_t ifindex;
uint16_t af;
uint32_t source_ip;
uint32_t dest_ip;
};
inline
const
uint8_t *
mrtd_traffic_file_read(FILE *fp, size_t& len)
{
mrt_header header;
if(fread(&header, sizeof(header), 1, fp) != 1) {
if(feof(fp))
return 0;
XLOG_WARNING("fread failed:%s", strerror(errno));
return 0;
}
len = ntohl(header.length) - sizeof(mrt_update);
mrt_update update;
if(fread(&update, sizeof(update), 1, fp) != 1) {
if(feof(fp))
return 0;
XLOG_WARNING("fread failed:%s", strerror(errno));
return 0;
}
uint8_t *buf = new uint8_t[len];
if(fread(buf, len, 1, fp) != 1) {
if(feof(fp))
return 0;
XLOG_WARNING("fread failed:%s", strerror(errno));
return 0;
}
return buf;
}
#if 0
// Code to read from a MRTD table dump.
// In order to hook this code up:
// 1) Modify send_dump to support "routeview" as well as "update".
// 2) Compare with mrtd_routeview_dump and rename to mrtd_routview_read.
// 3) Update the manual.
// Contributed by Ratul Mahajan
struct mrt_table {
uint16_t view;
uint16_t seqno;
uint32_t prefix;
uint8_t prefix_len;
uint8_t status;
uint32_t time;
uint32_t peerip;
uint16_t peeras;
uint16_t att_len;
};
inline
const
uint8_t *
mrtd_table_file_read(FILE *fp, size_t& len)
{
mrt_header header;
if(fread(&header, sizeof(header), 1, fp) != 1) {
if(feof(fp))
return 0;
XLOG_WARNING("fread failed:%s", strerror(errno));
return 0;
}
uint16_t type = ntohs(header.type);
if (type == 12) { // TABLE_DUMP
mrt_table table;
//sizeof(mrt_table) gives 24 which is incorrect
size_t sizeof_table = 22;
/*
** this did not work for me: the struct was not being populated
** correctly. no clue why!
*/
// if(fread(&table, sizeof_table, 1, fp) != 1) {
// if(feof(fp))
// return 0;
// XLOG_WARNING("fread failed:%s",strerror(errno));
// return 0;
// }
//uglier version of the above
if (! (fread(&table.view, 2, 1, fp) == 1 &&
fread(&table.seqno, 2, 1, fp) == 1 &&
fread(&table.prefix, 4, 1, fp) == 1 &&
fread(&table.prefix_len, 1, 1, fp) == 1 &&
fread(&table.status, 1, 1, fp) == 1 &&
fread(&table.time, 4, 1, fp) == 1 &&
fread(&table.peerip, 4, 1, fp) == 1 &&
fread(&table.peeras, 2, 1, fp) == 1 &&
fread(&table.att_len, 2, 1, fp) == 1
) ) {
if (feof(fp)) return 0;
XLOG_WARNING("fread failed:%s", strerror(errno));
return 0;
}
UpdatePacket update;
size_t pa_len = ntohl(header.length) - sizeof_table;
uint8_t * attributes = new uint8_t[pa_len];
if(fread(attributes, pa_len, 1, fp) != 1) {
if(feof(fp)) return 0;
XLOG_WARNING("fread failed:%s", strerror(errno));
return 0;
}
// this didn't work for me. i thought it should have, but didn't
// look closely when it didn't.
//update.add_nlri(BGPUpdateAttrib(IPv4(ntohl(table.prefix)),
// ntohs(table.prefix_len)));
//stupid way to do the above
uint8_t *pfx = new uint8_t[5];
memcpy(pfx, &(table.prefix_len), 1);
memcpy(pfx+1, &(table.prefix), 4);
BGPUpdateAttrib nlri(pfx);
update.add_nlri(nlri);
delete [] pfx;
uint8_t * d = attributes;
while (pa_len > 0) {
size_t used = 0;
PathAttribute *pa = PathAttribute::create(d, pa_len, used);
if (used == 0)
xorp_throw(CorruptMessage,
c_format("failed to read path attribute"),
UPDATEMSGERR, ATTRLEN);
update.add_pathatt(pa);
d += used;
pa_len -= used;
}
delete [] attributes;
debug_msg("update packet = %s\n", update.str().c_str());
const uint8_t *buf = update.encode(len);
return buf;
}
else {
XLOG_WARNING("incorrect record type: %d", type);
return 0;
}
}
#endif
/*
** peer send dump mrtd update fname <count>
** 0 1 2 3 4 5 6
*/
void
Peer::send_dump(const string& line, const vector<string>& words)
throw(InvalidString)
{
if(6 != words.size() && 7 != words.size())
xorp_throw(InvalidString,
c_format("Incorrect number of arguments:\n[%s]",
line.c_str()));
const char MRTD[] = "mrtd";
if(MRTD != words[3])
xorp_throw(InvalidString,
c_format("Third argument should be %s not <%s>\n[%s]",
MRTD, words[3].c_str(), line.c_str()));
const char UPDATE[] = "update";
if(UPDATE != words[4])
xorp_throw(InvalidString,
c_format("Fourth argument should be %s not <%s>\n[%s]",
UPDATE, words[4].c_str(), line.c_str()));
string fname = words[5];
FILE *fp = fopen(fname.c_str(), "r");
if(0 == fp)
xorp_throw(InvalidString,
c_format("fopen of %s failed: %s\n[%s]",
fname.c_str(), strerror(errno), line.c_str()));
size_t packets_to_send = 0;
if(7 == words.size())
packets_to_send = atoi(words[6].c_str());
// XXX - We don't check for this to be an integer, we assume
// failure returns zero. In which case the whole file is sent.
/*
** It could take quite a while to send a large file so we need to
** set up the transfer and then return.
*/
send_dump_callback(XrlError::OKAY(), fp, 0, packets_to_send,
"mrtd_traffic_send");
}
void
Peer::send_dump_callback(const XrlError& error, FILE *fp,
const size_t packet_number,
const size_t packets_to_send,
const char *comment)
{
debug_msg("callback %s %s\n", comment, error.str().c_str());
if(XrlError::OKAY() != error) {
XLOG_WARNING("callback: %s %s", comment, error.str().c_str());
fclose(fp);
return;
}
if(packets_to_send != 0 && packet_number == packets_to_send) {
fclose(fp);
return;
}
size_t len;
const uint8_t *buf;
while(0 != (buf = mrtd_traffic_file_read(fp, len))) {
uint8_t type = extract_8(buf + BGPPacket::TYPE_OFFSET);
if(MESSAGETYPEUPDATE == type) {
/*
** Save the update message in the sent trie.
*/
TimeVal tv;
_eventloop->current_time(tv);
_trie_sent.process_update_packet(tv, buf, len, _peerdata);
_smcb = callback(this, &Peer::send_dump_callback,
fp,
packet_number + 1, packets_to_send,
"mrtd_traffic_send");
send_message(buf, len, _smcb);
return;
} else {
delete [] buf;
}
}
fclose(fp);
}
void
Peer::trie(const string& line, const vector<string>& words)
throw(InvalidString)
{
if(words.size() < 4)
xorp_throw(InvalidString,
c_format("Insufficient arguments:\n[%s]", line.c_str()));
/*
** Each peer holds two tries. One holds updates sent the other
** holds updates received. Determine which trie we are about to
** operate on.
*/
Trie *op;
if("sent" == words[2]) {
op = &_trie_sent;
} else if("recv" == words[2]) {
op = &_trie_recv;
} else
xorp_throw(InvalidString,
c_format("\"sent\" or \"recv\" accepted not <%s>\n[%s]",
words[2].c_str(), line.c_str()));
/*
** The only operation currently supported is lookup
*/
if("lookup" != words[3])
xorp_throw(InvalidString,
c_format("\"lookup\" expected not <%s>\n[%s]",
words[3].c_str(), line.c_str()));
if(words.size() < 5)
xorp_throw(InvalidString,
c_format("No arguments for \"lookup\"\n[%s]",
line.c_str()));
const UpdatePacket *bgpupdate = op->lookup(words[4]);
if(0 == bgpupdate) {
if((words.size() == 6) && ("not" == words[5]))
return;
xorp_throw(InvalidString,
c_format("Lookup failed [%s]", line.c_str()));
}
if((words.size() == 6) && ("not" == words[5]))
xorp_throw(InvalidString,
c_format("Lookup failed entry exists [%s]", line.c_str()));
debug_msg("Found: %s\n", bgpupdate->str().c_str());
size_t size = words.size();
if(0 == (size % 2))
xorp_throw(InvalidString,
c_format("Incorrect number of arguments:\n[%s]",
line.c_str()));
for(size_t i = 5; i < size; i += 2) {
debug_msg("attribute: %s value: %s\n",
words[i].c_str(),
words[i + 1].c_str());
if("aspath" == words[i]) {
/*
** Search for the AS Path in the update packet.
*/
FPAList4Ref palist = const_cast<UpdatePacket*>(bgpupdate)->pa_list();
list<PathAttribute*>::const_iterator pai;
const ASPath *aspath = 0;
if (palist->aspath_att())
aspath = &(palist->aspath());
else
xorp_throw(InvalidString,
c_format("NO AS Path associated with route\n[%s]",
line.c_str()));
string aspath_search;
if("empty" != words[i + 1])
aspath_search = words[i + 1];
if(*aspath != ASPath(aspath_search.c_str()))
xorp_throw(InvalidString,
c_format("Looking for Path: <%s> Found: <%s>\n[%s]",
words[i + 1].c_str(),
aspath->str().c_str(),
line.c_str()));
} else
xorp_throw(InvalidString,
c_format("Illegal attribute: <%s>\n[%s]",
words[i].c_str(), line.c_str()));
}
}
/*
** peer expect packet ...
** 0 1 2
*/
void
Peer::expect(const string& line, const vector<string>& words)
throw(InvalidString)
{
if(words.size() < 3)
xorp_throw(InvalidString,
c_format("Insufficient arguments:\n[%s]", line.c_str()));
if("packet" == words[2]) {
_expect._list.push_back(packet(line, words, 3));
} else
xorp_throw(InvalidString,
c_format(
"\"packet\" accepted not <%s>\n[%s]",
words[2].c_str(), line.c_str()));
}
/*
** peer assert queue
** 0 1 2
**
** peer assert queue <len>
** 0 1 2 3
**
** peer assert connected
** 0 1 2
**
** peer assert established
** 0 1 2
**
** peer assert idle
** 0 1 2
*/
void
Peer::assertX(const string& line, const vector<string>& words)
throw(InvalidString)
{
if(words.size() < 3)
xorp_throw(InvalidString,
c_format("Insufficient arguments:\n[%s]", line.c_str()));
if("queue" == words[2]) {
if(!_expect._ok)
xorp_throw(InvalidString,
c_format("Expect queue violated\nExpect: %sReceived: %s",
_expect._list.front()->str().c_str(),
_expect._bad->str().c_str()));
switch(words.size()) {
case 3:
break;
case 4:
if(static_cast<unsigned int>(atoi(words[3].c_str())) !=
_expect._list.size())
xorp_throw(InvalidString,
c_format("Expected list size to be %d actual %u",
atoi(words[3].c_str()),
XORP_UINT_CAST(_expect._list.size())));
break;
default:
xorp_throw(InvalidString,
c_format("Illegal number of arguments to \"queue\"\n[%s]",
line.c_str()));
break;
}
} else if("connected" == words[2]) {
if(!_connected)
xorp_throw(InvalidString,
c_format("No TCP session established"));
} else if("established" == words[2]) {
if(!_established)
xorp_throw(InvalidString,
c_format("No BGP session established"));
} else if("idle" == words[2]) {
if(_connected)
xorp_throw(InvalidString,
c_format("Session established"));
} else
xorp_throw(InvalidString,
c_format(
"\"queue\" \"established\" \"idle\"accepted not <%s>\n[%s]",
words[2].c_str(), line.c_str()));
}
void
mrtd_traffic_dump(const uint8_t *buf, const size_t len , const TimeVal tv,
const string fname)
{
FILE *fp = fopen(fname.c_str(), "a");
if(0 == fp)
XLOG_FATAL("fopen of %s failed: %s", fname.c_str(), strerror(errno));
mrt_header header;
header.time = htonl(tv.sec());
header.type = htons(16);
header.subtype = htons(1);
header.length = htonl(len + sizeof(mrt_update));
if(fwrite(&header, sizeof(header), 1, fp) != 1)
XLOG_FATAL("fwrite of %s failed: %s", fname.c_str(), strerror(errno));