All pastes #2055664 Raw Edit

Unnamed

public cpp v1 · immutable
#2055664 ·published 2011-05-09 09:53 UTC
rendered paste body
#include <iostream>#include <string>#include <boost/bind.hpp>#include <boost/shared_ptr.hpp>#include <boost/enable_shared_from_this.hpp>#include <boost/asio.hpp>#include <ctime>#include <netinet/in.h>using boost::asio::ip::tcp;class BGP {	public:		BGP(unsigned int ip, unsigned short asn, unsigned short myas);		~BGP();		void Start();		std::string peer_state;		struct bgpHeader {			uint8_t	marker[16];			uint16_t len;			uint8_t	type;		};		struct openMsg {			uint8_t version;			uint16_t asn;			uint16_t holdtime;			uint32_t identifier;			uint8_t opl;		};	private:		unsigned int peer_ip;		unsigned short peer_asn;		unsigned short my_asn;};class tcp_connection  : public boost::enable_shared_from_this<tcp_connection>{public:  typedef boost::shared_ptr<tcp_connection> pointer;  static pointer create(boost::asio::io_service& io_service)  {    return pointer(new tcp_connection(io_service));  }  tcp::socket& socket()  {    return socket_;  }  unsigned short swapByteOrder(unsigned short us)  {      us = (us >> 8) |           (us << 8);      return us;  }  void start()  {    std::string message_ = "test string";    boost::array<char, 4096> buf;    boost::array<char, 4096> buf2;    boost::system::error_code error = boost::asio::error::host_not_found;    size_t len = socket_.read_some(boost::asio::buffer(buf,19), error);    BGP::bgpHeader bgph;    memcpy(&bgph,&buf,19);    std::cout << len << ":" << (int)bgph.type << ":" << ntohs(bgph.len) << std::endl;    if((int)bgph.type == 1) {    	len = socket_.read_some(boost::asio::buffer(buf2,(swapByteOrder(bgph.len))-19), error);    	BGP::openMsg om;    	memcpy(&om,&buf2,10);    	std::cout << len << "Version: " << (int)om.version << " ASN: " << ntohs(om.asn) << " HT: " <<  ntohs(om.holdtime) << " " << (int)om.opl << std::endl;    }  }private:  tcp_connection(boost::asio::io_service& io_service)    : socket_(io_service)  {  }  void handle_write(const boost::system::error_code& /*error*/,      size_t /*bytes_transferred*/)  {  }  tcp::socket socket_;  std::string message_;};class tcp_server{public:  tcp_server(boost::asio::io_service& io_service)    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 179))  {    start_accept();  }private:  void start_accept()  {    tcp_connection::pointer new_connection =      tcp_connection::create(acceptor_.io_service());    acceptor_.async_accept(new_connection->socket(),        boost::bind(&tcp_server::handle_accept, this, new_connection,          boost::asio::placeholders::error));  }  void handle_accept(tcp_connection::pointer new_connection,      const boost::system::error_code& error)  {    if (!error)    {      new_connection->start();      start_accept();    }  }  tcp::acceptor acceptor_;};