Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 1 | |
| 2 | #include <errno.h> |
| 3 | #include <string.h> |
| 4 | |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 5 | #define _GNU_SOURCE |
| 6 | #include <getopt.h> |
| 7 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 8 | #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 Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 20 | #include "client.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 21 | #include "debug.h" |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 22 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 23 | static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 24 | { |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 25 | RsproPDU_t *pdu = rspro_dec_msg(msg); |
| 26 | if (!pdu) { |
| 27 | fprintf(stderr, "Error decoding PDU\n"); |
| 28 | return -1; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 29 | } |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 30 | |
| 31 | switch (pdu->msg.present) { |
| 32 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 33 | /* Store 'identity' of bankd to in peer_comp_id */ |
| 34 | rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
Harald Welte | 417b961 | 2018-09-24 14:53:41 +0200 | [diff] [blame] | 35 | 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] | 36 | break; |
| 37 | default: |
| 38 | fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg)); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | return 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 45 | int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 46 | { |
| 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 Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 50 | int rc; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 51 | |
| 52 | if (msgb_length(msg) < sizeof(*hh)) |
| 53 | goto invalid; |
| 54 | msg->l2h = &hh->data[0]; |
Harald Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 55 | switch (hh->proto) { |
Harald Welte | 03b2411 | 2019-03-08 19:43:02 +0100 | [diff] [blame] | 56 | 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 Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 69 | 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 Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 83 | goto invalid; |
Harald Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 84 | } |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 85 | |
| 86 | return rc; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 87 | |
| 88 | invalid: |
| 89 | msgb_free(msg); |
| 90 | return -1; |
| 91 | } |
| 92 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 93 | static struct bankd_client *g_client; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 94 | static void *g_tall_ctx; |
| 95 | void __thread *talloc_asn1_ctx; |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 96 | int asn_debug; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 97 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 98 | /* handle incoming messages from server */ |
| 99 | static 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 Welte | ec628e9 | 2019-03-08 22:18:31 +0100 | [diff] [blame] | 111 | 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 Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 114 | /* 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 Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 132 | static 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 | |
| 143 | static 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 Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 188 | int main(int argc, char **argv) |
| 189 | { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 190 | struct rspro_server_conn *srvc; |
| 191 | int rc; |
| 192 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 193 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 194 | |
Harald Welte | c7995e7 | 2019-03-08 20:45:03 +0100 | [diff] [blame] | 195 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 196 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 197 | g_client = talloc_zero(g_tall_ctx, struct bankd_client); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 198 | |
| 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 Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 207 | |
| 208 | handle_options(argc, argv); |
| 209 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 210 | 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 Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 215 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 216 | asn_debug = 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 217 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 218 | if (bankd_conn_fsm_alloc(g_client) < 0) { |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 219 | fprintf(stderr, "Unable to connect: %s\n", strerror(errno)); |
| 220 | exit(1); |
| 221 | } |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 222 | |
| 223 | while (1) { |
| 224 | osmo_select_main(0); |
| 225 | } |
| 226 | } |