blob: 441ef567bd69a11fe2ae44c04b605a1d97df377c [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};
Amna38768302023-08-21 11:51:56 -040053};
54
Amna41848a22024-01-22 16:22:57 -050055static const constexpr struct option long_options[]
56 = {{"help", no_argument, nullptr, 'h'},
57 {"version", no_argument, nullptr, 'v'},
Amnac75ffe92024-02-08 17:23:29 -050058 {"port", required_argument, nullptr, 'P'},
Amna41848a22024-01-22 16:22:57 -050059 {"ip", required_argument, nullptr, 'i'},
60 {"listen", no_argument, nullptr, 'l'},
61 {"bootstrap", required_argument, nullptr, 'b'},
Amnac75ffe92024-02-08 17:23:29 -050062 {"privateKey", required_argument, nullptr, 'p'},
Amna41848a22024-01-22 16:22:57 -050063 {"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'},
Amnac75ffe92024-02-08 17:23:29 -050067 {"cert", required_argument, nullptr, 'c'},
68 {"configuration", required_argument, nullptr, 'd'},
Amna4325f0f2024-01-22 16:11:00 -050069 {"anonymous_cnx", no_argument, nullptr, 'a'},
Amna41848a22024-01-22 16:22:57 -050070 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040071
72dhtnc_params
73parse_args(int argc, char** argv)
74{
75 dhtnc_params params;
76 int opt;
Amna69996232024-07-23 14:04:44 -040077 int v_count = 0;
Amnac75ffe92024-02-08 17:23:29 -050078 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 -040079 switch (opt) {
80 case 'h':
81 params.help = true;
82 break;
Amna65a6ea82023-09-26 12:29:47 -040083 case 'v':
Amna69996232024-07-23 14:04:44 -040084 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 }
Amna38768302023-08-21 11:51:56 -040091 break;
Amnac75ffe92024-02-08 17:23:29 -050092 case 'P':
Adrien Béraudecde63f2023-08-26 18:11:21 -040093 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040094 break;
95 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040096 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040097 break;
98 case 'l':
99 params.listen = true;
100 break;
101 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -0400102 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -0400103 break;
Amnac75ffe92024-02-08 17:23:29 -0500104 case 'p':
105 params.privateKey = optarg;
Amna38768302023-08-21 11:51:56 -0400106 break;
Amna2f3539b2023-09-18 13:59:22 -0400107 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;
Amnac75ffe92024-02-08 17:23:29 -0500119 case 'c':
120 params.cert = optarg;
Amna41848a22024-01-22 16:22:57 -0500121 break;
122 case 'd':
Amnac75ffe92024-02-08 17:23:29 -0500123 params.configuration = optarg;
Amna4325f0f2024-01-22 16:11:00 -0500124 break;
125 case 'a':
126 params.anonymous_cnx = true;
127 break;
Amna38768302023-08-21 11:51:56 -0400128 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
Amna65a6ea82023-09-26 12:29:47 -0400135 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400136 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
Amna41848a22024-01-22 16:22:57 -0500145 // extract values from dnc yaml file
Amnac75ffe92024-02-08 17:23:29 -0500146 if (!params.configuration.empty()) {
147 printf("read configuration file: %s\n", params.configuration.c_str());
148 std::ifstream config_file(params.configuration);
Amna41848a22024-01-22 16:22:57 -0500149 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 }
Amnac75ffe92024-02-08 17:23:29 -0500156 if (config["privateKey"] && params.privateKey.empty()) {
157 params.privateKey = config["privateKey"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500158 }
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 }
Amnac75ffe92024-02-08 17:23:29 -0500171 if (config["certificate"] && params.cert.empty()) {
172 params.cert = config["certificate"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500173 }
174 if (config["ip"] && params.remote_host.empty()) {
Amnac75ffe92024-02-08 17:23:29 -0500175 params.configuration = config["ip"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500176 }
177 if (config["port"] && params.remote_port == 0) {
178 params.remote_port = config["port"].as<int>();
179 }
Amna4325f0f2024-01-22 16:11:00 -0500180 if (config["anonymous"] && !params.anonymous_cnx) {
181 params.anonymous_cnx = config["anonymous"].as<bool>();
182 }
Amna69996232024-07-23 14:04:44 -0400183 if (config["verbose"] && !params.verbose) {
184 params.verbose = config["verbose"].as<bool>();
185 }
Amna41848a22024-01-22 16:22:57 -0500186 }
187 }
Amna38768302023-08-21 11:51:56 -0400188 return params;
189}
190
191static void
192setSipLogLevel()
193{
Amna38768302023-08-21 11:51:56 -0400194 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400195 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400196 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400197 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400198 }
199
200 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500201 pj_log_set_log_func([](int level, const char* data, int len) {
202 fmt::print("{}", std::string_view(data, len));
203 });
Amna38768302023-08-21 11:51:56 -0400204}
205
206int
207main(int argc, char** argv)
208{
209 setSipLogLevel();
210 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400211
Amna41848a22024-01-22 16:22:57 -0500212 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"
Amnac75ffe92024-02-08 17:23:29 -0500217 " -P, --port Specify the port option with an argument.\n"
Amna41848a22024-01-22 16:22:57 -0500218 " -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"
Amna41848a22024-01-22 16:22:57 -0500221 " -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"
Amnac75ffe92024-02-08 17:23:29 -0500225 " -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"
Amna69996232024-07-23 14:04:44 -0400228 " -a, --anonymous_cnx Enable the anonymous mode.\n"
229 " -vv, --verbose Enable verbose mode.\n");
Amna65a6ea82023-09-26 12:29:47 -0400230 return EXIT_SUCCESS;
231 }
Amna4325f0f2024-01-22 16:11:00 -0500232
Amna65a6ea82023-09-26 12:29:47 -0400233 if (params.version) {
234 fmt::print("dnc v1.0\n");
235 return EXIT_SUCCESS;
236 }
Amna4325f0f2024-01-22 16:11:00 -0500237
Amnac75ffe92024-02-08 17:23:29 -0500238 auto identity = dhtnet::loadIdentity(params.privateKey, params.cert);
239 fmt::print("Loaded identity: {}\n", identity.second->getId());
Amna65a6ea82023-09-26 12:29:47 -0400240
241 fmt::print("dnc 1.0\n");
Amna38768302023-08-21 11:51:56 -0400242 std::unique_ptr<dhtnet::Dnc> dhtnc;
243 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400244 // create dnc instance
Amnac75ffe92024-02-08 17:23:29 -0500245 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
Amna41848a22024-01-22 16:22:57 -0500246 params.bootstrap,
247 params.turn_host,
248 params.turn_user,
249 params.turn_pass,
Amna4325f0f2024-01-22 16:11:00 -0500250 params.turn_realm,
Amna69996232024-07-23 14:04:44 -0400251 params.anonymous_cnx,
252 params.verbose);
Amna38768302023-08-21 11:51:56 -0400253 } else {
Amnac75ffe92024-02-08 17:23:29 -0500254 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400255 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400256 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400257 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400258 params.remote_port,
259 params.turn_host,
260 params.turn_user,
261 params.turn_pass,
Amna69996232024-07-23 14:04:44 -0400262 params.turn_realm,
263 params.verbose);
Amna38768302023-08-21 11:51:56 -0400264 }
265 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400266 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400267}