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/ip_address.h>
00029 #include <conexus/error.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       bool operator!=( const struct in6_addr&, const struct in6_addr& );
00060 
00075       class Address: public IPBase::AddressBase
00076       {
00077       public:
00078         static const struct in6_addr LOOPBACK;
00079         static const struct in6_addr IPV4_COMPATIBLE_PREFIX;
00080         static const struct in6_addr IPV4_MAPPED_PREFIX;
00081         
00082         typedef boost::shared_ptr<Address> pointer;
00083 
00091         Address(const struct in6_addr& address=in6addr_any, uint16_t port=0);
00092 
00093         Address(const struct sockaddr_in6& addr);
00094 
00095         Address(const struct sockaddr_storage& addr);
00096         
00097         Address(const IPv4::Address& ipv4addr, IPV4_EMBEDDED_TYPE ipv4type=IPV4_MAPPED);
00098 
00099         static Address::pointer create(const struct in6_addr& address=in6addr_any, uint16_t port=0);
00100 
00101         static Address::pointer create(const struct sockaddr_in6& addr);
00102 
00103         static Address::pointer create(const struct sockaddr_storage& addr);
00104 
00105         static Address::pointer create(const IPv4::Address& ipv4addr);
00106 
00107         virtual ~Address();
00108 
00110         struct in6_addr address() const;
00111 
00113         std::string address_string() const;
00114 
00116         struct in6_addr subnet_mask() const;
00117 
00119         std::string subnet_mask_string() const;
00120 
00122         unsigned prefix_length() const;
00123 
00125         std::string prefix_length_string() const;
00126 
00128         std::string address_subnet_mask_string() const;
00129 
00131         std::string cidr_address_string() const;
00132 
00134         struct in6_addr prefix() const;
00135 
00137         std::string prefix_string() const;
00138 
00140         struct in6_addr local_address() const;
00141 
00143         std::string local_address_string() const;
00144 
00146         uint16_t port() const;
00147 
00149         std::string port_string() const;
00150 
00152         void set_address(const struct in6_addr& address);
00153 
00167         void set_address(std::string address);
00168 
00170         virtual void set_address(std::string address, uint16_t port);
00171 
00173         void set_address(uint32_t address, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00174 
00176         void set_address_prefix_length(uint32_t address, unsigned prefix_length, IPV4_EMBEDDED_TYPE type=IPV4_MAPPED);
00177 
00179         void set_prefix_length(unsigned prefix_length);
00180 
00182         void set_prefix_length(std::string prefix_length);
00183 
00185         void set_port(uint16_t port);
00186 
00188         void set_port(std::string port);
00189 
00191         std::string hostname() const;
00192 
00194         std::string servicename() const;
00195 
00197         bool is_reserved() const;
00198         
00200         bool is_nsap() const;
00201 
00203         bool is_ipx() const;
00204 
00206         bool is_aggregatable_global() const;
00207 
00209         bool is_link_local() const;
00210         
00212         bool is_site_local() const;
00213         
00215         bool is_multicast() const;
00216 
00223         bool is_multicast_interface_local() const;
00224 
00231         bool is_multicast_link_local() const;
00232 
00238         bool is_multicast_site_local() const;
00239 
00246         bool is_multicast_organization_local() const;
00247 
00249         bool is_multicast_global() const;
00250 
00252         bool is_any() const;
00253 
00255         bool is_loopback() const;
00256 
00258         bool is_ipv4() const;
00259 
00261         bool is_ipv4_compatible() const;
00262 
00264         bool is_ipv4_mapped() const;
00265 
00267         socklen_t sockaddr_size() const;
00268 
00270         struct sockaddr_in6& sockaddr_in();
00271 
00273         struct sockaddr_in6* sockaddr_in_ptr();
00274 
00275         Address& operator=(const Address& other);
00276         
00277         virtual const std::string& object_type() { static std::string s("Conexus::IPv6::Address"); return s; }
00278 
00279         virtual bool is_ipv4() { return this->is_ipv4_compatible(); }
00280         
00281         virtual bool is_ipv6() { return true; }
00282 
00283         protected:
00284           struct sockaddr_in6* m_psockaddr_in6;
00285           unsigned m_prefix_length;
00286       };
00287 
00292       std::string address_to_string(const struct in6_addr&);
00293 
00298       struct in6_addr string_to_address(const std::string&);
00299 
00301       void string_to_address_prefix(const std::string&, struct in6_addr& address, int& prefix);
00302 
00307       bool is_valid_hostname(const std::string);
00308 
00317       std::string address_to_hostname( const struct in6_addr& address ) throw (address_error);
00318 
00323       std::string service_to_servicename( uint16_t service ) throw (address_error);
00324 
00330       struct in6_addr hostname_to_address( const std::string& hostname ) throw (address_error);
00331 
00336       uint16_t servicename_to_service( const std::string& servicename ) throw (address_error);
00337 
00339       bool validate_subnet_mask( const struct in6_addr& subnet_mask ) throw();
00340 
00342       struct in6_addr create_subnet_mask(unsigned prefix_length);
00343 
00344   }
00345 
00346 }
00347 
00348 #endif

Generated on Sun Aug 6 12:16:57 2006 by  doxygen 1.4.6