Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 1 | /* Generic Subscriber Update Protocol client */ |
| 2 | |
| 3 | /* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Jacob Erlbeck |
| 7 | * Author: Neels Hofmeyr |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0da9f2f | 2018-08-13 20:42:36 +0200 | [diff] [blame] | 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0da9f2f | 2018-08-13 20:42:36 +0200 | [diff] [blame] | 17 | * GNU General Public License for more details. |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 18 | * |
Harald Welte | 0da9f2f | 2018-08-13 20:42:36 +0200 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/gsupclient/gsup_client.h> |
| 25 | |
| 26 | #include <osmocom/abis/ipa.h> |
| 27 | #include <osmocom/gsm/oap_client.h> |
| 28 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 29 | #include <osmocom/core/msgb.h> |
| 30 | #include <osmocom/core/logging.h> |
| 31 | |
| 32 | #include <errno.h> |
| 33 | #include <string.h> |
| 34 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 35 | static void start_test_procedure(struct osmo_gsup_client *gsupc); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 36 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 37 | static void gsup_client_send_ping(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 38 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 39 | struct msgb *msg = osmo_gsup_client_msgb_alloc(); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 40 | |
| 41 | msg->l2h = msgb_put(msg, 1); |
| 42 | msg->l2h[0] = IPAC_MSGT_PING; |
| 43 | ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS); |
| 44 | ipa_client_conn_send(gsupc->link, msg); |
| 45 | } |
| 46 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 47 | static int gsup_client_connect(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 48 | { |
| 49 | int rc; |
| 50 | |
| 51 | if (gsupc->is_connected) |
| 52 | return 0; |
| 53 | |
| 54 | if (osmo_timer_pending(&gsupc->connect_timer)) { |
| 55 | LOGP(DLGSUP, LOGL_DEBUG, |
| 56 | "GSUP connect: connect timer already running\n"); |
| 57 | osmo_timer_del(&gsupc->connect_timer); |
| 58 | } |
| 59 | |
| 60 | if (osmo_timer_pending(&gsupc->ping_timer)) { |
| 61 | LOGP(DLGSUP, LOGL_DEBUG, |
| 62 | "GSUP connect: ping timer already running\n"); |
| 63 | osmo_timer_del(&gsupc->ping_timer); |
| 64 | } |
| 65 | |
| 66 | if (ipa_client_conn_clear_queue(gsupc->link) > 0) |
| 67 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); |
| 68 | |
| 69 | rc = ipa_client_conn_open(gsupc->link); |
| 70 | |
| 71 | if (rc >= 0) { |
| 72 | LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n", |
| 73 | gsupc->link->addr, gsupc->link->port); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n", |
| 78 | gsupc->link->addr, gsupc->link->port, strerror(-rc)); |
| 79 | |
| 80 | if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || |
| 81 | rc == -EINVAL) |
| 82 | return rc; |
| 83 | |
| 84 | osmo_timer_schedule(&gsupc->connect_timer, |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 85 | OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 86 | |
| 87 | LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n", |
| 88 | gsupc->link->addr, gsupc->link->port); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void connect_timer_cb(void *gsupc_) |
| 94 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 95 | struct osmo_gsup_client *gsupc = gsupc_; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 96 | |
| 97 | if (gsupc->is_connected) |
| 98 | return; |
| 99 | |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 100 | if (gsupc->up_down_cb) { |
| 101 | /* When the up_down_cb() returns false, the user asks us not to retry connecting. */ |
| 102 | if (!gsupc->up_down_cb(gsupc, false)) |
| 103 | return; |
| 104 | } |
| 105 | |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 106 | gsup_client_connect(gsupc); |
| 107 | } |
| 108 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 109 | static void client_send(struct osmo_gsup_client *gsupc, int proto_ext, |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 110 | struct msgb *msg_tx) |
| 111 | { |
| 112 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 113 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 114 | ipa_client_conn_send(gsupc->link, msg_tx); |
| 115 | /* msg_tx is now queued and will be freed. */ |
| 116 | } |
| 117 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 118 | static void gsup_client_oap_register(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 119 | { |
| 120 | struct msgb *msg_tx; |
| 121 | int rc; |
| 122 | rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx); |
| 123 | |
| 124 | if ((rc < 0) || (!msg_tx)) { |
| 125 | LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
| 130 | } |
| 131 | |
| 132 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 133 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 134 | struct osmo_gsup_client *gsupc = link->data; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 135 | |
| 136 | LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n", |
| 137 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 138 | |
| 139 | gsupc->is_connected = up; |
| 140 | |
| 141 | if (up) { |
| 142 | start_test_procedure(gsupc); |
| 143 | |
| 144 | if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED) |
| 145 | gsup_client_oap_register(gsupc); |
| 146 | |
| 147 | osmo_timer_del(&gsupc->connect_timer); |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 148 | |
| 149 | if (gsupc->up_down_cb) |
| 150 | gsupc->up_down_cb(gsupc, true); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 151 | } else { |
| 152 | osmo_timer_del(&gsupc->ping_timer); |
| 153 | |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 154 | if (gsupc->up_down_cb) { |
| 155 | /* When the up_down_cb() returns false, the user asks us not to retry connecting. */ |
| 156 | if (!gsupc->up_down_cb(gsupc, false)) |
| 157 | return; |
| 158 | } |
| 159 | |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 160 | osmo_timer_schedule(&gsupc->connect_timer, |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 161 | OSMO_GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 165 | static int gsup_client_oap_handle(struct osmo_gsup_client *gsupc, struct msgb *msg_rx) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 166 | { |
| 167 | int rc; |
| 168 | struct msgb *msg_tx; |
| 169 | |
| 170 | /* If the oap_state is disabled, this will reject the messages. */ |
| 171 | rc = osmo_oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); |
| 172 | msgb_free(msg_rx); |
| 173 | if (rc < 0) |
| 174 | return rc; |
| 175 | |
| 176 | if (msg_tx) |
| 177 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 183 | { |
| 184 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 185 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 186 | struct osmo_gsup_client *gsupc = (struct osmo_gsup_client *)link->data; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 187 | int rc; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 188 | |
Stefan Sperling | 55f5efa | 2018-12-04 11:40:30 +0100 | [diff] [blame] | 189 | OSMO_ASSERT(gsupc->unit_name); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 190 | |
| 191 | msg->l2h = &hh->data[0]; |
| 192 | |
Stefan Sperling | 55f5efa | 2018-12-04 11:40:30 +0100 | [diff] [blame] | 193 | rc = ipaccess_bts_handle_ccm(link, gsupc->ipa_dev, msg); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 194 | |
| 195 | if (rc < 0) { |
| 196 | LOGP(DLGSUP, LOGL_NOTICE, |
| 197 | "GSUP received an invalid IPA/CCM message from %s:%d\n", |
| 198 | link->addr, link->port); |
| 199 | /* Link has been closed */ |
| 200 | gsupc->is_connected = 0; |
| 201 | msgb_free(msg); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | if (rc == 1) { |
| 206 | uint8_t msg_type = *(msg->l2h); |
| 207 | /* CCM message */ |
| 208 | if (msg_type == IPAC_MSGT_PONG) { |
| 209 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n"); |
| 210 | gsupc->got_ipa_pong = 1; |
| 211 | } |
| 212 | |
| 213 | msgb_free(msg); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | if (hh->proto != IPAC_PROTO_OSMO) |
| 218 | goto invalid; |
| 219 | |
| 220 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
| 221 | goto invalid; |
| 222 | |
| 223 | msg->l2h = &he->data[0]; |
| 224 | |
| 225 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 226 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 227 | gsupc->read_cb(gsupc, msg); |
| 228 | /* expecting read_cb() to free msg */ |
| 229 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 230 | return gsup_client_oap_handle(gsupc, msg); |
| 231 | /* gsup_client_oap_handle frees msg */ |
| 232 | } else |
| 233 | goto invalid; |
| 234 | |
| 235 | return 0; |
| 236 | |
| 237 | invalid: |
| 238 | LOGP(DLGSUP, LOGL_NOTICE, |
| 239 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 240 | link->addr, link->port, msgb_length(msg)); |
| 241 | |
| 242 | msgb_free(msg); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | static void ping_timer_cb(void *gsupc_) |
| 247 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 248 | struct osmo_gsup_client *gsupc = gsupc_; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 249 | |
| 250 | LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", |
| 251 | gsupc->is_connected ? "connected" : "not connected", |
| 252 | gsupc->got_ipa_pong ? "got" : "didn't get"); |
| 253 | |
| 254 | if (gsupc->got_ipa_pong) { |
| 255 | start_test_procedure(gsupc); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); |
| 260 | ipa_client_conn_close(gsupc->link); |
| 261 | gsupc->is_connected = 0; |
| 262 | |
| 263 | gsup_client_connect(gsupc); |
| 264 | } |
| 265 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 266 | static void start_test_procedure(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 267 | { |
| 268 | osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc); |
| 269 | |
| 270 | gsupc->got_ipa_pong = 0; |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 271 | osmo_timer_schedule(&gsupc->ping_timer, OSMO_GSUP_CLIENT_PING_INTERVAL, 0); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 272 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n"); |
| 273 | gsup_client_send_ping(gsupc); |
| 274 | } |
| 275 | |
Stefan Sperling | 55f5efa | 2018-12-04 11:40:30 +0100 | [diff] [blame] | 276 | /*! |
| 277 | * Create a gsup client connecting to the specified IP address and TCP port. |
| 278 | * Use the provided ipaccess unit as the client-side identifier; ipa_dev should |
| 279 | * be allocated in talloc_ctx talloc_ctx as well. |
| 280 | * \param[in] talloc_ctx talloc context. |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 281 | * \param[in] config Parameters for setting up the GSUP client. |
| 282 | * \return a GSUP client connection, or NULL on failure. |
Stefan Sperling | 55f5efa | 2018-12-04 11:40:30 +0100 | [diff] [blame] | 283 | */ |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 284 | struct osmo_gsup_client *osmo_gsup_client_create3(void *talloc_ctx, struct osmo_gsup_client_config *config) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 285 | { |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 286 | struct osmo_gsup_client *gsupc; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 287 | int rc; |
| 288 | |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 289 | OSMO_ASSERT(config->ipa_dev->unit_name); |
| 290 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 291 | gsupc = talloc_zero(talloc_ctx, struct osmo_gsup_client); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 292 | OSMO_ASSERT(gsupc); |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 293 | *gsupc = (struct osmo_gsup_client){ |
| 294 | .unit_name = (const char *)config->ipa_dev->unit_name, /* API backwards compat */ |
| 295 | .ipa_dev = config->ipa_dev, |
| 296 | .read_cb = config->read_cb, |
| 297 | .up_down_cb = config->up_down_cb, |
| 298 | .data = config->data, |
| 299 | }; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 300 | |
| 301 | /* a NULL oapc_config will mark oap_state disabled. */ |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 302 | rc = osmo_oap_client_init(config->oapc_config, &gsupc->oap_state); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 303 | if (rc != 0) |
| 304 | goto failed; |
| 305 | |
Neels Hofmeyr | 4fa9653 | 2020-01-29 12:48:16 +0100 | [diff] [blame] | 306 | gsupc->link = ipa_client_conn_create2(gsupc, |
| 307 | /* no e1inp */ NULL, |
| 308 | 0, |
| 309 | /* no specific local IP:port */ NULL, 0, |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 310 | config->ip_addr, config->tcp_port, |
Neels Hofmeyr | 4fa9653 | 2020-01-29 12:48:16 +0100 | [diff] [blame] | 311 | gsup_client_updown_cb, |
| 312 | gsup_client_read_cb, |
| 313 | /* default write_cb */ NULL, |
| 314 | gsupc); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 315 | if (!gsupc->link) |
| 316 | goto failed; |
| 317 | |
| 318 | osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc); |
| 319 | |
| 320 | rc = gsup_client_connect(gsupc); |
| 321 | |
| 322 | if (rc < 0) |
| 323 | goto failed; |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 324 | return gsupc; |
| 325 | |
| 326 | failed: |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 327 | osmo_gsup_client_destroy(gsupc); |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
| 330 | |
Neels Hofmeyr | 939f508 | 2019-11-25 03:59:44 +0100 | [diff] [blame] | 331 | /*! Like osmo_gsup_client_create3() but without the up_down_cb and data arguments, and with the oapc_config argument in |
| 332 | * a different position. |
| 333 | */ |
| 334 | struct osmo_gsup_client *osmo_gsup_client_create2(void *talloc_ctx, |
| 335 | struct ipaccess_unit *ipa_dev, |
| 336 | const char *ip_addr, |
| 337 | unsigned int tcp_port, |
| 338 | osmo_gsup_client_read_cb_t read_cb, |
| 339 | struct osmo_oap_client_config *oapc_config) |
| 340 | { |
| 341 | struct osmo_gsup_client_config cfg = { |
| 342 | .ipa_dev = ipa_dev, |
| 343 | .ip_addr = ip_addr, |
| 344 | .tcp_port = tcp_port, |
| 345 | .oapc_config = oapc_config, |
| 346 | .read_cb = read_cb, |
| 347 | }; |
| 348 | return osmo_gsup_client_create3(talloc_ctx, &cfg); |
| 349 | } |
| 350 | |
Stefan Sperling | 55f5efa | 2018-12-04 11:40:30 +0100 | [diff] [blame] | 351 | /** |
| 352 | * Like osmo_gsup_client_create2() except it expects a unit name instead |
| 353 | * of a full-blown ipacess_unit as the client-side identifier. |
| 354 | */ |
| 355 | struct osmo_gsup_client *osmo_gsup_client_create(void *talloc_ctx, |
| 356 | const char *unit_name, |
| 357 | const char *ip_addr, |
| 358 | unsigned int tcp_port, |
| 359 | osmo_gsup_client_read_cb_t read_cb, |
| 360 | struct osmo_oap_client_config *oapc_config) |
| 361 | { |
| 362 | struct ipaccess_unit *ipa_dev = talloc_zero(talloc_ctx, struct ipaccess_unit); |
| 363 | ipa_dev->unit_name = talloc_strdup(ipa_dev, unit_name); |
| 364 | return osmo_gsup_client_create2(talloc_ctx, ipa_dev, ip_addr, tcp_port, read_cb, oapc_config); |
| 365 | } |
| 366 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 367 | void osmo_gsup_client_destroy(struct osmo_gsup_client *gsupc) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 368 | { |
| 369 | osmo_timer_del(&gsupc->connect_timer); |
| 370 | osmo_timer_del(&gsupc->ping_timer); |
| 371 | |
| 372 | if (gsupc->link) { |
| 373 | ipa_client_conn_close(gsupc->link); |
| 374 | ipa_client_conn_destroy(gsupc->link); |
| 375 | gsupc->link = NULL; |
| 376 | } |
| 377 | talloc_free(gsupc); |
| 378 | } |
| 379 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 380 | int osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 381 | { |
| 382 | if (!gsupc || !gsupc->is_connected) { |
| 383 | LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg)); |
| 384 | msgb_free(msg); |
| 385 | return -ENOTCONN; |
| 386 | } |
| 387 | |
| 388 | client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg); |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Vadim Yanitskiy | df8d454 | 2018-11-29 01:49:57 +0700 | [diff] [blame] | 393 | /*! Encode and send a GSUP message. |
| 394 | * \param[in] gsupc GSUP client. |
| 395 | * \param[in] gsup_msg GSUP message to be sent. |
| 396 | * \returns 0 in case of success, negative on error. |
| 397 | */ |
| 398 | int osmo_gsup_client_enc_send(struct osmo_gsup_client *gsupc, |
| 399 | const struct osmo_gsup_message *gsup_msg) |
| 400 | { |
| 401 | struct msgb *gsup_msgb; |
| 402 | int rc; |
| 403 | |
| 404 | gsup_msgb = osmo_gsup_client_msgb_alloc(); |
| 405 | if (!gsup_msgb) { |
| 406 | LOGP(DLGSUP, LOGL_ERROR, "Couldn't allocate GSUP message\n"); |
| 407 | return -ENOMEM; |
| 408 | } |
| 409 | |
| 410 | rc = osmo_gsup_encode(gsup_msgb, gsup_msg); |
| 411 | if (rc) { |
| 412 | LOGP(DLGSUP, LOGL_ERROR, "Couldn't encode GSUP message\n"); |
| 413 | goto error; |
| 414 | } |
| 415 | |
| 416 | rc = osmo_gsup_client_send(gsupc, gsup_msgb); |
| 417 | if (rc) { |
| 418 | LOGP(DLGSUP, LOGL_ERROR, "Couldn't send GSUP message\n"); |
Neels Hofmeyr | 7d2843d | 2019-10-30 03:57:01 +0100 | [diff] [blame] | 419 | /* Do not free, osmo_gsup_client_send() already has. */ |
| 420 | return rc; |
Vadim Yanitskiy | df8d454 | 2018-11-29 01:49:57 +0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | |
| 425 | error: |
| 426 | talloc_free(gsup_msgb); |
| 427 | return rc; |
| 428 | } |
| 429 | |
Harald Welte | 953d27c | 2018-07-23 11:11:22 +0200 | [diff] [blame] | 430 | struct msgb *osmo_gsup_client_msgb_alloc(void) |
Harald Welte | ec6915a | 2018-07-23 14:25:33 +0200 | [diff] [blame] | 431 | { |
| 432 | return msgb_alloc_headroom(4000, 64, __func__); |
| 433 | } |