/tmp/bitcoin/src/ipc/listen.cpp
Line | Count | Source |
1 | | // Copyright (c) 2026-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <common/url.h> |
6 | | #include <interfaces/ipc.h> |
7 | | #include <util/strencodings.h> |
8 | | #include <util/string.h> |
9 | | #include <util/translation.h> |
10 | | |
11 | | #include <cstdint> |
12 | | #include <string> |
13 | | #include <string_view> |
14 | | #include <utility> |
15 | | |
16 | | namespace { |
17 | | util::Result<int64_t> ParseIntOption(std::string_view name, std::string_view value, int64_t min_val, int64_t max_val) |
18 | 27 | { |
19 | 27 | if (value.empty()) return util::Error{Untranslated(strprintf("Missing value for %s option", name))}; |
20 | 25 | const auto parsed{ToIntegral<int64_t>(value)}; |
21 | 25 | if (!parsed) return util::Error{Untranslated(strprintf("Invalid %s value '%s'", name, value))}; |
22 | 24 | if (*parsed < min_val) return util::Error{Untranslated(strprintf("%s must be at least %d", name, min_val))}; |
23 | 20 | if (*parsed > max_val) return util::Error{Untranslated(strprintf("%s must be at most %d", name, max_val))}; |
24 | 18 | return *parsed; |
25 | 20 | } |
26 | | } // namespace |
27 | | |
28 | | namespace interfaces { |
29 | | util::Result<ipc::ListenAddress> Ipc::parseListenAddress(std::string address) |
30 | 55 | { |
31 | 55 | constexpr std::string_view UNIX_PREFIX{"unix:"}; |
32 | | |
33 | | // Only Unix socket -ipcbind values support inline socket options. Leave |
34 | | // other address families untouched so ipc::Process can report address |
35 | | // scheme errors consistently with -ipcconnect. |
36 | 55 | if (!address.starts_with(UNIX_PREFIX)) { |
37 | 23 | return ipc::ListenAddress{.address = std::move(address)}; |
38 | 23 | } |
39 | | |
40 | | // Socket options follow the first comma: |
41 | | // unix:,max-connections=8 |
42 | | // unix:/custom/path,max-connections=8 |
43 | | // Commas in socket paths must be URL-encoded as %2C. |
44 | | // |
45 | | // Subsequent commas separate individual options. |
46 | 32 | const size_t option_pos{address.find(',')}; |
47 | 32 | if (option_pos == std::string::npos) { |
48 | 4 | return ipc::ListenAddress{.address = UrlDecode(address)}; |
49 | 4 | } |
50 | | |
51 | 28 | const std::string_view address_view{address}; |
52 | | |
53 | 28 | ipc::ListenAddress listen_address{ |
54 | 28 | .address = UrlDecode(address_view.substr(0, option_pos)), |
55 | 28 | }; |
56 | | |
57 | 32 | for (const std::string& option_str : util::SplitString(address_view.substr(option_pos + 1), ',')) { |
58 | 32 | if (option_str.empty()) { |
59 | 3 | return util::Error{Untranslated("Empty socket option")}; |
60 | 3 | } |
61 | 29 | const size_t eq{option_str.find('=')}; |
62 | 29 | const std::string_view name{std::string_view{option_str}.substr(0, eq)}; |
63 | 29 | const std::string_view value{eq == std::string::npos ? std::string_view{} : std::string_view{option_str}.substr(eq + 1)}; |
64 | 29 | if (name == "max-connections") { |
65 | 27 | auto parsed{ParseIntOption(name, value, 1, static_cast<int64_t>(ipc::MAX_CONNECTIONS))}; |
66 | 27 | if (!parsed) return util::Error{util::ErrorString(parsed)}; |
67 | 18 | listen_address.max_connections = static_cast<size_t>(*parsed); |
68 | 18 | } else { |
69 | 2 | return util::Error{Untranslated(strprintf("Unknown socket option '%s'", name))}; |
70 | 2 | } |
71 | 29 | } |
72 | | |
73 | 14 | return listen_address; |
74 | 28 | } |
75 | | } // namespace interfaces |