conexus/ipv6_address.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
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 
00062       bool operator!=( const struct in6_addr&, const struct in6_addr& );
00063 
00078       class Address: public IPAddress
00079       {
00080       public:
00081 
00083         static const struct in6_addr LOOPBACK;
00084 
00086         static const struct in6_addr IPV4_COMPATIBLE_PREFIX;
00087 
00089         static const struct in6_addr IPV4_MAPPED_PREFIX;
00090 
00092         typedef ConexusPointer<Address> pointer;
00093 
00101         Address(const struct in6_addr& address = in6addr_any, uint16_t port = 0);
00102 
00103         Address(uint32_t host, uint16_t port=0);
00104 
00105         Address(const std::string& host, uint16_t port=0);
00106 
00108         Address(const struct sockaddr_in6& addr);
00109 
00111         Address(const struct sockaddr_storage& addr);
00112 
00114         Address(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type = IPV4_MAPPED);
00115 
00117         static pointer create(const struct in6_addr& address = in6addr_any, uint16_t port = 0);
00118 
00119         static pointer create( uint32_t host, uint16_t port );
00120 
00121         static pointer create( const std::string& host, uint16_t port );
00122 
00124         static pointer create(const struct sockaddr_in6& addr);
00125 
00127         static pointer create(const struct sockaddr_storage& addr);
00128 
00130         static pointer create(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type = IPV4_MAPPED);
00131 
00133         virtual ~Address();
00134 
00136         struct in6_addr address() const;
00137 
00139         std::string address_string() const;
00140 
00142         struct in6_addr subnet_mask() const;
00143 
00145         std::string subnet_mask_string() const;
00146 
00148         unsigned prefix_length() const;
00149 
00151         std::string prefix_length_string() const;
00152 
00154         std::string address_subnet_mask_string() const;
00155 
00157         std::string cidr_address_string() const;
00158 
00160         struct in6_addr prefix() const;
00161 
00163         std::string prefix_string() const;
00164 
00166         struct in6_addr local_address() const;
00167 
00169         std::string local_address_string() const;
00170 
00172         uint16_t port() const;
00173 
00175         std::string port_string() const;
00176 
00178         void set_address(const struct in6_addr& address);
00179 
00193         void set_address(const std::string& address);
00194 
00196         virtual void set_address(const std::string& address, uint16_t port);
00197 
00199         void set_address(uint32_t address, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00200 
00202         void set_address_prefix_length(uint32_t address, unsigned prefix_length, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00203 
00205         void set_prefix_length(unsigned prefix_length);
00206 
00208         void set_prefix_length(const std::string& prefix_length);
00209 
00211         void set_port(uint16_t port);
00212 
00214         void set_port(const std::string& port);
00215 
00216         uint32_t flowinfo();
00217 
00218         void set_flowinfo( uint32_t f );
00219 
00220         uint32_t scope_id();
00221 
00222         void set_scope_id( uint32_t sid );
00223 
00225         std::string hostname() const;
00226 
00228         std::string servicename() const;
00229 
00231         bool is_reserved() const;
00232 
00234         bool is_nsap() const;
00235 
00237         bool is_ipx() const;
00238 
00240         bool is_aggregatable_global() const;
00241 
00243         bool is_link_local() const;
00244 
00246         bool is_site_local() const;
00247 
00249         bool is_multicast() const;
00250 
00257         bool is_multicast_interface_local() const;
00258 
00265         bool is_multicast_link_local() const;
00266 
00272         bool is_multicast_site_local() const;
00273 
00280         bool is_multicast_organization_local() const;
00281 
00283         bool is_multicast_global() const;
00284 
00286         bool is_any() const;
00287 
00289         bool is_loopback() const;
00290 
00292         bool is_ipv4() const;
00293 
00295         bool is_ipv4_compatible() const;
00296 
00298         bool is_ipv4_mapped() const;
00299 
00301         socklen_t sockaddr_size() const;
00302 
00304         struct sockaddr_in6& sockaddr_in();
00305 
00307         struct sockaddr_in6* sockaddr_in_ptr();
00308 
00309         operator struct sockaddr_in6*();
00310 
00312         Address& operator=(const Address& other);
00313 
00314         Address& operator=( const Conexus::Address& other );
00315 
00317         bool operator==(const Address& other);
00318 
00320         virtual bool is_ipv4() { return this->is_ipv4_compatible(); }
00321 
00323         virtual bool is_ipv6() { return true; }
00324 
00325         virtual sigc::signal<void> signal_changed( );
00326 
00327         virtual sigc::signal<void> signal_address_changed( );
00328 
00329         virtual sigc::signal<void> signal_port_changed( );
00330 
00331         virtual sigc::signal<void> signal_flowinfo_changed( );
00332 
00333         virtual sigc::signal<void> signal_scope_id_changed( );
00334 
00335         virtual sigc::signal<void> signal_prefix_changed( );
00336 
00337         protected:
00338           struct in6_addr m_address;
00339 
00340           uint16_t m_port;
00341 
00342           uint32_t m_flowinfo;
00343 
00344           uint32_t m_scope_id;
00345 
00347           unsigned m_prefix_length;
00348 
00349           sigc::signal<void> m_signal_changed;
00350 
00351           sigc::signal<void> m_signal_address_changed;
00352 
00353           sigc::signal<void> m_signal_port_changed;
00354 
00355           sigc::signal<void> m_signal_flowinfo_changed;
00356 
00357           sigc::signal<void> m_signal_scope_id_changed;
00358 
00359           sigc::signal<void> m_signal_prefix_changed;
00360       };
00361 
00366       std::string address_to_string(const struct in6_addr&);
00367 
00372       struct in6_addr string_to_address(const std::string&);
00373 
00375       void string_to_address_prefix(const std::string&, struct in6_addr& address, int& prefix);
00376 
00381       bool is_valid_hostname(const std::string&);
00382 
00391       std::string address_to_hostname( const struct in6_addr& address ) throw (address_exception);
00392 
00397       std::string service_to_servicename( uint16_t service ) throw (address_exception);
00398 
00404       struct in6_addr hostname_to_address( const std::string& hostname ) throw (address_exception);
00405 
00410       uint16_t servicename_to_service( const std::string& servicename ) throw (address_exception);
00411 
00413       bool validate_subnet_mask( const struct in6_addr& subnet_mask ) throw();
00414 
00416       struct in6_addr create_subnet_mask(unsigned prefix_length);
00417 
00418   }
00419 
00420 }
00421 
00422 #endif

Generated on Tue Mar 13 19:54:45 2007 by  doxygen 1.5.1