LCOV - code coverage report
Current view: top level - src/libaddr - addr.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 16 100.0 %
Date: 2018-06-08 23:44:40 Functions: 7 7 100.0 %
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      396777 : constexpr struct sockaddr_in6 init_in6()
      60             : {
      61      396777 :     struct sockaddr_in6 in6 = sockaddr_in6();
      62      396777 :     in6.sin6_family = AF_INET6;
      63      396777 :     return in6;
      64             : }
      65             : 
      66             : 
      67             : 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 string_ip_t
      84             :     {
      85             :         STRING_IP_ONLY,
      86             :         STRING_IP_BRACKETS,         // IPv6 only
      87             :         STRING_IP_PORT,             // in IPv6, includes brackets
      88             :         STRING_IP_MASK,
      89             :         STRING_IP_BRACKETS_MASK,    // IPv6 only
      90             :         STRING_IP_ALL
      91             :     };
      92             : 
      93             :     typedef std::shared_ptr<addr>   pointer_t;
      94             :     typedef std::vector<addr>       vector_t;
      95             :     typedef int                     socket_flag_t;
      96             : 
      97             :     static socket_flag_t const      SOCKET_FLAG_CLOEXEC  = 0x01;
      98             :     static socket_flag_t const      SOCKET_FLAG_NONBLOCK = 0x02;
      99             :     static socket_flag_t const      SOCKET_FLAG_REUSE    = 0x04;
     100             : 
     101             :                                     addr();
     102             :                                     addr(struct sockaddr_in const & in);
     103             :                                     addr(struct sockaddr_in6 const & in6);
     104             : 
     105             :     void                            set_from_socket(int s, bool peer);
     106             :     void                            set_ipv4(struct sockaddr_in const & in);
     107             :     void                            set_ipv6(struct sockaddr_in6 const & in6);
     108             :     void                            set_port(int port);
     109             :     void                            set_protocol(char const * protocol);
     110             :     void                            set_protocol(int protocol);
     111             :     void                            set_mask(uint8_t const * mask);
     112             :     void                            apply_mask();
     113             : 
     114             :     bool                            is_default() const;
     115             :     bool                            is_ipv4() const;
     116             :     void                            get_ipv4(struct sockaddr_in & in) const;
     117             :     void                            get_ipv6(struct sockaddr_in6 & in6) const;
     118             :     std::string                     to_ipv4_string(string_ip_t mode) const;
     119             :     std::string                     to_ipv6_string(string_ip_t mode) const;
     120             :     std::string                     to_ipv4or6_string(string_ip_t mode) const;
     121             : 
     122             :     network_type_t                  get_network_type() const;
     123             :     std::string                     get_network_type_string() const;
     124             : 
     125             :     int                             create_socket(socket_flag_t flags) const;
     126             :     int                             connect(int s) const;
     127             :     int                             bind(int s) const;
     128             :     std::string                     get_name() const;
     129             :     std::string                     get_service() const;
     130             :     int                             get_port() const;
     131             :     int                             get_protocol() const;
     132             :     void                            get_mask(uint8_t * mask);
     133             : 
     134             :     bool                            match(addr const & ip) const;
     135             :     bool                            operator == (addr const & rhs) const;
     136             :     bool                            operator != (addr const & rhs) const;
     137             :     bool                            operator <  (addr const & rhs) const;
     138             :     bool                            operator <= (addr const & rhs) const;
     139             :     bool                            operator >  (addr const & rhs) const;
     140             :     bool                            operator >= (addr const & rhs) const;
     141             : 
     142             : private:
     143             :     void                            address_changed();
     144             : 
     145             :     // always keep address in an IPv6 structure
     146             :     //
     147             :     struct sockaddr_in6             f_address = init_in6();
     148             :     uint8_t                         f_mask[16] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
     149             :     int                             f_protocol = IPPROTO_TCP;
     150             :     mutable network_type_t          f_private_network_defined = network_type_t::NETWORK_TYPE_UNDEFINED;
     151             : };
     152             : 
     153             : 
     154             : 
     155             : 
     156             : 
     157             : }
     158             : // addr namespace
     159             : 
     160             : 
     161             : inline bool operator == (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     162             : {
     163             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) == 0;
     164             : }
     165             : 
     166             : 
     167             : inline bool operator != (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     168             : {
     169             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) != 0;
     170             : }
     171             : 
     172             : 
     173             : inline bool operator < (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     174             : {
     175             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) < 0;
     176             : }
     177             : 
     178             : 
     179             : inline bool operator <= (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     180             : {
     181             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) <= 0;
     182             : }
     183             : 
     184             : 
     185             : inline bool operator > (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     186             : {
     187             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) > 0;
     188             : }
     189             : 
     190             : 
     191             : inline bool operator >= (struct sockaddr_in6 const & a, struct sockaddr_in6 const & b)
     192             : {
     193             :     return memcmp(&a, &b, sizeof(struct sockaddr_in6)) >= 0;
     194             : }
     195             : 
     196             : 
     197          35 : inline bool operator == (in6_addr const & a, in6_addr const & b)
     198             : {
     199          35 :     return memcmp(&a, &b, sizeof(in6_addr)) == 0;
     200             : }
     201             : 
     202             : 
     203           9 : inline bool operator != (in6_addr const & a, in6_addr const & b)
     204             : {
     205           9 :     return memcmp(&a, &b, sizeof(in6_addr)) != 0;
     206             : }
     207             : 
     208             : 
     209           7 : inline bool operator < (in6_addr const & a, in6_addr const & b)
     210             : {
     211           7 :     return memcmp(&a, &b, sizeof(in6_addr)) < 0;
     212             : }
     213             : 
     214             : 
     215         685 : inline bool operator <= (in6_addr const & a, in6_addr const & b)
     216             : {
     217         685 :     return memcmp(&a, &b, sizeof(in6_addr)) <= 0;
     218             : }
     219             : 
     220             : 
     221          27 : inline bool operator > (in6_addr const & a, in6_addr const & b)
     222             : {
     223          27 :     return memcmp(&a, &b, sizeof(in6_addr)) > 0;
     224             : }
     225             : 
     226             : 
     227         290 : inline bool operator >= (in6_addr const & a, in6_addr const & b)
     228             : {
     229         290 :     return memcmp(&a, &b, sizeof(in6_addr)) >= 0;
     230             : }
     231             : 
     232             : 
     233             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12