blob: 2e4d1bf1a622cf32e304fb4ff2780c128cf6f4ff [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 {};
Amna38768302023-08-21 11:51:56 -040054};
55
Amna41848a22024-01-22 16:22:57 -050056static const constexpr struct option long_options[]
57 = {{"help", no_argument, nullptr, 'h'},
58 {"version", no_argument, nullptr, 'v'},
Amnac75ffe92024-02-08 17:23:29 -050059 {"port", required_argument, nullptr, 'P'},
Amna41848a22024-01-22 16:22:57 -050060 {"ip", required_argument, nullptr, 'i'},
61 {"listen", no_argument, nullptr, 'l'},
62 {"bootstrap", required_argument, nullptr, 'b'},
Amnac75ffe92024-02-08 17:23:29 -050063 {"privateKey", required_argument, nullptr, 'p'},
Amna41848a22024-01-22 16:22:57 -050064 {"turn_host", required_argument, nullptr, 't'},
65 {"turn_user", required_argument, nullptr, 'u'},
66 {"turn_pass", required_argument, nullptr, 'w'},
67 {"turn_realm", required_argument, nullptr, 'r'},
Amna77e27dc2024-07-24 13:09:40 -040068 {"certificate", required_argument, nullptr, 'c'},
Amnac75ffe92024-02-08 17:23:29 -050069 {"configuration", required_argument, nullptr, 'd'},
Amna77e27dc2024-07-24 13:09:40 -040070 {"anonymous", no_argument, nullptr, 'a'},
Amna41848a22024-01-22 16:22:57 -050071 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040072
73dhtnc_params
74parse_args(int argc, char** argv)
75{
76 dhtnc_params params;
77 int opt;
Amna69996232024-07-23 14:04:44 -040078 int v_count = 0;
Amnac75ffe92024-02-08 17:23:29 -050079 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 -040080 switch (opt) {
81 case 'h':
82 params.help = true;
83 break;
Amna65a6ea82023-09-26 12:29:47 -040084 case 'v':
Amna69996232024-07-23 14:04:44 -040085 v_count++;
86 if (v_count == 1) {
87 params.version = true;
88 }else if (v_count == 2) {
89 params.version = false;
90 params.verbose = true;
91 }
Amna38768302023-08-21 11:51:56 -040092 break;
Amnac75ffe92024-02-08 17:23:29 -050093 case 'P':
Adrien Béraudecde63f2023-08-26 18:11:21 -040094 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040095 break;
96 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040097 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040098 break;
99 case 'l':
100 params.listen = true;
101 break;
102 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -0400103 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -0400104 break;
Amnac75ffe92024-02-08 17:23:29 -0500105 case 'p':
106 params.privateKey = optarg;
Amna38768302023-08-21 11:51:56 -0400107 break;
Amna2f3539b2023-09-18 13:59:22 -0400108 case 't':
109 params.turn_host = optarg;
110 break;
111 case 'u':
112 params.turn_user = optarg;
113 break;
114 case 'w':
115 params.turn_pass = optarg;
116 break;
117 case 'r':
118 params.turn_realm = optarg;
119 break;
Amnac75ffe92024-02-08 17:23:29 -0500120 case 'c':
121 params.cert = optarg;
Amna41848a22024-01-22 16:22:57 -0500122 break;
123 case 'd':
Amnac75ffe92024-02-08 17:23:29 -0500124 params.configuration = optarg;
Amna4325f0f2024-01-22 16:11:00 -0500125 break;
126 case 'a':
127 params.anonymous_cnx = true;
128 break;
Amna38768302023-08-21 11:51:56 -0400129 default:
130 std::cerr << "Invalid option" << std::endl;
131 exit(EXIT_FAILURE);
132 }
133 }
134
135 // If not listening, the peer_id argument is required
Amna65a6ea82023-09-26 12:29:47 -0400136 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400137 if (optind < argc) {
138 params.peer_id = dht::InfoHash(argv[optind]);
139 optind++; // Move to the next argument
140 } else {
141 std::cerr << "Error: Missing peer_id argument.\n";
142 exit(EXIT_FAILURE);
143 }
144 }
145
Amna41848a22024-01-22 16:22:57 -0500146 // extract values from dnc yaml file
Amnac75ffe92024-02-08 17:23:29 -0500147 if (!params.configuration.empty()) {
Amnac03c4b52024-07-24 17:57:25 -0400148 Log("Read configuration file: {}\n", params.configuration.c_str());
Amnac75ffe92024-02-08 17:23:29 -0500149 std::ifstream config_file(params.configuration);
Amna41848a22024-01-22 16:22:57 -0500150 if (!config_file.is_open()) {
151 std::cerr << "Error: Could not open configuration file.\n";
152 } else {
153 YAML::Node config = YAML::Load(config_file);
154 if (config["bootstrap"] && params.bootstrap.empty()) {
155 params.bootstrap = config["bootstrap"].as<std::string>();
156 }
Amnac75ffe92024-02-08 17:23:29 -0500157 if (config["privateKey"] && params.privateKey.empty()) {
158 params.privateKey = config["privateKey"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500159 }
160 if (config["turn_host"] && params.turn_host.empty()) {
161 params.turn_host = config["turn_host"].as<std::string>();
162 }
163 if (config["turn_user"] && params.turn_user.empty()) {
164 params.turn_user = config["turn_user"].as<std::string>();
165 }
166 if (config["turn_pass"] && params.turn_pass.empty()) {
167 params.turn_pass = config["turn_pass"].as<std::string>();
168 }
169 if (config["turn_realm"] && params.turn_realm.empty()) {
170 params.turn_realm = config["turn_realm"].as<std::string>();
171 }
Amnac75ffe92024-02-08 17:23:29 -0500172 if (config["certificate"] && params.cert.empty()) {
173 params.cert = config["certificate"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500174 }
175 if (config["ip"] && params.remote_host.empty()) {
Amna2ee14f02024-07-24 15:15:55 -0400176 params.remote_host = config["ip"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500177 }
178 if (config["port"] && params.remote_port == 0) {
179 params.remote_port = config["port"].as<int>();
180 }
Amna4325f0f2024-01-22 16:11:00 -0500181 if (config["anonymous"] && !params.anonymous_cnx) {
182 params.anonymous_cnx = config["anonymous"].as<bool>();
183 }
Amna69996232024-07-23 14:04:44 -0400184 if (config["verbose"] && !params.verbose) {
185 params.verbose = config["verbose"].as<bool>();
186 }
Amna2ee14f02024-07-24 15:15:55 -0400187 if (config["authorized_services"]) {
188 for (auto service : config["authorized_services"]) {
189 std::string ip = service["ip"].as<std::string>();
190 int port = 0;
191 try {
192 port = service["port"].as<int>();
193 } catch (YAML::TypedBadConversion<int> e) {
194 std::cerr << "Error: Invalid port number in configuration file.\n";
195 exit(EXIT_FAILURE);
196 }
197 if (port < 1 || port > 65535 || ip.empty()) {
198 std::cerr << "Error: Invalid ip or port number in configuration file.\n";
199 exit(EXIT_FAILURE);
200 }
201 params.authorizedServices[ip].push_back(port);
202 }
203 }
Amna41848a22024-01-22 16:22:57 -0500204 }
205 }
Amna38768302023-08-21 11:51:56 -0400206 return params;
207}
208
209static void
210setSipLogLevel()
211{
Amna38768302023-08-21 11:51:56 -0400212 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400213 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400214 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400215 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400216 }
217
218 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500219 pj_log_set_log_func([](int level, const char* data, int len) {
Amnac03c4b52024-07-24 17:57:25 -0400220 Log("{}", std::string_view(data, len));
Amnabe363922023-11-20 15:24:54 -0500221 });
Amna38768302023-08-21 11:51:56 -0400222}
223
224int
225main(int argc, char** argv)
226{
227 setSipLogLevel();
228 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400229
Amna41848a22024-01-22 16:22:57 -0500230 if (params.help) {
Amnac03c4b52024-07-24 17:57:25 -0400231 Log("Usage: dnc [options] [PEER_ID]\n"
Amna41848a22024-01-22 16:22:57 -0500232 "\nOptions:\n"
Amna77e27dc2024-07-24 13:09:40 -0400233 " -h, --help Show this help message and exit.\n"
234 " -v, --version Display the program version.\n"
235 " -P, --port [PORT] Specify the port option with an argument.\n"
236 " -i, --ip [ADDRESS] Specify the ip option with an argument.\n"
237 " -l, --listen Start the program in listen mode.\n"
238 " -b, --bootstrap [ADDRESS] Specify the bootstrap option with an argument.\n"
239 " -t, --turn_host [ADDRESS] Specify the turn_host option with an argument.\n"
240 " -u, --turn_user [USER] Specify the turn_user option with an argument.\n"
241 " -w, --turn_pass [SECRET] Specify the turn_pass option with an argument.\n"
242 " -r, --turn_realm [REALM] Specify the turn_realm option with an argument.\n"
243 " -c, --certificate [FILE] Specify the certificate option with an argument.\n"
244 " -d, --configuration [FILE] Specify the configuration option with an argument.\n"
245 " -p, --privateKey [FILE] Specify the privateKey option with an argument.\n"
246 " -a, --anonymous Enable the anonymous mode.\n"
247 " -vv, --verbose Enable verbose mode.\n");
Amna65a6ea82023-09-26 12:29:47 -0400248 return EXIT_SUCCESS;
249 }
Amna4325f0f2024-01-22 16:11:00 -0500250
Amna65a6ea82023-09-26 12:29:47 -0400251 if (params.version) {
Amnac03c4b52024-07-24 17:57:25 -0400252 Log("dnc v1.0\n");
Amna65a6ea82023-09-26 12:29:47 -0400253 return EXIT_SUCCESS;
254 }
Amna4325f0f2024-01-22 16:11:00 -0500255
Amnac75ffe92024-02-08 17:23:29 -0500256 auto identity = dhtnet::loadIdentity(params.privateKey, params.cert);
Louis Maillard2da67252024-07-23 14:17:16 -0400257 if (!identity.first || !identity.second) {
258 fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n");
259 return EXIT_FAILURE;
260 }
Amnac03c4b52024-07-24 17:57:25 -0400261 Log("Loaded identity: {}\n", identity.second->getId());
Amna65a6ea82023-09-26 12:29:47 -0400262
Amnac03c4b52024-07-24 17:57:25 -0400263 Log("dnc 1.0\n");
Amna38768302023-08-21 11:51:56 -0400264 std::unique_ptr<dhtnet::Dnc> dhtnc;
265 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400266 // create dnc instance
Amnac75ffe92024-02-08 17:23:29 -0500267 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
Amna41848a22024-01-22 16:22:57 -0500268 params.bootstrap,
269 params.turn_host,
270 params.turn_user,
271 params.turn_pass,
Amna4325f0f2024-01-22 16:11:00 -0500272 params.turn_realm,
Amna69996232024-07-23 14:04:44 -0400273 params.anonymous_cnx,
Amna2ee14f02024-07-24 15:15:55 -0400274 params.verbose,
275 params.authorizedServices);
Amna38768302023-08-21 11:51:56 -0400276 } else {
Amnac75ffe92024-02-08 17:23:29 -0500277 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400278 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400279 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400280 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400281 params.remote_port,
282 params.turn_host,
283 params.turn_user,
284 params.turn_pass,
Amna69996232024-07-23 14:04:44 -0400285 params.turn_realm,
286 params.verbose);
Amna38768302023-08-21 11:51:56 -0400287 }
288 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400289 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400290}