conexus logo

ipv4_address.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This file is part of the conexus library.                             *
00006  *                                                                         *
00007  *   The conexus library is free software; you can redistribute it and/or  *
00008  *   modify it under the terms of the GNU General Public License           *
00009  *   version 3 as published by the Free Software Foundation.               *
00010  *                                                                         *
00011  *   The conexus library is distributed in the hope that it will be        *
00012  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
00013  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00014  *   General Public License for more details.                              *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this software. If not see <http://www.gnu.org/licenses/>.  *
00018  ***************************************************************************/
00019 #ifndef CONEXUS_IPV4ADDRESS_H
00020 #define CONEXUS_IPV4ADDRESS_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/address.h>
00029 #include <conexus/ipaddress_interface.h>
00030 #include <conexus/except.h>
00031 
00032 namespace Conexus
00033 {
00034 
00035   namespace IPv6
00036   {
00037     class Address;
00038   }
00039 
00046   namespace IPv4
00047   {
00048 
00063     class Address: public Conexus::Address, public virtual IPAddressInterface
00064     {
00065       public:
00066 
00068         typedef ConexusPointer<Address> pointer;
00069 
00079         Address( uint32_t host = INADDR_ANY, uint16_t port = 0 );
00080 
00081         Address( const std::string& host, uint16_t port = 0 );
00082 
00084         Address( const struct sockaddr_in& addr );
00085 
00087         Address( const struct sockaddr_storage& addr );
00088 
00090         Address( const struct sockaddr& addr );
00091 
00093         Address( const IPv6::Address& addr );
00094 
00096         static pointer create( uint32_t host = INADDR_ANY, uint16_t port = 0 );
00097 
00098         static pointer create( const std::string& host, uint16_t port );
00099 
00101         static pointer create( const struct sockaddr_in& addr );
00102 
00104         static pointer create( const struct sockaddr_storage& addr );
00105 
00107         static pointer create( const struct sockaddr& addr );
00108 
00110         static pointer create( const IPv6::Address& ipv6addr );
00111 
00113         virtual ~Address();
00114 
00116         uint32_t host() const;
00117 
00119         std::string host_string() const;
00120 
00122         uint32_t subnet_mask() const;
00123 
00125         std::string subnet_mask_string() const;
00126 
00128         unsigned prefix_length() const;
00129 
00131         std::string prefix_length_string() const;
00132 
00134         std::string host_subnet_mask_string() const;
00135 
00137         std::string cidr_host_string() const;
00138 
00140         uint32_t prefix() const;
00141 
00143         std::string prefix_string() const;
00144 
00146         uint32_t broadcast_address() const;
00147 
00149         std::string broadcast_address_string() const;
00150 
00152         uint32_t local_address() const;
00153 
00155         std::string local_address_string() const;
00156 
00158         uint16_t port() const;
00159 
00161         std::string port_string() const;
00162 
00164         void set_host( uint32_t host );
00165 
00180         void set_host( const std::string& host );
00181 
00183         virtual void set_host( const std::string& host, uint16_t port );
00184 
00186         void set_host_subnet_mask( uint32_t host, uint32_t subnet_mask );
00187 
00189         void set_host_prefix_length( uint32_t host, unsigned prefix_length );
00190 
00192         void set_subnet_mask( uint32_t subnet_mask );
00193 
00195         void set_subnet_mask( const std::string& subnet_mask );
00196 
00198         void set_prefix_length( unsigned prefix_length );
00199 
00201         void set_prefix_length( const std::string& prefix_length );
00202 
00204         void set_port( uint16_t port );
00205 
00207         void set_port( const std::string& port );
00208 
00213         bool is_valid_hostname( const std::string& ) const;
00214 
00216         std::string hostname() const;
00217 
00219         std::string servicename() const;
00220 
00229         bool is_private() const;
00230 
00232         bool is_reserved() const;
00233 
00235         bool is_loopback() const;
00236 
00238         bool is_broadcast() const;
00239 
00241         bool is_multicast() const;
00242 
00244         bool is_any() const;
00245 
00247         socklen_t sockaddr_size() const;
00248 
00250         struct sockaddr_in& sockaddr_in();
00251 
00253         struct sockaddr_in* sockaddr_in_ptr();
00254 
00255         operator struct sockaddr_in*();
00256 
00257         Address& operator=( const Address& other );
00258 
00259         Address& operator=( const Conexus::Address& other );
00260 
00262         bool operator==( const Address& other ) const;
00263 
00264         virtual sigc::signal<void>& signal_changed( );
00265 
00266         virtual sigc::signal<void>& signal_host_changed( );
00267 
00268         virtual sigc::signal<void>& signal_port_changed( );
00269 
00270         virtual sigc::signal<void>& signal_prefix_changed( );
00271 
00273         virtual bool is_ipv4();
00274 
00276         virtual bool is_ipv6();
00277 
00278       protected:
00279         uint32_t m_host;
00280 
00281         uint16_t m_port;
00282 
00284         unsigned m_prefix_length;
00285 
00286         sigc::signal<void> m_signal_host_changed;
00287 
00288         sigc::signal<void> m_signal_port_changed;
00289 
00290         sigc::signal<void> m_signal_prefix_changed;
00291 
00292         sigc::signal<void> m_signal_changed;
00293 
00294     };
00295 
00300     std::string host_to_string( uint32_t );
00301 
00306     uint32_t string_to_host( const std::string& );
00307 
00312     void string_to_host_prefix( const std::string&, uint32_t& host, int& prefix );
00313 
00319     bool is_valid_hostname( const std::string& );
00320 
00325     std::string host_to_hostname( uint32_t host ) throw ( address_exception );
00326 
00331     std::string service_to_servicename( uint16_t service ) throw ( address_exception );
00332 
00337     uint32_t hostname_to_host( const std::string& hostname ) throw ( address_exception );
00338 
00343     uint16_t servicename_to_service( const std::string& servicename ) throw ( address_exception );
00344 
00349     bool validate_subnet_mask( uint32_t subnet_mask ) throw();
00350 
00355     uint32_t create_subnet_mask( unsigned prefix_length );
00356 
00361     unsigned subnet_mask_length( uint32_t subnet_mask ) throw();
00362 
00363   }
00364 
00365 }
00366 
00367 #endif

Generated on Wed Jul 8 15:50:07 2009 for conexus by doxygen 1.5.8