Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 1 | /* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 22 | |
| 23 | #include <errno.h> |
| 24 | #include <string.h> |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 25 | #include <signal.h> |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 26 | |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 27 | #define _GNU_SOURCE |
| 28 | #include <getopt.h> |
| 29 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 30 | #include <talloc.h> |
| 31 | |
| 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/fsm.h> |
| 34 | #include <osmocom/core/utils.h> |
| 35 | #include <osmocom/core/logging.h> |
| 36 | #include <osmocom/core/application.h> |
| 37 | |
| 38 | #include <osmocom/abis/ipa.h> |
| 39 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 40 | |
| 41 | #include "rspro_util.h" |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 42 | #include "client.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 43 | #include "debug.h" |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 44 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 45 | static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 46 | { |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 47 | RsproPDU_t *pdu = rspro_dec_msg(msg); |
| 48 | if (!pdu) { |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 49 | LOGPFSML(bc->bankd_fi, LOGL_ERROR, "Error decoding PDU\n"); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 50 | return -1; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 51 | } |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 52 | |
Harald Welte | b49ac9c | 2019-03-09 20:36:07 +0100 | [diff] [blame] | 53 | LOGPFSM(bc->bankd_fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu)); |
| 54 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 55 | switch (pdu->msg.present) { |
| 56 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 57 | /* Store 'identity' of bankd to in peer_comp_id */ |
| 58 | 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] | 59 | 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] | 60 | break; |
| 61 | default: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 62 | LOGPFSML(bc->bankd_fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s: %s\n", |
| 63 | rspro_msgt_name(pdu), msgb_hexdump(msg)); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | return 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 70 | int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 71 | { |
| 72 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 73 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 74 | struct bankd_client *bc = conn->data; |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 75 | int rc; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 76 | |
| 77 | if (msgb_length(msg) < sizeof(*hh)) |
| 78 | goto invalid; |
| 79 | msg->l2h = &hh->data[0]; |
Harald Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 80 | switch (hh->proto) { |
Harald Welte | 03b2411 | 2019-03-08 19:43:02 +0100 | [diff] [blame] | 81 | case IPAC_PROTO_IPACCESS: |
| 82 | rc = ipaccess_bts_handle_ccm(conn, &bc->srv_conn.ipa_dev, msg); |
| 83 | if (rc < 0) |
| 84 | break; |
| 85 | switch (hh->data[0]) { |
| 86 | case IPAC_MSGT_PONG: |
| 87 | ipa_keepalive_fsm_pong_received(bc->srv_conn.keepalive_fi); |
| 88 | rc = 0; |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | break; |
Harald Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 94 | case IPAC_PROTO_OSMO: |
| 95 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
| 96 | goto invalid; |
| 97 | msg->l2h = &he->data[0]; |
| 98 | switch (he->proto) { |
| 99 | case IPAC_PROTO_EXT_RSPRO: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 100 | LOGPFSML(bc->bankd_fi, LOGL_DEBUG, "Received RSPRO %s\n", msgb_hexdump(msg)); |
Harald Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 101 | rc = bankd_handle_msg(bc, msg); |
| 102 | break; |
| 103 | default: |
| 104 | goto invalid; |
| 105 | } |
| 106 | break; |
| 107 | default: |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 108 | goto invalid; |
Harald Welte | 1a17104 | 2019-03-08 19:18:52 +0100 | [diff] [blame] | 109 | } |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 110 | |
Harald Welte | 573a5b9 | 2019-09-12 20:27:58 +0200 | [diff] [blame] | 111 | msgb_free(msg); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 112 | return rc; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 113 | |
| 114 | invalid: |
| 115 | msgb_free(msg); |
| 116 | return -1; |
| 117 | } |
| 118 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 119 | static struct bankd_client *g_client; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 120 | static void *g_tall_ctx; |
| 121 | void __thread *talloc_asn1_ctx; |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 122 | int asn_debug; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 123 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 124 | /* handle incoming messages from server */ |
| 125 | static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 126 | { |
| 127 | RsproPDU_t *resp; |
| 128 | |
| 129 | switch (pdu->msg.present) { |
| 130 | case RsproPDUchoice_PR_connectClientRes: |
| 131 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 132 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 133 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 134 | break; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 135 | case RsproPDUchoice_PR_configClientIdReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 136 | /* store/set the clientID as instructed by the server */ |
Harald Welte | ec628e9 | 2019-03-08 22:18:31 +0100 | [diff] [blame] | 137 | if (!g_client->srv_conn.clslot) |
| 138 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 139 | *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot; |
| 140 | /* send response to server */ |
| 141 | resp = rspro_gen_ConfigClientIdRes(ResultCode_ok); |
| 142 | server_conn_send_rspro(srvc, resp); |
| 143 | break; |
| 144 | case RsproPDUchoice_PR_configClientBankReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 145 | /* store/set the bankd ip/port as instructed by the server */ |
| 146 | osmo_talloc_replace_string(g_client, &g_client->bankd_host, |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 147 | rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip)); |
Harald Welte | 7a95088 | 2019-03-17 09:35:16 +0100 | [diff] [blame] | 148 | g_client->bankd_port = pdu->msg.choice.configClientBankReq.bankd.port; |
Harald Welte | 9cf013a | 2019-03-11 22:19:19 +0100 | [diff] [blame] | 149 | rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 150 | /* instruct bankd FSM to connect */ |
| 151 | osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL); |
| 152 | /* send response to server */ |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 153 | resp = rspro_gen_ConfigClientBankRes(ResultCode_ok); |
Harald Welte | a844bb0 | 2019-03-09 13:38:50 +0100 | [diff] [blame] | 154 | server_conn_send_rspro(srvc, resp); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 155 | break; |
| 156 | default: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 157 | LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n", |
| 158 | rspro_msgt_name(pdu)); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 165 | static void handle_sig_usr1(int signal) |
| 166 | { |
| 167 | OSMO_ASSERT(signal == SIGUSR1); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 168 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 171 | static void printf_help() |
| 172 | { |
| 173 | printf( |
| 174 | " -h --help Print this help message\n" |
| 175 | " -i --server-ip A.B.C.D remsim-server IP address\n" |
| 176 | " -p --server-port 13245 remsim-server TCP port\n" |
| 177 | " -i --client-id <0-65535> RSPRO ClientId of this client\n" |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 178 | " -n --client-slot <0-65535> RSPRO SlotNr of this client\n" |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 179 | ); |
| 180 | } |
| 181 | |
| 182 | static void handle_options(int argc, char **argv) |
| 183 | { |
| 184 | while (1) { |
| 185 | int option_index = 0, c; |
| 186 | static const struct option long_options[] = { |
| 187 | { "help", 0, 0, 'h' }, |
| 188 | { "server-ip", 1, 0, 'i' }, |
| 189 | { "server-port", 1, 0, 'p' }, |
| 190 | { "client-id", 1, 0, 'c' }, |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 191 | { "client-slot", 1, 0, 'n' }, |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 192 | { 0, 0, 0, 0 } |
| 193 | }; |
| 194 | |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 195 | c = getopt_long(argc, argv, "hi:p:c:n:", |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 196 | long_options, &option_index); |
| 197 | if (c == -1) |
| 198 | break; |
| 199 | |
| 200 | switch (c) { |
| 201 | case 'h': |
| 202 | printf_help(); |
| 203 | exit(0); |
| 204 | break; |
| 205 | case 'i': |
| 206 | g_client->srv_conn.server_host = optarg; |
| 207 | break; |
| 208 | case 'p': |
| 209 | g_client->srv_conn.server_port = atoi(optarg); |
| 210 | break; |
| 211 | case 'c': |
| 212 | if (!g_client->srv_conn.clslot) |
| 213 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 214 | g_client->srv_conn.clslot->clientId = atoi(optarg); |
| 215 | break; |
| 216 | case 's': |
| 217 | if (!g_client->srv_conn.clslot) |
| 218 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 219 | g_client->srv_conn.clslot->slotNr = atoi(optarg); |
| 220 | break; |
| 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 227 | int main(int argc, char **argv) |
| 228 | { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 229 | struct rspro_server_conn *srvc; |
| 230 | int rc; |
| 231 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 232 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 233 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
Harald Welte | f7442b5 | 2019-07-18 18:54:05 +0200 | [diff] [blame] | 234 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 235 | |
Harald Welte | c7995e7 | 2019-03-08 20:45:03 +0100 | [diff] [blame] | 236 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 237 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 238 | g_client = talloc_zero(g_tall_ctx, struct bankd_client); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 239 | |
| 240 | srvc = &g_client->srv_conn; |
| 241 | srvc->server_host = "localhost"; |
| 242 | srvc->server_port = 9998; |
| 243 | srvc->handle_rx = srvc_handle_rx; |
| 244 | srvc->own_comp_id.type = ComponentType_remsimClient; |
| 245 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name"); |
| 246 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client"); |
| 247 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 248 | |
| 249 | handle_options(argc, argv); |
| 250 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 251 | signal(SIGUSR1, handle_sig_usr1); |
| 252 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 253 | rc = server_conn_fsm_alloc(g_client, srvc); |
| 254 | if (rc < 0) { |
| 255 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 256 | exit(1); |
| 257 | } |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 258 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 259 | asn_debug = 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 260 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 261 | if (bankd_conn_fsm_alloc(g_client) < 0) { |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 262 | fprintf(stderr, "Unable to connect: %s\n", strerror(errno)); |
| 263 | exit(1); |
| 264 | } |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 265 | |
| 266 | while (1) { |
| 267 | osmo_select_main(0); |
| 268 | } |
| 269 | } |