Coverage Report

Created: 2025-06-16 06:21

/src/libtorrent/src/socket_io.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2009-2010, 2013-2014, 2017, 2019, Arvid Norberg
4
Copyright (c) 2016, 2018, Alden Torres
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions
9
are met:
10
11
    * Redistributions of source code must retain the above copyright
12
      notice, this list of conditions and the following disclaimer.
13
    * Redistributions in binary form must reproduce the above copyright
14
      notice, this list of conditions and the following disclaimer in
15
      the documentation and/or other materials provided with the distribution.
16
    * Neither the name of the author nor the names of its
17
      contributors may be used to endorse or promote products derived
18
      from this software without specific prior written permission.
19
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
POSSIBILITY OF SUCH DAMAGE.
31
32
*/
33
34
#include <string>
35
36
#include "libtorrent/error_code.hpp"
37
#include "libtorrent/socket.hpp"
38
#include "libtorrent/socket_io.hpp"
39
#include "libtorrent/address.hpp"
40
#include "libtorrent/io.hpp" // for write_uint16
41
#include "libtorrent/hasher.hpp" // for hasher
42
#include "libtorrent/aux_/escape_string.hpp" // for trim
43
44
namespace libtorrent {
45
46
  std::string print_address(address const& addr)
47
0
  {
48
0
    return addr.to_string();
49
0
  }
50
51
  std::string address_to_bytes(address const& a)
52
0
  {
53
0
    std::string ret;
54
0
    std::back_insert_iterator<std::string> out(ret);
55
0
    aux::write_address(a, out);
56
0
    return ret;
57
0
  }
58
59
  std::string endpoint_to_bytes(udp::endpoint const& ep)
60
0
  {
61
0
    std::string ret;
62
0
    std::back_insert_iterator<std::string> out(ret);
63
0
    aux::write_endpoint(ep, out);
64
0
    return ret;
65
0
  }
66
67
  std::string print_endpoint(address const& addr, int port)
68
0
  {
69
0
    char buf[200];
70
0
    if (addr.is_v6())
71
0
      std::snprintf(buf, sizeof(buf), "[%s]:%d", addr.to_string().c_str(), port);
72
0
    else
73
0
      std::snprintf(buf, sizeof(buf), "%s:%d", addr.to_string().c_str(), port);
74
0
    return buf;
75
0
  }
76
77
  std::string print_endpoint(tcp::endpoint const& ep)
78
0
  {
79
0
    return print_endpoint(ep.address(), ep.port());
80
0
  }
81
82
  std::string print_endpoint(udp::endpoint const& ep)
83
0
  {
84
0
    return print_endpoint(ep.address(), ep.port());
85
0
  }
86
87
  tcp::endpoint parse_endpoint(string_view str, error_code& ec)
88
0
  {
89
0
    tcp::endpoint ret;
90
91
0
    str = trim(str);
92
93
0
    string_view addr;
94
0
    string_view port;
95
96
0
    if (str.empty())
97
0
    {
98
0
      ec = errors::invalid_port;
99
0
      return ret;
100
0
    }
101
102
    // this is for IPv6 addresses
103
0
    if (str.front() == '[')
104
0
    {
105
0
      auto const close_bracket = str.find_first_of(']');
106
0
      if (close_bracket == string_view::npos)
107
0
      {
108
0
        ec = errors::expected_close_bracket_in_address;
109
0
        return ret;
110
0
      }
111
0
      addr = str.substr(1, close_bracket - 1);
112
0
      port = str.substr(close_bracket + 1);
113
0
      if (port.empty() || port.front() != ':')
114
0
      {
115
0
        ec = errors::invalid_port;
116
0
        return ret;
117
0
      }
118
      // shave off the ':'
119
0
      port = port.substr(1);
120
0
      ret.address(make_address_v6(addr.to_string(), ec));
121
0
      if (ec) return ret;
122
0
    }
123
0
    else
124
0
    {
125
0
      auto const port_pos = str.find_first_of(':');
126
0
      if (port_pos == string_view::npos)
127
0
      {
128
0
        ec = errors::invalid_port;
129
0
        return ret;
130
0
      }
131
0
      addr = str.substr(0, port_pos);
132
0
      port = str.substr(port_pos + 1);
133
0
      ret.address(make_address_v4(addr.to_string(), ec));
134
0
      if (ec) return ret;
135
0
    }
136
137
0
    if (port.empty())
138
0
    {
139
0
      ec = errors::invalid_port;
140
0
      return ret;
141
0
    }
142
143
0
    int const port_num = std::atoi(port.to_string().c_str());
144
0
    if (port_num <= 0 || port_num > std::numeric_limits<std::uint16_t>::max())
145
0
    {
146
0
      ec = errors::invalid_port;
147
0
      return ret;
148
0
    }
149
0
    ret.port(static_cast<std::uint16_t>(port_num));
150
0
    return ret;
151
0
  }
152
153
  sha1_hash hash_address(address const& ip)
154
0
  {
155
0
    if (ip.is_v6())
156
0
    {
157
0
      address_v6::bytes_type b = ip.to_v6().to_bytes();
158
0
      return hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
159
0
    }
160
0
    else
161
0
    {
162
0
      address_v4::bytes_type b = ip.to_v4().to_bytes();
163
0
      return hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
164
0
    }
165
0
  }
166
167
}