Coverage Report

Created: 2025-12-04 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtorrent/src/ip_helpers.cpp
Line
Count
Source
1
/*
2
3
Copyright (c) 2020, Arvid Norberg
4
All rights reserved.
5
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
8
are met:
9
10
    * Redistributions of source code must retain the above copyright
11
      notice, this list of conditions and the following disclaimer.
12
    * Redistributions in binary form must reproduce the above copyright
13
      notice, this list of conditions and the following disclaimer in
14
      the documentation and/or other materials provided with the distribution.
15
    * Neither the name of the author nor the names of its
16
      contributors may be used to endorse or promote products derived
17
      from this software without specific prior written permission.
18
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
30
31
*/
32
33
#include "libtorrent/config.hpp"
34
#include "libtorrent/address.hpp"
35
#include "libtorrent/error_code.hpp"
36
#include "libtorrent/io_context.hpp"
37
#include "libtorrent/socket.hpp"
38
#include "libtorrent/aux_/ip_helpers.hpp"
39
40
namespace libtorrent {
41
namespace aux {
42
43
  bool is_ip_address(std::string const& host)
44
0
  {
45
0
    error_code ec;
46
0
    make_address(host, ec);
47
0
    return !ec;
48
0
  }
49
50
  bool is_global(address const& a)
51
0
  {
52
0
    if (a.is_v6())
53
0
    {
54
      // https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
55
0
      address_v6 const a6 = a.to_v6();
56
0
      return (a6.to_bytes()[0] & 0xe0) == 0x20;
57
0
    }
58
0
    else
59
0
    {
60
0
      address_v4 const a4 = a.to_v4();
61
0
      return !(a4.is_multicast() || a4.is_unspecified() || is_local(a));
62
0
    }
63
0
  }
64
65
  bool is_link_local(address const& a)
66
0
  {
67
0
    if (a.is_v6())
68
0
    {
69
0
      address_v6 const a6 = a.to_v6();
70
0
      return a6.is_link_local()
71
0
        || a6.is_multicast_link_local();
72
0
    }
73
0
    address_v4 const a4 = a.to_v4();
74
0
    std::uint32_t ip = a4.to_uint();
75
0
    return (ip & 0xffff0000) == 0xa9fe0000; // 169.254.x.x
76
0
  }
77
78
  bool is_local(address const& a)
79
0
  {
80
0
    try
81
0
    {
82
0
      if (a.is_v6())
83
0
      {
84
        // NOTE: site local is deprecated but by
85
        // https://www.ietf.org/rfc/rfc3879.txt:
86
        // routers SHOULD be configured to prevent
87
        // routing of this prefix by default.
88
89
0
        address_v6 const a6 = a.to_v6();
90
0
        return a6.is_loopback()
91
0
          || a6.is_link_local()
92
0
          || a6.is_site_local()
93
0
          || a6.is_multicast_link_local()
94
0
          || a6.is_multicast_site_local()
95
          //  fc00::/7, unique local address
96
0
          || (a6.to_bytes()[0] & 0xfe) == 0xfc;
97
0
      }
98
0
      address_v4 a4 = a.to_v4();
99
0
      std::uint32_t const ip = a4.to_uint();
100
0
      return ((ip & 0xff000000) == 0x0a000000 // 10.x.x.x
101
0
        || (ip & 0xfff00000) == 0xac100000 // 172.16.x.x
102
0
        || (ip & 0xffff0000) == 0xc0a80000 // 192.168.x.x
103
0
        || (ip & 0xffff0000) == 0xa9fe0000 // 169.254.x.x
104
0
        || (ip & 0xff000000) == 0x7f000000 // 127.x.x.x
105
0
        || (ip & 0xffc00000) == 0x64400000 // 100.64.0.0/10
106
0
        );
107
0
    }
108
0
    catch (std::exception const&) { return false; }
109
0
  }
110
111
  bool is_teredo(address const& addr)
112
0
  {
113
0
    try
114
0
    {
115
0
      if (!addr.is_v6()) return false;
116
0
      static const std::uint8_t teredo_prefix[] = {0x20, 0x01, 0, 0};
117
0
      address_v6::bytes_type b = addr.to_v6().to_bytes();
118
0
      return std::memcmp(b.data(), teredo_prefix, 4) == 0;
119
0
    }
120
0
    catch (std::exception const&) { return false; }
121
0
  }
122
123
  address ensure_v6(address const& a)
124
0
  {
125
0
    return a == address_v4() ? address_v6() : a;
126
0
  }
127
128
}
129
}
130