Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 1 | /* |
| 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Ă©raud | 277edaf | 2023-09-20 10:57:43 -0400 | [diff] [blame] | 26 | #if __has_include(<fmt/std.h>) |
| 27 | #include <fmt/std.h> |
| 28 | #else |
| 29 | #include <fmt/ostream.h> |
| 30 | #endif |
| 31 | |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 32 | |
| 33 | struct 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 | |
| 44 | static 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 | |
| 52 | dhtsh_params |
| 53 | parse_args(int argc, char** argv) |
| 54 | { |
| 55 | dhtsh_params params; |
| 56 | int opt; |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 57 | while ((opt = getopt_long(argc, argv, "hvls:I:p:i:", long_options, nullptr)) != -1) { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 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 |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 84 | if (!params.listen && !params.help && !params.version) { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 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 | |
| 104 | static void |
| 105 | setSipLogLevel() |
| 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 | |
| 122 | int |
| 123 | main(int argc, char** argv) |
| 124 | { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 125 | setSipLogLevel(); |
| 126 | auto params = parse_args(argc, argv); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 127 | |
| 128 | if (params.help){ |
| 129 | fmt::print("Usage: dsh [OPTIONS] [PEER_ID]\n" |
| 130 | "\nOptions:\n" |
| 131 | " -h, --help Show this help message and exit.\n" |
| 132 | " -v, --version Display the program version.\n" |
| 133 | " -l, --listen Start the program in listen mode.\n" |
| 134 | " -b, --bootstrap Specify the bootstrap option with an argument.\n" |
| 135 | " -s, --binary Specify the binary option with an argument.\n" |
| 136 | " -I, --id_path Specify the id_path option with an argument.\n"); |
| 137 | return EXIT_SUCCESS; |
| 138 | } |
| 139 | if (params.version){ |
| 140 | fmt::print("dsh v1.0\n"); |
| 141 | return EXIT_SUCCESS; |
| 142 | } |
| 143 | |
| 144 | fmt::print("dsh 1.0\n"); |
| 145 | |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 146 | auto identity = dhtnet::loadIdentity(params.path); |
| 147 | fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path); |
| 148 | |
| 149 | std::unique_ptr<dhtnet::Dsh> dhtsh; |
| 150 | if (params.listen) { |
| 151 | // create dnc instance |
| 152 | dhtsh = std::make_unique<dhtnet::Dsh>(params.path, identity, params.bootstrap); |
| 153 | } else { |
| 154 | dhtsh = std::make_unique<dhtnet::Dsh>(params.path, |
| 155 | identity, |
| 156 | params.bootstrap, |
| 157 | params.peer_id, |
| 158 | params.binary); |
| 159 | } |
| 160 | |
| 161 | dhtsh->run(); |
| 162 | return EXIT_SUCCESS; |
| 163 | |
| 164 | } |