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" |
Adrien Béraud | c1cac45 | 2023-08-22 20:32:36 -0400 | [diff] [blame] | 19 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | #include <iostream> |
| 23 | #include <unistd.h> |
| 24 | #include <getopt.h> |
Amna | b31c374 | 2023-08-28 13:58:31 -0400 | [diff] [blame] | 25 | #if __has_include(<fmt/std.h>) |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 26 | #include <fmt/std.h> |
Amna | b31c374 | 2023-08-28 13:58:31 -0400 | [diff] [blame] | 27 | #else |
| 28 | #include <fmt/ostream.h> |
| 29 | #endif |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 30 | #include <netinet/in.h> |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 31 | #include <yaml-cpp/yaml.h> |
| 32 | #include <fstream> |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 33 | |
| 34 | struct dhtnc_params |
| 35 | { |
| 36 | bool help {false}; |
| 37 | bool version {false}; |
| 38 | bool listen {false}; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 39 | std::filesystem::path path {}; |
| 40 | std::string bootstrap {}; |
| 41 | std::string remote_host {}; |
| 42 | in_port_t remote_port {}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 43 | dht::InfoHash peer_id {}; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 44 | std::string turn_host {}; |
| 45 | std::string turn_user {}; |
| 46 | std::string turn_pass {}; |
| 47 | std::string turn_realm {}; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 48 | std::string ca {}; |
| 49 | std::string dnc_configuration {}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 52 | static const constexpr struct option long_options[] |
| 53 | = {{"help", no_argument, nullptr, 'h'}, |
| 54 | {"version", no_argument, nullptr, 'v'}, |
| 55 | {"port", required_argument, nullptr, 'p'}, |
| 56 | {"ip", required_argument, nullptr, 'i'}, |
| 57 | {"listen", no_argument, nullptr, 'l'}, |
| 58 | {"bootstrap", required_argument, nullptr, 'b'}, |
| 59 | {"id_path", required_argument, nullptr, 'I'}, |
| 60 | {"turn_host", required_argument, nullptr, 't'}, |
| 61 | {"turn_user", required_argument, nullptr, 'u'}, |
| 62 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 63 | {"turn_realm", required_argument, nullptr, 'r'}, |
| 64 | {"CA", required_argument, nullptr, 'C'}, |
| 65 | {"dnc_configuration", required_argument, nullptr, 'd'}, |
| 66 | {nullptr, 0, nullptr, 0}}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 67 | |
| 68 | dhtnc_params |
| 69 | parse_args(int argc, char** argv) |
| 70 | { |
| 71 | dhtnc_params params; |
| 72 | int opt; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 73 | while ((opt = getopt_long(argc, argv, "hvlw:r:u:t:I:b:p:i:C:d:", long_options, nullptr)) != -1) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 74 | switch (opt) { |
| 75 | case 'h': |
| 76 | params.help = true; |
| 77 | break; |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 78 | case 'v': |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 79 | params.version = true; |
| 80 | break; |
| 81 | case 'p': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 82 | params.remote_port = std::stoi(optarg); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 83 | break; |
| 84 | case 'i': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 85 | params.remote_host = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 86 | break; |
| 87 | case 'l': |
| 88 | params.listen = true; |
| 89 | break; |
| 90 | case 'b': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 91 | params.bootstrap = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 92 | break; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 93 | case 'I': |
| 94 | params.path = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 95 | break; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 96 | case 't': |
| 97 | params.turn_host = optarg; |
| 98 | break; |
| 99 | case 'u': |
| 100 | params.turn_user = optarg; |
| 101 | break; |
| 102 | case 'w': |
| 103 | params.turn_pass = optarg; |
| 104 | break; |
| 105 | case 'r': |
| 106 | params.turn_realm = optarg; |
| 107 | break; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 108 | case 'C': |
| 109 | params.ca = optarg; |
| 110 | break; |
| 111 | case 'd': |
| 112 | params.dnc_configuration = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 113 | default: |
| 114 | std::cerr << "Invalid option" << std::endl; |
| 115 | exit(EXIT_FAILURE); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // If not listening, the peer_id argument is required |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 120 | if (!params.listen && !params.help && !params.version) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 121 | if (optind < argc) { |
| 122 | params.peer_id = dht::InfoHash(argv[optind]); |
| 123 | optind++; // Move to the next argument |
| 124 | } else { |
| 125 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 126 | exit(EXIT_FAILURE); |
| 127 | } |
| 128 | } |
| 129 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 130 | // extract values from dnc yaml file |
| 131 | if (!params.dnc_configuration.empty()) { |
| 132 | printf("read configuration file: %s\n", params.dnc_configuration.c_str()); |
| 133 | std::ifstream config_file(params.dnc_configuration); |
| 134 | if (!config_file.is_open()) { |
| 135 | std::cerr << "Error: Could not open configuration file.\n"; |
| 136 | } else { |
| 137 | YAML::Node config = YAML::Load(config_file); |
| 138 | if (config["bootstrap"] && params.bootstrap.empty()) { |
| 139 | params.bootstrap = config["bootstrap"].as<std::string>(); |
| 140 | } |
| 141 | if (config["id_path"] && params.path.empty()) { |
| 142 | params.path = config["id_path"].as<std::string>(); |
| 143 | } |
| 144 | if (config["turn_host"] && params.turn_host.empty()) { |
| 145 | params.turn_host = config["turn_host"].as<std::string>(); |
| 146 | } |
| 147 | if (config["turn_user"] && params.turn_user.empty()) { |
| 148 | params.turn_user = config["turn_user"].as<std::string>(); |
| 149 | } |
| 150 | if (config["turn_pass"] && params.turn_pass.empty()) { |
| 151 | params.turn_pass = config["turn_pass"].as<std::string>(); |
| 152 | } |
| 153 | if (config["turn_realm"] && params.turn_realm.empty()) { |
| 154 | params.turn_realm = config["turn_realm"].as<std::string>(); |
| 155 | } |
| 156 | if (config["CA"] && params.ca.empty()) { |
| 157 | params.ca = config["CA"].as<std::string>(); |
| 158 | } |
| 159 | if (config["ip"] && params.remote_host.empty()) { |
| 160 | params.dnc_configuration = config["ip"].as<std::string>(); |
| 161 | } |
| 162 | if (config["port"] && params.remote_port == 0) { |
| 163 | params.remote_port = config["port"].as<int>(); |
| 164 | } |
| 165 | } |
| 166 | } |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 167 | return params; |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | setSipLogLevel() |
| 172 | { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 173 | int level = 0; |
Adrien Béraud | 6c6a962 | 2023-08-27 12:49:31 -0400 | [diff] [blame] | 174 | if (char* envvar = getenv("SIPLOGLEVEL")) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 175 | // From 0 (min) to 6 (max) |
Adrien Béraud | 6c6a962 | 2023-08-27 12:49:31 -0400 | [diff] [blame] | 176 | level = std::clamp(std::stoi(envvar), 0, 6); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | pj_log_set_level(level); |
Amna | be36392 | 2023-11-20 15:24:54 -0500 | [diff] [blame] | 180 | pj_log_set_log_func([](int level, const char* data, int len) { |
| 181 | fmt::print("{}", std::string_view(data, len)); |
| 182 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int |
| 186 | main(int argc, char** argv) |
| 187 | { |
| 188 | setSipLogLevel(); |
| 189 | auto params = parse_args(argc, argv); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 190 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 191 | if (params.help) { |
| 192 | fmt::print("Usage: dnc [options] [PEER_ID]\n" |
| 193 | "\nOptions:\n" |
| 194 | " -h, --help Show this help message and exit.\n" |
| 195 | " -v, --version Display the program version.\n" |
| 196 | " -p, --port Specify the port option with an argument.\n" |
| 197 | " -i, --ip Specify the ip option with an argument.\n" |
| 198 | " -l, --listen Start the program in listen mode.\n" |
| 199 | " -b, --bootstrap Specify the bootstrap option with an argument.\n" |
| 200 | " -I, --id_path Specify the id_path option with an argument.\n" |
| 201 | " -t, --turn_host Specify the turn_host option with an argument.\n" |
| 202 | " -u, --turn_user Specify the turn_user option with an argument.\n" |
| 203 | " -w, --turn_pass Specify the turn_pass option with an argument.\n" |
| 204 | " -r, --turn_realm Specify the turn_realm option with an argument.\n" |
| 205 | " -C, --CA Specify the CA option with an argument.\n"); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 206 | return EXIT_SUCCESS; |
| 207 | } |
| 208 | if (params.version) { |
| 209 | fmt::print("dnc v1.0\n"); |
| 210 | return EXIT_SUCCESS; |
| 211 | } |
| 212 | |
| 213 | fmt::print("dnc 1.0\n"); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 214 | auto identity = dhtnet::loadIdentity(params.path, params.ca); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 215 | fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 216 | |
| 217 | std::unique_ptr<dhtnet::Dnc> dhtnc; |
| 218 | if (params.listen) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 219 | // create dnc instance |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 220 | dhtnc = std::make_unique<dhtnet::Dnc>(params.path, |
| 221 | identity, |
| 222 | params.bootstrap, |
| 223 | params.turn_host, |
| 224 | params.turn_user, |
| 225 | params.turn_pass, |
| 226 | params.turn_realm); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 227 | } else { |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 228 | dhtnc = std::make_unique<dhtnet::Dnc>(params.path, |
| 229 | identity, |
| 230 | params.bootstrap, |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 231 | params.peer_id, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 232 | params.remote_host, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 233 | params.remote_port, |
| 234 | params.turn_host, |
| 235 | params.turn_user, |
| 236 | params.turn_pass, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 237 | params.turn_realm); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 238 | } |
| 239 | dhtnc->run(); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 240 | return EXIT_SUCCESS; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 241 | } |