Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef SRC_BGP_BGP_SESSION_H_ 7 : #define SRC_BGP_BGP_SESSION_H_ 8 : 9 : #include <boost/scoped_ptr.hpp> 10 : 11 : #include <string> 12 : 13 : #include "bgp/bgp_proto.h" 14 : #include "io/tcp_session.h" 15 : 16 : class BgpPeer; 17 : class BgpSessionManager; 18 : 19 : class BgpMessageReader : public TcpMessageReader { 20 : public: 21 : BgpMessageReader(TcpSession *session, ReceiveCallback callback); 22 : virtual ~BgpMessageReader(); 23 : 24 : protected: 25 : virtual int MsgLength(Buffer buffer, int offset); 26 : 27 100586 : virtual const int GetHeaderLenSize() { 28 100586 : return kHeaderLenSize; 29 : } 30 : 31 2 : virtual const int GetMaxMessageSize() { 32 2 : return kMaxMessageSize; 33 : } 34 : 35 : private: 36 : static const int kHeaderLenSize = 18; 37 : static const int kMaxMessageSize = 4096; 38 : 39 : DISALLOW_COPY_AND_ASSIGN(BgpMessageReader); 40 : }; 41 : 42 : class BgpSession : public TcpSession { 43 : public: 44 : BgpSession(BgpSessionManager *session_mgr, Socket *socket); 45 : virtual ~BgpSession(); 46 : 47 : void SendNotification(int code, int subcode, 48 : const std::string &data = std::string()); 49 101820 : virtual int GetSessionInstance() const { return task_instance_; } 50 : void ProcessWriteReady(); 51 : 52 : void set_peer(BgpPeer *peer); 53 : void clear_peer(); 54 212229 : BgpPeer *peer() { return peer_; } 55 : void LogNotification(int code, int subcode, const std::string &direction, 56 : const std::string &peer_key, 57 : const BgpProto::Notification &msg) const; 58 : 59 : protected: 60 100584 : virtual void OnRead(Buffer buffer) { 61 100584 : reader_->OnRead(buffer); 62 100589 : } 63 : 64 : private: 65 : bool ReceiveMsg(const u_int8_t *msg, size_t size); 66 : virtual void WriteReady(const boost::system::error_code &error); 67 : 68 : BgpSessionManager *session_mgr_; 69 : BgpPeer *peer_; 70 : int task_instance_; 71 : boost::scoped_ptr<BgpMessageReader> reader_; 72 : 73 : DISALLOW_COPY_AND_ASSIGN(BgpSession); 74 : }; 75 : 76 : #endif // SRC_BGP_BGP_SESSION_H_