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 00066 typedef boost::shared_ptr<Address> pointer; 00067 00077 Address(uint32_t address=INADDR_ANY, uint16_t port=0); 00078 00080 Address(const struct sockaddr_in& addr); 00081 00083 Address(const struct sockaddr_storage& addr); 00084 00086 Address(const struct sockaddr& addr); 00087 00089 Address(const IPv6::Address& addr); 00090 00092 static Address::pointer create( uint32_t address=INADDR_ANY, uint16_t port=0 ); 00093 00095 static Address::pointer create(const struct sockaddr_in& addr); 00096 00098 static Address::pointer create(const struct sockaddr_storage& addr); 00099 00101 static Address::pointer create(const struct sockaddr& addr); 00102 00104 static Address::pointer create(const IPv6::Address& ipv6addr); 00105 00107 virtual ~Address(); 00108 00110 uint32_t address() const; 00111 00113 std::string address_string() const; 00114 00116 uint32_t 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 uint32_t prefix() const; 00135 00137 std::string prefix_string() const; 00138 00140 uint32_t broadcast_address() const; 00141 00143 std::string broadcast_address_string() const; 00144 00146 uint32_t local_address() const; 00147 00149 std::string local_address_string() const; 00150 00152 uint16_t port() const; 00153 00155 std::string port_string() const; 00156 00158 void set_address(uint32_t address); 00159 00174 void set_address(std::string address); 00175 00177 virtual void set_address(std::string address, uint16_t port); 00178 00180 void set_address_subnet_mask(uint32_t address, uint32_t subnet_mask); 00181 00183 void set_address_prefix_length(uint32_t address, unsigned prefix_length); 00184 00186 void set_subnet_mask(uint32_t subnet_mask); 00187 00189 void set_subnet_mask(std::string subnet_mask); 00190 00192 void set_prefix_length(unsigned prefix_length); 00193 00195 void set_prefix_length(std::string prefix_length); 00196 00198 void set_port(uint16_t port); 00199 00201 void set_port(std::string port); 00202 00207 bool is_valid_hostname(const std::string) const; 00208 00210 std::string hostname() const; 00211 00213 std::string servicename() const; 00214 00223 bool is_private() const; 00224 00226 bool is_reserved() const; 00227 00229 bool is_loopback() const; 00230 00232 bool is_broadcast() const; 00233 00235 bool is_multicast() const; 00236 00238 bool is_any() const; 00239 00241 socklen_t sockaddr_size() const; 00242 00244 struct sockaddr_in& sockaddr_in(); 00245 00247 struct sockaddr_in* sockaddr_in_ptr(); 00248 00249 Address& operator=(const Address& other); 00250 00252 bool operator==(const Address& other) const; 00253 00255 virtual const std::string& object_type() { static std::string s("Conexus::IPv4::Address"); return s; } 00256 00258 virtual bool is_ipv4() { return true; } 00259 00261 virtual bool is_ipv6() { return false; } 00262 00263 protected: 00268 struct sockaddr_in* m_psockaddr_in; 00269 00271 unsigned m_prefix_length; 00272 00273 }; 00274 00276 std::string address_to_string(uint32_t); 00277 00279 uint32_t string_to_address(std::string); 00280 00282 void string_to_address_prefix(const std::string&, uint32_t& address, int& prefix); 00283 00288 bool is_valid_hostname(const std::string); 00289 00291 std::string address_to_hostname( uint32_t address ) throw (address_error); 00292 00294 std::string service_to_servicename( uint16_t service ) throw (address_error); 00295 00297 uint32_t hostname_to_address( std::string hostname ) throw (address_error); 00298 00300 uint16_t servicename_to_service( std::string servicename ) throw (address_error); 00301 00303 bool validate_subnet_mask( uint32_t subnet_mask ) throw(); 00304 00306 uint32_t create_subnet_mask(unsigned prefix_length); 00307 00309 unsigned subnet_mask_length( uint32_t subnet_mask ) throw(); 00310 00311 } 00312 00313 } 00314 00315 #endif