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/ip_address.h>
00029 #include <conexus/error.h>
00030 
00031 namespace Conexus
00032   {
00033 
00034     namespace IPv6 {
00035       class Address;
00036     }
00037     
00044   namespace IPv4
00045     {
00046 
00061       class Address: public IPBase::AddressBase
00062       {
00063       public:
00064         typedef boost::shared_ptr<Address> pointer;
00065 
00075         Address(uint32_t address=INADDR_ANY, uint16_t port=0);
00076 
00077         Address(const struct sockaddr_in& addr);
00078 
00079         Address(const struct sockaddr_storage& addr);
00080 
00081         Address(const struct sockaddr& addr);
00082         
00083         Address(const IPv6::Address& addr);
00084         
00085         static Address::pointer create( uint32_t address=INADDR_ANY, uint16_t port=0 );
00086 
00087         static Address::pointer create(const struct sockaddr_in& addr);
00088 
00089         static Address::pointer create(const struct sockaddr_storage& addr);
00090 
00091         static Address::pointer create(const struct sockaddr& addr);
00092         
00093         static Address::pointer create(const IPv6::Address& ipv6addr);
00094         
00095         virtual ~Address();
00096 
00098         uint32_t address() const;
00099 
00101         std::string address_string() const;
00102 
00104         uint32_t subnet_mask() const;
00105 
00107         std::string subnet_mask_string() const;
00108 
00110         unsigned prefix_length() const;
00111 
00113         std::string prefix_length_string() const;
00114 
00116         std::string address_subnet_mask_string() const;
00117 
00119         std::string cidr_address_string() const;
00120 
00122         uint32_t prefix() const;
00123 
00125         std::string prefix_string() const;
00126 
00128         uint32_t broadcast_address() const;
00129 
00131         std::string broadcast_address_string() const;
00132 
00134         uint32_t local_address() const;
00135 
00137         std::string local_address_string() const;
00138 
00140         uint16_t port() const;
00141 
00143         std::string port_string() const;
00144 
00146         void set_address(uint32_t address);
00147 
00162         void set_address(std::string address);
00163 
00165         virtual void set_address(std::string address, uint16_t port);
00166 
00168         void set_address_subnet_mask(uint32_t address, uint32_t subnet_mask);
00169 
00171         void set_address_prefix_length(uint32_t address, unsigned prefix_length);
00172 
00174         void set_subnet_mask(uint32_t subnet_mask);
00175 
00177         void set_subnet_mask(std::string subnet_mask);
00178 
00180         void set_prefix_length(unsigned prefix_length);
00181 
00183         void set_prefix_length(std::string prefix_length);
00184 
00186         void set_port(uint16_t port);
00187 
00189         void set_port(std::string port);
00190 
00195         bool is_valid_hostname(const std::string) const;
00196 
00198         std::string hostname() const;
00199 
00201         std::string servicename() const;
00202 
00211         bool is_private() const;
00212 
00214         bool is_reserved() const;
00215 
00217         bool is_loopback() const;
00218 
00220         bool is_broadcast() const;
00221 
00223         bool is_multicast() const;
00224 
00226         bool is_any() const;
00227 
00229         socklen_t sockaddr_size() const;
00230 
00232         struct sockaddr_in& sockaddr_in();
00233 
00235         struct sockaddr_in* sockaddr_in_ptr();
00236 
00237         Address& operator=(const Address& other);
00238         
00239         virtual const std::string& object_type() { static std::string s("Conexus::IPv4::Address"); return s; }
00240 
00241         virtual bool is_ipv4() { return true; }
00242         
00243         virtual bool is_ipv6() { return false; }
00244 
00245         protected:
00246         struct sockaddr_in* m_psockaddr_in;
00247         unsigned m_prefix_length;
00248 
00249       };
00250 
00251       std::string address_to_string(uint32_t);
00252 
00253       uint32_t string_to_address(std::string);
00254 
00256       void string_to_address_prefix(const std::string&, uint32_t& address, int& prefix);
00257 
00262       bool is_valid_hostname(const std::string);
00263 
00264       std::string address_to_hostname( uint32_t address ) throw (address_error);
00265 
00266       std::string service_to_servicename( uint16_t service ) throw (address_error);
00267 
00268       uint32_t hostname_to_address( std::string hostname ) throw (address_error);
00269 
00270       uint16_t servicename_to_service( std::string servicename ) throw (address_error);
00271 
00273       bool validate_subnet_mask( uint32_t subnet_mask ) throw();
00274 
00276       uint32_t create_subnet_mask(unsigned prefix_length);
00277 
00278       unsigned subnet_mask_length( uint32_t subnet_mask ) throw();
00279 
00280   }
00281 
00282 }
00283 
00284 #endif

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