blob: b6f4cbea16c028465722195154f127ae76f2fd7b [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'},
49 {"version", no_argument, nullptr, 'V'},
50 {"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;
Amna2f3539b2023-09-18 13:59:22 -040066 while ((opt = getopt_long(argc, argv, "hVlw:r:u:t:I:b:p:i:", long_options, nullptr)) != -1) {
Amnab31c3742023-08-28 13:58:31 -040067 // fmt::print("opt: {} {}\n", opt, optarg);
Amna38768302023-08-21 11:51:56 -040068 switch (opt) {
69 case 'h':
70 params.help = true;
71 break;
Adrien Béraudecde63f2023-08-26 18:11:21 -040072 case 'V':
Amna38768302023-08-21 11:51:56 -040073 params.version = true;
74 break;
75 case 'p':
Adrien Béraudecde63f2023-08-26 18:11:21 -040076 params.remote_port = std::stoi(optarg);
Amna38768302023-08-21 11:51:56 -040077 break;
78 case 'i':
Adrien Béraudecde63f2023-08-26 18:11:21 -040079 params.remote_host = optarg;
Amna38768302023-08-21 11:51:56 -040080 break;
81 case 'l':
82 params.listen = true;
83 break;
84 case 'b':
Adrien Béraudecde63f2023-08-26 18:11:21 -040085 params.bootstrap = optarg;
Amna38768302023-08-21 11:51:56 -040086 break;
Adrien Béraudecde63f2023-08-26 18:11:21 -040087 case 'I':
88 params.path = optarg;
Amna38768302023-08-21 11:51:56 -040089 break;
Amna2f3539b2023-09-18 13:59:22 -040090 case 't':
91 params.turn_host = optarg;
92 break;
93 case 'u':
94 params.turn_user = optarg;
95 break;
96 case 'w':
97 params.turn_pass = optarg;
98 break;
99 case 'r':
100 params.turn_realm = optarg;
101 break;
Amna38768302023-08-21 11:51:56 -0400102 default:
103 std::cerr << "Invalid option" << std::endl;
104 exit(EXIT_FAILURE);
105 }
106 }
107
108 // If not listening, the peer_id argument is required
109 if (!params.listen) {
110 if (optind < argc) {
111 params.peer_id = dht::InfoHash(argv[optind]);
112 optind++; // Move to the next argument
113 } else {
114 std::cerr << "Error: Missing peer_id argument.\n";
115 exit(EXIT_FAILURE);
116 }
117 }
118
119 // default values
Adrien Béraudecde63f2023-08-26 18:11:21 -0400120 if (params.remote_port == 0)
Amnab31c3742023-08-28 13:58:31 -0400121 params.remote_port = 22;
Adrien Béraudecde63f2023-08-26 18:11:21 -0400122 if (params.remote_host.empty())
123 params.remote_host = "127.0.0.1";
124 if (params.bootstrap.empty())
125 params.bootstrap = "bootstrap.jami.net";
126 if (params.path.empty())
127 params.path = std::filesystem::path(getenv("HOME")) / ".dhtnet";
Amna2f3539b2023-09-18 13:59:22 -0400128 if (params.turn_host.empty())
129 params.turn_host = "turn.jami.net";
130 if (params.turn_user.empty())
131 params.turn_user = "ring";
132 if (params.turn_pass.empty())
133 params.turn_pass = "ring";
134 if (params.turn_realm.empty())
135 params.turn_realm = "ring";
Amna38768302023-08-21 11:51:56 -0400136 return params;
137}
138
139static void
140setSipLogLevel()
141{
Amna38768302023-08-21 11:51:56 -0400142 int level = 0;
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400143 if (char* envvar = getenv("SIPLOGLEVEL")) {
Amna38768302023-08-21 11:51:56 -0400144 // From 0 (min) to 6 (max)
Adrien Béraud6c6a9622023-08-27 12:49:31 -0400145 level = std::clamp(std::stoi(envvar), 0, 6);
Amna38768302023-08-21 11:51:56 -0400146 }
147
148 pj_log_set_level(level);
Amna2f3539b2023-09-18 13:59:22 -0400149 pj_log_set_log_func([](int level, const char* data, int /*len*/) {});
Amna38768302023-08-21 11:51:56 -0400150}
151
152int
153main(int argc, char** argv)
154{
Adrien Béraudecde63f2023-08-26 18:11:21 -0400155 fmt::print("dnc 1.0\n");
Amna38768302023-08-21 11:51:56 -0400156 setSipLogLevel();
157 auto params = parse_args(argc, argv);
Adrien Béraudecde63f2023-08-26 18:11:21 -0400158 auto identity = dhtnet::loadIdentity(params.path);
159 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
Amna38768302023-08-21 11:51:56 -0400160
161 std::unique_ptr<dhtnet::Dnc> dhtnc;
162 if (params.listen) {
Amna38768302023-08-21 11:51:56 -0400163 // create dnc instance
Amna2f3539b2023-09-18 13:59:22 -0400164 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 -0400165 } else {
Adrien Béraudecde63f2023-08-26 18:11:21 -0400166 dhtnc = std::make_unique<dhtnet::Dnc>(params.path,
167 identity,
168 params.bootstrap,
Amna38768302023-08-21 11:51:56 -0400169 params.peer_id,
Adrien Béraudecde63f2023-08-26 18:11:21 -0400170 params.remote_host,
Amna2f3539b2023-09-18 13:59:22 -0400171 params.remote_port,
172 params.turn_host,
173 params.turn_user,
174 params.turn_pass,
175 params.turn_realm
176 );
Amna38768302023-08-21 11:51:56 -0400177 }
178 dhtnc->run();
Amna2f3539b2023-09-18 13:59:22 -0400179 return EXIT_SUCCESS;
Amna38768302023-08-21 11:51:56 -0400180}