blob: 0e91e91f72c2de14343d17308c61449ee55764f7 [file] [log] [blame]
Amna4a70f5c2023-09-14 17:32:05 -04001/*
2 * Copyright (C) 2023 Savoir-faire Linux Inc.
3 *
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 "dsh.h"
18#include "../common.h"
Amna423b25b2024-02-06 13:21:19 -050019#include "dhtnet_crtmgr/dhtnet_crtmgr.h"
20
Amna4a70f5c2023-09-14 17:32:05 -040021#include <string>
22#include <vector>
23#include <iostream>
24#include <unistd.h>
25#include <getopt.h>
26
27#include <netinet/in.h>
Adrien BĂ©raud277edaf2023-09-20 10:57:43 -040028#if __has_include(<fmt/std.h>)
29#include <fmt/std.h>
30#else
31#include <fmt/ostream.h>
32#endif
Amna2b5b07f2024-01-22 17:04:36 -050033#include <yaml-cpp/yaml.h>
34#include <fstream>
Amna4a70f5c2023-09-14 17:32:05 -040035
36struct dhtsh_params
37{
38 bool help {false};
39 bool version {false};
40 bool listen {false};
41 std::filesystem::path path {};
42 std::string bootstrap {};
43 dht::InfoHash peer_id {};
44 std::string binary {};
Amna2b5b07f2024-01-22 17:04:36 -050045 std::string ca {};
46 std::string turn_host {};
47 std::string turn_user {};
48 std::string turn_pass {};
49 std::string turn_realm {};
50 std::string dsh_configuration {};
Amna4325f0f2024-01-22 16:11:00 -050051 bool anonymous_cnx {false};
Amna4a70f5c2023-09-14 17:32:05 -040052};
53
Amna2b5b07f2024-01-22 17:04:36 -050054static const constexpr struct option long_options[]
55 = {{"help", no_argument, nullptr, 'h'},
56 {"version", no_argument, nullptr, 'v'},
57 {"listen", no_argument, nullptr, 'l'},
58 {"bootstrap", required_argument, nullptr, 'b'},
59 {"binary", required_argument, nullptr, 's'},
60 {"id_path", required_argument, nullptr, 'I'},
61 {"CA", required_argument, nullptr, 'C'},
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 {"dsh_configuration", required_argument, nullptr, 'd'},
Amna4325f0f2024-01-22 16:11:00 -050067 {"anonymous", no_argument, nullptr, 'a'},
Amna2b5b07f2024-01-22 17:04:36 -050068 {nullptr, 0, nullptr, 0}};
Amna4a70f5c2023-09-14 17:32:05 -040069
70dhtsh_params
71parse_args(int argc, char** argv)
72{
73 dhtsh_params params;
74 int opt;
Amna2b5b07f2024-01-22 17:04:36 -050075 while ((opt = getopt_long(argc, argv, "hvls:I:p:i:C:r:w:u:t:d:", long_options, nullptr)) != -1) {
Amna4a70f5c2023-09-14 17:32:05 -040076 switch (opt) {
77 case 'h':
78 params.help = true;
79 break;
80 case 'v':
81 params.version = true;
82 break;
83 case 'l':
84 params.listen = true;
85 break;
86 case 'b':
87 params.bootstrap = optarg;
88 break;
89 case 's':
90 params.binary = optarg;
91 break;
92 case 'I':
93 params.path = optarg;
94 break;
Amna2b5b07f2024-01-22 17:04:36 -050095 case 't':
96 params.turn_host = optarg;
97 break;
98 case 'u':
99 params.turn_user = optarg;
100 break;
101 case 'w':
102 params.turn_pass = optarg;
103 break;
104 case 'r':
105 params.turn_realm = optarg;
106 break;
107 case 'C':
108 params.ca = optarg;
109 break;
110 case 'd':
111 params.dsh_configuration = optarg;
Amna4325f0f2024-01-22 16:11:00 -0500112 break;
113 case 'a':
114 params.anonymous_cnx = true;
115 break;
Amna4a70f5c2023-09-14 17:32:05 -0400116 default:
117 std::cerr << "Invalid option" << std::endl;
118 exit(EXIT_FAILURE);
119 }
120 }
121
122 // If not listening, the peer_id argument is required
Amna77bb5852023-09-28 10:31:11 -0400123 if (!params.listen && !params.help && !params.version) {
Amna4a70f5c2023-09-14 17:32:05 -0400124 if (optind < argc) {
125 params.peer_id = dht::InfoHash(argv[optind]);
126 optind++; // Move to the next argument
127 } else {
128 std::cerr << "Error: Missing peer_id argument.\n";
129 exit(EXIT_FAILURE);
130 }
131 }
132
Amna41848a22024-01-22 16:22:57 -0500133 // extract values from dsh yaml file
134 if (!params.dsh_configuration.empty()) {
135 printf("read configuration file: %s\n", params.dsh_configuration.c_str());
136 std::ifstream config_file(params.dsh_configuration);
137 if (!config_file.is_open()) {
138 std::cerr << "Error: Could not open configuration file.\n";
139 } else {
140 YAML::Node config = YAML::Load(config_file);
141 if (config["bootstrap"] && params.bootstrap.empty()) {
142 params.bootstrap = config["bootstrap"].as<std::string>();
143 }
144 if (config["id_path"] && params.path.empty()) {
145 params.path = config["id_path"].as<std::string>();
146 }
147 if (config["turn_host"] && params.turn_host.empty()) {
148 params.turn_host = config["turn_host"].as<std::string>();
149 }
150 if (config["turn_user"] && params.turn_user.empty()) {
151 params.turn_user = config["turn_user"].as<std::string>();
152 }
153 if (config["turn_pass"] && params.turn_pass.empty()) {
154 params.turn_pass = config["turn_pass"].as<std::string>();
155 }
156 if (config["turn_realm"] && params.turn_realm.empty()) {
157 params.turn_realm = config["turn_realm"].as<std::string>();
158 }
159 if (config["CA"] && params.ca.empty()) {
160 params.ca = config["CA"].as<std::string>();
161 }
162 if (config["binary"] && params.binary.empty()) {
163 params.binary = config["binary"].as<std::string>();
164 }
Amna4325f0f2024-01-22 16:11:00 -0500165 if (config["anonymous"] && !params.anonymous_cnx) {
166 params.anonymous_cnx = config["anonymous"].as<bool>();
167 }
168
Amna41848a22024-01-22 16:22:57 -0500169 }
170 }
Amna4a70f5c2023-09-14 17:32:05 -0400171 return params;
172}
173
174static void
175setSipLogLevel()
176{
177 char* envvar = getenv("SIPLOGLEVEL");
178
179 int level = 0;
180
181 if (envvar != nullptr) {
182 level = std::stoi(envvar);
183
184 // From 0 (min) to 6 (max)
185 level = std::max(0, std::min(level, 6));
186 }
187
188 pj_log_set_level(level);
189 pj_log_set_log_func([](int level, const char* data, int /*len*/) {});
190}
191
192int
193main(int argc, char** argv)
194{
Amna4a70f5c2023-09-14 17:32:05 -0400195 setSipLogLevel();
196 auto params = parse_args(argc, argv);
Amna77bb5852023-09-28 10:31:11 -0400197
198 if (params.help){
199 fmt::print("Usage: dsh [OPTIONS] [PEER_ID]\n"
Amna2b5b07f2024-01-22 17:04:36 -0500200 "\nOptions:\n"
201 " -h, --help Show this help message and exit.\n"
202 " -v, --version Display the program version.\n"
203 " -l, --listen Start the program in listen mode.\n"
204 " -b, --bootstrap Specify the bootstrap option with an argument.\n"
205 " -s, --binary Specify the binary option with an argument.\n"
206 " -I, --id_path Specify the id_path option with an argument.\n"
207 " -C, --CA Specify the CA option with an argument.\n"
208 " -t, --turn_host Specify the turn_host option with an argument.\n"
209 " -u, --turn_user Specify the turn_user option with an argument.\n"
210 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
211 " -r, --turn_realm Specify the turn_realm option with an argument.\n");
Amna77bb5852023-09-28 10:31:11 -0400212 return EXIT_SUCCESS;
213 }
214 if (params.version){
215 fmt::print("dsh v1.0\n");
216 return EXIT_SUCCESS;
217 }
218
219 fmt::print("dsh 1.0\n");
220
Amna41848a22024-01-22 16:22:57 -0500221 auto identity = dhtnet::loadIdentity(params.path, params.ca);
Amna4a70f5c2023-09-14 17:32:05 -0400222 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
223
224 std::unique_ptr<dhtnet::Dsh> dhtsh;
225 if (params.listen) {
Amna4325f0f2024-01-22 16:11:00 -0500226 // create dsh instance
Amna2b5b07f2024-01-22 17:04:36 -0500227 dhtsh = std::make_unique<dhtnet::Dsh>(params.path,
228 identity,
229 params.bootstrap,
230 params.turn_host,
231 params.turn_user,
232 params.turn_pass,
Amna4325f0f2024-01-22 16:11:00 -0500233 params.turn_realm,
234 params.anonymous_cnx);
Amna4a70f5c2023-09-14 17:32:05 -0400235 } else {
236 dhtsh = std::make_unique<dhtnet::Dsh>(params.path,
237 identity,
238 params.bootstrap,
239 params.peer_id,
Amna2b5b07f2024-01-22 17:04:36 -0500240 params.binary,
241 params.turn_host,
242 params.turn_user,
243 params.turn_pass,
244 params.turn_realm);
Amna4a70f5c2023-09-14 17:32:05 -0400245 }
246
247 dhtsh->run();
248 return EXIT_SUCCESS;
Amna4a70f5c2023-09-14 17:32:05 -0400249}