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