blob: 7c5e006a6dbf8cdd2dc01caab4006a65d93f93c9 [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"
19#include <string>
20#include <vector>
21#include <iostream>
22#include <unistd.h>
23#include <getopt.h>
24
25#include <netinet/in.h>
Adrien BĂ©raud277edaf2023-09-20 10:57:43 -040026#if __has_include(<fmt/std.h>)
27#include <fmt/std.h>
28#else
29#include <fmt/ostream.h>
30#endif
Amna2b5b07f2024-01-22 17:04:36 -050031#include <yaml-cpp/yaml.h>
32#include <fstream>
Amna4a70f5c2023-09-14 17:32:05 -040033
34struct dhtsh_params
35{
36 bool help {false};
37 bool version {false};
38 bool listen {false};
39 std::filesystem::path path {};
40 std::string bootstrap {};
41 dht::InfoHash peer_id {};
42 std::string binary {};
Amna2b5b07f2024-01-22 17:04:36 -050043 std::string ca {};
44 std::string turn_host {};
45 std::string turn_user {};
46 std::string turn_pass {};
47 std::string turn_realm {};
48 std::string dsh_configuration {};
Amna4a70f5c2023-09-14 17:32:05 -040049};
50
Amna2b5b07f2024-01-22 17:04:36 -050051static const constexpr struct option long_options[]
52 = {{"help", no_argument, nullptr, 'h'},
53 {"version", no_argument, nullptr, 'v'},
54 {"listen", no_argument, nullptr, 'l'},
55 {"bootstrap", required_argument, nullptr, 'b'},
56 {"binary", required_argument, nullptr, 's'},
57 {"id_path", required_argument, nullptr, 'I'},
58 {"CA", required_argument, nullptr, 'C'},
59 {"turn_host", required_argument, nullptr, 't'},
60 {"turn_user", required_argument, nullptr, 'u'},
61 {"turn_pass", required_argument, nullptr, 'w'},
62 {"turn_realm", required_argument, nullptr, 'r'},
63 {"dsh_configuration", required_argument, nullptr, 'd'},
64 {nullptr, 0, nullptr, 0}};
Amna4a70f5c2023-09-14 17:32:05 -040065
66dhtsh_params
67parse_args(int argc, char** argv)
68{
69 dhtsh_params params;
70 int opt;
Amna2b5b07f2024-01-22 17:04:36 -050071 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 -040072 switch (opt) {
73 case 'h':
74 params.help = true;
75 break;
76 case 'v':
77 params.version = true;
78 break;
79 case 'l':
80 params.listen = true;
81 break;
82 case 'b':
83 params.bootstrap = optarg;
84 break;
85 case 's':
86 params.binary = optarg;
87 break;
88 case 'I':
89 params.path = optarg;
90 break;
Amna2b5b07f2024-01-22 17:04:36 -050091 case 't':
92 params.turn_host = optarg;
93 break;
94 case 'u':
95 params.turn_user = optarg;
96 break;
97 case 'w':
98 params.turn_pass = optarg;
99 break;
100 case 'r':
101 params.turn_realm = optarg;
102 break;
103 case 'C':
104 params.ca = optarg;
105 break;
106 case 'd':
107 params.dsh_configuration = optarg;
Amna4a70f5c2023-09-14 17:32:05 -0400108 default:
109 std::cerr << "Invalid option" << std::endl;
110 exit(EXIT_FAILURE);
111 }
112 }
113
114 // If not listening, the peer_id argument is required
Amna77bb5852023-09-28 10:31:11 -0400115 if (!params.listen && !params.help && !params.version) {
Amna4a70f5c2023-09-14 17:32:05 -0400116 if (optind < argc) {
117 params.peer_id = dht::InfoHash(argv[optind]);
118 optind++; // Move to the next argument
119 } else {
120 std::cerr << "Error: Missing peer_id argument.\n";
121 exit(EXIT_FAILURE);
122 }
123 }
124
Amna41848a22024-01-22 16:22:57 -0500125 // extract values from dsh yaml file
126 if (!params.dsh_configuration.empty()) {
127 printf("read configuration file: %s\n", params.dsh_configuration.c_str());
128 std::ifstream config_file(params.dsh_configuration);
129 if (!config_file.is_open()) {
130 std::cerr << "Error: Could not open configuration file.\n";
131 } else {
132 YAML::Node config = YAML::Load(config_file);
133 if (config["bootstrap"] && params.bootstrap.empty()) {
134 params.bootstrap = config["bootstrap"].as<std::string>();
135 }
136 if (config["id_path"] && params.path.empty()) {
137 params.path = config["id_path"].as<std::string>();
138 }
139 if (config["turn_host"] && params.turn_host.empty()) {
140 params.turn_host = config["turn_host"].as<std::string>();
141 }
142 if (config["turn_user"] && params.turn_user.empty()) {
143 params.turn_user = config["turn_user"].as<std::string>();
144 }
145 if (config["turn_pass"] && params.turn_pass.empty()) {
146 params.turn_pass = config["turn_pass"].as<std::string>();
147 }
148 if (config["turn_realm"] && params.turn_realm.empty()) {
149 params.turn_realm = config["turn_realm"].as<std::string>();
150 }
151 if (config["CA"] && params.ca.empty()) {
152 params.ca = config["CA"].as<std::string>();
153 }
154 if (config["binary"] && params.binary.empty()) {
155 params.binary = config["binary"].as<std::string>();
156 }
157 }
158 }
Amna4a70f5c2023-09-14 17:32:05 -0400159 return params;
160}
161
162static void
163setSipLogLevel()
164{
165 char* envvar = getenv("SIPLOGLEVEL");
166
167 int level = 0;
168
169 if (envvar != nullptr) {
170 level = std::stoi(envvar);
171
172 // From 0 (min) to 6 (max)
173 level = std::max(0, std::min(level, 6));
174 }
175
176 pj_log_set_level(level);
177 pj_log_set_log_func([](int level, const char* data, int /*len*/) {});
178}
179
180int
181main(int argc, char** argv)
182{
Amna4a70f5c2023-09-14 17:32:05 -0400183 setSipLogLevel();
184 auto params = parse_args(argc, argv);
Amna77bb5852023-09-28 10:31:11 -0400185
186 if (params.help){
187 fmt::print("Usage: dsh [OPTIONS] [PEER_ID]\n"
Amna2b5b07f2024-01-22 17:04:36 -0500188 "\nOptions:\n"
189 " -h, --help Show this help message and exit.\n"
190 " -v, --version Display the program version.\n"
191 " -l, --listen Start the program in listen mode.\n"
192 " -b, --bootstrap Specify the bootstrap option with an argument.\n"
193 " -s, --binary Specify the binary option with an argument.\n"
194 " -I, --id_path Specify the id_path option with an argument.\n"
195 " -C, --CA Specify the CA option with an argument.\n"
196 " -t, --turn_host Specify the turn_host option with an argument.\n"
197 " -u, --turn_user Specify the turn_user option with an argument.\n"
198 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
199 " -r, --turn_realm Specify the turn_realm option with an argument.\n");
Amna77bb5852023-09-28 10:31:11 -0400200 return EXIT_SUCCESS;
201 }
202 if (params.version){
203 fmt::print("dsh v1.0\n");
204 return EXIT_SUCCESS;
205 }
206
207 fmt::print("dsh 1.0\n");
208
Amna41848a22024-01-22 16:22:57 -0500209 auto identity = dhtnet::loadIdentity(params.path, params.ca);
Amna4a70f5c2023-09-14 17:32:05 -0400210 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
211
212 std::unique_ptr<dhtnet::Dsh> dhtsh;
213 if (params.listen) {
214 // create dnc instance
Amna2b5b07f2024-01-22 17:04:36 -0500215 dhtsh = std::make_unique<dhtnet::Dsh>(params.path,
216 identity,
217 params.bootstrap,
218 params.turn_host,
219 params.turn_user,
220 params.turn_pass,
221 params.turn_realm);
Amna4a70f5c2023-09-14 17:32:05 -0400222 } else {
223 dhtsh = std::make_unique<dhtnet::Dsh>(params.path,
224 identity,
225 params.bootstrap,
226 params.peer_id,
Amna2b5b07f2024-01-22 17:04:36 -0500227 params.binary,
228 params.turn_host,
229 params.turn_user,
230 params.turn_pass,
231 params.turn_realm);
Amna4a70f5c2023-09-14 17:32:05 -0400232 }
233
234 dhtsh->run();
235 return EXIT_SUCCESS;
Amna4a70f5c2023-09-14 17:32:05 -0400236}