blob: 5d550b0a757f2ed95715b2149311f7d645aac269 [file] [log] [blame]
Amna38768302023-08-21 11:51:56 -04001/*
2 * Copyright (C) 2004-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 "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>
25
26#include <netinet/in.h>
27
28struct dhtnc_params
29{
30 bool help {false};
31 bool version {false};
32 bool listen {false};
33 std::string ip_add {};
34 std::string bootstrap_ip {};
35 std::string bootstrap_port {};
36 in_port_t port {};
37 dht::InfoHash peer_id {};
38};
39
40static const constexpr struct option long_options[]
41 = {{"help", no_argument, nullptr, 'h'},
42 {"version", no_argument, nullptr, 'v'},
43 {"port", required_argument, nullptr, 'p'},
44 {"ip", required_argument, nullptr, 'i'},
45 {"listen", no_argument, nullptr, 'l'},
46 {"bootstrap_ip", required_argument, nullptr, 'b'},
47 {"bootstrap_port", required_argument, nullptr, 'P'},
48 {nullptr, 0, nullptr, 0}};
49
50dhtnc_params
51parse_args(int argc, char** argv)
52{
53 dhtnc_params params;
54 int opt;
55 while ((opt = getopt_long(argc, argv, "hvp:i:", long_options, nullptr)) != -1) {
56 switch (opt) {
57 case 'h':
58 params.help = true;
59 break;
60 case 'v':
61 params.version = true;
62 break;
63 case 'p':
64 params.port = std::stoi(optarg);
65 break;
66 case 'i':
67 params.ip_add = optarg;
68 break;
69 case 'l':
70 params.listen = true;
71 break;
72 case 'b':
73 params.bootstrap_ip = optarg;
74 break;
75 case 'P':
76 params.bootstrap_port = optarg;
77 break;
78 default:
79 std::cerr << "Invalid option" << std::endl;
80 exit(EXIT_FAILURE);
81 }
82 }
83
84 // If not listening, the peer_id argument is required
85 if (!params.listen) {
86 if (optind < argc) {
87 params.peer_id = dht::InfoHash(argv[optind]);
88 optind++; // Move to the next argument
89 } else {
90 std::cerr << "Error: Missing peer_id argument.\n";
91 exit(EXIT_FAILURE);
92 }
93 }
94
95 // default values
96 if (params.port == 0)
97 params.port = 22;
98 if (params.ip_add.empty())
99 params.ip_add = "127.0.0.1";
100 if (params.bootstrap_ip.empty())
101 params.bootstrap_ip = "bootstrap.jami.net";
102 if (params.bootstrap_port.empty())
103 params.bootstrap_port = "4222";
104 return params;
105}
106
107static void
108setSipLogLevel()
109{
110 char* envvar = getenv("SIPLOGLEVEL");
111
112 int level = 0;
113
114 if (envvar != nullptr) {
115 level = std::stoi(envvar);
116
117 // From 0 (min) to 6 (max)
118 level = std::max(0, std::min(level, 6));
119 }
120
121 pj_log_set_level(level);
122 pj_log_set_log_func([](int level, const char* data, int /*len*/) {
123 });
124}
125
126int
127main(int argc, char** argv)
128{
129 setSipLogLevel();
130 auto params = parse_args(argc, argv);
131
132 std::unique_ptr<dhtnet::Dnc> dhtnc;
133 if (params.listen) {
134 auto identity = dhtnet::loadIdentity(true);
135 // create dnc instance
136 dhtnc = std::make_unique<dhtnet::Dnc>(identity, params.bootstrap_ip, params.bootstrap_port);
137 fmt::print("DhtNC 1.1\n");
138 fmt::print("Loaded identity: {}\n", identity.second->getId());
139 } else {
140 auto identity = dhtnet::loadIdentity(false);
141 dhtnc = std::make_unique<dhtnet::Dnc>(identity,
142 params.bootstrap_ip,
143 params.bootstrap_port,
144 params.peer_id,
145 params.port,
146 params.ip_add);
147 fmt::print("DhtNC 1.0\n");
148 fmt::print("Loaded identity: {}\n", identity.second->getId());
149 }
150 dhtnc->run();
151}