Harald Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 1 | /* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 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 | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 25 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 26 | #include <talloc.h> |
| 27 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 28 | #include <osmocom/core/fsm.h> |
| 29 | #include <osmocom/core/utils.h> |
| 30 | #include <osmocom/core/logging.h> |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 31 | |
| 32 | #include "rspro_util.h" |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 33 | #include "client.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 34 | #include "debug.h" |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 35 | |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 36 | static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 37 | { |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 38 | switch (pdu->msg.present) { |
| 39 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 40 | /* Store 'identity' of bankd to in peer_comp_id */ |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 41 | rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 42 | osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 43 | break; |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 44 | case RsproPDUchoice_PR_tpduCardToModem: |
| 45 | case RsproPDUchoice_PR_setAtrReq: |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 46 | return client_user_bankd_handle_rx(bankdc, pdu); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 47 | default: |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 48 | LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU %s\n", |
| 49 | rspro_msgt_name(pdu)); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 56 | /* handle incoming messages from server */ |
| 57 | static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 58 | { |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 59 | struct bankd_client *bc = srvc2bankd_client(srvc); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 60 | RsproPDU_t *resp; |
| 61 | |
| 62 | switch (pdu->msg.present) { |
| 63 | case RsproPDUchoice_PR_connectClientRes: |
| 64 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 65 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 66 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 67 | break; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 68 | case RsproPDUchoice_PR_configClientIdReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 69 | /* store/set the clientID as instructed by the server */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 70 | if (!srvc->clslot) |
| 71 | srvc->clslot = talloc_zero(srvc, ClientSlot_t); |
| 72 | *srvc->clslot = pdu->msg.choice.configClientIdReq.clientSlot; |
| 73 | if (!bc->bankd_conn.clslot) |
| 74 | bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t); |
| 75 | *bc->bankd_conn.clslot = *bc->srv_conn.clslot; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 76 | /* send response to server */ |
| 77 | resp = rspro_gen_ConfigClientIdRes(ResultCode_ok); |
| 78 | server_conn_send_rspro(srvc, resp); |
| 79 | break; |
| 80 | case RsproPDUchoice_PR_configClientBankReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 81 | /* store/set the bankd ip/port as instructed by the server */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 82 | osmo_talloc_replace_string(bc, &bc->bankd_conn.server_host, |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 83 | rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip)); |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 84 | rspro2bank_slot(&bc->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot); |
| 85 | bc->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port; |
Harald Welte | 1b6696f | 2020-02-16 15:27:15 +0100 | [diff] [blame] | 86 | /* bankd port 0 is a magic value to indicate "no bankd" */ |
| 87 | if (bc->bankd_conn.server_port == 0) |
| 88 | osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_DISCONNECT, NULL); |
| 89 | else |
| 90 | osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_ESTABLISH, NULL); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 91 | /* send response to server */ |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 92 | resp = rspro_gen_ConfigClientBankRes(ResultCode_ok); |
Harald Welte | a844bb0 | 2019-03-09 13:38:50 +0100 | [diff] [blame] | 93 | server_conn_send_rspro(srvc, resp); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 94 | break; |
| 95 | default: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 96 | LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n", |
| 97 | rspro_msgt_name(pdu)); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Harald Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 104 | struct bankd_client *remsim_client_create(void *ctx, const char *name, const char *software) |
| 105 | { |
| 106 | struct bankd_client *bc = talloc_zero(ctx, struct bankd_client); |
| 107 | struct rspro_server_conn *srvc, *bankdc; |
| 108 | int rc; |
| 109 | |
| 110 | if (!bc) |
| 111 | return NULL; |
| 112 | |
| 113 | /* create and [attempt to] establish connection to remsim-server */ |
| 114 | srvc = &bc->srv_conn; |
| 115 | srvc->server_host = "localhost"; |
| 116 | srvc->server_port = 9998; |
| 117 | srvc->handle_rx = srvc_handle_rx; |
| 118 | srvc->own_comp_id.type = ComponentType_remsimClient; |
| 119 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, name); |
| 120 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, software); |
| 121 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
| 122 | |
| 123 | rc = server_conn_fsm_alloc(bc, srvc); |
| 124 | if (rc < 0) { |
| 125 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 126 | exit(1); |
| 127 | } |
| 128 | |
| 129 | bankdc = &bc->bankd_conn; |
| 130 | /* server_host / server_port are configured from remsim-server */ |
| 131 | bankdc->handle_rx = bankd_handle_rx; |
| 132 | memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id)); |
| 133 | rc = server_conn_fsm_alloc(bc, bankdc); |
| 134 | if (rc < 0) { |
| 135 | fprintf(stderr, "Unable to connect bankd conn FSM: %s\n", strerror(errno)); |
| 136 | exit(1); |
| 137 | } |
| 138 | osmo_fsm_inst_update_id(bankdc->fi, "bankd"); |
| 139 | |
| 140 | return bc; |
| 141 | } |
| 142 | |
| 143 | void remsim_client_set_clslot(struct bankd_client *bc, int client_id, int slot_nr) |
| 144 | { |
| 145 | if (!bc->srv_conn.clslot) { |
| 146 | bc->srv_conn.clslot = talloc_zero(bc, ClientSlot_t); |
| 147 | OSMO_ASSERT(bc->srv_conn.clslot); |
| 148 | } |
| 149 | |
| 150 | if (!bc->bankd_conn.clslot) { |
| 151 | bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t); |
| 152 | OSMO_ASSERT(bc->bankd_conn.clslot); |
| 153 | } |
| 154 | |
| 155 | if (client_id >= 0) { |
| 156 | bc->srv_conn.clslot->clientId = client_id; |
| 157 | bc->bankd_conn.clslot->clientId = client_id; |
| 158 | } |
| 159 | |
| 160 | if (slot_nr >= 0) { |
| 161 | bc->srv_conn.clslot->slotNr = slot_nr; |
| 162 | bc->bankd_conn.clslot->slotNr = slot_nr; |
| 163 | } |
| 164 | } |