blob: f759ff5087c369c6b688d4e04f505b6b572070dc [file] [log] [blame]
Amna38768302023-08-21 11:51:56 -04001/*
Amna2f3539b2023-09-18 13:59:22 -04002 * Copyright (C) 2023 Savoir-faire Linux Inc.
Amna38768302023-08-21 11:51:56 -04003 *
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"
Amna423b25b2024-02-06 13:21:19 -050019#include "dhtnet_crtmgr/dhtnet_crtmgr.h"
Adrien Béraudc1cac452023-08-22 20:32:36 -040020
Amna38768302023-08-21 11:51:56 -040021#include <string>
22#include <vector>
23#include <iostream>
24#include <unistd.h>
25#include <getopt.h>
Amnab31c3742023-08-28 13:58:31 -040026#if __has_include(<fmt/std.h>)
Adrien Béraudecde63f2023-08-26 18:11:21 -040027#include <fmt/std.h>
Amnab31c3742023-08-28 13:58:31 -040028#else
29#include <fmt/ostream.h>
30#endif
Amna38768302023-08-21 11:51:56 -040031#include <netinet/in.h>
Amna41848a22024-01-22 16:22:57 -050032#include <yaml-cpp/yaml.h>
33#include <fstream>
Amna38768302023-08-21 11:51:56 -040034
35struct dhtnc_params
36{
37 bool help {false};
38 bool version {false};
39 bool listen {false};
Amnac75ffe92024-02-08 17:23:29 -050040 std::filesystem::path privateKey {};
41 std::filesystem::path cert {};
Adrien Béraudecde63f2023-08-26 18:11:21 -040042 std::string bootstrap {};
43 std::string remote_host {};
44 in_port_t remote_port {};
Amna38768302023-08-21 11:51:56 -040045 dht::InfoHash peer_id {};
Amna2f3539b2023-09-18 13:59:22 -040046 std::string turn_host {};
47 std::string turn_user {};
48 std::string turn_pass {};
49 std::string turn_realm {};
Amnac75ffe92024-02-08 17:23:29 -050050 std::string configuration {};
Amna4325f0f2024-01-22 16:11:00 -050051 bool anonymous_cnx {false};
Amna69996232024-07-23 14:04:44 -040052 bool verbose {false};
Amna2ee14f02024-07-24 15:15:55 -040053 std::map<std::string, std::vector<int>> authorizedServices {};
Amna45db7762024-07-24 18:33:48 -040054 bool enable_upnp {true};
Amna38768302023-08-21 11:51:56 -040055};
56
Amna41848a22024-01-22 16:22:57 -050057static const constexpr struct option long_options[]
58 = {{"help", no_argument, nullptr, 'h'},
59 {"version", no_argument, nullptr, 'v'},
Amnac75ffe92024-02-08 17:23:29 -050060 {"port", required_argument, nullptr, 'P'},
Amna41848a22024-01-22 16:22:57 -050061 {"ip", required_argument, nullptr, 'i'},
62 {"listen", no_argument, nullptr, 'l'},
63 {"bootstrap", required_argument, nullptr, 'b'},
Amnac75ffe92024-02-08 17:23:29 -050064 {"privateKey", required_argument, nullptr, 'p'},
Amna41848a22024-01-22 16:22:57 -050065 {"turn_host", required_argument, nullptr, 't'},
66 {"turn_user", required_argument, nullptr, 'u'},
67 {"turn_pass", required_argument, nullptr, 'w'},
68 {"turn_realm", required_argument, nullptr, 'r'},
Amna77e27dc2024-07-24 13:09:40 -040069 {"certificate", required_argument, nullptr, 'c'},
Amnac75ffe92024-02-08 17:23:29 -050070 {"configuration", required_argument, nullptr, 'd'},
Amna77e27dc2024-07-24 13:09:40 -040071 {"anonymous", no_argument, nullptr, 'a'},
Amna41848a22024-01-22 16:22:57 -050072 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040073
74dhtnc_params
75parse_args(int argc, char** argv)
76{
77 dhtnc_params params;
78 int opt;
Amna69996232024-07-23 14:04:44 -040079 int v_count = 0;
Amnac75ffe92024-02-08 17:23:29 -050080 while ((opt = getopt_long(argc, argv, "ahvlw:r:u:t:P:b:p:i:c:d:", long_options, nullptr)) != -1) {
Amna38768302023-08-21 11:51:56 -040081 switch (opt) {
82 case 'h':
83 params.help = true;
84 break;
Amna65a6ea82023-09-26 12:29:47 -040085 case 'v':
Amna69996232024-07-23 14:04:44 -040086 v_count++;
87 if (v_count == 1) {
88 params.version = true;
89 }else if (v_count == 2) {
90 params.version = false;
91 params.verbose = true;
92 }
Amna38768302023-08-21 11:51:56 -040093 break;
Amnac75ffe92024-02-08 17:23:29 -050094 case 'P':
Adrien Béraudecde63f2023-08-26 18:11:21 -040095 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040096 break;
97 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040098 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040099 break;
100 case 'l':
101 params.listen = true;
102 break;
103 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -0400104 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -0400105 break;
Amnac75ffe92024-02-08 17:23:29 -0500106 case 'p':
107 params.privateKey = optarg;
Amna38768302023-08-21 11:51:56 -0400108 break;
Amna2f3539b2023-09-18 13:59:22 -0400109 case 't':
110 params.turn_host = optarg;
111 break;
112 case 'u':
113 params.turn_user = optarg;
114 break;
115 case 'w':
116 params.turn_pass = optarg;
117 break;
118 case 'r':
119 params.turn_realm = optarg;
120 break;
Amnac75ffe92024-02-08 17:23:29 -0500121 case 'c':
122 params.cert = optarg;
Amna41848a22024-01-22 16:22:57 -0500123 break;
124 case 'd':
Amnac75ffe92024-02-08 17:23:29 -0500125 params.configuration = optarg;
Amna4325f0f2024-01-22 16:11:00 -0500126 break;
127 case 'a':
128 params.anonymous_cnx = true;
129 break;
Amna38768302023-08-21 11:51:56 -0400130 default:
131 std::cerr << "Invalid option" << std::endl;
132 exit(EXIT_FAILURE);
133 }
134 }
135
136 // If not listening, the peer_id argument is required
Amna65a6ea82023-09-26 12:29:47 -0400137 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400138 if (optind < argc) {
139 params.peer_id = dht::InfoHash(argv[optind]);
140 optind++; // Move to the next argument
141 } else {
142 std::cerr << "Error: Missing peer_id argument.\n";
143 exit(EXIT_FAILURE);
144 }
145 }
146
Amna41848a22024-01-22 16:22:57 -0500147 // extract values from dnc yaml file
Amnac75ffe92024-02-08 17:23:29 -0500148 if (!params.configuration.empty()) {
Amnac03c4b52024-07-24 17:57:25 -0400149 Log("Read configuration file: {}\n", params.configuration.c_str());
Amnac75ffe92024-02-08 17:23:29 -0500150 std::ifstream config_file(params.configuration);
Amna41848a22024-01-22 16:22:57 -0500151 if (!config_file.is_open()) {
152 std::cerr << "Error: Could not open configuration file.\n";
153 } else {
154 YAML::Node config = YAML::Load(config_file);
155 if (config["bootstrap"] && params.bootstrap.empty()) {
156 params.bootstrap = config["bootstrap"].as<std::string>();
157 }
Amnac75ffe92024-02-08 17:23:29 -0500158 if (config["privateKey"] && params.privateKey.empty()) {
159 params.privateKey = config["privateKey"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500160 }
161 if (config["turn_host"] && params.turn_host.empty()) {
162 params.turn_host = config["turn_host"].as<std::string>();
163 }
164 if (config["turn_user"] && params.turn_user.empty()) {
165 params.turn_user = config["turn_user"].as<std::string>();
166 }
167 if (config["turn_pass"] && params.turn_pass.empty()) {
168 params.turn_pass = config["turn_pass"].as<std::string>();
169 }
170 if (config["turn_realm"] && params.turn_realm.empty()) {
171 params.turn_realm = config["turn_realm"].as<std::string>();
172 }
Amnac75ffe92024-02-08 17:23:29 -0500173 if (config["certificate"] && params.cert.empty()) {
174 params.cert = config["certificate"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500175 }
176 if (config["ip"] && params.remote_host.empty()) {
Amna2ee14f02024-07-24 15:15:55 -0400177 params.remote_host = config["ip"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500178 }
179 if (config["port"] && params.remote_port == 0) {
180 params.remote_port = config["port"].as<int>();
181 }
Amna4325f0f2024-01-22 16:11:00 -0500182 if (config["anonymous"] && !params.anonymous_cnx) {
183 params.anonymous_cnx = config["anonymous"].as<bool>();
184 }
Amna69996232024-07-23 14:04:44 -0400185 if (config["verbose"] && !params.verbose) {
186 params.verbose = config["verbose"].as<bool>();
187 }
Amna2ee14f02024-07-24 15:15:55 -0400188 if (config["authorized_services"]) {
189 for (auto service : config["authorized_services"]) {
190 std::string ip = service["ip"].as<std::string>();
191 int port = 0;
192 try {
193 port = service["port"].as<int>();
194 } catch (YAML::TypedBadConversion<int> e) {
195 std::cerr << "Error: Invalid port number in configuration file.\n";
196 exit(EXIT_FAILURE);
197 }
198 if (port < 1 || port > 65535 || ip.empty()) {
199 std::cerr << "Error: Invalid ip or port number in configuration file.\n";
200 exit(EXIT_FAILURE);
201 }
202 params.authorizedServices[ip].push_back(port);
203 }
204 }
Amna45db7762024-07-24 18:33:48 -0400205 if (config["enable_upnp"]) {
206 params.enable_upnp = config["enable_upnp"].as<bool>();
207 }
Amna41848a22024-01-22 16:22:57 -0500208 }
209 }
Amna38768302023-08-21 11:51:56 -0400210 return params;
211}
212
213static void
214setSipLogLevel()
215{
Amna38768302023-08-21 11:51:56 -0400216 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400217 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400218 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400219 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400220 }
221
222 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500223 pj_log_set_log_func([](int level, const char* data, int len) {
Amnac03c4b52024-07-24 17:57:25 -0400224 Log("{}", std::string_view(data, len));
Amnabe363922023-11-20 15:24:54 -0500225 });
Amna38768302023-08-21 11:51:56 -0400226}
227
228int
229main(int argc, char** argv)
230{
231 setSipLogLevel();
232 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400233
Amna41848a22024-01-22 16:22:57 -0500234 if (params.help) {
Amnac03c4b52024-07-24 17:57:25 -0400235 Log("Usage: dnc [options] [PEER_ID]\n"
Amna41848a22024-01-22 16:22:57 -0500236 "\nOptions:\n"
Amna77e27dc2024-07-24 13:09:40 -0400237 " -h, --help Show this help message and exit.\n"
238 " -v, --version Display the program version.\n"
239 " -P, --port [PORT] Specify the port option with an argument.\n"
240 " -i, --ip [ADDRESS] Specify the ip option with an argument.\n"
241 " -l, --listen Start the program in listen mode.\n"
242 " -b, --bootstrap [ADDRESS] Specify the bootstrap option with an argument.\n"
243 " -t, --turn_host [ADDRESS] Specify the turn_host option with an argument.\n"
244 " -u, --turn_user [USER] Specify the turn_user option with an argument.\n"
245 " -w, --turn_pass [SECRET] Specify the turn_pass option with an argument.\n"
246 " -r, --turn_realm [REALM] Specify the turn_realm option with an argument.\n"
247 " -c, --certificate [FILE] Specify the certificate option with an argument.\n"
248 " -d, --configuration [FILE] Specify the configuration option with an argument.\n"
249 " -p, --privateKey [FILE] Specify the privateKey option with an argument.\n"
250 " -a, --anonymous Enable the anonymous mode.\n"
251 " -vv, --verbose Enable verbose mode.\n");
Amna65a6ea82023-09-26 12:29:47 -0400252 return EXIT_SUCCESS;
253 }
Amna4325f0f2024-01-22 16:11:00 -0500254
Amna65a6ea82023-09-26 12:29:47 -0400255 if (params.version) {
Amnac03c4b52024-07-24 17:57:25 -0400256 Log("dnc v1.0\n");
Amna65a6ea82023-09-26 12:29:47 -0400257 return EXIT_SUCCESS;
258 }
Amna4325f0f2024-01-22 16:11:00 -0500259
Amnac75ffe92024-02-08 17:23:29 -0500260 auto identity = dhtnet::loadIdentity(params.privateKey, params.cert);
Louis Maillard2da67252024-07-23 14:17:16 -0400261 if (!identity.first || !identity.second) {
262 fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n");
263 return EXIT_FAILURE;
264 }
Amnac03c4b52024-07-24 17:57:25 -0400265 Log("Loaded identity: {}\n", identity.second->getId());
Amna65a6ea82023-09-26 12:29:47 -0400266
Amnac03c4b52024-07-24 17:57:25 -0400267 Log("dnc 1.0\n");
Amna38768302023-08-21 11:51:56 -0400268 std::unique_ptr<dhtnet::Dnc> dhtnc;
269 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400270 // create dnc instance
Amnac75ffe92024-02-08 17:23:29 -0500271 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
Amna41848a22024-01-22 16:22:57 -0500272 params.bootstrap,
273 params.turn_host,
274 params.turn_user,
275 params.turn_pass,
Amna4325f0f2024-01-22 16:11:00 -0500276 params.turn_realm,
Amna69996232024-07-23 14:04:44 -0400277 params.anonymous_cnx,
Amna2ee14f02024-07-24 15:15:55 -0400278 params.verbose,
Amna45db7762024-07-24 18:33:48 -0400279 params.authorizedServices,
280 params.enable_upnp);
Amna38768302023-08-21 11:51:56 -0400281 } else {
Amnac75ffe92024-02-08 17:23:29 -0500282 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400283 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400284 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400285 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400286 params.remote_port,
287 params.turn_host,
288 params.turn_user,
289 params.turn_pass,
Amna69996232024-07-23 14:04:44 -0400290 params.turn_realm,
Amna45db7762024-07-24 18:33:48 -0400291 params.verbose,
292 params.enable_upnp);
Amna38768302023-08-21 11:51:56 -0400293 }
294 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400295 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400296}