blob: 0ba8b572ce97b20c08f7d4adbbe81e833715487b [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 {};
Amna38768302023-08-21 11:51:56 -040050};
51
Amna41848a22024-01-22 16:22:57 -050052static const constexpr struct option long_options[]
53 = {{"help", no_argument, nullptr, 'h'},
54 {"version", no_argument, nullptr, 'v'},
55 {"port", required_argument, nullptr, 'p'},
56 {"ip", required_argument, nullptr, 'i'},
57 {"listen", no_argument, nullptr, 'l'},
58 {"bootstrap", required_argument, nullptr, 'b'},
59 {"id_path", required_argument, nullptr, 'I'},
60 {"turn_host", required_argument, nullptr, 't'},
61 {"turn_user", required_argument, nullptr, 'u'},
62 {"turn_pass", required_argument, nullptr, 'w'},
63 {"turn_realm", required_argument, nullptr, 'r'},
64 {"CA", required_argument, nullptr, 'C'},
65 {"dnc_configuration", required_argument, nullptr, 'd'},
66 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040067
68dhtnc_params
69parse_args(int argc, char** argv)
70{
71 dhtnc_params params;
72 int opt;
Amna41848a22024-01-22 16:22:57 -050073 while ((opt = getopt_long(argc, argv, "hvlw:r:u:t:I:b:p:i:C:d:", long_options, nullptr)) != -1) {
Amna38768302023-08-21 11:51:56 -040074 switch (opt) {
75 case 'h':
76 params.help = true;
77 break;
Amna65a6ea82023-09-26 12:29:47 -040078 case 'v':
Amna38768302023-08-21 11:51:56 -040079 params.version = true;
80 break;
81 case 'p':
Adrien Béraudecde63f2023-08-26 18:11:21 -040082 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040083 break;
84 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040085 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040086 break;
87 case 'l':
88 params.listen = true;
89 break;
90 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -040091 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -040092 break;
Adrien Béraudecde63f2023-08-26 18:11:21 -040093 case 'I':
94 params.path = optarg;
Amna38768302023-08-21 11:51:56 -040095 break;
Amna2f3539b2023-09-18 13:59:22 -040096 case 't':
97 params.turn_host = optarg;
98 break;
99 case 'u':
100 params.turn_user = optarg;
101 break;
102 case 'w':
103 params.turn_pass = optarg;
104 break;
105 case 'r':
106 params.turn_realm = optarg;
107 break;
Amna41848a22024-01-22 16:22:57 -0500108 case 'C':
109 params.ca = optarg;
110 break;
111 case 'd':
112 params.dnc_configuration = optarg;
Amna38768302023-08-21 11:51:56 -0400113 default:
114 std::cerr << "Invalid option" << std::endl;
115 exit(EXIT_FAILURE);
116 }
117 }
118
119 // If not listening, the peer_id argument is required
Amna65a6ea82023-09-26 12:29:47 -0400120 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400121 if (optind < argc) {
122 params.peer_id = dht::InfoHash(argv[optind]);
123 optind++; // Move to the next argument
124 } else {
125 std::cerr << "Error: Missing peer_id argument.\n";
126 exit(EXIT_FAILURE);
127 }
128 }
129
Amna41848a22024-01-22 16:22:57 -0500130 // extract values from dnc yaml file
131 if (!params.dnc_configuration.empty()) {
132 printf("read configuration file: %s\n", params.dnc_configuration.c_str());
133 std::ifstream config_file(params.dnc_configuration);
134 if (!config_file.is_open()) {
135 std::cerr << "Error: Could not open configuration file.\n";
136 } else {
137 YAML::Node config = YAML::Load(config_file);
138 if (config["bootstrap"] && params.bootstrap.empty()) {
139 params.bootstrap = config["bootstrap"].as<std::string>();
140 }
141 if (config["id_path"] && params.path.empty()) {
142 params.path = config["id_path"].as<std::string>();
143 }
144 if (config["turn_host"] && params.turn_host.empty()) {
145 params.turn_host = config["turn_host"].as<std::string>();
146 }
147 if (config["turn_user"] && params.turn_user.empty()) {
148 params.turn_user = config["turn_user"].as<std::string>();
149 }
150 if (config["turn_pass"] && params.turn_pass.empty()) {
151 params.turn_pass = config["turn_pass"].as<std::string>();
152 }
153 if (config["turn_realm"] && params.turn_realm.empty()) {
154 params.turn_realm = config["turn_realm"].as<std::string>();
155 }
156 if (config["CA"] && params.ca.empty()) {
157 params.ca = config["CA"].as<std::string>();
158 }
159 if (config["ip"] && params.remote_host.empty()) {
160 params.dnc_configuration = config["ip"].as<std::string>();
161 }
162 if (config["port"] && params.remote_port == 0) {
163 params.remote_port = config["port"].as<int>();
164 }
165 }
166 }
Amna38768302023-08-21 11:51:56 -0400167 return params;
168}
169
170static void
171setSipLogLevel()
172{
Amna38768302023-08-21 11:51:56 -0400173 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400174 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400175 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400176 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400177 }
178
179 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500180 pj_log_set_log_func([](int level, const char* data, int len) {
181 fmt::print("{}", std::string_view(data, len));
182 });
Amna38768302023-08-21 11:51:56 -0400183}
184
185int
186main(int argc, char** argv)
187{
188 setSipLogLevel();
189 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400190
Amna41848a22024-01-22 16:22:57 -0500191 if (params.help) {
192 fmt::print("Usage: dnc [options] [PEER_ID]\n"
193 "\nOptions:\n"
194 " -h, --help Show this help message and exit.\n"
195 " -v, --version Display the program version.\n"
196 " -p, --port Specify the port option with an argument.\n"
197 " -i, --ip Specify the ip option with an argument.\n"
198 " -l, --listen Start the program in listen mode.\n"
199 " -b, --bootstrap Specify the bootstrap option with an argument.\n"
200 " -I, --id_path Specify the id_path option with an argument.\n"
201 " -t, --turn_host Specify the turn_host option with an argument.\n"
202 " -u, --turn_user Specify the turn_user option with an argument.\n"
203 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
204 " -r, --turn_realm Specify the turn_realm option with an argument.\n"
205 " -C, --CA Specify the CA option with an argument.\n");
Amna65a6ea82023-09-26 12:29:47 -0400206 return EXIT_SUCCESS;
207 }
208 if (params.version) {
209 fmt::print("dnc v1.0\n");
210 return EXIT_SUCCESS;
211 }
212
213 fmt::print("dnc 1.0\n");
Amna41848a22024-01-22 16:22:57 -0500214 auto identity = dhtnet::loadIdentity(params.path, params.ca);
Adrien Béraudecde63f2023-08-26 18:11:21 -0400215 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
Amna38768302023-08-21 11:51:56 -0400216
217 std::unique_ptr<dhtnet::Dnc> dhtnc;
218 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400219 // create dnc instance
Amna41848a22024-01-22 16:22:57 -0500220 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
221 identity,
222 params.bootstrap,
223 params.turn_host,
224 params.turn_user,
225 params.turn_pass,
226 params.turn_realm);
Amna38768302023-08-21 11:51:56 -0400227 } else {
Adrien Béraudecde63f2023-08-26 18:11:21 -0400228 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
229 identity,
230 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400231 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400232 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400233 params.remote_port,
234 params.turn_host,
235 params.turn_user,
236 params.turn_pass,
Amna41848a22024-01-22 16:22:57 -0500237 params.turn_realm);
Amna38768302023-08-21 11:51:56 -0400238 }
239 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400240 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400241}