00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef CONEXUS_IPV6ADDRESS_H
00020 #define CONEXUS_IPV6ADDRESS_H
00021
00022 #include <string>
00023
00024 #include <sys/socket.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027
00028 #include <conexus/ipaddress.h>
00029 #include <conexus/except.h>
00030
00031 namespace Conexus
00032 {
00033
00034 namespace IPv4 {
00035 class Address;
00036 }
00037
00044 namespace IPv6
00045 {
00046
00047 typedef enum IPV4_EMBEDDED_TYPE { IPV4_COMPATIBLE, IPV4_MAPPED } IPV4_EMBEDDED_TYPE;
00048
00049 struct in6_addr operator&( const struct in6_addr& first, const struct in6_addr& second );
00050 struct in6_addr operator|( const struct in6_addr& first, const struct in6_addr& second );
00051 struct in6_addr operator^( const struct in6_addr& first, const struct in6_addr& second );
00052 struct in6_addr operator~( const struct in6_addr& addr );
00053
00054 struct in6_addr& operator&=( struct in6_addr& first, const struct in6_addr& second );
00055 struct in6_addr& operator|=( struct in6_addr& first, const struct in6_addr& second );
00056 struct in6_addr& operator^=( struct in6_addr& first, const struct in6_addr& second );
00057
00058 bool operator==( const struct in6_addr&, const struct in6_addr& );
00059
00060 bool operator!=( const struct in6_addr&, const struct in6_addr& );
00061
00076 class Address: public IPAddress
00077 {
00078 public:
00080 static const struct in6_addr LOOPBACK;
00081
00083 static const struct in6_addr IPV4_COMPATIBLE_PREFIX;
00084
00086 static const struct in6_addr IPV4_MAPPED_PREFIX;
00087
00089 typedef ConexusPointer<Address> pointer;
00090
00098 Address(const struct in6_addr& address = in6addr_any, uint16_t port = 0);
00099
00101 Address(const struct sockaddr_in6& addr);
00102
00104 Address(const struct sockaddr_storage& addr);
00105
00107 Address(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type = IPV4_MAPPED);
00108
00110 static Address::pointer create(const struct in6_addr& address = in6addr_any, uint16_t port = 0);
00111
00113 static Address::pointer create(const struct sockaddr_in6& addr);
00114
00116 static Address::pointer create(const struct sockaddr_storage& addr);
00117
00119 static Address::pointer create(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type = IPV4_MAPPED);
00120
00122 virtual ~Address();
00123
00125 struct in6_addr address() const;
00126
00128 std::string address_string() const;
00129
00131 struct in6_addr subnet_mask() const;
00132
00134 std::string subnet_mask_string() const;
00135
00137 unsigned prefix_length() const;
00138
00140 std::string prefix_length_string() const;
00141
00143 std::string address_subnet_mask_string() const;
00144
00146 std::string cidr_address_string() const;
00147
00149 struct in6_addr prefix() const;
00150
00152 std::string prefix_string() const;
00153
00155 struct in6_addr local_address() const;
00156
00158 std::string local_address_string() const;
00159
00161 uint16_t port() const;
00162
00164 std::string port_string() const;
00165
00167 void set_address(const struct in6_addr& address);
00168
00182 void set_address(std::string address);
00183
00185 virtual void set_address(std::string address, uint16_t port);
00186
00188 void set_address(uint32_t address, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00189
00191 void set_address_prefix_length(uint32_t address, unsigned prefix_length, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00192
00194 void set_prefix_length(unsigned prefix_length);
00195
00197 void set_prefix_length(std::string prefix_length);
00198
00200 void set_port(uint16_t port);
00201
00203 void set_port(std::string port);
00204
00206 std::string hostname() const;
00207
00209 std::string servicename() const;
00210
00212 bool is_reserved() const;
00213
00215 bool is_nsap() const;
00216
00218 bool is_ipx() const;
00219
00221 bool is_aggregatable_global() const;
00222
00224 bool is_link_local() const;
00225
00227 bool is_site_local() const;
00228
00230 bool is_multicast() const;
00231
00238 bool is_multicast_interface_local() const;
00239
00246 bool is_multicast_link_local() const;
00247
00253 bool is_multicast_site_local() const;
00254
00261 bool is_multicast_organization_local() const;
00262
00264 bool is_multicast_global() const;
00265
00267 bool is_any() const;
00268
00270 bool is_loopback() const;
00271
00273 bool is_ipv4() const;
00274
00276 bool is_ipv4_compatible() const;
00277
00279 bool is_ipv4_mapped() const;
00280
00282 socklen_t sockaddr_size() const;
00283
00285 struct sockaddr_in6& sockaddr_in();
00286
00288 struct sockaddr_in6* sockaddr_in_ptr();
00289
00290 operator struct sockaddr_in6*();
00291
00293 Address& operator=(const Address& other);
00294
00296 bool operator==(const Address& other);
00297
00299 virtual const std::string& object_type() { static std::string s("Conexus::IPv6::Address"); return s; }
00300
00302 virtual bool is_ipv4() { return this->is_ipv4_compatible(); }
00303
00305 virtual bool is_ipv6() { return true; }
00306
00307 protected:
00312 struct sockaddr_in6* m_psockaddr_in6;
00313
00315 unsigned m_prefix_length;
00316 };
00317
00322 std::string address_to_string(const struct in6_addr&);
00323
00328 struct in6_addr string_to_address(const std::string&);
00329
00331 void string_to_address_prefix(const std::string&, struct in6_addr& address, int& prefix);
00332
00337 bool is_valid_hostname(const std::string);
00338
00347 std::string address_to_hostname( const struct in6_addr& address ) throw (address_exception);
00348
00353 std::string service_to_servicename( uint16_t service ) throw (address_exception);
00354
00360 struct in6_addr hostname_to_address( const std::string& hostname ) throw (address_exception);
00361
00366 uint16_t servicename_to_service( const std::string& servicename ) throw (address_exception);
00367
00369 bool validate_subnet_mask( const struct in6_addr& subnet_mask ) throw();
00370
00372 struct in6_addr create_subnet_mask(unsigned prefix_length);
00373
00374 }
00375
00376 }
00377
00378 #endif