blob: 50919f916d17f1192e762384af87a5a917a4fcd3 [file] [log] [blame]
Harald Welte2ff0ab92018-08-17 22:10:49 +02001
2#include <errno.h>
3#include <string.h>
4
Harald Welte228af8a2019-03-08 22:20:21 +01005#define _GNU_SOURCE
6#include <getopt.h>
7
Harald Welte2ff0ab92018-08-17 22:10:49 +02008#include <talloc.h>
9
10#include <osmocom/core/msgb.h>
11#include <osmocom/core/fsm.h>
12#include <osmocom/core/utils.h>
13#include <osmocom/core/logging.h>
14#include <osmocom/core/application.h>
15
16#include <osmocom/abis/ipa.h>
17#include <osmocom/gsm/protocol/ipaccess.h>
18
19#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020020#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010021#include "debug.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020022
Harald Welte24173fb2018-08-24 20:37:28 +020023static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020024{
Harald Welte24173fb2018-08-24 20:37:28 +020025 RsproPDU_t *pdu = rspro_dec_msg(msg);
26 if (!pdu) {
27 fprintf(stderr, "Error decoding PDU\n");
28 return -1;
Harald Welte2ff0ab92018-08-17 22:10:49 +020029 }
Harald Welte24173fb2018-08-24 20:37:28 +020030
31 switch (pdu->msg.present) {
32 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010033 /* Store 'identity' of bankd to in peer_comp_id */
34 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Harald Welte417b9612018-09-24 14:53:41 +020035 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020036 break;
37 default:
38 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
39 return -1;
40 }
41
42 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020043}
44
Harald Welte24173fb2018-08-24 20:37:28 +020045int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020046{
47 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
48 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
49 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020050 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020051
52 if (msgb_length(msg) < sizeof(*hh))
53 goto invalid;
54 msg->l2h = &hh->data[0];
Harald Welte1a171042019-03-08 19:18:52 +010055 switch (hh->proto) {
Harald Welte03b24112019-03-08 19:43:02 +010056 case IPAC_PROTO_IPACCESS:
57 rc = ipaccess_bts_handle_ccm(conn, &bc->srv_conn.ipa_dev, msg);
58 if (rc < 0)
59 break;
60 switch (hh->data[0]) {
61 case IPAC_MSGT_PONG:
62 ipa_keepalive_fsm_pong_received(bc->srv_conn.keepalive_fi);
63 rc = 0;
64 break;
65 default:
66 break;
67 }
68 break;
Harald Welte1a171042019-03-08 19:18:52 +010069 case IPAC_PROTO_OSMO:
70 if (!he || msgb_l2len(msg) < sizeof(*he))
71 goto invalid;
72 msg->l2h = &he->data[0];
73 switch (he->proto) {
74 case IPAC_PROTO_EXT_RSPRO:
75 printf("Received RSPRO %s\n", msgb_hexdump(msg));
76 rc = bankd_handle_msg(bc, msg);
77 break;
78 default:
79 goto invalid;
80 }
81 break;
82 default:
Harald Welte2ff0ab92018-08-17 22:10:49 +020083 goto invalid;
Harald Welte1a171042019-03-08 19:18:52 +010084 }
Harald Welte24173fb2018-08-24 20:37:28 +020085
86 return rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020087
88invalid:
89 msgb_free(msg);
90 return -1;
91}
92
Harald Welte24173fb2018-08-24 20:37:28 +020093static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +020094static void *g_tall_ctx;
95void __thread *talloc_asn1_ctx;
Harald Welte43ab79f2018-10-03 23:34:21 +020096int asn_debug;
Harald Welte2ff0ab92018-08-17 22:10:49 +020097
Harald Weltee56f2b92019-03-02 17:02:13 +010098/* handle incoming messages from server */
99static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
100{
101 RsproPDU_t *resp;
102
103 switch (pdu->msg.present) {
104 case RsproPDUchoice_PR_connectClientRes:
105 /* Store 'identity' of server in srvc->peer_comp_id */
106 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
107 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
108 break;
109 case RsproPDUchoice_PR_configClientReq:
110 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100111 if (!g_client->srv_conn.clslot)
112 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
113 *g_client->srv_conn.clslot = pdu->msg.choice.configClientReq.clientSlot;
Harald Weltee56f2b92019-03-02 17:02:13 +0100114 /* store/set the bankd ip/port as instructed by the server */
115 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
116 rspro_IpAddr2str(&pdu->msg.choice.configClientReq.bankd.ip));
117 g_client->bankd_port = ntohs(pdu->msg.choice.configClientReq.bankd.port);
118 /* instruct bankd FSM to connect */
119 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
120 /* send response to server */
121 resp = rspro_gen_ConfigClientRes(ResultCode_ok);
122 ipa_client_conn_send_rspro(srvc->conn, resp);
123 break;
124 default:
125 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
126 return -1;
127 }
128
129 return 0;
130}
131
Harald Welte228af8a2019-03-08 22:20:21 +0100132static void printf_help()
133{
134 printf(
135 " -h --help Print this help message\n"
136 " -i --server-ip A.B.C.D remsim-server IP address\n"
137 " -p --server-port 13245 remsim-server TCP port\n"
138 " -i --client-id <0-65535> RSPRO ClientId of this client\n"
139 " -s --client-slot <0-65535> RSPRO SlotNr of this client\n"
140 );
141}
142
143static void handle_options(int argc, char **argv)
144{
145 while (1) {
146 int option_index = 0, c;
147 static const struct option long_options[] = {
148 { "help", 0, 0, 'h' },
149 { "server-ip", 1, 0, 'i' },
150 { "server-port", 1, 0, 'p' },
151 { "client-id", 1, 0, 'c' },
152 { "client-slot", 1, 0, 's' },
153 { 0, 0, 0, 0 }
154 };
155
156 c = getopt_long(argc, argv, "hi:p:c:s:",
157 long_options, &option_index);
158 if (c == -1)
159 break;
160
161 switch (c) {
162 case 'h':
163 printf_help();
164 exit(0);
165 break;
166 case 'i':
167 g_client->srv_conn.server_host = optarg;
168 break;
169 case 'p':
170 g_client->srv_conn.server_port = atoi(optarg);
171 break;
172 case 'c':
173 if (!g_client->srv_conn.clslot)
174 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
175 g_client->srv_conn.clslot->clientId = atoi(optarg);
176 break;
177 case 's':
178 if (!g_client->srv_conn.clslot)
179 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
180 g_client->srv_conn.clslot->slotNr = atoi(optarg);
181 break;
182 default:
183 break;
184 }
185 }
186}
187
Harald Welte2ff0ab92018-08-17 22:10:49 +0200188int main(int argc, char **argv)
189{
Harald Weltee56f2b92019-03-02 17:02:13 +0100190 struct rspro_server_conn *srvc;
191 int rc;
192
Harald Welte2ff0ab92018-08-17 22:10:49 +0200193 g_tall_ctx = talloc_named_const(NULL, 0, "global");
194
Harald Weltec7995e72019-03-08 20:45:03 +0100195 osmo_init_logging2(g_tall_ctx, &log_info);
196
Harald Welte24173fb2018-08-24 20:37:28 +0200197 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100198
199 srvc = &g_client->srv_conn;
200 srvc->server_host = "localhost";
201 srvc->server_port = 9998;
202 srvc->handle_rx = srvc_handle_rx;
203 srvc->own_comp_id.type = ComponentType_remsimClient;
204 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
205 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
206 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte228af8a2019-03-08 22:20:21 +0100207
208 handle_options(argc, argv);
209
Harald Weltee56f2b92019-03-02 17:02:13 +0100210 rc = server_conn_fsm_alloc(g_client, srvc);
211 if (rc < 0) {
212 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
213 exit(1);
214 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200215
Harald Welte43ab79f2018-10-03 23:34:21 +0200216 asn_debug = 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +0200217
Harald Welte24173fb2018-08-24 20:37:28 +0200218 if (bankd_conn_fsm_alloc(g_client) < 0) {
Harald Welte2ff0ab92018-08-17 22:10:49 +0200219 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
220 exit(1);
221 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200222
223 while (1) {
224 osmo_select_main(0);
225 }
226}