blob: de926f71f0b58232cba421213e38f4129ceb626f [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 Welte24173fb2018-08-24 20:37:28 +020045static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020046{
Harald Welte24173fb2018-08-24 20:37:28 +020047 RsproPDU_t *pdu = rspro_dec_msg(msg);
48 if (!pdu) {
Harald Welte8d8d4f12019-03-27 22:50:39 +010049 LOGPFSML(bc->bankd_fi, LOGL_ERROR, "Error decoding PDU\n");
Harald Welte24173fb2018-08-24 20:37:28 +020050 return -1;
Harald Welte2ff0ab92018-08-17 22:10:49 +020051 }
Harald Welte24173fb2018-08-24 20:37:28 +020052
Harald Welteb49ac9c2019-03-09 20:36:07 +010053 LOGPFSM(bc->bankd_fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
54
Harald Welte24173fb2018-08-24 20:37:28 +020055 switch (pdu->msg.present) {
56 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010057 /* Store 'identity' of bankd to in peer_comp_id */
58 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Harald Welte417b9612018-09-24 14:53:41 +020059 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020060 break;
61 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +010062 LOGPFSML(bc->bankd_fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s: %s\n",
63 rspro_msgt_name(pdu), msgb_hexdump(msg));
Harald Welte24173fb2018-08-24 20:37:28 +020064 return -1;
65 }
66
67 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020068}
69
Harald Welte24173fb2018-08-24 20:37:28 +020070int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020071{
72 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
73 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
74 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020075 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020076
77 if (msgb_length(msg) < sizeof(*hh))
78 goto invalid;
79 msg->l2h = &hh->data[0];
Harald Welte1a171042019-03-08 19:18:52 +010080 switch (hh->proto) {
Harald Welte03b24112019-03-08 19:43:02 +010081 case IPAC_PROTO_IPACCESS:
82 rc = ipaccess_bts_handle_ccm(conn, &bc->srv_conn.ipa_dev, msg);
83 if (rc < 0)
84 break;
85 switch (hh->data[0]) {
86 case IPAC_MSGT_PONG:
87 ipa_keepalive_fsm_pong_received(bc->srv_conn.keepalive_fi);
88 rc = 0;
89 break;
90 default:
91 break;
92 }
93 break;
Harald Welte1a171042019-03-08 19:18:52 +010094 case IPAC_PROTO_OSMO:
95 if (!he || msgb_l2len(msg) < sizeof(*he))
96 goto invalid;
97 msg->l2h = &he->data[0];
98 switch (he->proto) {
99 case IPAC_PROTO_EXT_RSPRO:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100100 LOGPFSML(bc->bankd_fi, LOGL_DEBUG, "Received RSPRO %s\n", msgb_hexdump(msg));
Harald Welte1a171042019-03-08 19:18:52 +0100101 rc = bankd_handle_msg(bc, msg);
102 break;
103 default:
104 goto invalid;
105 }
106 break;
107 default:
Harald Welte2ff0ab92018-08-17 22:10:49 +0200108 goto invalid;
Harald Welte1a171042019-03-08 19:18:52 +0100109 }
Harald Welte24173fb2018-08-24 20:37:28 +0200110
111 return rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200112
113invalid:
114 msgb_free(msg);
115 return -1;
116}
117
Harald Welte24173fb2018-08-24 20:37:28 +0200118static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200119static void *g_tall_ctx;
120void __thread *talloc_asn1_ctx;
Harald Welte43ab79f2018-10-03 23:34:21 +0200121int asn_debug;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200122
Harald Weltee56f2b92019-03-02 17:02:13 +0100123/* handle incoming messages from server */
124static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
125{
126 RsproPDU_t *resp;
127
128 switch (pdu->msg.present) {
129 case RsproPDUchoice_PR_connectClientRes:
130 /* Store 'identity' of server in srvc->peer_comp_id */
131 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
132 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
133 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100134 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100135 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100136 if (!g_client->srv_conn.clslot)
137 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100138 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
139 /* send response to server */
140 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
141 server_conn_send_rspro(srvc, resp);
142 break;
143 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100144 /* store/set the bankd ip/port as instructed by the server */
145 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100146 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte7a950882019-03-17 09:35:16 +0100147 g_client->bankd_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Welte9cf013a2019-03-11 22:19:19 +0100148 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Weltee56f2b92019-03-02 17:02:13 +0100149 /* instruct bankd FSM to connect */
150 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
151 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100152 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100153 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100154 break;
155 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100156 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
157 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100158 return -1;
159 }
160
161 return 0;
162}
163
Harald Weltece638d82019-03-17 09:36:04 +0100164static void handle_sig_usr1(int signal)
165{
166 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200167 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100168}
169
Harald Welte228af8a2019-03-08 22:20:21 +0100170static void printf_help()
171{
172 printf(
173 " -h --help Print this help message\n"
174 " -i --server-ip A.B.C.D remsim-server IP address\n"
175 " -p --server-port 13245 remsim-server TCP port\n"
176 " -i --client-id <0-65535> RSPRO ClientId of this client\n"
Harald Welte72cde102019-03-30 10:43:06 +0100177 " -n --client-slot <0-65535> RSPRO SlotNr of this client\n"
Harald Welte228af8a2019-03-08 22:20:21 +0100178 );
179}
180
181static void handle_options(int argc, char **argv)
182{
183 while (1) {
184 int option_index = 0, c;
185 static const struct option long_options[] = {
186 { "help", 0, 0, 'h' },
187 { "server-ip", 1, 0, 'i' },
188 { "server-port", 1, 0, 'p' },
189 { "client-id", 1, 0, 'c' },
Harald Welte72cde102019-03-30 10:43:06 +0100190 { "client-slot", 1, 0, 'n' },
Harald Welte228af8a2019-03-08 22:20:21 +0100191 { 0, 0, 0, 0 }
192 };
193
Harald Welte72cde102019-03-30 10:43:06 +0100194 c = getopt_long(argc, argv, "hi:p:c:n:",
Harald Welte228af8a2019-03-08 22:20:21 +0100195 long_options, &option_index);
196 if (c == -1)
197 break;
198
199 switch (c) {
200 case 'h':
201 printf_help();
202 exit(0);
203 break;
204 case 'i':
205 g_client->srv_conn.server_host = optarg;
206 break;
207 case 'p':
208 g_client->srv_conn.server_port = atoi(optarg);
209 break;
210 case 'c':
211 if (!g_client->srv_conn.clslot)
212 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
213 g_client->srv_conn.clslot->clientId = atoi(optarg);
214 break;
215 case 's':
216 if (!g_client->srv_conn.clslot)
217 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
218 g_client->srv_conn.clslot->slotNr = atoi(optarg);
219 break;
220 default:
221 break;
222 }
223 }
224}
225
Harald Welte2ff0ab92018-08-17 22:10:49 +0200226int main(int argc, char **argv)
227{
Harald Weltee56f2b92019-03-02 17:02:13 +0100228 struct rspro_server_conn *srvc;
229 int rc;
230
Harald Welte2ff0ab92018-08-17 22:10:49 +0200231 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +0200232 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Welte2ff0ab92018-08-17 22:10:49 +0200233
Harald Weltec7995e72019-03-08 20:45:03 +0100234 osmo_init_logging2(g_tall_ctx, &log_info);
235
Harald Welte24173fb2018-08-24 20:37:28 +0200236 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100237
238 srvc = &g_client->srv_conn;
239 srvc->server_host = "localhost";
240 srvc->server_port = 9998;
241 srvc->handle_rx = srvc_handle_rx;
242 srvc->own_comp_id.type = ComponentType_remsimClient;
243 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
244 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
245 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte228af8a2019-03-08 22:20:21 +0100246
247 handle_options(argc, argv);
248
Harald Weltece638d82019-03-17 09:36:04 +0100249 signal(SIGUSR1, handle_sig_usr1);
250
Harald Weltee56f2b92019-03-02 17:02:13 +0100251 rc = server_conn_fsm_alloc(g_client, srvc);
252 if (rc < 0) {
253 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
254 exit(1);
255 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200256
Harald Welte43ab79f2018-10-03 23:34:21 +0200257 asn_debug = 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200258
Harald Welte24173fb2018-08-24 20:37:28 +0200259 if (bankd_conn_fsm_alloc(g_client) < 0) {
Harald Welte2ff0ab92018-08-17 22:10:49 +0200260 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
261 exit(1);
262 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200263
264 while (1) {
265 osmo_select_main(0);
266 }
267}