blob: 41361c03eff3b29875d9e2d6e7ebbfbe064ef14d [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
Harald Welte2ff0ab92018-08-17 22:10:49 +020022
23#include <errno.h>
24#include <string.h>
Harald Weltece638d82019-03-17 09:36:04 +010025#include <signal.h>
Harald Welte2ff0ab92018-08-17 22:10:49 +020026
Harald Welte228af8a2019-03-08 22:20:21 +010027#define _GNU_SOURCE
28#include <getopt.h>
29
Harald Welte2ff0ab92018-08-17 22:10:49 +020030#include <talloc.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/fsm.h>
34#include <osmocom/core/utils.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/application.h>
37
38#include <osmocom/abis/ipa.h>
39#include <osmocom/gsm/protocol/ipaccess.h>
40
41#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020042#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010043#include "debug.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020044
Harald Welted5a87292020-02-13 16:44:56 +010045static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Harald Welte2ff0ab92018-08-17 22:10:49 +020046{
Harald Welte24173fb2018-08-24 20:37:28 +020047 switch (pdu->msg.present) {
48 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010049 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welted5a87292020-02-13 16:44:56 +010050 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
51 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020052 break;
Harald Welted5a87292020-02-13 16:44:56 +010053 case RsproPDUchoice_PR_tpduCardToModem:
54 case RsproPDUchoice_PR_setAtrReq:
Harald Welte24173fb2018-08-24 20:37:28 +020055 default:
Harald Welted5a87292020-02-13 16:44:56 +010056 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU %s\n",
57 rspro_msgt_name(pdu));
Harald Welte24173fb2018-08-24 20:37:28 +020058 return -1;
59 }
60
61 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020062}
63
Harald Welte24173fb2018-08-24 20:37:28 +020064static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +020065static void *g_tall_ctx;
66void __thread *talloc_asn1_ctx;
Harald Welte43ab79f2018-10-03 23:34:21 +020067int asn_debug;
Harald Welte2ff0ab92018-08-17 22:10:49 +020068
Harald Weltee56f2b92019-03-02 17:02:13 +010069/* handle incoming messages from server */
70static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
71{
72 RsproPDU_t *resp;
73
74 switch (pdu->msg.present) {
75 case RsproPDUchoice_PR_connectClientRes:
76 /* Store 'identity' of server in srvc->peer_comp_id */
77 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
78 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
79 break;
Harald Welted571a3e2019-03-11 22:09:50 +010080 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +010081 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +010082 if (!g_client->srv_conn.clslot)
83 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +010084 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welted5a87292020-02-13 16:44:56 +010085 if (!g_client->bankd_conn.clslot)
86 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
87 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +010088 /* send response to server */
89 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
90 server_conn_send_rspro(srvc, resp);
91 break;
92 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +010093 /* store/set the bankd ip/port as instructed by the server */
Harald Welted5a87292020-02-13 16:44:56 +010094 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +010095 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +010096 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welted5a87292020-02-13 16:44:56 +010097 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Weltee56f2b92019-03-02 17:02:13 +010098 /* instruct bankd FSM to connect */
Harald Welted5a87292020-02-13 16:44:56 +010099 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100100 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100101 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100102 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100103 break;
104 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100105 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
106 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100107 return -1;
108 }
109
110 return 0;
111}
112
Harald Weltece638d82019-03-17 09:36:04 +0100113static void handle_sig_usr1(int signal)
114{
115 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200116 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100117}
118
Harald Welte228af8a2019-03-08 22:20:21 +0100119static void printf_help()
120{
121 printf(
122 " -h --help Print this help message\n"
123 " -i --server-ip A.B.C.D remsim-server IP address\n"
124 " -p --server-port 13245 remsim-server TCP port\n"
125 " -i --client-id <0-65535> RSPRO ClientId of this client\n"
Harald Welte72cde102019-03-30 10:43:06 +0100126 " -n --client-slot <0-65535> RSPRO SlotNr of this client\n"
Harald Welte228af8a2019-03-08 22:20:21 +0100127 );
128}
129
130static void handle_options(int argc, char **argv)
131{
132 while (1) {
133 int option_index = 0, c;
134 static const struct option long_options[] = {
135 { "help", 0, 0, 'h' },
136 { "server-ip", 1, 0, 'i' },
137 { "server-port", 1, 0, 'p' },
138 { "client-id", 1, 0, 'c' },
Harald Welte72cde102019-03-30 10:43:06 +0100139 { "client-slot", 1, 0, 'n' },
Harald Welte228af8a2019-03-08 22:20:21 +0100140 { 0, 0, 0, 0 }
141 };
142
Harald Welte72cde102019-03-30 10:43:06 +0100143 c = getopt_long(argc, argv, "hi:p:c:n:",
Harald Welte228af8a2019-03-08 22:20:21 +0100144 long_options, &option_index);
145 if (c == -1)
146 break;
147
148 switch (c) {
149 case 'h':
150 printf_help();
151 exit(0);
152 break;
153 case 'i':
154 g_client->srv_conn.server_host = optarg;
155 break;
156 case 'p':
157 g_client->srv_conn.server_port = atoi(optarg);
158 break;
159 case 'c':
160 if (!g_client->srv_conn.clslot)
161 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
162 g_client->srv_conn.clslot->clientId = atoi(optarg);
163 break;
164 case 's':
165 if (!g_client->srv_conn.clslot)
166 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
167 g_client->srv_conn.clslot->slotNr = atoi(optarg);
168 break;
169 default:
170 break;
171 }
172 }
173}
174
Harald Welte2ff0ab92018-08-17 22:10:49 +0200175int main(int argc, char **argv)
176{
Harald Welted5a87292020-02-13 16:44:56 +0100177 struct rspro_server_conn *srvc, *bankdc;
Harald Weltee56f2b92019-03-02 17:02:13 +0100178 int rc;
179
Harald Welte2ff0ab92018-08-17 22:10:49 +0200180 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +0200181 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +0200182 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte2ff0ab92018-08-17 22:10:49 +0200183
Harald Weltec7995e72019-03-08 20:45:03 +0100184 osmo_init_logging2(g_tall_ctx, &log_info);
185
Harald Welte24173fb2018-08-24 20:37:28 +0200186 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100187
188 srvc = &g_client->srv_conn;
189 srvc->server_host = "localhost";
190 srvc->server_port = 9998;
191 srvc->handle_rx = srvc_handle_rx;
192 srvc->own_comp_id.type = ComponentType_remsimClient;
193 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
194 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
195 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte228af8a2019-03-08 22:20:21 +0100196
197 handle_options(argc, argv);
198
Harald Weltece638d82019-03-17 09:36:04 +0100199 signal(SIGUSR1, handle_sig_usr1);
200
Harald Weltee56f2b92019-03-02 17:02:13 +0100201 rc = server_conn_fsm_alloc(g_client, srvc);
202 if (rc < 0) {
203 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
204 exit(1);
205 }
Harald Welted2192e22019-11-07 18:10:57 +0100206 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte2ff0ab92018-08-17 22:10:49 +0200207
Harald Welte43ab79f2018-10-03 23:34:21 +0200208 asn_debug = 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200209
Harald Welted5a87292020-02-13 16:44:56 +0100210 bankdc = &g_client->bankd_conn;
211 /* server_host / server_port are configured from remsim-server */
212 bankdc->handle_rx = bankd_handle_rx;
213 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
214 rc = server_conn_fsm_alloc(g_client, bankdc);
215 if (rc < 0) {
216 fprintf(stderr, "Unable to connect bankd conn FSM: %s\n", strerror(errno));
Harald Welte2ff0ab92018-08-17 22:10:49 +0200217 exit(1);
218 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200219
220 while (1) {
221 osmo_select_main(0);
222 }
223}