1 /* net6 - Library providing IPv4/IPv6 network access 2 * Copyright (C) 2005 Armin Burgmeier / 0x539 dev group 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free 16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 */ 18 19 #ifndef _NET6_ADDRESS_HPP_ 20 #define _NET6_ADDRESS_HPP_ 21 22 #include <inttypes.h> 23 24 #ifdef WIN32 25 #include <winsock2.h> 26 #include <ws2tcpip.h> 27 #else 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <netinet/in.h> 31 #include <netinet/in_systm.h> 32 #include <netinet/ip.h> 33 #endif 34 35 #include <list> 36 #include <string> 37 38 namespace net6 39 { 40 41 /** Abstract base class representing an internet address 42 */ 43 44 class address 45 { 46 public: 47 address(); 48 virtual ~address(); 49 50 /** Creates a copy of this address. The caller is responsible for 51 * freeing it. 52 */ 53 virtual address* clone() const = 0; 54 55 /** Returns the address family of this address (AF_INET or AF_INET6). 56 */ 57 int get_family() const; 58 59 /** Returns a human-readable string of this address 60 * (for example 127.0.0.1, or ::1). 61 */ 62 virtual std::string get_name() const = 0; 63 64 /** Returns the size of underlaying C sockaddr object. 65 */ 66 virtual socklen_t get_size() const = 0; 67 68 /** Returns the underlaying C sockaddr object. 69 */ cobj()70 sockaddr* cobj() { return addr; } cobj() const71 const sockaddr* cobj() const { return addr; } 72 73 protected: 74 sockaddr* addr; 75 }; 76 77 /** IPv4 internet address 78 */ 79 80 class ipv4_address : public address 81 { 82 public: 83 static const uint32_t ANY; 84 static const uint32_t NONE; 85 static const uint32_t BROADCAST; 86 static const uint32_t LOOPBACK; 87 88 /** Creates a new IPv4 address of type ANY with port <em>port</em>. 89 */ 90 ipv4_address(unsigned int port = 0); 91 92 /** Creates a new IPv4 address from a 32 bit integer in network 93 * byte order and with port <em>port</em>. 94 */ 95 static ipv4_address create_from_address(uint32_t ip_address, 96 unsigned int port = 0); 97 98 /** Performs a DNS lookup of the host <em>hostname</em> and stores 99 * its IPv4 address, if any. Otherwise, a net6::error is thrown. 100 */ 101 static ipv4_address create_from_hostname(const std::string& hostname, 102 unsigned int port = 0); 103 104 /** Creates an IPv4 address by copying a C sockaddr object. 105 */ 106 ipv4_address(const sockaddr_in* other); 107 108 /** Creates a copy of an IPv4 address. 109 */ 110 ipv4_address(const ipv4_address& other); 111 virtual ~ipv4_address(); 112 113 /** Performs a DNS lookup of <em>hostname</em> and returns a list 114 * with all addresses found for this host. 115 */ 116 static std::list<ipv4_address> list(const std::string& hostname, 117 unsigned int port = 0); 118 119 /** Copies an IPv4 address. 120 */ 121 ipv4_address& operator=(const ipv4_address& other); 122 123 /** Assigns a C sockaddr object to this IPv4 address. 124 */ 125 ipv4_address& operator=(const sockaddr_in* other); 126 127 /** Creates a copy of this IPv4 address on the heap. The caller 128 * is responsible for freeing it. 129 */ 130 virtual address* clone() const; 131 132 /** Returns a human-readable string of this IPv4 address (for 133 * example 127.0.0.1). 134 */ 135 virtual std::string get_name() const; 136 137 /** Returns the size of the underlaying C sockaddr_in object. 138 */ 139 virtual socklen_t get_size() const; 140 141 /** Returns the port assigned to this IPv4 address. 142 */ 143 unsigned int get_port() const; 144 145 /** Changes the port assigned to this IPv4 address. 146 */ 147 void set_port(unsigned int port); 148 149 /** Provides access to the underlaying C sockaddr_in object. 150 */ cobj()151 sockaddr_in* cobj() { return reinterpret_cast<sockaddr_in*>(addr); } cobj() const152 const sockaddr_in* cobj() const 153 { return reinterpret_cast<sockaddr_in*>(addr); } 154 protected: 155 // ipv4_address(); 156 }; 157 158 /** IPv6 internet address 159 */ 160 161 class ipv6_address : public address 162 { 163 public: 164 static const uint8_t ANY[16]; 165 static const uint8_t LOOPBACK[16]; 166 167 /** Creates a new IPv6 address of type ANY and port <em>port</em>. 168 */ 169 ipv6_address(unsigned int port = 0, unsigned int flowinfo = 0, 170 unsigned int scope_id = 0); 171 172 /** Creates a new IPv6 address from the given byte array in 173 * network byte order and with port <em>port</em>. 174 */ 175 static ipv6_address create_from_address(const uint8_t ip_address[16], 176 unsigned int port = 0, 177 unsigned int flowinfo = 0, 178 unsigned int scope_id = 0); 179 180 /** Performs a DNS lookup to resolve <em>hostname</em> to an 181 * IPv6 address. If the host could not be resolved, a net6::error is 182 * thrown. 183 */ 184 static ipv6_address create_from_hostname(const std::string& hostname, 185 unsigned int port = 0, 186 unsigned int flowinfo = 0, 187 unsigned int scope_id = 0); 188 189 /** Creates an IPv6 address from a C sockaddr_in6 object. 190 */ 191 ipv6_address(const sockaddr_in6* other); 192 193 /** Creates a copy of an IPv6 address. 194 */ 195 ipv6_address(const ipv6_address& other); 196 virtual ~ipv6_address(); 197 198 /** Performs a DNS lookup to retrieve a list with all IPv6 199 * addresses of <em>hostname</em>. 200 */ 201 static std::list<ipv6_address> list(const std::string& hostname, 202 unsigned int port = 0, 203 unsigned int flowinfo = 0, 204 unsigned int scope_id = 0); 205 206 /** Creates a copy of an IPv6 address. 207 */ 208 ipv6_address& operator=(const ipv6_address& other); 209 210 /** Assigns a C sockaddr_in6 object to this IPv6 address object. 211 */ 212 ipv6_address& operator=(const sockaddr_in6* addr); 213 214 /** Creates a copy of this IPv6 address on the heap. The caller is 215 * responsible for freeing it. 216 */ 217 virtual address* clone() const; 218 219 /** Returns a human-readable string of the IPv6 address (like ::1). 220 */ 221 virtual std::string get_name() const; 222 223 /** Returns the size of the underlaying C sockaddr_in6 object. 224 */ 225 virtual socklen_t get_size() const; 226 227 /** Returns the port associated with this IPv6 address object. 228 */ 229 unsigned int get_port() const; 230 231 /** Returns the IPv6 flowinfo assigned to this IPv6 address. 232 */ 233 unsigned int get_flowinfo() const; 234 235 /** Returns the IPv6 scope id assigned to this IPv6 address. 236 */ 237 unsigned int get_scope_id() const; 238 239 /** Changes the port associated with this IPv6 address object. 240 */ 241 void set_port(unsigned int port); 242 243 /** Changes the IPv6 flowinfo assigned to this IPv6 address. 244 */ 245 void set_flowinfo(unsigned int flowinfo); 246 247 /** Changes the IPv6 scope id assigned to this IPv6 address. 248 */ 249 void set_scope_id(unsigned int scope_id); 250 251 /** Provides access to the underlaying C sockaddr_in6 object. 252 */ cobj()253 sockaddr_in6* cobj() { return reinterpret_cast<sockaddr_in6*>(addr); } cobj() const254 const sockaddr_in6* cobj() const 255 { return reinterpret_cast<sockaddr_in6*>(addr); } 256 257 protected: 258 // ipv6_address(); 259 }; 260 261 } 262 263 #endif 264