| 1 | /* | 
|---|
| 2 | * tcpip_endpoint.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: 26.03.2013 | 
|---|
| 5 | *      Author: mario | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "tcpip_endpoint.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | // system | 
|---|
| 11 | #include <sstream> | 
|---|
| 12 | #include <stdexcept> | 
|---|
| 13 |  | 
|---|
| 14 | using namespace std; | 
|---|
| 15 | using namespace boost::asio::ip; | 
|---|
| 16 |  | 
|---|
| 17 | namespace ariba { | 
|---|
| 18 | namespace addressing2 { | 
|---|
| 19 |  | 
|---|
| 20 | #define TCPIPv6_SIZE 18 | 
|---|
| 21 | #define TCPIPv4_SIZE 6 | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | /// factories | 
|---|
| 25 | TcpIP_EndpointPtr tcpip_endpoint::create_TcpIP_Endpoint( | 
|---|
| 26 | const tcp::endpoint& asio_endpoint) | 
|---|
| 27 | { | 
|---|
| 28 | TcpIP_EndpointPtr ptr(new tcpip_endpoint(asio_endpoint)); | 
|---|
| 29 |  | 
|---|
| 30 | return ptr; | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | /// constructors | 
|---|
| 35 | tcpip_endpoint::tcpip_endpoint(const std::string& ip_addr, const int port) | 
|---|
| 36 | { | 
|---|
| 37 | address addr = address::from_string(ip_addr); | 
|---|
| 38 | asio_endpoint = tcp::endpoint(addr, static_cast<uint16_t>(port)); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | tcpip_endpoint::tcpip_endpoint(const ENDPOINT_TYPE type, const uint8_t* const byte_array, const int read_max) | 
|---|
| 42 | { | 
|---|
| 43 | // IPv4 | 
|---|
| 44 | if ( type == endpoint_type::TCPIPv4 ) | 
|---|
| 45 | { | 
|---|
| 46 | // boundary check | 
|---|
| 47 | if ( read_max < TCPIPv4_SIZE ) | 
|---|
| 48 | throw invalid_argument("Not enough bytes to read an TCPIPv4 endpoint."); | 
|---|
| 49 |  | 
|---|
| 50 | asio_endpoint = tcp::endpoint( address_v4( *((uint32_t*) byte_array) ), | 
|---|
| 51 | *((uint16_t*) (byte_array+sizeof(uint32_t))) ); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | // IPv6 | 
|---|
| 55 | else if ( type == endpoint_type::TCPIPv6 ) | 
|---|
| 56 | { | 
|---|
| 57 | // boundary check | 
|---|
| 58 | if ( read_max < TCPIPv6_SIZE ) | 
|---|
| 59 | throw invalid_argument("Not enough bytes to read an TCPIPv6 endpoint."); | 
|---|
| 60 |  | 
|---|
| 61 | address_v6::bytes_type bytes_; | 
|---|
| 62 | for (size_t i=0; i<bytes_.size(); i++) | 
|---|
| 63 | { | 
|---|
| 64 | bytes_[i] = byte_array[i]; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | asio_endpoint = tcp::endpoint( address_v6(bytes_), | 
|---|
| 68 | *((uint16_t*) (byte_array+bytes_.size())) ); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | // Error | 
|---|
| 72 | else | 
|---|
| 73 | { | 
|---|
| 74 | throw invalid_argument("Corrupt data. Can't deserialize endpoint."); | 
|---|
| 75 | } | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | tcpip_endpoint::tcpip_endpoint(const tcp::endpoint& asio_endpoint)  : | 
|---|
| 79 | asio_endpoint(asio_endpoint) | 
|---|
| 80 | { | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | tcpip_endpoint::~tcpip_endpoint() | 
|---|
| 85 | { | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 | ENDPOINT_CATEGORY tcpip_endpoint::get_category() const | 
|---|
| 91 | { | 
|---|
| 92 | return endpoint_category::TCPIP; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | ENDPOINT_TYPE tcpip_endpoint::get_type() const | 
|---|
| 96 | { | 
|---|
| 97 | // IPv4 | 
|---|
| 98 | if ( asio_endpoint.address().is_v4() ) | 
|---|
| 99 | { | 
|---|
| 100 | return endpoint_type::TCPIPv4; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | // IPv6 | 
|---|
| 104 | else if ( asio_endpoint.address().is_v6() ) | 
|---|
| 105 | { | 
|---|
| 106 | return endpoint_type::TCPIPv6; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | // Invalid | 
|---|
| 110 | else | 
|---|
| 111 | { | 
|---|
| 112 | return endpoint_type::INVALID; | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 | std::string tcpip_endpoint::to_string() const | 
|---|
| 118 | { | 
|---|
| 119 | ostringstream out; | 
|---|
| 120 |  | 
|---|
| 121 | out << asio_endpoint; | 
|---|
| 122 |  | 
|---|
| 123 | return out.str(); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | size_t tcpip_endpoint::to_byte_array(uint8_t* buffer) const | 
|---|
| 129 | { | 
|---|
| 130 | // IPv4 | 
|---|
| 131 | if ( asio_endpoint.address().is_v4() ) | 
|---|
| 132 | { | 
|---|
| 133 | uint32_t ip = asio_endpoint.address().to_v4().to_ulong(); | 
|---|
| 134 | uint16_t port = asio_endpoint.port(); | 
|---|
| 135 |  | 
|---|
| 136 | memcpy(buffer, &ip, sizeof(uint32_t)); | 
|---|
| 137 | memcpy(buffer+sizeof(uint32_t), &port, sizeof(uint16_t)); | 
|---|
| 138 |  | 
|---|
| 139 | return TCPIPv4_SIZE; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | // IPv6 | 
|---|
| 143 | else | 
|---|
| 144 | { | 
|---|
| 145 | address_v6::bytes_type bytes_ = asio_endpoint.address().to_v6().to_bytes(); | 
|---|
| 146 |  | 
|---|
| 147 | for (size_t i=0; i<bytes_.size(); i++) | 
|---|
| 148 | { | 
|---|
| 149 | buffer[i] = bytes_[i]; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | uint16_t port = asio_endpoint.port(); | 
|---|
| 153 | memcpy(buffer+bytes_.size(), &port, sizeof(uint16_t)); | 
|---|
| 154 |  | 
|---|
| 155 | return TCPIPv6_SIZE; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 |  | 
|---|
| 161 | int tcpip_endpoint::size() const | 
|---|
| 162 | { | 
|---|
| 163 | // IPv4 | 
|---|
| 164 | if ( asio_endpoint.protocol() == tcp::v4() ) | 
|---|
| 165 | return TCPIPv4_SIZE; | 
|---|
| 166 |  | 
|---|
| 167 | // IPv6 | 
|---|
| 168 | else | 
|---|
| 169 | return TCPIPv6_SIZE; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | const tcp::endpoint& tcpip_endpoint::to_asio() const | 
|---|
| 173 | { | 
|---|
| 174 | return asio_endpoint; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | bool tcpip_endpoint::equals(const TcpIP_EndpointPtr& rhs) const | 
|---|
| 178 | { | 
|---|
| 179 | return asio_endpoint == rhs->asio_endpoint; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | }} /* namespace ariba::addressing2 */ | 
|---|