blob: 73a17986687eec88037015468a5390e8b7cadca0 [file] [log] [blame]
Harald Welte2ff0ab92018-08-17 22:10:49 +02001
2#include <errno.h>
3#include <string.h>
4
5#include <talloc.h>
6
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/fsm.h>
9#include <osmocom/core/utils.h>
10#include <osmocom/core/logging.h>
11#include <osmocom/core/application.h>
12
13#include <osmocom/abis/ipa.h>
14#include <osmocom/gsm/protocol/ipaccess.h>
15
16#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020017#include "client.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020018
Harald Weltef29e0d02018-08-24 21:42:22 +020019static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
Harald Welte2ff0ab92018-08-17 22:10:49 +020020{
21 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
22 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
Harald Weltef29e0d02018-08-24 21:42:22 +020023 ipa_client_conn_send(ipa, msg_tx);
Harald Welte2ff0ab92018-08-17 22:10:49 +020024 /* msg_tx is now queued and will be freed. */
25}
26
Harald Weltef29e0d02018-08-24 21:42:22 +020027void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
Harald Welte2ff0ab92018-08-17 22:10:49 +020028{
29 struct msgb *msg = rspro_enc_msg(rspro);
30 OSMO_ASSERT(msg);
Harald Weltef29e0d02018-08-24 21:42:22 +020031 push_and_send(ipa, msg);
Harald Welte2ff0ab92018-08-17 22:10:49 +020032}
33
Harald Welte24173fb2018-08-24 20:37:28 +020034static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020035{
Harald Welted08d5052018-10-03 20:43:31 +020036 printf("Decoding RSPRO %s\n", msgb_hexdump(msg));
Harald Welte24173fb2018-08-24 20:37:28 +020037 RsproPDU_t *pdu = rspro_dec_msg(msg);
38 if (!pdu) {
39 fprintf(stderr, "Error decoding PDU\n");
40 return -1;
Harald Welte2ff0ab92018-08-17 22:10:49 +020041 }
Harald Welte24173fb2018-08-24 20:37:28 +020042
43 switch (pdu->msg.present) {
44 case RsproPDUchoice_PR_connectClientRes:
Harald Welte417b9612018-09-24 14:53:41 +020045 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020046 break;
47 default:
48 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
49 return -1;
50 }
51
52 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020053}
54
Harald Welte24173fb2018-08-24 20:37:28 +020055int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020056{
57 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
58 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
59 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020060 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020061
62 if (msgb_length(msg) < sizeof(*hh))
63 goto invalid;
64 msg->l2h = &hh->data[0];
65 if (hh->proto != IPAC_PROTO_OSMO)
66 goto invalid;
67 if (!he || msgb_l2len(msg) < sizeof(*he))
68 goto invalid;
69 msg->l2h = &he->data[0];
70
71 if (he->proto != IPAC_PROTO_EXT_RSPRO)
72 goto invalid;
73
Harald Welte2ff0ab92018-08-17 22:10:49 +020074 printf("Received RSPRO %s\n", msgb_hexdump(msg));
75
Harald Welte24173fb2018-08-24 20:37:28 +020076 rc = bankd_handle_msg(bc, msg);
Harald Welte24173fb2018-08-24 20:37:28 +020077
78 return rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020079
80invalid:
81 msgb_free(msg);
82 return -1;
83}
84
85static const struct log_info_cat default_categories[] = {
Harald Welte24173fb2018-08-24 20:37:28 +020086 [DMAIN] = {
87 .name = "DMAIN",
88 .loglevel = LOGL_DEBUG,
89 .enabled = 1,
90 },
Harald Welte2ff0ab92018-08-17 22:10:49 +020091};
92
93static const struct log_info log_info = {
94 .cat = default_categories,
95 .num_cat = ARRAY_SIZE(default_categories),
96};
97
Harald Welte24173fb2018-08-24 20:37:28 +020098static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +020099static void *g_tall_ctx;
100void __thread *talloc_asn1_ctx;
101extern int asn_debug;
102
103int main(int argc, char **argv)
104{
Harald Welte2ff0ab92018-08-17 22:10:49 +0200105 g_tall_ctx = talloc_named_const(NULL, 0, "global");
106
Harald Welte24173fb2018-08-24 20:37:28 +0200107 osmo_fsm_register(&remsim_client_bankd_fsm);
108 osmo_fsm_register(&remsim_client_server_fsm);
109
110 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
111 g_client->bankd_host = "localhost";
112 g_client->bankd_port = 9999;
113 g_client->own_comp_id.type = ComponentType_remsimClient;
114 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name");
115 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
116 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte2ff0ab92018-08-17 22:10:49 +0200117
118 //asn_debug = 1;
119 osmo_init_logging2(g_tall_ctx, &log_info);
120
Harald Welte24173fb2018-08-24 20:37:28 +0200121 if (bankd_conn_fsm_alloc(g_client) < 0) {
Harald Welte2ff0ab92018-08-17 22:10:49 +0200122 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
123 exit(1);
124 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200125
126 while (1) {
127 osmo_select_main(0);
128 }
129}