Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 1 | /* |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 2 | * Copyright (C) 2023 Savoir-faire Linux Inc. |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "dnc.h" |
| 18 | #include "common.h" |
Amna | 423b25b | 2024-02-06 13:21:19 -0500 | [diff] [blame] | 19 | #include "dhtnet_crtmgr/dhtnet_crtmgr.h" |
Adrien Béraud | c1cac45 | 2023-08-22 20:32:36 -0400 | [diff] [blame] | 20 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | #include <iostream> |
| 24 | #include <unistd.h> |
| 25 | #include <getopt.h> |
Amna | b31c374 | 2023-08-28 13:58:31 -0400 | [diff] [blame] | 26 | #if __has_include(<fmt/std.h>) |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 27 | #include <fmt/std.h> |
Amna | b31c374 | 2023-08-28 13:58:31 -0400 | [diff] [blame] | 28 | #else |
| 29 | #include <fmt/ostream.h> |
| 30 | #endif |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 31 | #include <netinet/in.h> |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 32 | #include <yaml-cpp/yaml.h> |
| 33 | #include <fstream> |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 34 | |
| 35 | struct dhtnc_params |
| 36 | { |
| 37 | bool help {false}; |
| 38 | bool version {false}; |
| 39 | bool listen {false}; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 40 | std::filesystem::path privateKey {}; |
| 41 | std::filesystem::path cert {}; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 42 | std::string bootstrap {}; |
| 43 | std::string remote_host {}; |
| 44 | in_port_t remote_port {}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 45 | dht::InfoHash peer_id {}; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 46 | std::string turn_host {}; |
| 47 | std::string turn_user {}; |
| 48 | std::string turn_pass {}; |
| 49 | std::string turn_realm {}; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 50 | std::string configuration {}; |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 51 | bool anonymous_cnx {false}; |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 52 | bool verbose {false}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 53 | }; |
| 54 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 55 | static const constexpr struct option long_options[] |
| 56 | = {{"help", no_argument, nullptr, 'h'}, |
| 57 | {"version", no_argument, nullptr, 'v'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 58 | {"port", required_argument, nullptr, 'P'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 59 | {"ip", required_argument, nullptr, 'i'}, |
| 60 | {"listen", no_argument, nullptr, 'l'}, |
| 61 | {"bootstrap", required_argument, nullptr, 'b'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 62 | {"privateKey", required_argument, nullptr, 'p'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 63 | {"turn_host", required_argument, nullptr, 't'}, |
| 64 | {"turn_user", required_argument, nullptr, 'u'}, |
| 65 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 66 | {"turn_realm", required_argument, nullptr, 'r'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 67 | {"cert", required_argument, nullptr, 'c'}, |
| 68 | {"configuration", required_argument, nullptr, 'd'}, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 69 | {"anonymous_cnx", no_argument, nullptr, 'a'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 70 | {nullptr, 0, nullptr, 0}}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 71 | |
| 72 | dhtnc_params |
| 73 | parse_args(int argc, char** argv) |
| 74 | { |
| 75 | dhtnc_params params; |
| 76 | int opt; |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 77 | int v_count = 0; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 78 | while ((opt = getopt_long(argc, argv, "ahvlw:r:u:t:P:b:p:i:c:d:", long_options, nullptr)) != -1) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 79 | switch (opt) { |
| 80 | case 'h': |
| 81 | params.help = true; |
| 82 | break; |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 83 | case 'v': |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 84 | v_count++; |
| 85 | if (v_count == 1) { |
| 86 | params.version = true; |
| 87 | }else if (v_count == 2) { |
| 88 | params.version = false; |
| 89 | params.verbose = true; |
| 90 | } |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 91 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 92 | case 'P': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 93 | params.remote_port = std::stoi(optarg); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 94 | break; |
| 95 | case 'i': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 96 | params.remote_host = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 97 | break; |
| 98 | case 'l': |
| 99 | params.listen = true; |
| 100 | break; |
| 101 | case 'b': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 102 | params.bootstrap = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 103 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 104 | case 'p': |
| 105 | params.privateKey = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 106 | break; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 107 | case 't': |
| 108 | params.turn_host = optarg; |
| 109 | break; |
| 110 | case 'u': |
| 111 | params.turn_user = optarg; |
| 112 | break; |
| 113 | case 'w': |
| 114 | params.turn_pass = optarg; |
| 115 | break; |
| 116 | case 'r': |
| 117 | params.turn_realm = optarg; |
| 118 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 119 | case 'c': |
| 120 | params.cert = optarg; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 121 | break; |
| 122 | case 'd': |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 123 | params.configuration = optarg; |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 124 | break; |
| 125 | case 'a': |
| 126 | params.anonymous_cnx = true; |
| 127 | break; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 128 | default: |
| 129 | std::cerr << "Invalid option" << std::endl; |
| 130 | exit(EXIT_FAILURE); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // If not listening, the peer_id argument is required |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 135 | if (!params.listen && !params.help && !params.version) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 136 | if (optind < argc) { |
| 137 | params.peer_id = dht::InfoHash(argv[optind]); |
| 138 | optind++; // Move to the next argument |
| 139 | } else { |
| 140 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 141 | exit(EXIT_FAILURE); |
| 142 | } |
| 143 | } |
| 144 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 145 | // extract values from dnc yaml file |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 146 | if (!params.configuration.empty()) { |
| 147 | printf("read configuration file: %s\n", params.configuration.c_str()); |
| 148 | std::ifstream config_file(params.configuration); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 149 | if (!config_file.is_open()) { |
| 150 | std::cerr << "Error: Could not open configuration file.\n"; |
| 151 | } else { |
| 152 | YAML::Node config = YAML::Load(config_file); |
| 153 | if (config["bootstrap"] && params.bootstrap.empty()) { |
| 154 | params.bootstrap = config["bootstrap"].as<std::string>(); |
| 155 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 156 | if (config["privateKey"] && params.privateKey.empty()) { |
| 157 | params.privateKey = config["privateKey"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 158 | } |
| 159 | if (config["turn_host"] && params.turn_host.empty()) { |
| 160 | params.turn_host = config["turn_host"].as<std::string>(); |
| 161 | } |
| 162 | if (config["turn_user"] && params.turn_user.empty()) { |
| 163 | params.turn_user = config["turn_user"].as<std::string>(); |
| 164 | } |
| 165 | if (config["turn_pass"] && params.turn_pass.empty()) { |
| 166 | params.turn_pass = config["turn_pass"].as<std::string>(); |
| 167 | } |
| 168 | if (config["turn_realm"] && params.turn_realm.empty()) { |
| 169 | params.turn_realm = config["turn_realm"].as<std::string>(); |
| 170 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 171 | if (config["certificate"] && params.cert.empty()) { |
| 172 | params.cert = config["certificate"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 173 | } |
| 174 | if (config["ip"] && params.remote_host.empty()) { |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 175 | params.configuration = config["ip"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 176 | } |
| 177 | if (config["port"] && params.remote_port == 0) { |
| 178 | params.remote_port = config["port"].as<int>(); |
| 179 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 180 | if (config["anonymous"] && !params.anonymous_cnx) { |
| 181 | params.anonymous_cnx = config["anonymous"].as<bool>(); |
| 182 | } |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 183 | if (config["verbose"] && !params.verbose) { |
| 184 | params.verbose = config["verbose"].as<bool>(); |
| 185 | } |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 188 | return params; |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | setSipLogLevel() |
| 193 | { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 194 | int level = 0; |
Adrien Béraud | 6c6a962 | 2023-08-27 12:49:31 -0400 | [diff] [blame] | 195 | if (char* envvar = getenv("SIPLOGLEVEL")) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 196 | // From 0 (min) to 6 (max) |
Adrien Béraud | 6c6a962 | 2023-08-27 12:49:31 -0400 | [diff] [blame] | 197 | level = std::clamp(std::stoi(envvar), 0, 6); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | pj_log_set_level(level); |
Amna | be36392 | 2023-11-20 15:24:54 -0500 | [diff] [blame] | 201 | pj_log_set_log_func([](int level, const char* data, int len) { |
| 202 | fmt::print("{}", std::string_view(data, len)); |
| 203 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int |
| 207 | main(int argc, char** argv) |
| 208 | { |
| 209 | setSipLogLevel(); |
| 210 | auto params = parse_args(argc, argv); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 211 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 212 | if (params.help) { |
| 213 | fmt::print("Usage: dnc [options] [PEER_ID]\n" |
| 214 | "\nOptions:\n" |
| 215 | " -h, --help Show this help message and exit.\n" |
| 216 | " -v, --version Display the program version.\n" |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 217 | " -P, --port Specify the port option with an argument.\n" |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 218 | " -i, --ip Specify the ip option with an argument.\n" |
| 219 | " -l, --listen Start the program in listen mode.\n" |
| 220 | " -b, --bootstrap Specify the bootstrap option with an argument.\n" |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 221 | " -t, --turn_host Specify the turn_host option with an argument.\n" |
| 222 | " -u, --turn_user Specify the turn_user option with an argument.\n" |
| 223 | " -w, --turn_pass Specify the turn_pass option with an argument.\n" |
| 224 | " -r, --turn_realm Specify the turn_realm option with an argument.\n" |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 225 | " -c, --certificate Specify the certificate option with an argument.\n" |
| 226 | " -d, --configuration Specify the configuration option with an argument.\n" |
| 227 | " -p, --privateKey Specify the privateKey option with an argument.\n" |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 228 | " -a, --anonymous_cnx Enable the anonymous mode.\n" |
| 229 | " -vv, --verbose Enable verbose mode.\n"); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 230 | return EXIT_SUCCESS; |
| 231 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 232 | |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 233 | if (params.version) { |
| 234 | fmt::print("dnc v1.0\n"); |
| 235 | return EXIT_SUCCESS; |
| 236 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 237 | |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 238 | auto identity = dhtnet::loadIdentity(params.privateKey, params.cert); |
Louis Maillard | 2da6725 | 2024-07-23 14:17:16 -0400 | [diff] [blame^] | 239 | if (!identity.first || !identity.second) { |
| 240 | fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n"); |
| 241 | return EXIT_FAILURE; |
| 242 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 243 | fmt::print("Loaded identity: {}\n", identity.second->getId()); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 244 | |
| 245 | fmt::print("dnc 1.0\n"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 246 | std::unique_ptr<dhtnet::Dnc> dhtnc; |
| 247 | if (params.listen) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 248 | // create dnc instance |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 249 | dhtnc = std::make_unique<dhtnet::Dnc>(identity, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 250 | params.bootstrap, |
| 251 | params.turn_host, |
| 252 | params.turn_user, |
| 253 | params.turn_pass, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 254 | params.turn_realm, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 255 | params.anonymous_cnx, |
| 256 | params.verbose); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 257 | } else { |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 258 | dhtnc = std::make_unique<dhtnet::Dnc>(identity, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 259 | params.bootstrap, |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 260 | params.peer_id, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 261 | params.remote_host, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 262 | params.remote_port, |
| 263 | params.turn_host, |
| 264 | params.turn_user, |
| 265 | params.turn_pass, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 266 | params.turn_realm, |
| 267 | params.verbose); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 268 | } |
| 269 | dhtnc->run(); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 270 | return EXIT_SUCCESS; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 271 | } |