blob: bf549c5f381495b3d41fc5b5f2fded3df9edc1e2 [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>
25
Harald Welte228af8a2019-03-08 22:20:21 +010026#define _GNU_SOURCE
27#include <getopt.h>
28
Harald Welte2ff0ab92018-08-17 22:10:49 +020029#include <talloc.h>
30
31#include <osmocom/core/msgb.h>
32#include <osmocom/core/fsm.h>
33#include <osmocom/core/utils.h>
34#include <osmocom/core/logging.h>
35#include <osmocom/core/application.h>
36
37#include <osmocom/abis/ipa.h>
38#include <osmocom/gsm/protocol/ipaccess.h>
39
40#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020041#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010042#include "debug.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020043
Harald Welte24173fb2018-08-24 20:37:28 +020044static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020045{
Harald Welte24173fb2018-08-24 20:37:28 +020046 RsproPDU_t *pdu = rspro_dec_msg(msg);
47 if (!pdu) {
48 fprintf(stderr, "Error decoding PDU\n");
49 return -1;
Harald Welte2ff0ab92018-08-17 22:10:49 +020050 }
Harald Welte24173fb2018-08-24 20:37:28 +020051
52 switch (pdu->msg.present) {
53 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010054 /* Store 'identity' of bankd to in peer_comp_id */
55 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Harald Welte417b9612018-09-24 14:53:41 +020056 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020057 break;
58 default:
59 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
60 return -1;
61 }
62
63 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020064}
65
Harald Welte24173fb2018-08-24 20:37:28 +020066int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020067{
68 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
69 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
70 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020071 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020072
73 if (msgb_length(msg) < sizeof(*hh))
74 goto invalid;
75 msg->l2h = &hh->data[0];
Harald Welte1a171042019-03-08 19:18:52 +010076 switch (hh->proto) {
Harald Welte03b24112019-03-08 19:43:02 +010077 case IPAC_PROTO_IPACCESS:
78 rc = ipaccess_bts_handle_ccm(conn, &bc->srv_conn.ipa_dev, msg);
79 if (rc < 0)
80 break;
81 switch (hh->data[0]) {
82 case IPAC_MSGT_PONG:
83 ipa_keepalive_fsm_pong_received(bc->srv_conn.keepalive_fi);
84 rc = 0;
85 break;
86 default:
87 break;
88 }
89 break;
Harald Welte1a171042019-03-08 19:18:52 +010090 case IPAC_PROTO_OSMO:
91 if (!he || msgb_l2len(msg) < sizeof(*he))
92 goto invalid;
93 msg->l2h = &he->data[0];
94 switch (he->proto) {
95 case IPAC_PROTO_EXT_RSPRO:
96 printf("Received RSPRO %s\n", msgb_hexdump(msg));
97 rc = bankd_handle_msg(bc, msg);
98 break;
99 default:
100 goto invalid;
101 }
102 break;
103 default:
Harald Welte2ff0ab92018-08-17 22:10:49 +0200104 goto invalid;
Harald Welte1a171042019-03-08 19:18:52 +0100105 }
Harald Welte24173fb2018-08-24 20:37:28 +0200106
107 return rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200108
109invalid:
110 msgb_free(msg);
111 return -1;
112}
113
Harald Welte24173fb2018-08-24 20:37:28 +0200114static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200115static void *g_tall_ctx;
116void __thread *talloc_asn1_ctx;
Harald Welte43ab79f2018-10-03 23:34:21 +0200117int asn_debug;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200118
Harald Weltee56f2b92019-03-02 17:02:13 +0100119/* handle incoming messages from server */
120static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
121{
122 RsproPDU_t *resp;
123
124 switch (pdu->msg.present) {
125 case RsproPDUchoice_PR_connectClientRes:
126 /* Store 'identity' of server in srvc->peer_comp_id */
127 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
128 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
129 break;
130 case RsproPDUchoice_PR_configClientReq:
131 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100132 if (!g_client->srv_conn.clslot)
133 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
134 *g_client->srv_conn.clslot = pdu->msg.choice.configClientReq.clientSlot;
Harald Weltee56f2b92019-03-02 17:02:13 +0100135 /* store/set the bankd ip/port as instructed by the server */
136 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
137 rspro_IpAddr2str(&pdu->msg.choice.configClientReq.bankd.ip));
138 g_client->bankd_port = ntohs(pdu->msg.choice.configClientReq.bankd.port);
139 /* instruct bankd FSM to connect */
140 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
141 /* send response to server */
142 resp = rspro_gen_ConfigClientRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100143 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100144 break;
145 default:
146 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
147 return -1;
148 }
149
150 return 0;
151}
152
Harald Welte228af8a2019-03-08 22:20:21 +0100153static void printf_help()
154{
155 printf(
156 " -h --help Print this help message\n"
157 " -i --server-ip A.B.C.D remsim-server IP address\n"
158 " -p --server-port 13245 remsim-server TCP port\n"
159 " -i --client-id <0-65535> RSPRO ClientId of this client\n"
160 " -s --client-slot <0-65535> RSPRO SlotNr of this client\n"
161 );
162}
163
164static void handle_options(int argc, char **argv)
165{
166 while (1) {
167 int option_index = 0, c;
168 static const struct option long_options[] = {
169 { "help", 0, 0, 'h' },
170 { "server-ip", 1, 0, 'i' },
171 { "server-port", 1, 0, 'p' },
172 { "client-id", 1, 0, 'c' },
173 { "client-slot", 1, 0, 's' },
174 { 0, 0, 0, 0 }
175 };
176
177 c = getopt_long(argc, argv, "hi:p:c:s:",
178 long_options, &option_index);
179 if (c == -1)
180 break;
181
182 switch (c) {
183 case 'h':
184 printf_help();
185 exit(0);
186 break;
187 case 'i':
188 g_client->srv_conn.server_host = optarg;
189 break;
190 case 'p':
191 g_client->srv_conn.server_port = atoi(optarg);
192 break;
193 case 'c':
194 if (!g_client->srv_conn.clslot)
195 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
196 g_client->srv_conn.clslot->clientId = atoi(optarg);
197 break;
198 case 's':
199 if (!g_client->srv_conn.clslot)
200 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
201 g_client->srv_conn.clslot->slotNr = atoi(optarg);
202 break;
203 default:
204 break;
205 }
206 }
207}
208
Harald Welte2ff0ab92018-08-17 22:10:49 +0200209int main(int argc, char **argv)
210{
Harald Weltee56f2b92019-03-02 17:02:13 +0100211 struct rspro_server_conn *srvc;
212 int rc;
213
Harald Welte2ff0ab92018-08-17 22:10:49 +0200214 g_tall_ctx = talloc_named_const(NULL, 0, "global");
215
Harald Weltec7995e72019-03-08 20:45:03 +0100216 osmo_init_logging2(g_tall_ctx, &log_info);
217
Harald Welte24173fb2018-08-24 20:37:28 +0200218 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100219
220 srvc = &g_client->srv_conn;
221 srvc->server_host = "localhost";
222 srvc->server_port = 9998;
223 srvc->handle_rx = srvc_handle_rx;
224 srvc->own_comp_id.type = ComponentType_remsimClient;
225 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
226 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
227 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte228af8a2019-03-08 22:20:21 +0100228
229 handle_options(argc, argv);
230
Harald Weltee56f2b92019-03-02 17:02:13 +0100231 rc = server_conn_fsm_alloc(g_client, srvc);
232 if (rc < 0) {
233 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
234 exit(1);
235 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200236
Harald Welte43ab79f2018-10-03 23:34:21 +0200237 asn_debug = 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200238
Harald Welte24173fb2018-08-24 20:37:28 +0200239 if (bankd_conn_fsm_alloc(g_client) < 0) {
Harald Welte2ff0ab92018-08-17 22:10:49 +0200240 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
241 exit(1);
242 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200243
244 while (1) {
245 osmo_select_main(0);
246 }
247}