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
00048 typedef enum IPV4_EMBEDDED_TYPE { IPV4_COMPATIBLE, IPV4_MAPPED } IPV4_EMBEDDED_TYPE;
00049
00051 struct in6_addr operator&( const struct in6_addr& first, const struct in6_addr& second );
00052
00054 struct in6_addr operator|( const struct in6_addr& first, const struct in6_addr& second );
00055
00057 struct in6_addr operator^( const struct in6_addr& first, const struct in6_addr& second );
00058
00060 struct in6_addr operator~( const struct in6_addr& addr );
00061
00063 struct in6_addr& operator&=( struct in6_addr& first, const struct in6_addr& second );
00064
00066 struct in6_addr& operator|=( struct in6_addr& first, const struct in6_addr& second );
00067
00069 struct in6_addr& operator^=( struct in6_addr& first, const struct in6_addr& second );
00070
00072 bool operator==( const struct in6_addr&, const struct in6_addr& );
00073
00075 bool operator<( const struct in6_addr&, const struct in6_addr& );
00076
00078 bool operator!=( const struct in6_addr&, const struct in6_addr& );
00079
00094 class Address: public IPAddress
00095 {
00096 public:
00097
00099 static const struct in6_addr LOOPBACK;
00100
00102 static const struct in6_addr IPV4_COMPATIBLE_PREFIX;
00103
00105 static const struct in6_addr IPV4_MAPPED_PREFIX;
00106
00108 typedef ConexusPointer<Address> pointer;
00109
00117 Address(const struct in6_addr& address = in6addr_any, uint16_t port = 0);
00118
00119 Address(uint32_t host, uint16_t port=0);
00120
00121 Address(const std::string& host, uint16_t port=0);
00122
00124 Address(const struct sockaddr_in6& addr);
00125
00127 Address(const struct sockaddr_storage& addr);
00128
00130 Address(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type = IPV4_MAPPED);
00131
00133 static pointer create(const struct in6_addr& address = in6addr_any, uint16_t port = 0);
00134
00135 static pointer create( uint32_t host, uint16_t port );
00136
00137 static pointer create( const std::string& host, uint16_t port );
00138
00140 static pointer create(const struct sockaddr_in6& addr);
00141
00143 static pointer create(const struct sockaddr_storage& addr);
00144
00146 static pointer create(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type = IPV4_MAPPED);
00147
00149 virtual ~Address();
00150
00152 struct in6_addr address() const;
00153
00155 std::string address_string() const;
00156
00158 struct in6_addr subnet_mask() const;
00159
00161 std::string subnet_mask_string() const;
00162
00164 unsigned prefix_length() const;
00165
00167 std::string prefix_length_string() const;
00168
00170 std::string address_subnet_mask_string() const;
00171
00173 std::string cidr_address_string() const;
00174
00176 struct in6_addr prefix() const;
00177
00179 std::string prefix_string() const;
00180
00182 struct in6_addr local_address() const;
00183
00185 std::string local_address_string() const;
00186
00188 uint16_t port() const;
00189
00191 std::string port_string() const;
00192
00194 void set_address(const struct in6_addr& address);
00195
00209 void set_address(const std::string& address);
00210
00212 virtual void set_address(const std::string& address, uint16_t port);
00213
00215 void set_address(uint32_t address, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00216
00218 void set_address_prefix_length(uint32_t address, unsigned prefix_length, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00219
00221 void set_prefix_length(unsigned prefix_length);
00222
00224 void set_prefix_length(const std::string& prefix_length);
00225
00227 void set_port(uint16_t port);
00228
00230 void set_port(const std::string& port);
00231
00232 uint32_t flowinfo();
00233
00234 void set_flowinfo( uint32_t f );
00235
00236 uint32_t scope_id();
00237
00238 void set_scope_id( uint32_t sid );
00239
00241 std::string hostname() const;
00242
00244 std::string servicename() const;
00245
00247 bool is_reserved() const;
00248
00250 bool is_nsap() const;
00251
00253 bool is_ipx() const;
00254
00256 bool is_aggregatable_global() const;
00257
00259 bool is_link_local() const;
00260
00262 bool is_site_local() const;
00263
00265 bool is_multicast() const;
00266
00273 bool is_multicast_interface_local() const;
00274
00281 bool is_multicast_link_local() const;
00282
00288 bool is_multicast_site_local() const;
00289
00296 bool is_multicast_organization_local() const;
00297
00299 bool is_multicast_global() const;
00300
00302 bool is_any() const;
00303
00305 bool is_loopback() const;
00306
00308 bool is_ipv4() const;
00309
00311 bool is_ipv4_compatible() const;
00312
00314 bool is_ipv4_mapped() const;
00315
00317 socklen_t sockaddr_size() const;
00318
00320 struct sockaddr_in6& sockaddr_in();
00321
00323 struct sockaddr_in6* sockaddr_in_ptr();
00324
00325 operator struct sockaddr_in6*();
00326
00328 Address& operator=(const Address& other);
00329
00330 Address& operator=( const Conexus::Address& other );
00331
00333 bool operator==(const Address& other);
00334
00336 virtual bool is_ipv4() { return this->is_ipv4_compatible(); }
00337
00339 virtual bool is_ipv6() { return true; }
00340
00341 virtual sigc::signal<void> signal_changed( );
00342
00343 virtual sigc::signal<void> signal_address_changed( );
00344
00345 virtual sigc::signal<void> signal_port_changed( );
00346
00347 virtual sigc::signal<void> signal_flowinfo_changed( );
00348
00349 virtual sigc::signal<void> signal_scope_id_changed( );
00350
00351 virtual sigc::signal<void> signal_prefix_changed( );
00352
00353 protected:
00354 struct in6_addr m_address;
00355
00356 uint16_t m_port;
00357
00358 uint32_t m_flowinfo;
00359
00360 uint32_t m_scope_id;
00361
00363 unsigned m_prefix_length;
00364
00365 sigc::signal<void> m_signal_changed;
00366
00367 sigc::signal<void> m_signal_address_changed;
00368
00369 sigc::signal<void> m_signal_port_changed;
00370
00371 sigc::signal<void> m_signal_flowinfo_changed;
00372
00373 sigc::signal<void> m_signal_scope_id_changed;
00374
00375 sigc::signal<void> m_signal_prefix_changed;
00376 };
00377
00383 std::string address_to_string(const struct in6_addr&);
00384
00390 struct in6_addr string_to_address(const std::string&);
00391
00396 void string_to_address_prefix(const std::string&, struct in6_addr& address, int& prefix);
00397
00403 bool is_valid_hostname(const std::string&);
00404
00414 std::string address_to_hostname( const struct in6_addr& address ) throw (address_exception);
00415
00421 std::string service_to_servicename( uint16_t service ) throw (address_exception);
00422
00429 struct in6_addr hostname_to_address( const std::string& hostname ) throw (address_exception);
00430
00436 uint16_t servicename_to_service( const std::string& servicename ) throw (address_exception);
00437
00442 bool validate_subnet_mask( const struct in6_addr& subnet_mask ) throw();
00443
00448 struct in6_addr create_subnet_mask(unsigned prefix_length);
00449
00450 }
00451
00452 }
00453
00454 #endif