blob: 0fb9f21460a5052de8b0911aa7935aba50f8f705 [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
Harald Welteb49ac9c2019-03-09 20:36:07 +010052 LOGPFSM(bc->bankd_fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
53
Harald Welte24173fb2018-08-24 20:37:28 +020054 switch (pdu->msg.present) {
55 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010056 /* Store 'identity' of bankd to in peer_comp_id */
57 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Harald Welte417b9612018-09-24 14:53:41 +020058 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020059 break;
60 default:
61 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
62 return -1;
63 }
64
65 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020066}
67
Harald Welte24173fb2018-08-24 20:37:28 +020068int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020069{
70 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
71 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
72 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020073 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020074
75 if (msgb_length(msg) < sizeof(*hh))
76 goto invalid;
77 msg->l2h = &hh->data[0];
Harald Welte1a171042019-03-08 19:18:52 +010078 switch (hh->proto) {
Harald Welte03b24112019-03-08 19:43:02 +010079 case IPAC_PROTO_IPACCESS:
80 rc = ipaccess_bts_handle_ccm(conn, &bc->srv_conn.ipa_dev, msg);
81 if (rc < 0)
82 break;
83 switch (hh->data[0]) {
84 case IPAC_MSGT_PONG:
85 ipa_keepalive_fsm_pong_received(bc->srv_conn.keepalive_fi);
86 rc = 0;
87 break;
88 default:
89 break;
90 }
91 break;
Harald Welte1a171042019-03-08 19:18:52 +010092 case IPAC_PROTO_OSMO:
93 if (!he || msgb_l2len(msg) < sizeof(*he))
94 goto invalid;
95 msg->l2h = &he->data[0];
96 switch (he->proto) {
97 case IPAC_PROTO_EXT_RSPRO:
98 printf("Received RSPRO %s\n", msgb_hexdump(msg));
99 rc = bankd_handle_msg(bc, msg);
100 break;
101 default:
102 goto invalid;
103 }
104 break;
105 default:
Harald Welte2ff0ab92018-08-17 22:10:49 +0200106 goto invalid;
Harald Welte1a171042019-03-08 19:18:52 +0100107 }
Harald Welte24173fb2018-08-24 20:37:28 +0200108
109 return rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200110
111invalid:
112 msgb_free(msg);
113 return -1;
114}
115
Harald Welte24173fb2018-08-24 20:37:28 +0200116static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200117static void *g_tall_ctx;
118void __thread *talloc_asn1_ctx;
Harald Welte43ab79f2018-10-03 23:34:21 +0200119int asn_debug;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200120
Harald Weltee56f2b92019-03-02 17:02:13 +0100121/* handle incoming messages from server */
122static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
123{
124 RsproPDU_t *resp;
125
126 switch (pdu->msg.present) {
127 case RsproPDUchoice_PR_connectClientRes:
128 /* Store 'identity' of server in srvc->peer_comp_id */
129 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
130 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
131 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100132 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100133 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100134 if (!g_client->srv_conn.clslot)
135 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100136 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
137 /* send response to server */
138 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
139 server_conn_send_rspro(srvc, resp);
140 break;
141 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100142 /* store/set the bankd ip/port as instructed by the server */
143 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100144 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
145 g_client->bankd_port = ntohs(pdu->msg.choice.configClientBankReq.bankd.port);
Harald Welte9cf013a2019-03-11 22:19:19 +0100146 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Weltee56f2b92019-03-02 17:02:13 +0100147 /* instruct bankd FSM to connect */
148 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
149 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100150 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100151 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100152 break;
153 default:
154 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
155 return -1;
156 }
157
158 return 0;
159}
160
Harald Welte228af8a2019-03-08 22:20:21 +0100161static void printf_help()
162{
163 printf(
164 " -h --help Print this help message\n"
165 " -i --server-ip A.B.C.D remsim-server IP address\n"
166 " -p --server-port 13245 remsim-server TCP port\n"
167 " -i --client-id <0-65535> RSPRO ClientId of this client\n"
168 " -s --client-slot <0-65535> RSPRO SlotNr of this client\n"
169 );
170}
171
172static void handle_options(int argc, char **argv)
173{
174 while (1) {
175 int option_index = 0, c;
176 static const struct option long_options[] = {
177 { "help", 0, 0, 'h' },
178 { "server-ip", 1, 0, 'i' },
179 { "server-port", 1, 0, 'p' },
180 { "client-id", 1, 0, 'c' },
181 { "client-slot", 1, 0, 's' },
182 { 0, 0, 0, 0 }
183 };
184
185 c = getopt_long(argc, argv, "hi:p:c:s:",
186 long_options, &option_index);
187 if (c == -1)
188 break;
189
190 switch (c) {
191 case 'h':
192 printf_help();
193 exit(0);
194 break;
195 case 'i':
196 g_client->srv_conn.server_host = optarg;
197 break;
198 case 'p':
199 g_client->srv_conn.server_port = atoi(optarg);
200 break;
201 case 'c':
202 if (!g_client->srv_conn.clslot)
203 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
204 g_client->srv_conn.clslot->clientId = atoi(optarg);
205 break;
206 case 's':
207 if (!g_client->srv_conn.clslot)
208 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
209 g_client->srv_conn.clslot->slotNr = atoi(optarg);
210 break;
211 default:
212 break;
213 }
214 }
215}
216
Harald Welte2ff0ab92018-08-17 22:10:49 +0200217int main(int argc, char **argv)
218{
Harald Weltee56f2b92019-03-02 17:02:13 +0100219 struct rspro_server_conn *srvc;
220 int rc;
221
Harald Welte2ff0ab92018-08-17 22:10:49 +0200222 g_tall_ctx = talloc_named_const(NULL, 0, "global");
223
Harald Weltec7995e72019-03-08 20:45:03 +0100224 osmo_init_logging2(g_tall_ctx, &log_info);
225
Harald Welte24173fb2018-08-24 20:37:28 +0200226 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100227
228 srvc = &g_client->srv_conn;
229 srvc->server_host = "localhost";
230 srvc->server_port = 9998;
231 srvc->handle_rx = srvc_handle_rx;
232 srvc->own_comp_id.type = ComponentType_remsimClient;
233 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
234 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
235 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte228af8a2019-03-08 22:20:21 +0100236
237 handle_options(argc, argv);
238
Harald Weltee56f2b92019-03-02 17:02:13 +0100239 rc = server_conn_fsm_alloc(g_client, srvc);
240 if (rc < 0) {
241 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
242 exit(1);
243 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200244
Harald Welte43ab79f2018-10-03 23:34:21 +0200245 asn_debug = 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200246
Harald Welte24173fb2018-08-24 20:37:28 +0200247 if (bankd_conn_fsm_alloc(g_client) < 0) {
Harald Welte2ff0ab92018-08-17 22:10:49 +0200248 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
249 exit(1);
250 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200251
252 while (1) {
253 osmo_select_main(0);
254 }
255}