blob: 02debd54e6c12f01faaacb858bc8667e83b114dd [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};
Adrien Béraudecde63f2023-08-26 18:11:21 -040040 std::filesystem::path path {};
41 std::string bootstrap {};
42 std::string remote_host {};
43 in_port_t remote_port {};
Amna38768302023-08-21 11:51:56 -040044 dht::InfoHash peer_id {};
Amna2f3539b2023-09-18 13:59:22 -040045 std::string turn_host {};
46 std::string turn_user {};
47 std::string turn_pass {};
48 std::string turn_realm {};
Amna41848a22024-01-22 16:22:57 -050049 std::string ca {};
50 std::string dnc_configuration {};
Amna4325f0f2024-01-22 16:11:00 -050051 bool anonymous_cnx {false};
Amna38768302023-08-21 11:51:56 -040052};
53
Amna41848a22024-01-22 16:22:57 -050054static const constexpr struct option long_options[]
55 = {{"help", no_argument, nullptr, 'h'},
56 {"version", no_argument, nullptr, 'v'},
57 {"port", required_argument, nullptr, 'p'},
58 {"ip", required_argument, nullptr, 'i'},
59 {"listen", no_argument, nullptr, 'l'},
60 {"bootstrap", required_argument, nullptr, 'b'},
61 {"id_path", required_argument, nullptr, 'I'},
62 {"turn_host", required_argument, nullptr, 't'},
63 {"turn_user", required_argument, nullptr, 'u'},
64 {"turn_pass", required_argument, nullptr, 'w'},
65 {"turn_realm", required_argument, nullptr, 'r'},
66 {"CA", required_argument, nullptr, 'C'},
67 {"dnc_configuration", required_argument, nullptr, 'd'},
Amna4325f0f2024-01-22 16:11:00 -050068 {"anonymous_cnx", no_argument, nullptr, 'a'},
Amna41848a22024-01-22 16:22:57 -050069 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040070
71dhtnc_params
72parse_args(int argc, char** argv)
73{
74 dhtnc_params params;
75 int opt;
Amna4325f0f2024-01-22 16:11:00 -050076 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 -040077 switch (opt) {
78 case 'h':
79 params.help = true;
80 break;
Amna65a6ea82023-09-26 12:29:47 -040081 case 'v':
Amna38768302023-08-21 11:51:56 -040082 params.version = true;
83 break;
84 case 'p':
Adrien Béraudecde63f2023-08-26 18:11:21 -040085 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040086 break;
87 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040088 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040089 break;
90 case 'l':
91 params.listen = true;
92 break;
93 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -040094 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -040095 break;
Adrien Béraudecde63f2023-08-26 18:11:21 -040096 case 'I':
97 params.path = optarg;
Amna38768302023-08-21 11:51:56 -040098 break;
Amna2f3539b2023-09-18 13:59:22 -040099 case 't':
100 params.turn_host = optarg;
101 break;
102 case 'u':
103 params.turn_user = optarg;
104 break;
105 case 'w':
106 params.turn_pass = optarg;
107 break;
108 case 'r':
109 params.turn_realm = optarg;
110 break;
Amna41848a22024-01-22 16:22:57 -0500111 case 'C':
112 params.ca = optarg;
113 break;
114 case 'd':
115 params.dnc_configuration = optarg;
Amna4325f0f2024-01-22 16:11:00 -0500116 break;
117 case 'a':
118 params.anonymous_cnx = true;
119 break;
Amna38768302023-08-21 11:51:56 -0400120 default:
121 std::cerr << "Invalid option" << std::endl;
122 exit(EXIT_FAILURE);
123 }
124 }
125
126 // If not listening, the peer_id argument is required
Amna65a6ea82023-09-26 12:29:47 -0400127 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400128 if (optind < argc) {
129 params.peer_id = dht::InfoHash(argv[optind]);
130 optind++; // Move to the next argument
131 } else {
132 std::cerr << "Error: Missing peer_id argument.\n";
133 exit(EXIT_FAILURE);
134 }
135 }
136
Amna41848a22024-01-22 16:22:57 -0500137 // extract values from dnc yaml file
138 if (!params.dnc_configuration.empty()) {
139 printf("read configuration file: %s\n", params.dnc_configuration.c_str());
140 std::ifstream config_file(params.dnc_configuration);
141 if (!config_file.is_open()) {
142 std::cerr << "Error: Could not open configuration file.\n";
143 } else {
144 YAML::Node config = YAML::Load(config_file);
145 if (config["bootstrap"] && params.bootstrap.empty()) {
146 params.bootstrap = config["bootstrap"].as<std::string>();
147 }
148 if (config["id_path"] && params.path.empty()) {
149 params.path = config["id_path"].as<std::string>();
150 }
151 if (config["turn_host"] && params.turn_host.empty()) {
152 params.turn_host = config["turn_host"].as<std::string>();
153 }
154 if (config["turn_user"] && params.turn_user.empty()) {
155 params.turn_user = config["turn_user"].as<std::string>();
156 }
157 if (config["turn_pass"] && params.turn_pass.empty()) {
158 params.turn_pass = config["turn_pass"].as<std::string>();
159 }
160 if (config["turn_realm"] && params.turn_realm.empty()) {
161 params.turn_realm = config["turn_realm"].as<std::string>();
162 }
163 if (config["CA"] && params.ca.empty()) {
164 params.ca = config["CA"].as<std::string>();
165 }
166 if (config["ip"] && params.remote_host.empty()) {
167 params.dnc_configuration = config["ip"].as<std::string>();
168 }
169 if (config["port"] && params.remote_port == 0) {
170 params.remote_port = config["port"].as<int>();
171 }
Amna4325f0f2024-01-22 16:11:00 -0500172 if (config["anonymous"] && !params.anonymous_cnx) {
173 params.anonymous_cnx = config["anonymous"].as<bool>();
174 }
Amna41848a22024-01-22 16:22:57 -0500175 }
176 }
Amna38768302023-08-21 11:51:56 -0400177 return params;
178}
179
180static void
181setSipLogLevel()
182{
Amna38768302023-08-21 11:51:56 -0400183 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400184 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400185 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400186 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400187 }
188
189 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500190 pj_log_set_log_func([](int level, const char* data, int len) {
191 fmt::print("{}", std::string_view(data, len));
192 });
Amna38768302023-08-21 11:51:56 -0400193}
194
195int
196main(int argc, char** argv)
197{
198 setSipLogLevel();
199 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400200
Amna41848a22024-01-22 16:22:57 -0500201 if (params.help) {
202 fmt::print("Usage: dnc [options] [PEER_ID]\n"
203 "\nOptions:\n"
204 " -h, --help Show this help message and exit.\n"
205 " -v, --version Display the program version.\n"
206 " -p, --port Specify the port option with an argument.\n"
207 " -i, --ip Specify the ip option with an argument.\n"
208 " -l, --listen Start the program in listen mode.\n"
209 " -b, --bootstrap Specify the bootstrap option with an argument.\n"
210 " -I, --id_path Specify the id_path option with an argument.\n"
211 " -t, --turn_host Specify the turn_host option with an argument.\n"
212 " -u, --turn_user Specify the turn_user option with an argument.\n"
213 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
214 " -r, --turn_realm Specify the turn_realm option with an argument.\n"
Amna4325f0f2024-01-22 16:11:00 -0500215 " -C, --CA Specify the CA option with an argument.\n"
216 " -d, --dnc_configuration Specify the dnc_configuration option with an argument.\n"
217 " -a, --anonymous_cnx Enable the anonymous mode.\n");
Amna65a6ea82023-09-26 12:29:47 -0400218 return EXIT_SUCCESS;
219 }
Amna4325f0f2024-01-22 16:11:00 -0500220
Amna65a6ea82023-09-26 12:29:47 -0400221 if (params.version) {
222 fmt::print("dnc v1.0\n");
223 return EXIT_SUCCESS;
224 }
Amna4325f0f2024-01-22 16:11:00 -0500225 auto identity = dhtnet::loadIdentity(params.path, params.ca);
226
Amna65a6ea82023-09-26 12:29:47 -0400227
228 fmt::print("dnc 1.0\n");
Adrien Béraudecde63f2023-08-26 18:11:21 -0400229 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
Amna38768302023-08-21 11:51:56 -0400230
231 std::unique_ptr<dhtnet::Dnc> dhtnc;
232 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400233 // create dnc instance
Amna41848a22024-01-22 16:22:57 -0500234 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
235 identity,
236 params.bootstrap,
237 params.turn_host,
238 params.turn_user,
239 params.turn_pass,
Amna4325f0f2024-01-22 16:11:00 -0500240 params.turn_realm,
241 params.anonymous_cnx);
Amna38768302023-08-21 11:51:56 -0400242 } else {
Adrien Béraudecde63f2023-08-26 18:11:21 -0400243 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
244 identity,
245 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400246 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400247 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400248 params.remote_port,
249 params.turn_host,
250 params.turn_user,
251 params.turn_pass,
Amna41848a22024-01-22 16:22:57 -0500252 params.turn_realm);
Amna38768302023-08-21 11:51:56 -0400253 }
254 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400255 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400256}