Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 1 | |
| 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 Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 17 | #include "client.h" |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 18 | |
Harald Welte | f29e0d0 | 2018-08-24 21:42:22 +0200 | [diff] [blame] | 19 | static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 20 | { |
| 21 | ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO); |
| 22 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
Harald Welte | f29e0d0 | 2018-08-24 21:42:22 +0200 | [diff] [blame] | 23 | ipa_client_conn_send(ipa, msg_tx); |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 24 | /* msg_tx is now queued and will be freed. */ |
| 25 | } |
| 26 | |
Harald Welte | f29e0d0 | 2018-08-24 21:42:22 +0200 | [diff] [blame] | 27 | void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 28 | { |
| 29 | struct msgb *msg = rspro_enc_msg(rspro); |
| 30 | OSMO_ASSERT(msg); |
Harald Welte | f29e0d0 | 2018-08-24 21:42:22 +0200 | [diff] [blame] | 31 | push_and_send(ipa, msg); |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 34 | static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 35 | { |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 36 | RsproPDU_t *pdu = rspro_dec_msg(msg); |
| 37 | if (!pdu) { |
| 38 | fprintf(stderr, "Error decoding PDU\n"); |
| 39 | return -1; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 40 | } |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 41 | |
| 42 | switch (pdu->msg.present) { |
| 43 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | 417b961 | 2018-09-24 14:53:41 +0200 | [diff] [blame] | 44 | osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 45 | break; |
| 46 | default: |
| 47 | fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg)); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | return 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 54 | int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 55 | { |
| 56 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 57 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 58 | struct bankd_client *bc = conn->data; |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 59 | int rc; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 60 | |
| 61 | if (msgb_length(msg) < sizeof(*hh)) |
| 62 | goto invalid; |
| 63 | msg->l2h = &hh->data[0]; |
| 64 | if (hh->proto != IPAC_PROTO_OSMO) |
| 65 | goto invalid; |
| 66 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
| 67 | goto invalid; |
| 68 | msg->l2h = &he->data[0]; |
| 69 | |
| 70 | if (he->proto != IPAC_PROTO_EXT_RSPRO) |
| 71 | goto invalid; |
| 72 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 73 | printf("Received RSPRO %s\n", msgb_hexdump(msg)); |
| 74 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 75 | rc = bankd_handle_msg(bc, msg); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 76 | |
| 77 | return rc; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 78 | |
| 79 | invalid: |
| 80 | msgb_free(msg); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | static const struct log_info_cat default_categories[] = { |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 85 | [DMAIN] = { |
| 86 | .name = "DMAIN", |
| 87 | .loglevel = LOGL_DEBUG, |
| 88 | .enabled = 1, |
| 89 | }, |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | static const struct log_info log_info = { |
| 93 | .cat = default_categories, |
| 94 | .num_cat = ARRAY_SIZE(default_categories), |
| 95 | }; |
| 96 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 97 | static struct bankd_client *g_client; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 98 | static void *g_tall_ctx; |
| 99 | void __thread *talloc_asn1_ctx; |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 100 | int asn_debug; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 101 | |
| 102 | int main(int argc, char **argv) |
| 103 | { |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 104 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 105 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 106 | osmo_fsm_register(&remsim_client_bankd_fsm); |
| 107 | osmo_fsm_register(&remsim_client_server_fsm); |
| 108 | |
| 109 | g_client = talloc_zero(g_tall_ctx, struct bankd_client); |
| 110 | g_client->bankd_host = "localhost"; |
| 111 | g_client->bankd_port = 9999; |
| 112 | g_client->own_comp_id.type = ComponentType_remsimClient; |
| 113 | OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name"); |
| 114 | OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client"); |
| 115 | OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION); |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 116 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 117 | asn_debug = 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 118 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 119 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 120 | if (bankd_conn_fsm_alloc(g_client) < 0) { |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 121 | fprintf(stderr, "Unable to connect: %s\n", strerror(errno)); |
| 122 | exit(1); |
| 123 | } |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 124 | |
| 125 | while (1) { |
| 126 | osmo_select_main(0); |
| 127 | } |
| 128 | } |