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