2018-08-17 20:10:49 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-08 21:20:21 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
|
2018-08-17 20:10:49 +00:00
|
|
|
#include <talloc.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/fsm.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/application.h>
|
|
|
|
|
|
|
|
#include <osmocom/abis/ipa.h>
|
|
|
|
#include <osmocom/gsm/protocol/ipaccess.h>
|
|
|
|
|
|
|
|
#include "rspro_util.h"
|
2018-08-24 18:37:28 +00:00
|
|
|
#include "client.h"
|
2019-03-03 14:43:07 +00:00
|
|
|
#include "debug.h"
|
2018-08-17 20:10:49 +00:00
|
|
|
|
2018-08-24 18:37:28 +00:00
|
|
|
static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
|
2018-08-17 20:10:49 +00:00
|
|
|
{
|
2018-08-24 18:37:28 +00:00
|
|
|
RsproPDU_t *pdu = rspro_dec_msg(msg);
|
|
|
|
if (!pdu) {
|
|
|
|
fprintf(stderr, "Error decoding PDU\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-08-17 20:10:49 +00:00
|
|
|
|
2018-08-24 18:37:28 +00:00
|
|
|
switch (pdu->msg.present) {
|
|
|
|
case RsproPDUchoice_PR_connectClientRes:
|
2019-03-02 16:02:13 +00:00
|
|
|
/* Store 'identity' of bankd to in peer_comp_id */
|
|
|
|
rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
|
2018-09-24 12:53:41 +00:00
|
|
|
osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
|
2018-08-24 18:37:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
|
|
|
|
return -1;
|
2018-08-17 20:10:49 +00:00
|
|
|
}
|
2018-08-24 18:37:28 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-08-17 20:10:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:37:28 +00:00
|
|
|
int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
|
2018-08-17 20:10:49 +00:00
|
|
|
{
|
|
|
|
struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
|
|
|
|
struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
|
|
|
|
struct bankd_client *bc = conn->data;
|
2018-08-24 18:37:28 +00:00
|
|
|
int rc;
|
2018-08-17 20:10:49 +00:00
|
|
|
|
|
|
|
if (msgb_length(msg) < sizeof(*hh))
|
|
|
|
goto invalid;
|
|
|
|
msg->l2h = &hh->data[0];
|
2019-03-08 18:18:52 +00:00
|
|
|
switch (hh->proto) {
|
2019-03-08 18:43:02 +00:00
|
|
|
case IPAC_PROTO_IPACCESS:
|
|
|
|
rc = ipaccess_bts_handle_ccm(conn, &bc->srv_conn.ipa_dev, msg);
|
|
|
|
if (rc < 0)
|
|
|
|
break;
|
|
|
|
switch (hh->data[0]) {
|
|
|
|
case IPAC_MSGT_PONG:
|
|
|
|
ipa_keepalive_fsm_pong_received(bc->srv_conn.keepalive_fi);
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2019-03-08 18:18:52 +00:00
|
|
|
case IPAC_PROTO_OSMO:
|
|
|
|
if (!he || msgb_l2len(msg) < sizeof(*he))
|
|
|
|
goto invalid;
|
|
|
|
msg->l2h = &he->data[0];
|
|
|
|
switch (he->proto) {
|
|
|
|
case IPAC_PROTO_EXT_RSPRO:
|
|
|
|
printf("Received RSPRO %s\n", msgb_hexdump(msg));
|
|
|
|
rc = bankd_handle_msg(bc, msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-17 20:10:49 +00:00
|
|
|
goto invalid;
|
2019-03-08 18:18:52 +00:00
|
|
|
}
|
2018-08-24 18:37:28 +00:00
|
|
|
|
|
|
|
return rc;
|
2018-08-17 20:10:49 +00:00
|
|
|
|
|
|
|
invalid:
|
|
|
|
msgb_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-08-24 18:37:28 +00:00
|
|
|
static struct bankd_client *g_client;
|
2018-08-17 20:10:49 +00:00
|
|
|
static void *g_tall_ctx;
|
|
|
|
void __thread *talloc_asn1_ctx;
|
2018-10-03 21:34:21 +00:00
|
|
|
int asn_debug;
|
2018-08-17 20:10:49 +00:00
|
|
|
|
2019-03-02 16:02:13 +00:00
|
|
|
/* handle incoming messages from server */
|
|
|
|
static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
|
|
|
|
{
|
|
|
|
RsproPDU_t *resp;
|
|
|
|
|
|
|
|
switch (pdu->msg.present) {
|
|
|
|
case RsproPDUchoice_PR_connectClientRes:
|
|
|
|
/* Store 'identity' of server in srvc->peer_comp_id */
|
|
|
|
rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
|
|
|
|
osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
|
|
|
|
break;
|
|
|
|
case RsproPDUchoice_PR_configClientReq:
|
|
|
|
/* store/set the clientID as instructed by the server */
|
2019-03-08 21:18:31 +00:00
|
|
|
if (!g_client->srv_conn.clslot)
|
|
|
|
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
|
|
|
|
*g_client->srv_conn.clslot = pdu->msg.choice.configClientReq.clientSlot;
|
2019-03-02 16:02:13 +00:00
|
|
|
/* store/set the bankd ip/port as instructed by the server */
|
|
|
|
osmo_talloc_replace_string(g_client, &g_client->bankd_host,
|
|
|
|
rspro_IpAddr2str(&pdu->msg.choice.configClientReq.bankd.ip));
|
|
|
|
g_client->bankd_port = ntohs(pdu->msg.choice.configClientReq.bankd.port);
|
|
|
|
/* instruct bankd FSM to connect */
|
|
|
|
osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
|
|
|
|
/* send response to server */
|
|
|
|
resp = rspro_gen_ConfigClientRes(ResultCode_ok);
|
|
|
|
ipa_client_conn_send_rspro(srvc->conn, resp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-08 21:20:21 +00:00
|
|
|
static void printf_help()
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
" -h --help Print this help message\n"
|
|
|
|
" -i --server-ip A.B.C.D remsim-server IP address\n"
|
|
|
|
" -p --server-port 13245 remsim-server TCP port\n"
|
|
|
|
" -i --client-id <0-65535> RSPRO ClientId of this client\n"
|
|
|
|
" -s --client-slot <0-65535> RSPRO SlotNr of this client\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_options(int argc, char **argv)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "server-ip", 1, 0, 'i' },
|
|
|
|
{ "server-port", 1, 0, 'p' },
|
|
|
|
{ "client-id", 1, 0, 'c' },
|
|
|
|
{ "client-slot", 1, 0, 's' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "hi:p:c:s:",
|
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
printf_help();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
g_client->srv_conn.server_host = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
g_client->srv_conn.server_port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (!g_client->srv_conn.clslot)
|
|
|
|
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
|
|
|
|
g_client->srv_conn.clslot->clientId = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (!g_client->srv_conn.clslot)
|
|
|
|
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
|
|
|
|
g_client->srv_conn.clslot->slotNr = atoi(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 20:10:49 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-03-02 16:02:13 +00:00
|
|
|
struct rspro_server_conn *srvc;
|
|
|
|
int rc;
|
|
|
|
|
2018-08-17 20:10:49 +00:00
|
|
|
g_tall_ctx = talloc_named_const(NULL, 0, "global");
|
|
|
|
|
2019-03-08 19:45:03 +00:00
|
|
|
osmo_init_logging2(g_tall_ctx, &log_info);
|
|
|
|
|
2018-08-24 18:37:28 +00:00
|
|
|
g_client = talloc_zero(g_tall_ctx, struct bankd_client);
|
2019-03-02 16:02:13 +00:00
|
|
|
|
|
|
|
srvc = &g_client->srv_conn;
|
|
|
|
srvc->server_host = "localhost";
|
|
|
|
srvc->server_port = 9998;
|
|
|
|
srvc->handle_rx = srvc_handle_rx;
|
|
|
|
srvc->own_comp_id.type = ComponentType_remsimClient;
|
|
|
|
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
|
|
|
|
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
|
|
|
|
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
|
2019-03-08 21:20:21 +00:00
|
|
|
|
|
|
|
handle_options(argc, argv);
|
|
|
|
|
2019-03-02 16:02:13 +00:00
|
|
|
rc = server_conn_fsm_alloc(g_client, srvc);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-08-17 20:10:49 +00:00
|
|
|
|
2018-10-03 21:34:21 +00:00
|
|
|
asn_debug = 0;
|
2018-08-17 20:10:49 +00:00
|
|
|
|
2018-08-24 18:37:28 +00:00
|
|
|
if (bankd_conn_fsm_alloc(g_client) < 0) {
|
2018-08-17 20:10:49 +00:00
|
|
|
fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
osmo_select_main(0);
|
|
|
|
}
|
|
|
|
}
|