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