source: source/ariba/utility/addressing2/tcpip_endpoint.cpp@ 12060

Last change on this file since 12060 was 12060, checked in by hock@…, 11 years ago

Reintegrate branch: 20130111-hock-message_classes

improvements:

  • new message classes (reboost, zero-copy)
  • "fast path" for direct links (skip overlay layer)
  • link-properties accessible from the application
  • SystemQueue can call boost::bind functions
  • protlib compatibility removed (32bit overhead saved in every message)
  • addressing2
  • AddressDiscovery discoveres only addresses on which we're actually listening
  • ariba serialization usage reduced (sill used in OverlayMsg)
  • Node::connect, easier and cleaner interface to start-up ariba from the application
  • ariba configs via JSON, XML, etc (boost::property_tree)
  • keep-alive overhead greatly reduced
  • (relayed) overlay links can actually be closed now
  • lost messages are detected in most cases
  • notification to the application when link is transformed into direct-link
  • overlay routing: send message to second best hop if it would be dropped otherwise
  • SequenceNumbers (only mechanisms, so for: upward compatibility)
  • various small fixes


regressions:

  • bluetooth is not yet working again
  • bootstrap modules deactivated
  • liblog4xx is not working (use cout-logging)

This patch brings great performance and stability improvements at cost of backward compatibility.
Also bluetooth and the bootstrap modules have not been ported to the new interfaces, yet.

File size: 3.7 KB
Line 
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
14using namespace std;
15using namespace boost::asio::ip;
16
17namespace ariba {
18namespace addressing2 {
19
20#define TCPIPv6_SIZE 18
21#define TCPIPv4_SIZE 6
22
23
24/// factories
25TcpIP_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
35tcpip_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
41tcpip_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
78tcpip_endpoint::tcpip_endpoint(const tcp::endpoint& asio_endpoint) :
79 asio_endpoint(asio_endpoint)
80{
81}
82
83
84tcpip_endpoint::~tcpip_endpoint()
85{
86}
87
88
89
90ENDPOINT_CATEGORY tcpip_endpoint::get_category() const
91{
92 return endpoint_category::TCPIP;
93}
94
95ENDPOINT_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
117std::string tcpip_endpoint::to_string() const
118{
119 ostringstream out;
120
121 out << asio_endpoint;
122
123 return out.str();
124}
125
126
127
128size_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
161int 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
172const tcp::endpoint& tcpip_endpoint::to_asio() const
173{
174 return asio_endpoint;
175}
176
177bool tcpip_endpoint::equals(const TcpIP_EndpointPtr& rhs) const
178{
179 return asio_endpoint == rhs->asio_endpoint;
180}
181
182}} /* namespace ariba::addressing2 */
Note: See TracBrowser for help on using the repository browser.