blob: 5a23d82c7ba487b51b5a8fed8801500cb10de949 [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 Weltec8c77e02022-07-24 12:54:24 +020036 " -r --atr-ignore-rspro Ignore any ATR from bankd; use only ATR given by -a)\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010037 " -e --event-script <path> event script to be called by client\n"
38#ifdef USB_SUPPORT
39 " -V --usb-vendor VENDOR_ID\n"
40 " -P --usb-product PRODUCT_ID\n"
41 " -C --usb-config CONFIG_ID\n"
42 " -I --usb-interface INTERFACE_ID\n"
43 " -S --usb-altsetting ALTSETTING_ID\n"
44 " -A --usb-address ADDRESS\n"
45 " -H --usb-path PATH\n"
46#endif
Harald Weltea9caad82020-02-14 14:32:02 +010047 );
48}
49
Harald Welte0e968cc2020-02-22 18:16:16 +010050static void handle_options(struct client_config *cfg, int argc, char **argv)
Harald Weltea9caad82020-02-14 14:32:02 +010051{
Harald Welte0e968cc2020-02-22 18:16:16 +010052 int rc;
53
Harald Weltea9caad82020-02-14 14:32:02 +010054 while (1) {
55 int option_index = 0, c;
56 static const struct option long_options[] = {
57 { "help", 0, 0, 'h' },
Harald Welte0e968cc2020-02-22 18:16:16 +010058 { "version", 0, 0, 'v' },
Harald Welte80a01102020-05-25 14:55:12 +020059 { "debug", 1, 0, 'd' },
Harald Weltea9caad82020-02-14 14:32:02 +010060 { "server-ip", 1, 0, 'i' },
61 { "server-port", 1, 0, 'p' },
62 { "client-id", 1, 0, 'c' },
63 { "client-slot", 1, 0, 'n' },
Harald Welte0e968cc2020-02-22 18:16:16 +010064 { "atr", 1, 0, 'a' },
Harald Weltec8c77e02022-07-24 12:54:24 +020065 { "atr-ignore-rspro", 0, 0, 'r' },
Harald Welte0e968cc2020-02-22 18:16:16 +010066 { "event-script", 1, 0, 'e' },
67#ifdef USB_SUPPORT
68 { "usb-vendor", 1, 0, 'V' },
69 { "usb-product", 1, 0, 'P' },
70 { "usb-config", 1, 0, 'C' },
71 { "usb-interface", 1, 0, 'I' },
72 { "usb-altsetting", 1, 0, 'S' },
73 { "usb-address", 1, 0, 'A' },
74 { "usb-path", 1, 0, 'H' },
75#endif
Harald Weltea9caad82020-02-14 14:32:02 +010076 { 0, 0, 0, 0 }
77 };
78
Harald Weltec8c77e02022-07-24 12:54:24 +020079 c = getopt_long(argc, argv, "hvd:i:p:c:n:a:re:"
Harald Welte0e968cc2020-02-22 18:16:16 +010080#ifdef USB_SUPPORT
81 "V:P:C:I:S:A:H:"
82#endif
83 ,
Harald Weltea9caad82020-02-14 14:32:02 +010084 long_options, &option_index);
85 if (c == -1)
86 break;
87
88 switch (c) {
89 case 'h':
90 printf_help();
91 exit(0);
92 break;
Harald Welte0e968cc2020-02-22 18:16:16 +010093 case 'v':
94 printf("osmo-remsim-client version %s\n", VERSION);
95 exit(0);
96 break;
Harald Welte80a01102020-05-25 14:55:12 +020097 case 'd':
98 log_parse_category_mask(osmo_stderr_target, optarg);
99 break;
Harald Weltea9caad82020-02-14 14:32:02 +0100100 case 'i':
Harald Welte0e968cc2020-02-22 18:16:16 +0100101 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100102 break;
103 case 'p':
Harald Welte0e968cc2020-02-22 18:16:16 +0100104 cfg->server_port = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100105 break;
106 case 'c':
Harald Welte0e968cc2020-02-22 18:16:16 +0100107 cfg->client_id = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100108 break;
109 case 'n':
Harald Welte0e968cc2020-02-22 18:16:16 +0100110 cfg->client_slot = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100111 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100112 case 'a':
113 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
114 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
115 fprintf(stderr, "ATR malformed\n");
116 exit(2);
117 }
118 break;
Harald Weltec8c77e02022-07-24 12:54:24 +0200119 case 'r':
120 cfg->atr_ignore_rspro = true;
121 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100122 case 'e':
123 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
124 break;
125#ifdef USB_SUPPORT
126 case 'V':
127 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
128 break;
129 case 'P':
130 cfg->usb.product_id = strtol(optarg, NULL, 16);
131 break;
132 case 'C':
133 cfg->usb.config_id = atoi(optarg);
134 break;
135 case 'I':
136 cfg->usb.if_num = atoi(optarg);
137 break;
138 case 'S':
139 cfg->usb.altsetting = atoi(optarg);
140 break;
141 case 'A':
142 cfg->usb.addr = atoi(optarg);
143 break;
144 case 'H':
145 cfg->usb.path = optarg;
146 break;
147#endif
Harald Weltea9caad82020-02-14 14:32:02 +0100148 default:
149 break;
150 }
151 }
152}
153
Harald Weltedc347642020-10-20 19:14:55 +0200154
155static int avoid_zombies(void)
156{
157 static struct sigaction sa_chld;
158
159 sa_chld.sa_handler = SIG_IGN;
160 sigemptyset(&sa_chld.sa_mask);
161 sa_chld.sa_flags = SA_NOCLDWAIT;
162 sa_chld.sa_restorer = NULL;
163
164 return sigaction(SIGCHLD, &sa_chld, NULL);
165}
166
Harald Weltea9caad82020-02-14 14:32:02 +0100167int main(int argc, char **argv)
168{
169 struct bankd_client *g_client;
Harald Welte0e968cc2020-02-22 18:16:16 +0100170 struct client_config *cfg;
Harald Weltea9caad82020-02-14 14:32:02 +0100171 char hostname[256];
172
173 gethostname(hostname, sizeof(hostname));
174
175 g_tall_ctx = talloc_named_const(NULL, 0, "global");
176 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
177 msgb_talloc_ctx_init(g_tall_ctx, 0);
178
179 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +0100180 log_set_print_level(osmo_stderr_target, 1);
181 log_set_print_category(osmo_stderr_target, 1);
182 log_set_print_category_hex(osmo_stderr_target, 0);
183 osmo_fsm_log_addr(0);
Harald Weltea9caad82020-02-14 14:32:02 +0100184
Harald Welte0e968cc2020-02-22 18:16:16 +0100185 cfg = client_config_init(g_tall_ctx);
186 OSMO_ASSERT(cfg);
187 handle_options(cfg, argc, argv);
Harald Weltea9caad82020-02-14 14:32:02 +0100188
Harald Welte0e968cc2020-02-22 18:16:16 +0100189 g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg);
Harald Weltea9caad82020-02-14 14:32:02 +0100190
191 osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
192
193 signal(SIGUSR1, handle_sig_usr1);
194
Harald Weltedc347642020-10-20 19:14:55 +0200195 /* Silently (and portably) reap children. */
196 if (avoid_zombies() < 0) {
Harald Welte7293e7b2021-12-08 21:29:11 +0100197 LOGP(DMAIN, LOGL_FATAL, "Unable to silently reap children: %s\n", strerror(errno));
Harald Weltedc347642020-10-20 19:14:55 +0200198 exit(1);
199 }
200
Harald Weltea9caad82020-02-14 14:32:02 +0100201 asn_debug = 0;
202
203 client_user_main(g_client);
204}