LCOV - code coverage report
Current view: top level - src/libaddr - addr.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 17 17 100.0 %
Date: 2018-05-30 23:58:02 Functions: 11 12 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Network Address -- classes functions to ease handling IP addresses
       2             : // Copyright (c) 2012-2018  Made to Order Software Corp.  All Rights Reserved
       3             : //
       4             : // https://snapwebsites.org/project/libaddr
       5             : //
       6             : // Permission is hereby granted, free of charge, to any person obtaining a
       7             : // copy of this software and associated documentation files (the
       8             : // "Software"), to deal in the Software without restriction, including
       9             : // without limitation the rights to use, copy, modify, merge, publish,
      10             : // distribute, sublicense, and/or sell copies of the Software, and to
      11             : // permit persons to whom the Software is furnished to do so, subject to
      12             : // the following conditions:
      13             : //
      14             : // The above copyright notice and this permission notice shall be included
      15             : // in all copies or substantial portions of the Software.
      16             : //
      17             : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      18             : // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      19             : // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      20             : // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      21             : // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      22             : // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      23             : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      24             : //
      25             : #pragma once
      26             : 
      27             : /** \file
      28             :  * \brief The various libaddr classes.
      29             :  *
      30             :  * This header includes the base addr class used to handle one binary
      31             :  * address.
      32             :  */
      33             : 
      34             : // C++ library
      35             : //
      36             : #include <memory>
      37             : #include <cstring>
      38             : #include <vector>
      39             : 
      40             : // C library
      41             : //
      42             : #include <arpa/inet.h>
      43             : 
      44             : 
      45             : 
      46             : namespace addr
      47             : {
      48             : 
      49             : 
      50             : 
      51             : 
      52             : /** \brief Initialize an IPv6 address as such.
      53             :  *
      54             :  * This function initializes a sockaddr_in6 with all zeroes except
      55             :  * for the sin6_family which is set to AF_INET6.
      56             :  *
      57             :  * return The initialized IPv6 address.
      58             :  */
      59      396121 : constexpr struct sockaddr_in6 init_in6()
      60             : {
      61      396121 :     struct sockaddr_in6 in6 = sockaddr_in6();
      62      396121 :     in6.sin6_family = AF_INET6;
      63      396121 :     return in6;
      64             : }
      65             : 
      66             : 
      67     1321025 : class addr
      68             : {
      69             : public:
      70             :     enum class network_type_t
      71             :     {
      72             :         NETWORK_TYPE_UNDEFINED,
      73             :         NETWORK_TYPE_PRIVATE,
      74             :         NETWORK_TYPE_CARRIER,
      75             :         NETWORK_TYPE_LINK_LOCAL,
      76             :         NETWORK_TYPE_MULTICAST,
      77             :         NETWORK_TYPE_LOOPBACK,
      78             :         NETWORK_TYPE_ANY,
      79             :         NETWORK_TYPE_UNKNOWN,
      80             :         NETWORK_TYPE_PUBLIC = NETWORK_TYPE_UNKNOWN  // we currently do not distinguish public and unknown
      81             :     };
      82             : 
      83             :     enum class computer_interface_address_t
      84             :     {
      85             :         COMPUTER_INTERFACE_ADDRESS_ERROR = -1,
      86             :         COMPUTER_INTERFACE_ADDRESS_FALSE,
      87             :         COMPUTER_INTERFACE_ADDRESS_TRUE
      88             :     };
      89             : 
      90             :     enum class string_ip_t
      91             :     {
      92             :         STRING_IP_ONLY,
      93             :         STRING_IP_BRACKETS,         // IPv6 only
      94             :         STRING_IP_PORT,             // in IPv6, includes brackets
      95             :         STRING_IP_MASK,
      96             :         STRING_IP_BRACKETS_MASK,    // IPv6 only
      97             :         STRING_IP_ALL
      98             :     };
      99             : 
     100             :     typedef std::shared_ptr<addr>   pointer_t;
     101             :     typedef std::vector<addr>       vector_t;
     102             :     typedef int                     socket_flag_t;
     103             : 
     104             :     static socket_flag_t const      SOCKET_FLAG_CLOEXEC  = 0x01;
     105             :     static socket_flag_t const      SOCKET_FLAG_NONBLOCK = 0x02;
     106             :     static socket_flag_t const      SOCKET_FLAG_REUSE    = 0x04;
     107             : 
     108             :                                     addr();
     109             :                                     addr(struct sockaddr_in const & in);
     110             :                                     addr(struct sockaddr_in6 const & in6);
     111             : 
     112             :     static vector_t                 get_local_addresses();
     113             : 
     114             :     void                            set_from_socket(int s, bool peer);
     115             :     void                            set_ipv4(struct sockaddr_in const & in);
     116             :     void                            set_ipv6(struct sockaddr_in6 const & in6);
     117             :     void                            set_port(int port);
     118             :     void                            set_protocol(char const * protocol);
     119             :     void                            set_protocol(int protocol);
     120             :     void                            set_mask(uint8_t const * mask);
     121             :     void                            apply_mask();
     122             : 
     123             :     bool                            is_ipv4() const;
     124             :     void                            get_ipv4(struct sockaddr_in & in) const;
     125             :     void                            get_ipv6(struct sockaddr_in6 & in6) const;
     126             :     std::string                     to_ipv4_string(string_ip_t mode) const;
     127             :     std::string                     to_ipv6_string(string_ip_t mode) const;
     128             :     std::string                     to_ipv4or6_string(string_ip_t mode) const;
     129             : 
     130             :     network_type_t                  get_network_type() const;
     131             :     std::string                     get_network_type_string() const;
     132             :     computer_interface_address_t    is_computer_interface_address() const;
     133             : 
     134             :     std::string                     get_iface_name() const;
     135             :     int                             create_socket(socket_flag_t flags) const;
     136             :     int                             connect(int s) const;
     137             :     int                             bind(int s) const;
     138             :     std::string                     get_name() const;
     139             :     std::string                     get_service() const;
     140             :     int                             get_port() const;
     141             :     int                             get_protocol() const;
     142             :     void                            get_mask(uint8_t * mask);
     143             : 
     144             :     bool                            match(addr const & ip) const;
     145             :     bool                            operator == (addr const & rhs) const;
     146             :     bool                            operator != (addr const & rhs) const;
     147             :     bool                            operator <  (addr const & rhs) const;
     148             :     bool                            operator <= (addr const & rhs) const;
     149             :     bool                            operator >  (addr const & rhs) const;
     150             :     bool                            operator >= (addr const & rhs) const;
     151             : 
     152             : private:
     153             :     void                            address_changed();
     154             : 
     155             :     // always keep address in an IPv6 structure
     156             :     //
     157             :     struct sockaddr_in6             f_address = init_in6();
     158             :     uint8_t                         f_mask[16] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
     159             :     std::string                     f_iface_name;
     160             :     int                             f_protocol = IPPROTO_TCP;
     161             :     mutable network_type_t          f_private_network_defined = network_type_t::NETWORK_TYPE_UNDEFINED;
     162             : };
     163             : 
     164             : 
     165             : 
     166             : 
     167             : 
     168             : }
     169             : // addr namespace
     170             : 
     171             : 
     172             : inline bool operator == (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     173             : {
     174             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) == 0;
     175             : }
     176             : 
     177             : 
     178             : inline bool operator != (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     179             : {
     180             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) != 0;
     181             : }
     182             : 
     183             : 
     184             : inline bool operator < (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     185             : {
     186             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) < 0;
     187             : }
     188             : 
     189             : 
     190             : inline bool operator <= (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     191             : {
     192             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) <= 0;
     193             : }
     194             : 
     195             : 
     196             : inline bool operator > (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     197             : {
     198             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) > 0;
     199             : }
     200             : 
     201             : 
     202             : inline bool operator >= (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     203             : {
     204             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) >= 0;
     205             : }
     206             : 
     207             : 
     208          35 : inline bool operator == (in6_addr const & a, in6_addr const & b)
     209             : {
     210          35 :     return memcmp(&a, &b, sizeof(in6_addr)) == 0;
     211             : }
     212             : 
     213             : 
     214           9 : inline bool operator != (in6_addr const & a, in6_addr const & b)
     215             : {
     216           9 :     return memcmp(&a, &b, sizeof(in6_addr)) != 0;
     217             : }
     218             : 
     219             : 
     220           7 : inline bool operator < (in6_addr const & a, in6_addr const & b)
     221             : {
     222           7 :     return memcmp(&a, &b, sizeof(in6_addr)) < 0;
     223             : }
     224             : 
     225             : 
     226         685 : inline bool operator <= (in6_addr const & a, in6_addr const & b)
     227             : {
     228         685 :     return memcmp(&a, &b, sizeof(in6_addr)) <= 0;
     229             : }
     230             : 
     231             : 
     232          27 : inline bool operator > (in6_addr const & a, in6_addr const & b)
     233             : {
     234          27 :     return memcmp(&a, &b, sizeof(in6_addr)) > 0;
     235             : }
     236             : 
     237             : 
     238         290 : inline bool operator >= (in6_addr const & a, in6_addr const & b)
     239             : {
     240         290 :     return memcmp(&a, &b, sizeof(in6_addr)) >= 0;
     241             : }
     242             : 
     243             : 
     244             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12