|
xorp
|
00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- 00002 00003 // Copyright (c) 2001-2009 XORP, Inc. 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License, Version 2, June 00007 // 1991 as published by the Free Software Foundation. Redistribution 00008 // and/or modification of this program under the terms of any other 00009 // version of the GNU General Public License is not permitted. 00010 // 00011 // This program is distributed in the hope that it will be useful, but 00012 // WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details, 00014 // see the GNU General Public License, Version 2, a copy of which can be 00015 // found in the XORP LICENSE.gpl file. 00016 // 00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA; 00018 // http://xorp.net 00019 00020 // $XORP: xorp/bgp/harness/peer.hh,v 1.26 2008/10/02 21:56:26 bms Exp $ 00021 00022 #ifndef __BGP_HARNESS_PEER_HH__ 00023 #define __BGP_HARNESS_PEER_HH__ 00024 00025 #include "trie.hh" 00026 #include "libxorp/callback.hh" 00027 #include "libxorp/tokenize.hh" 00028 #include "bgp/peer.hh" 00029 00030 class EventLoop; 00031 class TimeVal; 00032 00033 class Peer { 00034 public: 00035 00036 Peer(EventLoop *eventloop, 00037 XrlStdRouter *xrlrouter, 00038 const string& peer_name, 00039 const uint32_t genid, 00040 const string& target_hostname, 00041 const string& target_port); 00042 00043 Peer(const Peer& rhs); 00044 Peer& operator=(const Peer& rhs); 00045 void copy(const Peer& rhs); 00046 00047 ~Peer(); 00048 00049 enum PeerState {YES_ITS_ME, NO_ITS_NOT_ME, PLEASE_DELETE_ME}; 00050 00051 PeerState is_this_you(const string& peer_name) const; 00052 00053 PeerState is_this_you(const string& peer_name, const uint32_t genid) const; 00054 00055 void shutdown(); 00056 00057 bool up() const; 00058 00059 void status(string& status); 00060 00061 bool pending(); 00062 00063 void listen(const string& line, const vector<string>& words) 00064 throw(InvalidString); 00065 00066 void connect(const string& line, const vector<string>& words) 00067 throw(InvalidString); 00068 00069 void disconnect(const string& line, const vector<string>& words) 00070 throw(InvalidString); 00071 00072 void establish(const string& line, const vector<string>& words) 00073 throw(InvalidString); 00074 00075 void send(const string& line, const vector<string>& words) 00076 throw(InvalidString); 00077 00078 void send_packet(const string& line, const vector<string>& words) 00079 throw(InvalidString); 00080 00081 void send_dump(const string& line, const vector<string>& words) 00082 throw(InvalidString); 00083 00084 void trie(const string& line, const vector<string>& words) 00085 throw(InvalidString); 00086 00087 void expect(const string& line, const vector<string>& words) 00088 throw(InvalidString); 00089 00090 void assertX(const string& line, const vector<string>& words) 00091 throw(InvalidString); 00092 00093 void dump(const string& line, const vector<string>& words) 00094 throw(InvalidString); 00095 00096 void check_expect(BGPPacket *rec); 00097 00098 void xrl_callback(const XrlError& error, const char *comment); 00099 void xrl_callback_connected(const XrlError& error, const char *comment); 00100 00101 void datain(const bool& status, const TimeVal& tv, 00102 const vector<uint8_t>& data); 00103 void datain_error(const string& reason); 00104 void datain_closed(); 00105 00106 PathAttribute *path_attribute(const char *) const throw(InvalidString); 00107 const BGPPacket *packet(const string& line, const vector<string>& words, 00108 int index) const throw(InvalidString); 00109 protected: 00110 typedef XorpCallback1<void, const XrlError&>::RefPtr SMCB; 00111 SMCB _smcb; 00112 void send_message(const uint8_t *buf, const size_t len, SMCB cb); 00113 void send_dump_callback(const XrlError& error, FILE *fp, 00114 const size_t packet_number, 00115 const size_t packets_to_send, 00116 const char *comment); 00117 void send_open(); 00118 void send_keepalive(); 00119 00120 private: 00121 EventLoop *_eventloop; 00122 XrlStdRouter *_xrlrouter; 00123 00124 /* we need these because the attribute encode methods reference them */ 00125 LocalData *_localdata; 00126 BGPPeerData *_peerdata; 00127 00128 string _peername; 00129 uint32_t _genid; 00130 string _target_hostname; 00131 string _target_port; 00132 00133 bool _up; // True if this peer has not been shutdown 00134 TimeVal _shutdown_time; // Time this peer was shutdown 00135 00136 uint32_t _busy; // Count of outstanding transactions. 00137 00138 bool _connected; // True if we believe a TCP connection session. 00139 bool _session; // We are attempting to form a BGP session. 00140 bool _passive; // We are passively trying to create a session. 00141 bool _established; // True if we believe a session has been formed. 00142 bool _keepalive; // If true echo all received keepalives. 00143 00144 AsNum _as; // Our AS number. 00145 int _holdtime; // The holdtime sent in the BGP open message. 00146 IPv4 _id; // The ID sent in the open message. 00147 bool _ipv6; // Should we announce an ipv6 capability in 00148 // the open message. 00149 00150 Trie _trie_recv; // Update messages received. 00151 Trie _trie_sent; // Update messages sent. 00152 00153 // Number of update messages received or sent since last 00154 // disconnect, used to compute the number of messages in the 00155 // current session. 00156 uint32_t _last_recv; 00157 uint32_t _last_sent; 00158 00159 typedef XorpCallback3<void, const uint8_t*, size_t, TimeVal>::RefPtr Dumper; 00160 Dumper _traffic_recv; 00161 Dumper _traffic_sent; 00162 00163 struct expect { 00164 expect() : _ok(true), _bad(0) { 00165 } 00166 ~expect() { 00167 list<const BGPPacket *>::iterator i; 00168 for(i = _list.begin(); i != _list.end(); i++) 00169 delete *i; 00170 delete _bad; 00171 } 00172 00173 list<const BGPPacket *> _list; // Messages that we are expecting. 00174 bool _ok; // True while expected messages arrive. 00175 const BGPPacket *_bad; // The mismatched packet. 00176 } _expect; 00177 }; 00178 00179 #endif // __BGP_HARNESS_PEER_HH__