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