Branch data Line data Source code
1 : : // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include <config.h>
16 : :
17 : : #include <unistd.h> // for some IPC/network system calls
18 : : #include <stdint.h>
19 : : #include <sys/socket.h>
20 : : #include <netinet/in.h>
21 : :
22 : : #include <asio.hpp>
23 : :
24 : : #include <exceptions/exceptions.h>
25 : : #include <asiolink/io_address.h>
26 : : #include <asiolink/io_error.h>
27 : : #include <boost/static_assert.hpp>
28 : :
29 : : using namespace asio;
30 : : using asio::ip::udp;
31 : : using asio::ip::tcp;
32 : :
33 : : using namespace std;
34 : :
35 : : namespace isc {
36 : : namespace asiolink {
37 : :
38 : : // XXX: we cannot simply construct the address in the initialization list,
39 : : // because we'd like to throw our own exception on failure.
40 : 531 : IOAddress::IOAddress(const std::string& address_str) {
41 : : asio::error_code err;
42 : 531 : asio_address_ = ip::address::from_string(address_str, err);
43 [ + + ]: 531 : if (err) {
44 [ + - ][ + - ]: 20 : isc_throw(IOError, "Failed to convert string to address '"
[ + - ][ + - ]
[ + - ]
45 : : << address_str << "': " << err.message());
46 : : }
47 : 521 : }
48 : :
49 : 317 : IOAddress::IOAddress(const asio::ip::address& asio_address) :
50 : 317 : asio_address_(asio_address)
51 : 317 : {}
52 : :
53 : 1 : IOAddress::IOAddress(uint32_t v4address):
54 : 2 : asio_address_(asio::ip::address_v4(v4address)) {
55 : :
56 : 1 : }
57 : :
58 : : string
59 : 422457 : IOAddress::toText() const {
60 : 422457 : return (asio_address_.to_string());
61 : : }
62 : :
63 : : IOAddress
64 : 2 : IOAddress::from_bytes(short family, const uint8_t* data) {
65 [ - + ]: 2 : if (data == NULL) {
66 [ # # ][ # # ]: 0 : isc_throw(BadValue, "NULL pointer received.");
67 : : } else
68 [ - + ]: 2 : if ( (family != AF_INET) && (family != AF_INET6) ) {
69 [ # # ][ # # ]: 0 : isc_throw(BadValue, "Invalid family type. Only AF_INET and AF_INET6"
[ # # ]
70 : : << "are supported");
71 : : }
72 : :
73 : : BOOST_STATIC_ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
74 : : char addr_str[INET6_ADDRSTRLEN];
75 : 2 : inet_ntop(family, data, addr_str, INET6_ADDRSTRLEN);
76 [ + - ]: 2 : return IOAddress(string(addr_str));
77 : : }
78 : :
79 : : short
80 : 78 : IOAddress::getFamily() const {
81 [ + + ]: 78 : if (asio_address_.is_v4()) {
82 : : return (AF_INET);
83 : : } else {
84 : 78 : return (AF_INET6);
85 : : }
86 : : }
87 : :
88 : : const asio::ip::address&
89 : 2 : IOAddress::getAddress() const {
90 : 2 : return asio_address_;
91 : : }
92 : :
93 : 1 : IOAddress::operator uint32_t() const {
94 [ + - ]: 1 : if (getAddress().is_v4()) {
95 : 2 : return (getAddress().to_v4().to_ulong());
96 : : } else {
97 [ # # ][ # # ]: 0 : isc_throw(BadValue, "Can't convert " << toText()
[ # # ][ # # ]
[ # # ]
98 : : << " address to IPv4.");
99 : : }
100 : : }
101 : :
102 : : } // namespace asiolink
103 [ + - ][ + - ]: 30 : } // namespace isc
|