blob: 4238864de986ceb1dbfbbf138cd4789eb7687b8a [file] [log] [blame]
Harald Weltea9caad82020-02-14 14:32:02 +01001
Harald Weltedc347642020-10-20 19:14:55 +02002#include <errno.h>
Harald Weltea9caad82020-02-14 14:32:02 +01003#include <signal.h>
4#include <unistd.h>
5#define _GNU_SOURCE
6#include <getopt.h>
7
8#include <osmocom/core/msgb.h>
Harald Welte73bbd542021-12-08 20:39:55 +01009#include <osmocom/core/logging.h>
10#include <osmocom/core/fsm.h>
Harald Weltea9caad82020-02-14 14:32:02 +010011#include <osmocom/core/application.h>
12
13#include "client.h"
14
15static void *g_tall_ctx;
16void __thread *talloc_asn1_ctx;
17int asn_debug;
18
19static void handle_sig_usr1(int signal)
20{
21 OSMO_ASSERT(signal == SIGUSR1);
22 talloc_report_full(g_tall_ctx, stderr);
23}
24
25static void printf_help()
26{
27 printf(
28 " -h --help Print this help message\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010029 " -v --version Print program version\n"
Harald Welte80a01102020-05-25 14:55:12 +020030 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltea9caad82020-02-14 14:32:02 +010031 " -i --server-ip A.B.C.D remsim-server IP address\n"
32 " -p --server-port 13245 remsim-server TCP port\n"
Harald Welte55f12b82022-01-16 14:34:12 +010033 " -c --client-id <0-1023> RSPRO ClientId of this client\n"
34 " -n --client-slot <0-1023> RSPRO SlotNr of this client\n"
Harald Welte445d2842022-07-24 12:50:43 +020035 " -a --atr HEXSTRING default ATR to simulate (until bankd overrides it)\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010036 " -e --event-script <path> event script to be called by client\n"
37#ifdef USB_SUPPORT
38 " -V --usb-vendor VENDOR_ID\n"
39 " -P --usb-product PRODUCT_ID\n"
40 " -C --usb-config CONFIG_ID\n"
41 " -I --usb-interface INTERFACE_ID\n"
42 " -S --usb-altsetting ALTSETTING_ID\n"
43 " -A --usb-address ADDRESS\n"
44 " -H --usb-path PATH\n"
45#endif
Harald Weltea9caad82020-02-14 14:32:02 +010046 );
47}
48
Harald Welte0e968cc2020-02-22 18:16:16 +010049static void handle_options(struct client_config *cfg, int argc, char **argv)
Harald Weltea9caad82020-02-14 14:32:02 +010050{
Harald Welte0e968cc2020-02-22 18:16:16 +010051 int rc;
52
Harald Weltea9caad82020-02-14 14:32:02 +010053 while (1) {
54 int option_index = 0, c;
55 static const struct option long_options[] = {
56 { "help", 0, 0, 'h' },
Harald Welte0e968cc2020-02-22 18:16:16 +010057 { "version", 0, 0, 'v' },
Harald Welte80a01102020-05-25 14:55:12 +020058 { "debug", 1, 0, 'd' },
Harald Weltea9caad82020-02-14 14:32:02 +010059 { "server-ip", 1, 0, 'i' },
60 { "server-port", 1, 0, 'p' },
61 { "client-id", 1, 0, 'c' },
62 { "client-slot", 1, 0, 'n' },
Harald Welte0e968cc2020-02-22 18:16:16 +010063 { "atr", 1, 0, 'a' },
64 { "event-script", 1, 0, 'e' },
65#ifdef USB_SUPPORT
66 { "usb-vendor", 1, 0, 'V' },
67 { "usb-product", 1, 0, 'P' },
68 { "usb-config", 1, 0, 'C' },
69 { "usb-interface", 1, 0, 'I' },
70 { "usb-altsetting", 1, 0, 'S' },
71 { "usb-address", 1, 0, 'A' },
72 { "usb-path", 1, 0, 'H' },
73#endif
Harald Weltea9caad82020-02-14 14:32:02 +010074 { 0, 0, 0, 0 }
75 };
76
Harald Welte445d2842022-07-24 12:50:43 +020077 c = getopt_long(argc, argv, "hvd:i:p:c:n:a:e:"
Harald Welte0e968cc2020-02-22 18:16:16 +010078#ifdef USB_SUPPORT
79 "V:P:C:I:S:A:H:"
80#endif
81 ,
Harald Weltea9caad82020-02-14 14:32:02 +010082 long_options, &option_index);
83 if (c == -1)
84 break;
85
86 switch (c) {
87 case 'h':
88 printf_help();
89 exit(0);
90 break;
Harald Welte0e968cc2020-02-22 18:16:16 +010091 case 'v':
92 printf("osmo-remsim-client version %s\n", VERSION);
93 exit(0);
94 break;
Harald Welte80a01102020-05-25 14:55:12 +020095 case 'd':
96 log_parse_category_mask(osmo_stderr_target, optarg);
97 break;
Harald Weltea9caad82020-02-14 14:32:02 +010098 case 'i':
Harald Welte0e968cc2020-02-22 18:16:16 +010099 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100100 break;
101 case 'p':
Harald Welte0e968cc2020-02-22 18:16:16 +0100102 cfg->server_port = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100103 break;
104 case 'c':
Harald Welte0e968cc2020-02-22 18:16:16 +0100105 cfg->client_id = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100106 break;
107 case 'n':
Harald Welte0e968cc2020-02-22 18:16:16 +0100108 cfg->client_slot = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100109 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100110 case 'a':
111 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
112 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
113 fprintf(stderr, "ATR malformed\n");
114 exit(2);
115 }
116 break;
117 case 'e':
118 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
119 break;
120#ifdef USB_SUPPORT
121 case 'V':
122 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
123 break;
124 case 'P':
125 cfg->usb.product_id = strtol(optarg, NULL, 16);
126 break;
127 case 'C':
128 cfg->usb.config_id = atoi(optarg);
129 break;
130 case 'I':
131 cfg->usb.if_num = atoi(optarg);
132 break;
133 case 'S':
134 cfg->usb.altsetting = atoi(optarg);
135 break;
136 case 'A':
137 cfg->usb.addr = atoi(optarg);
138 break;
139 case 'H':
140 cfg->usb.path = optarg;
141 break;
142#endif
Harald Weltea9caad82020-02-14 14:32:02 +0100143 default:
144 break;
145 }
146 }
147}
148
Harald Weltedc347642020-10-20 19:14:55 +0200149
150static int avoid_zombies(void)
151{
152 static struct sigaction sa_chld;
153
154 sa_chld.sa_handler = SIG_IGN;
155 sigemptyset(&sa_chld.sa_mask);
156 sa_chld.sa_flags = SA_NOCLDWAIT;
157 sa_chld.sa_restorer = NULL;
158
159 return sigaction(SIGCHLD, &sa_chld, NULL);
160}
161
Harald Weltea9caad82020-02-14 14:32:02 +0100162int main(int argc, char **argv)
163{
164 struct bankd_client *g_client;
Harald Welte0e968cc2020-02-22 18:16:16 +0100165 struct client_config *cfg;
Harald Weltea9caad82020-02-14 14:32:02 +0100166 char hostname[256];
167
168 gethostname(hostname, sizeof(hostname));
169
170 g_tall_ctx = talloc_named_const(NULL, 0, "global");
171 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
172 msgb_talloc_ctx_init(g_tall_ctx, 0);
173
174 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +0100175 log_set_print_level(osmo_stderr_target, 1);
176 log_set_print_category(osmo_stderr_target, 1);
177 log_set_print_category_hex(osmo_stderr_target, 0);
178 osmo_fsm_log_addr(0);
Harald Weltea9caad82020-02-14 14:32:02 +0100179
Harald Welte0e968cc2020-02-22 18:16:16 +0100180 cfg = client_config_init(g_tall_ctx);
181 OSMO_ASSERT(cfg);
182 handle_options(cfg, argc, argv);
Harald Weltea9caad82020-02-14 14:32:02 +0100183
Harald Welte0e968cc2020-02-22 18:16:16 +0100184 g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg);
Harald Weltea9caad82020-02-14 14:32:02 +0100185
186 osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
187
188 signal(SIGUSR1, handle_sig_usr1);
189
Harald Weltedc347642020-10-20 19:14:55 +0200190 /* Silently (and portably) reap children. */
191 if (avoid_zombies() < 0) {
Harald Welte7293e7b2021-12-08 21:29:11 +0100192 LOGP(DMAIN, LOGL_FATAL, "Unable to silently reap children: %s\n", strerror(errno));
Harald Weltedc347642020-10-20 19:14:55 +0200193 exit(1);
194 }
195
Harald Weltea9caad82020-02-14 14:32:02 +0100196 asn_debug = 0;
197
198 client_user_main(g_client);
199}