conexus/ipv4_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_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/ipaddress.h>
00029 #include <conexus/except.h>
00030 
00031 namespace Conexus
00032 {
00033 
00034   namespace IPv6
00035   {
00036     class Address;
00037   }
00038 
00045   namespace IPv4
00046   {
00047 
00062     class Address: public IPAddress
00063     {
00064       public:
00065 
00067         typedef ConexusPointer<Address> pointer;
00068 
00078         Address( uint32_t address = INADDR_ANY, uint16_t port = 0 );
00079 
00080         Address( const std::string& host, uint16_t port = 0 );
00081 
00083         Address( const struct sockaddr_in& addr );
00084 
00086         Address( const struct sockaddr_storage& addr );
00087 
00089         Address( const struct sockaddr& addr );
00090 
00092         Address( const IPv6::Address& addr );
00093 
00095         static pointer create( uint32_t address = INADDR_ANY, uint16_t port = 0 );
00096 
00097         static pointer create( const std::string& host, uint16_t port );
00098 
00100         static pointer create( const struct sockaddr_in& addr );
00101 
00103         static pointer create( const struct sockaddr_storage& addr );
00104 
00106         static pointer create( const struct sockaddr& addr );
00107 
00109         static pointer create( const IPv6::Address& ipv6addr );
00110 
00112         virtual ~Address();
00113 
00115         uint32_t address() const;
00116 
00118         std::string address_string() const;
00119 
00121         uint32_t subnet_mask() const;
00122 
00124         std::string subnet_mask_string() const;
00125 
00127         unsigned prefix_length() const;
00128 
00130         std::string prefix_length_string() const;
00131 
00133         std::string address_subnet_mask_string() const;
00134 
00136         std::string cidr_address_string() const;
00137 
00139         uint32_t prefix() const;
00140 
00142         std::string prefix_string() const;
00143 
00145         uint32_t broadcast_address() const;
00146 
00148         std::string broadcast_address_string() const;
00149 
00151         uint32_t local_address() const;
00152 
00154         std::string local_address_string() const;
00155 
00157         uint16_t port() const;
00158 
00160         std::string port_string() const;
00161 
00163         void set_address( uint32_t address );
00164 
00179         void set_address( const std::string& address );
00180 
00182         virtual void set_address( const std::string& address, uint16_t port );
00183 
00185         void set_address_subnet_mask( uint32_t address, uint32_t subnet_mask );
00186 
00188         void set_address_prefix_length( uint32_t address, unsigned prefix_length );
00189 
00191         void set_subnet_mask( uint32_t subnet_mask );
00192 
00194         void set_subnet_mask( const std::string& subnet_mask );
00195 
00197         void set_prefix_length( unsigned prefix_length );
00198 
00200         void set_prefix_length( const std::string& prefix_length );
00201 
00203         void set_port( uint16_t port );
00204 
00206         void set_port( const std::string& port );
00207 
00212         bool is_valid_hostname( const std::string& ) const;
00213 
00215         std::string hostname() const;
00216 
00218         std::string servicename() const;
00219 
00228         bool is_private() const;
00229 
00231         bool is_reserved() const;
00232 
00234         bool is_loopback() const;
00235 
00237         bool is_broadcast() const;
00238 
00240         bool is_multicast() const;
00241 
00243         bool is_any() const;
00244 
00246         socklen_t sockaddr_size() const;
00247 
00249         struct sockaddr_in& sockaddr_in();
00250 
00252         struct sockaddr_in* sockaddr_in_ptr();
00253 
00254         operator struct sockaddr_in*();
00255 
00256         Address& operator=( const Address& other );
00257 
00258         Address& operator=( const Conexus::Address& other );
00259 
00261         bool operator==( const Address& other ) const;
00262 
00263         virtual sigc::signal<void> signal_changed( );
00264 
00265         virtual sigc::signal<void> signal_address_changed( );
00266 
00267         virtual sigc::signal<void> signal_port_changed( );
00268 
00269         virtual sigc::signal<void> signal_prefix_changed( );
00270 
00272         virtual bool is_ipv4();
00273 
00275         virtual bool is_ipv6();
00276 
00277       protected:
00278         uint32_t m_address;
00279 
00280         uint16_t m_port;
00281 
00283         unsigned m_prefix_length;
00284 
00285         sigc::signal<void> m_signal_address_changed;
00286 
00287         sigc::signal<void> m_signal_port_changed;
00288 
00289         sigc::signal<void> m_signal_prefix_changed;
00290 
00291         sigc::signal<void> m_signal_changed;
00292 
00293     };
00294 
00296     std::string address_to_string( uint32_t );
00297 
00299     uint32_t string_to_address( const std::string& );
00300 
00302     void string_to_address_prefix( const std::string&, uint32_t& address, int& prefix );
00303 
00308     bool is_valid_hostname( const std::string& );
00309 
00311     std::string address_to_hostname( uint32_t address ) throw ( address_exception );
00312 
00314     std::string service_to_servicename( uint16_t service ) throw ( address_exception );
00315 
00317     uint32_t hostname_to_address( const std::string& hostname ) throw ( address_exception );
00318 
00320     uint16_t servicename_to_service( const std::string& servicename ) throw ( address_exception );
00321 
00323     bool validate_subnet_mask( uint32_t subnet_mask ) throw();
00324 
00326     uint32_t create_subnet_mask( unsigned prefix_length );
00327 
00329     unsigned subnet_mask_length( uint32_t subnet_mask ) throw();
00330 
00331   }
00332 
00333 }
00334 
00335 #endif

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