Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 1 | |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 2 | #include <errno.h> |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 3 | #include <signal.h> |
| 4 | #include <unistd.h> |
| 5 | #define _GNU_SOURCE |
| 6 | #include <getopt.h> |
| 7 | |
| 8 | #include <osmocom/core/msgb.h> |
| 9 | #include <osmocom/core/application.h> |
| 10 | |
| 11 | #include "client.h" |
| 12 | |
| 13 | static void *g_tall_ctx; |
| 14 | void __thread *talloc_asn1_ctx; |
| 15 | int asn_debug; |
| 16 | |
| 17 | static void handle_sig_usr1(int signal) |
| 18 | { |
| 19 | OSMO_ASSERT(signal == SIGUSR1); |
| 20 | talloc_report_full(g_tall_ctx, stderr); |
| 21 | } |
| 22 | |
| 23 | static void printf_help() |
| 24 | { |
| 25 | printf( |
| 26 | " -h --help Print this help message\n" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 27 | " -v --version Print program version\n" |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 28 | " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n" |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 29 | " -i --server-ip A.B.C.D remsim-server IP address\n" |
| 30 | " -p --server-port 13245 remsim-server TCP port\n" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 31 | " -c --client-id <0-65535> RSPRO ClientId of this client\n" |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 32 | " -n --client-slot <0-65535> RSPRO SlotNr of this client\n" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 33 | " -e --event-script <path> event script to be called by client\n" |
| 34 | #ifdef USB_SUPPORT |
| 35 | " -V --usb-vendor VENDOR_ID\n" |
| 36 | " -P --usb-product PRODUCT_ID\n" |
| 37 | " -C --usb-config CONFIG_ID\n" |
| 38 | " -I --usb-interface INTERFACE_ID\n" |
| 39 | " -S --usb-altsetting ALTSETTING_ID\n" |
| 40 | " -A --usb-address ADDRESS\n" |
| 41 | " -H --usb-path PATH\n" |
| 42 | #endif |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 43 | ); |
| 44 | } |
| 45 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 46 | static void handle_options(struct client_config *cfg, int argc, char **argv) |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 47 | { |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 48 | int rc; |
| 49 | |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 50 | while (1) { |
| 51 | int option_index = 0, c; |
| 52 | static const struct option long_options[] = { |
| 53 | { "help", 0, 0, 'h' }, |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 54 | { "version", 0, 0, 'v' }, |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 55 | { "debug", 1, 0, 'd' }, |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 56 | { "server-ip", 1, 0, 'i' }, |
| 57 | { "server-port", 1, 0, 'p' }, |
| 58 | { "client-id", 1, 0, 'c' }, |
| 59 | { "client-slot", 1, 0, 'n' }, |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 60 | { "atr", 1, 0, 'a' }, |
| 61 | { "event-script", 1, 0, 'e' }, |
| 62 | #ifdef USB_SUPPORT |
| 63 | { "usb-vendor", 1, 0, 'V' }, |
| 64 | { "usb-product", 1, 0, 'P' }, |
| 65 | { "usb-config", 1, 0, 'C' }, |
| 66 | { "usb-interface", 1, 0, 'I' }, |
| 67 | { "usb-altsetting", 1, 0, 'S' }, |
| 68 | { "usb-address", 1, 0, 'A' }, |
| 69 | { "usb-path", 1, 0, 'H' }, |
| 70 | #endif |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 71 | { 0, 0, 0, 0 } |
| 72 | }; |
| 73 | |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 74 | c = getopt_long(argc, argv, "hvd:i:p:c:n:e:" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 75 | #ifdef USB_SUPPORT |
| 76 | "V:P:C:I:S:A:H:" |
| 77 | #endif |
| 78 | , |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 79 | long_options, &option_index); |
| 80 | if (c == -1) |
| 81 | break; |
| 82 | |
| 83 | switch (c) { |
| 84 | case 'h': |
| 85 | printf_help(); |
| 86 | exit(0); |
| 87 | break; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 88 | case 'v': |
| 89 | printf("osmo-remsim-client version %s\n", VERSION); |
| 90 | exit(0); |
| 91 | break; |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 92 | case 'd': |
| 93 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 94 | break; |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 95 | case 'i': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 96 | osmo_talloc_replace_string(cfg, &cfg->server_host, optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 97 | break; |
| 98 | case 'p': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 99 | cfg->server_port = atoi(optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 100 | break; |
| 101 | case 'c': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 102 | cfg->client_id = atoi(optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 103 | break; |
| 104 | case 'n': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 105 | cfg->client_slot = atoi(optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 106 | break; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 107 | case 'a': |
| 108 | rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data)); |
| 109 | if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) { |
| 110 | fprintf(stderr, "ATR malformed\n"); |
| 111 | exit(2); |
| 112 | } |
| 113 | break; |
| 114 | case 'e': |
| 115 | osmo_talloc_replace_string(cfg, &cfg->event_script, optarg); |
| 116 | break; |
| 117 | #ifdef USB_SUPPORT |
| 118 | case 'V': |
| 119 | cfg->usb.vendor_id = strtol(optarg, NULL, 16); |
| 120 | break; |
| 121 | case 'P': |
| 122 | cfg->usb.product_id = strtol(optarg, NULL, 16); |
| 123 | break; |
| 124 | case 'C': |
| 125 | cfg->usb.config_id = atoi(optarg); |
| 126 | break; |
| 127 | case 'I': |
| 128 | cfg->usb.if_num = atoi(optarg); |
| 129 | break; |
| 130 | case 'S': |
| 131 | cfg->usb.altsetting = atoi(optarg); |
| 132 | break; |
| 133 | case 'A': |
| 134 | cfg->usb.addr = atoi(optarg); |
| 135 | break; |
| 136 | case 'H': |
| 137 | cfg->usb.path = optarg; |
| 138 | break; |
| 139 | #endif |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 140 | default: |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 146 | |
| 147 | static int avoid_zombies(void) |
| 148 | { |
| 149 | static struct sigaction sa_chld; |
| 150 | |
| 151 | sa_chld.sa_handler = SIG_IGN; |
| 152 | sigemptyset(&sa_chld.sa_mask); |
| 153 | sa_chld.sa_flags = SA_NOCLDWAIT; |
| 154 | sa_chld.sa_restorer = NULL; |
| 155 | |
| 156 | return sigaction(SIGCHLD, &sa_chld, NULL); |
| 157 | } |
| 158 | |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 159 | int main(int argc, char **argv) |
| 160 | { |
| 161 | struct bankd_client *g_client; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 162 | struct client_config *cfg; |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 163 | char hostname[256]; |
| 164 | |
| 165 | gethostname(hostname, sizeof(hostname)); |
| 166 | |
| 167 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 168 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
| 169 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
| 170 | |
| 171 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 172 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 173 | cfg = client_config_init(g_tall_ctx); |
| 174 | OSMO_ASSERT(cfg); |
| 175 | handle_options(cfg, argc, argv); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 176 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 177 | g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 178 | |
| 179 | osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL); |
| 180 | |
| 181 | signal(SIGUSR1, handle_sig_usr1); |
| 182 | |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 183 | /* Silently (and portably) reap children. */ |
| 184 | if (avoid_zombies() < 0) { |
| 185 | fprintf(stderr, "Unable to silently reap children: %s\n", strerror(errno)); |
| 186 | exit(1); |
| 187 | } |
| 188 | |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 189 | asn_debug = 0; |
| 190 | |
| 191 | client_user_main(g_client); |
| 192 | } |