blob: 78d9593c28b4c41a30215c4fee96e08e3e02e0f5 [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
31
Amna4a70f5c2023-09-14 17:32:05 -040032
33struct dhtsh_params
34{
35 bool help {false};
36 bool version {false};
37 bool listen {false};
38 std::filesystem::path path {};
39 std::string bootstrap {};
40 dht::InfoHash peer_id {};
41 std::string binary {};
42};
43
44static const constexpr struct option long_options[] = {{"help", no_argument, nullptr, 'h'},
45 {"version", no_argument, nullptr, 'v'},
46 {"listen", no_argument, nullptr, 'l'},
47 {"bootstrap", required_argument, nullptr, 'b'},
48 {"binary", required_argument, nullptr, 's'},
49 {"id_path", required_argument, nullptr, 'I'},
50 {nullptr, 0, nullptr, 0}};
51
52dhtsh_params
53parse_args(int argc, char** argv)
54{
55 dhtsh_params params;
56 int opt;
57 while ((opt = getopt_long(argc, argv, "hvlsI:p:i:", long_options, nullptr)) != -1) {
58 switch (opt) {
59 case 'h':
60 params.help = true;
61 break;
62 case 'v':
63 params.version = true;
64 break;
65 case 'l':
66 params.listen = true;
67 break;
68 case 'b':
69 params.bootstrap = optarg;
70 break;
71 case 's':
72 params.binary = optarg;
73 break;
74 case 'I':
75 params.path = 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.bootstrap.empty())
96 params.bootstrap = "bootstrap.jami.net";
97 if (params.binary.empty())
98 params.binary = "bash";
99 if (params.path.empty())
100 params.path = std::filesystem::path(getenv("HOME")) / ".dhtnet";
101 return params;
102}
103
104static void
105setSipLogLevel()
106{
107 char* envvar = getenv("SIPLOGLEVEL");
108
109 int level = 0;
110
111 if (envvar != nullptr) {
112 level = std::stoi(envvar);
113
114 // From 0 (min) to 6 (max)
115 level = std::max(0, std::min(level, 6));
116 }
117
118 pj_log_set_level(level);
119 pj_log_set_log_func([](int level, const char* data, int /*len*/) {});
120}
121
122int
123main(int argc, char** argv)
124{
125 fmt::print("DSH 1.0\n");
126 setSipLogLevel();
127 auto params = parse_args(argc, argv);
128 auto identity = dhtnet::loadIdentity(params.path);
129 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
130
131 std::unique_ptr<dhtnet::Dsh> dhtsh;
132 if (params.listen) {
133 // create dnc instance
134 dhtsh = std::make_unique<dhtnet::Dsh>(params.path, identity, params.bootstrap);
135 } else {
136 dhtsh = std::make_unique<dhtnet::Dsh>(params.path,
137 identity,
138 params.bootstrap,
139 params.peer_id,
140 params.binary);
141 }
142
143 dhtsh->run();
144 return EXIT_SUCCESS;
145
146}