blob: f89661e8379836262581ffc3c1c1ad08fd405a05 [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>
31
32struct dhtnc_params
33{
34 bool help {false};
35 bool version {false};
36 bool listen {false};
Adrien Béraudecde63f2023-08-26 18:11:21 -040037 std::filesystem::path path {};
38 std::string bootstrap {};
39 std::string remote_host {};
40 in_port_t remote_port {};
Amna38768302023-08-21 11:51:56 -040041 dht::InfoHash peer_id {};
Amna2f3539b2023-09-18 13:59:22 -040042 std::string turn_host {};
43 std::string turn_user {};
44 std::string turn_pass {};
45 std::string turn_realm {};
Amna38768302023-08-21 11:51:56 -040046};
47
Amna2f3539b2023-09-18 13:59:22 -040048static const constexpr struct option long_options[] = {{"help", no_argument, nullptr, 'h'},
Amna65a6ea82023-09-26 12:29:47 -040049 {"version", no_argument, nullptr, 'v'},
Amna2f3539b2023-09-18 13:59:22 -040050 {"port", required_argument, nullptr, 'p'},
51 {"ip", required_argument, nullptr, 'i'},
52 {"listen", no_argument, nullptr, 'l'},
53 {"bootstrap", required_argument, nullptr, 'b'},
54 {"id_path", required_argument, nullptr, 'I'},
55 {"turn_host", required_argument, nullptr, 't'},
56 {"turn_user", required_argument, nullptr, 'u'},
57 {"turn_pass", required_argument, nullptr, 'w'},
58 {"turn_realm", required_argument, nullptr, 'r'},
59 {nullptr, 0, nullptr, 0}};
Amna38768302023-08-21 11:51:56 -040060
61dhtnc_params
62parse_args(int argc, char** argv)
63{
64 dhtnc_params params;
65 int opt;
Amna65a6ea82023-09-26 12:29:47 -040066 while ((opt = getopt_long(argc, argv, "hvlw:r:u:t:I:b:p:i:", long_options, nullptr)) != -1) {
Amna38768302023-08-21 11:51:56 -040067 switch (opt) {
68 case 'h':
69 params.help = true;
70 break;
Amna65a6ea82023-09-26 12:29:47 -040071 case 'v':
Amna38768302023-08-21 11:51:56 -040072 params.version = true;
73 break;
74 case 'p':
Adrien Béraudecde63f2023-08-26 18:11:21 -040075 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040076 break;
77 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040078 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040079 break;
80 case 'l':
81 params.listen = true;
82 break;
83 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -040084 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -040085 break;
Adrien Béraudecde63f2023-08-26 18:11:21 -040086 case 'I':
87 params.path = optarg;
Amna38768302023-08-21 11:51:56 -040088 break;
Amna2f3539b2023-09-18 13:59:22 -040089 case 't':
90 params.turn_host = optarg;
91 break;
92 case 'u':
93 params.turn_user = optarg;
94 break;
95 case 'w':
96 params.turn_pass = optarg;
97 break;
98 case 'r':
99 params.turn_realm = optarg;
100 break;
Amna38768302023-08-21 11:51:56 -0400101 default:
102 std::cerr << "Invalid option" << std::endl;
103 exit(EXIT_FAILURE);
104 }
105 }
106
107 // If not listening, the peer_id argument is required
Amna65a6ea82023-09-26 12:29:47 -0400108 if (!params.listen && !params.help && !params.version) {
Amna38768302023-08-21 11:51:56 -0400109 if (optind < argc) {
110 params.peer_id = dht::InfoHash(argv[optind]);
111 optind++; // Move to the next argument
112 } else {
113 std::cerr << "Error: Missing peer_id argument.\n";
114 exit(EXIT_FAILURE);
115 }
116 }
117
118 // default values
Adrien Béraudecde63f2023-08-26 18:11:21 -0400119 if (params.remote_port == 0)
Amnab31c3742023-08-28 13:58:31 -0400120 params.remote_port = 22;
Adrien Béraudecde63f2023-08-26 18:11:21 -0400121 if (params.remote_host.empty())
122 params.remote_host = "127.0.0.1";
123 if (params.bootstrap.empty())
124 params.bootstrap = "bootstrap.jami.net";
125 if (params.path.empty())
126 params.path = std::filesystem::path(getenv("HOME")) / ".dhtnet";
Amna2f3539b2023-09-18 13:59:22 -0400127 if (params.turn_host.empty())
128 params.turn_host = "turn.jami.net";
129 if (params.turn_user.empty())
130 params.turn_user = "ring";
131 if (params.turn_pass.empty())
132 params.turn_pass = "ring";
133 if (params.turn_realm.empty())
134 params.turn_realm = "ring";
Amna38768302023-08-21 11:51:56 -0400135 return params;
136}
137
138static void
139setSipLogLevel()
140{
Amna38768302023-08-21 11:51:56 -0400141 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400142 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400143 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400144 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400145 }
146
147 pj_log_set_level(level);
Amnabe363922023-11-20 15:24:54 -0500148 pj_log_set_log_func([](int level, const char* data, int len) {
149 fmt::print("{}", std::string_view(data, len));
150 });
Amna38768302023-08-21 11:51:56 -0400151}
152
153int
154main(int argc, char** argv)
155{
156 setSipLogLevel();
157 auto params = parse_args(argc, argv);
Amna65a6ea82023-09-26 12:29:47 -0400158
159 if (params.help ){
160 fmt::print("Usage: dnc [options] [PEER_ID]\n"
161 "\nOptions:\n"
162 " -h, --help Show this help message and exit.\n"
163 " -v, --version Display the program version.\n"
164 " -p, --port Specify the port option with an argument.\n"
165 " -i, --ip Specify the ip option with an argument.\n"
166 " -l, --listen Start the program in listen mode.\n"
167 " -b, --bootstrap Specify the bootstrap option with an argument.\n"
168 " -I, --id_path Specify the id_path option with an argument.\n"
169 " -t, --turn_host Specify the turn_host option with an argument.\n"
170 " -u, --turn_user Specify the turn_user option with an argument.\n"
171 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
172 " -r, --turn_realm Specify the turn_realm option with an argument.\n");
173 return EXIT_SUCCESS;
174 }
175 if (params.version) {
176 fmt::print("dnc v1.0\n");
177 return EXIT_SUCCESS;
178 }
179
180 fmt::print("dnc 1.0\n");
Adrien Béraudecde63f2023-08-26 18:11:21 -0400181 auto identity = dhtnet::loadIdentity(params.path);
182 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
Amna38768302023-08-21 11:51:56 -0400183
184 std::unique_ptr<dhtnet::Dnc> dhtnc;
185 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400186 // create dnc instance
Amna2f3539b2023-09-18 13:59:22 -0400187 dhtnc = std::make_unique<dhtnet::Dnc>(params.path, identity, params.bootstrap, params.turn_host, params.turn_user, params.turn_pass, params.turn_realm);
Amna38768302023-08-21 11:51:56 -0400188 } else {
Adrien Béraudecde63f2023-08-26 18:11:21 -0400189 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
190 identity,
191 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400192 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400193 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400194 params.remote_port,
195 params.turn_host,
196 params.turn_user,
197 params.turn_pass,
198 params.turn_realm
199 );
Amna38768302023-08-21 11:51:56 -0400200 }
201 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400202 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400203}