Home | History | Annotate | Download | only in ip
      1 //
      2 // ip/address_v4.hpp
      3 // ~~~~~~~~~~~~~~~~~
      4 //
      5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
      6 //
      7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
      8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      9 //
     10 
     11 #ifndef ASIO_IP_ADDRESS_V4_HPP
     12 #define ASIO_IP_ADDRESS_V4_HPP
     13 
     14 
     15 #include "asio/detail/config.hpp"
     16 #include <string>
     17 #include "asio/detail/array.hpp"
     18 #include "asio/detail/socket_types.hpp"
     19 #include "asio/detail/winsock_init.hpp"
     20 #include "asio/error_code.hpp"
     21 
     22 
     23 #include "asio/detail/push_options.hpp"
     24 
     25 namespace asio {
     26 namespace ip {
     27 
     28 /// Implements IP version 4 style addresses.
     29 /**
     30  * The asio::ip::address_v4 class provides the ability to use and
     31  * manipulate IP version 4 addresses.
     32  *
     33  * @par Thread Safety
     34  * @e Distinct @e objects: Safe.@n
     35  * @e Shared @e objects: Unsafe.
     36  */
     37 class address_v4
     38 {
     39 public:
     40   /// The type used to represent an address as an array of bytes.
     41   /**
     42    * @note This type is defined in terms of the C++0x template @c std::array
     43    * when it is available. Otherwise, it uses @c boost:array.
     44    */
     45   typedef asio::detail::array<unsigned char, 4> bytes_type;
     46 
     47   /// Default constructor.
     48   address_v4()
     49   {
     50     addr_.s_addr = 0;
     51   }
     52 
     53   /// Construct an address from raw bytes.
     54   ASIO_DECL explicit address_v4(const bytes_type& bytes);
     55 
     56   /// Construct an address from a unsigned long in host byte order.
     57   ASIO_DECL explicit address_v4(unsigned long addr);
     58 
     59   /// Copy constructor.
     60   address_v4(const address_v4& other)
     61     : addr_(other.addr_)
     62   {
     63   }
     64 
     65   /// Move constructor.
     66   address_v4(address_v4&& other)
     67     : addr_(other.addr_)
     68   {
     69   }
     70 
     71   /// Assign from another address.
     72   address_v4& operator=(const address_v4& other)
     73   {
     74     addr_ = other.addr_;
     75     return *this;
     76   }
     77 
     78   /// Move-assign from another address.
     79   address_v4& operator=(address_v4&& other)
     80   {
     81     addr_ = other.addr_;
     82     return *this;
     83   }
     84 
     85   /// Get the address in bytes, in network byte order.
     86   ASIO_DECL bytes_type to_bytes() const;
     87 
     88   /// Get the address as an unsigned long in host byte order
     89   ASIO_DECL unsigned long to_ulong() const;
     90 
     91   /// Get the address as a string in dotted decimal format.
     92   ASIO_DECL std::string to_string() const;
     93 
     94   /// Get the address as a string in dotted decimal format.
     95   ASIO_DECL std::string to_string(asio::error_code& ec) const;
     96 
     97   /// Create an address from an IP address string in dotted decimal form.
     98   ASIO_DECL static address_v4 from_string(const char* str);
     99 
    100   /// Create an address from an IP address string in dotted decimal form.
    101   ASIO_DECL static address_v4 from_string(
    102       const char* str, asio::error_code& ec);
    103 
    104   /// Create an address from an IP address string in dotted decimal form.
    105   ASIO_DECL static address_v4 from_string(const std::string& str);
    106 
    107   /// Create an address from an IP address string in dotted decimal form.
    108   ASIO_DECL static address_v4 from_string(
    109       const std::string& str, asio::error_code& ec);
    110 
    111   /// Determine whether the address is a loopback address.
    112   ASIO_DECL bool is_loopback() const;
    113 
    114   /// Determine whether the address is unspecified.
    115   ASIO_DECL bool is_unspecified() const;
    116 
    117   /// Determine whether the address is a class A address.
    118   ASIO_DECL bool is_class_a() const;
    119 
    120   /// Determine whether the address is a class B address.
    121   ASIO_DECL bool is_class_b() const;
    122 
    123   /// Determine whether the address is a class C address.
    124   ASIO_DECL bool is_class_c() const;
    125 
    126   /// Determine whether the address is a multicast address.
    127   ASIO_DECL bool is_multicast() const;
    128 
    129   /// Compare two addresses for equality.
    130   friend bool operator==(const address_v4& a1, const address_v4& a2)
    131   {
    132     return a1.addr_.s_addr == a2.addr_.s_addr;
    133   }
    134 
    135   /// Compare two addresses for inequality.
    136   friend bool operator!=(const address_v4& a1, const address_v4& a2)
    137   {
    138     return a1.addr_.s_addr != a2.addr_.s_addr;
    139   }
    140 
    141   /// Compare addresses for ordering.
    142   friend bool operator<(const address_v4& a1, const address_v4& a2)
    143   {
    144     return a1.to_ulong() < a2.to_ulong();
    145   }
    146 
    147   /// Compare addresses for ordering.
    148   friend bool operator>(const address_v4& a1, const address_v4& a2)
    149   {
    150     return a1.to_ulong() > a2.to_ulong();
    151   }
    152 
    153   /// Compare addresses for ordering.
    154   friend bool operator<=(const address_v4& a1, const address_v4& a2)
    155   {
    156     return a1.to_ulong() <= a2.to_ulong();
    157   }
    158 
    159   /// Compare addresses for ordering.
    160   friend bool operator>=(const address_v4& a1, const address_v4& a2)
    161   {
    162     return a1.to_ulong() >= a2.to_ulong();
    163   }
    164 
    165   /// Obtain an address object that represents any address.
    166   static address_v4 any()
    167   {
    168     return address_v4();
    169   }
    170 
    171   /// Obtain an address object that represents the loopback address.
    172   static address_v4 loopback()
    173   {
    174     return address_v4(0x7F000001);
    175   }
    176 
    177   /// Obtain an address object that represents the broadcast address.
    178   static address_v4 broadcast()
    179   {
    180     return address_v4(0xFFFFFFFF);
    181   }
    182 
    183   /// Obtain an address object that represents the broadcast address that
    184   /// corresponds to the specified address and netmask.
    185   ASIO_DECL static address_v4 broadcast(
    186       const address_v4& addr, const address_v4& mask);
    187 
    188   /// Obtain the netmask that corresponds to the address, based on its address
    189   /// class.
    190   ASIO_DECL static address_v4 netmask(const address_v4& addr);
    191 
    192 private:
    193   // The underlying IPv4 address.
    194   asio::detail::in4_addr_type addr_;
    195 };
    196 
    197 
    198 } // namespace ip
    199 } // namespace asio
    200 
    201 #include "asio/detail/pop_options.hpp"
    202 
    203 #include "asio/ip/impl/address_v4.hpp"
    204 # include "asio/ip/impl/address_v4.ipp"
    205 
    206 #endif // ASIO_IP_ADDRESS_V4_HPP
    207