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