blob: 363e05e6d30f4459356b32c67d4e3affe085760b [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"
Adrien Béraudc1cac452023-08-22 20:32:36 -040019
Amna38768302023-08-21 11:51:56 -040020#include <string>
21#include <vector>
22#include <iostream>
23#include <unistd.h>
24#include <getopt.h>
Amnab31c3742023-08-28 13:58:31 -040025#if __has_include(<fmt/std.h>)
Adrien Béraudecde63f2023-08-26 18:11:21 -040026#include <fmt/std.h>
Amnab31c3742023-08-28 13:58:31 -040027#else
28#include <fmt/ostream.h>
29#endif
Amna38768302023-08-21 11:51:56 -040030#include <netinet/in.h>
Amna41848a22024-01-22 16:22:57 -050031#include <yaml-cpp/yaml.h>
32#include <fstream>
Amna38768302023-08-21 11:51:56 -040033
34struct dhtnc_params
35{
36 bool help {false};
37 bool version {false};
38 bool listen {false};
Adrien Béraudecde63f2023-08-26 18:11:21 -040039 std::filesystem::path path {};
40 std::string bootstrap {};
41 std::string remote_host {};
42 in_port_t remote_port {};
Amna38768302023-08-21 11:51:56 -040043 dht::InfoHash peer_id {};
Amna2f3539b2023-09-18 13:59:22 -040044 std::string turn_host {};
45 std::string turn_user {};
46 std::string turn_pass {};
47 std::string turn_realm {};
Amna41848a22024-01-22 16:22:57 -050048 std::string ca {};
49 std::string dnc_configuration {};
Amna4325f0f2024-01-22 16:11:00 -050050 bool anonymous_cnx {false};
Amna38768302023-08-21 11:51:56 -040051};
52
Amna41848a22024-01-22 16:22:57 -050053static const constexpr struct option long_options[]
54 = {{"help", no_argument, nullptr, 'h'},
55 {"version", no_argument, nullptr, 'v'},
56 {"port", required_argument, nullptr, 'p'},
57 {"ip", required_argument, nullptr, 'i'},
58 {"listen", no_argument, nullptr, 'l'},
59 {"bootstrap", required_argument, nullptr, 'b'},
60 {"id_path", required_argument, nullptr, 'I'},
61 {"turn_host", required_argument, nullptr, 't'},
62 {"turn_user", required_argument, nullptr, 'u'},
63 {"turn_pass", required_argument, nullptr, 'w'},
64 {"turn_realm", required_argument, nullptr, 'r'},
65 {"CA", required_argument, nullptr, 'C'},
66 {"dnc_configuration", required_argument, nullptr, 'd'},
Amna4325f0f2024-01-22 16:11:00 -050067 {"anonymous_cnx", no_argument, nullptr, 'a'},
Amna41848a22024-01-22 16:22:57 -050068 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040069
70dhtnc_params
71parse_args(int argc, char** argv)
72{
73 dhtnc_params params;
74 int opt;
Amna4325f0f2024-01-22 16:11:00 -050075 while ((opt = getopt_long(argc, argv, "ahvlw:r:u:t:I:b:p:i:C:d:", long_options, nullptr)) != -1) {
Amna38768302023-08-21 11:51:56 -040076 switch (opt) {
77 case 'h':
78 params.help = true;
79 break;
Amna65a6ea82023-09-26 12:29:47 -040080 case 'v':
Amna38768302023-08-21 11:51:56 -040081 params.version = true;
82 break;
83 case 'p':
Adrien Béraudecde63f2023-08-26 18:11:21 -040084 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040085 break;
86 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040087 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040088 break;
89 case 'l':
90 params.listen = true;
91 break;
92 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -040093 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -040094 break;
Adrien Béraudecde63f2023-08-26 18:11:21 -040095 case 'I':
96 params.path = optarg;
Amna38768302023-08-21 11:51:56 -040097 break;
Amna2f3539b2023-09-18 13:59:22 -040098 case 't':
99 params.turn_host = optarg;
100 break;
101 case 'u':
102 params.turn_user = optarg;
103 break;
104 case 'w':
105 params.turn_pass = optarg;
106 break;
107 case 'r':
108 params.turn_realm = optarg;
109 break;
Amna41848a22024-01-22 16:22:57 -0500110 case 'C':
111 params.ca = optarg;
112 break;
113 case 'd':
114 params.dnc_configuration = optarg;
Amna4325f0f2024-01-22 16:11:00 -0500115 break;
116 case 'a':
117 params.anonymous_cnx = true;
118 break;
Amna38768302023-08-21 11:51:56 -0400119 default:
120 std::cerr << "Invalid option" << std::endl;
121 exit(EXIT_FAILURE);
122 }
123 }
124
125 // If not listening, the peer_id argument is required
Amna65a6ea82023-09-26 12:29:47 -0400126 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400127 if (optind < argc) {
128 params.peer_id = dht::InfoHash(argv[optind]);
129 optind++; // Move to the next argument
130 } else {
131 std::cerr << "Error: Missing peer_id argument.\n";
132 exit(EXIT_FAILURE);
133 }
134 }
135
Amna41848a22024-01-22 16:22:57 -0500136 // extract values from dnc yaml file
137 if (!params.dnc_configuration.empty()) {
138 printf("read configuration file: %s\n", params.dnc_configuration.c_str());
139 std::ifstream config_file(params.dnc_configuration);
140 if (!config_file.is_open()) {
141 std::cerr << "Error: Could not open configuration file.\n";
142 } else {
143 YAML::Node config = YAML::Load(config_file);
144 if (config["bootstrap"] && params.bootstrap.empty()) {
145 params.bootstrap = config["bootstrap"].as<std::string>();
146 }
147 if (config["id_path"] && params.path.empty()) {
148 params.path = config["id_path"].as<std::string>();
149 }
150 if (config["turn_host"] && params.turn_host.empty()) {
151 params.turn_host = config["turn_host"].as<std::string>();
152 }
153 if (config["turn_user"] && params.turn_user.empty()) {
154 params.turn_user = config["turn_user"].as<std::string>();
155 }
156 if (config["turn_pass"] && params.turn_pass.empty()) {
157 params.turn_pass = config["turn_pass"].as<std::string>();
158 }
159 if (config["turn_realm"] && params.turn_realm.empty()) {
160 params.turn_realm = config["turn_realm"].as<std::string>();
161 }
162 if (config["CA"] && params.ca.empty()) {
163 params.ca = config["CA"].as<std::string>();
164 }
165 if (config["ip"] && params.remote_host.empty()) {
166 params.dnc_configuration = config["ip"].as<std::string>();
167 }
168 if (config["port"] && params.remote_port == 0) {
169 params.remote_port = config["port"].as<int>();
170 }
Amna4325f0f2024-01-22 16:11:00 -0500171 if (config["anonymous"] && !params.anonymous_cnx) {
172 params.anonymous_cnx = config["anonymous"].as<bool>();
173 }
Amna41848a22024-01-22 16:22:57 -0500174 }
175 }
Amna38768302023-08-21 11:51:56 -0400176 return params;
177}
178
179static void
180setSipLogLevel()
181{
Amna38768302023-08-21 11:51:56 -0400182 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400183 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400184 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400185 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400186 }
187
188 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500189 pj_log_set_log_func([](int level, const char* data, int len) {
190 fmt::print("{}", std::string_view(data, len));
191 });
Amna38768302023-08-21 11:51:56 -0400192}
193
194int
195main(int argc, char** argv)
196{
197 setSipLogLevel();
198 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400199
Amna41848a22024-01-22 16:22:57 -0500200 if (params.help) {
201 fmt::print("Usage: dnc [options] [PEER_ID]\n"
202 "\nOptions:\n"
203 " -h, --help Show this help message and exit.\n"
204 " -v, --version Display the program version.\n"
205 " -p, --port Specify the port option with an argument.\n"
206 " -i, --ip Specify the ip option with an argument.\n"
207 " -l, --listen Start the program in listen mode.\n"
208 " -b, --bootstrap Specify the bootstrap option with an argument.\n"
209 " -I, --id_path Specify the id_path option with an argument.\n"
210 " -t, --turn_host Specify the turn_host option with an argument.\n"
211 " -u, --turn_user Specify the turn_user option with an argument.\n"
212 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
213 " -r, --turn_realm Specify the turn_realm option with an argument.\n"
Amna4325f0f2024-01-22 16:11:00 -0500214 " -C, --CA Specify the CA option with an argument.\n"
215 " -d, --dnc_configuration Specify the dnc_configuration option with an argument.\n"
216 " -a, --anonymous_cnx Enable the anonymous mode.\n");
Amna65a6ea82023-09-26 12:29:47 -0400217 return EXIT_SUCCESS;
218 }
Amna4325f0f2024-01-22 16:11:00 -0500219
Amna65a6ea82023-09-26 12:29:47 -0400220 if (params.version) {
221 fmt::print("dnc v1.0\n");
222 return EXIT_SUCCESS;
223 }
Amna4325f0f2024-01-22 16:11:00 -0500224 auto identity = dhtnet::loadIdentity(params.path, params.ca);
225
Amna65a6ea82023-09-26 12:29:47 -0400226
227 fmt::print("dnc 1.0\n");
Adrien Béraudecde63f2023-08-26 18:11:21 -0400228 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
Amna38768302023-08-21 11:51:56 -0400229
230 std::unique_ptr<dhtnet::Dnc> dhtnc;
231 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400232 // create dnc instance
Amna41848a22024-01-22 16:22:57 -0500233 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
234 identity,
235 params.bootstrap,
236 params.turn_host,
237 params.turn_user,
238 params.turn_pass,
Amna4325f0f2024-01-22 16:11:00 -0500239 params.turn_realm,
240 params.anonymous_cnx);
Amna38768302023-08-21 11:51:56 -0400241 } else {
Adrien Béraudecde63f2023-08-26 18:11:21 -0400242 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
243 identity,
244 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400245 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400246 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400247 params.remote_port,
248 params.turn_host,
249 params.turn_user,
250 params.turn_pass,
Amna41848a22024-01-22 16:22:57 -0500251 params.turn_realm);
Amna38768302023-08-21 11:51:56 -0400252 }
253 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400254 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400255}