Neels Hofmeyr | 3df5d53 | 2016-12-08 21:17:01 +0100 | [diff] [blame] | 1 | /* Generic Subscriber Update Protocol client */ |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 2 | |
Neels Hofmeyr | 3df5d53 | 2016-12-08 21:17:01 +0100 | [diff] [blame] | 3 | /* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Jacob Erlbeck |
Neels Hofmeyr | 3df5d53 | 2016-12-08 21:17:01 +0100 | [diff] [blame] | 7 | * Author: Neels Hofmeyr |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 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 |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
Neels Hofmeyr | eaaee92 | 2016-12-08 21:22:58 +0100 | [diff] [blame] | 24 | #include <openbsc/gsup_client.h> |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 25 | |
| 26 | #include <osmocom/abis/ipa.h> |
| 27 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 28 | #include <osmocom/core/msgb.h> |
Harald Welte | 4f8e34b | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 29 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 30 | |
| 31 | #include <openbsc/debug.h> |
| 32 | |
| 33 | #include <errno.h> |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 34 | #include <string.h> |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 35 | |
| 36 | extern void *tall_bsc_ctx; |
| 37 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 38 | static void start_test_procedure(struct gsup_client *gsupc); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 39 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 40 | static void gsup_client_send_ping(struct gsup_client *gsupc) |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 41 | { |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 42 | struct msgb *msg = gsup_client_msgb_alloc(); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 43 | |
| 44 | msg->l2h = msgb_put(msg, 1); |
| 45 | msg->l2h[0] = IPAC_MSGT_PING; |
| 46 | ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS); |
| 47 | ipa_client_conn_send(gsupc->link, msg); |
| 48 | } |
| 49 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 50 | static int gsup_client_connect(struct gsup_client *gsupc) |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 51 | { |
| 52 | int rc; |
| 53 | |
| 54 | if (gsupc->is_connected) |
| 55 | return 0; |
| 56 | |
| 57 | if (osmo_timer_pending(&gsupc->connect_timer)) { |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 58 | LOGP(DLGSUP, LOGL_DEBUG, |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 59 | "GSUP connect: connect timer already running\n"); |
| 60 | osmo_timer_del(&gsupc->connect_timer); |
| 61 | } |
| 62 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 63 | if (osmo_timer_pending(&gsupc->ping_timer)) { |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 64 | LOGP(DLGSUP, LOGL_DEBUG, |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 65 | "GSUP connect: ping timer already running\n"); |
| 66 | osmo_timer_del(&gsupc->ping_timer); |
| 67 | } |
| 68 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 69 | if (ipa_client_conn_clear_queue(gsupc->link) > 0) |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 70 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 71 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 72 | rc = ipa_client_conn_open(gsupc->link); |
| 73 | |
Jacob Erlbeck | 69e16b9 | 2014-12-19 19:00:56 +0100 | [diff] [blame] | 74 | if (rc >= 0) { |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 75 | LOGP(DLGSUP, LOGL_INFO, "GSUP connecting to %s:%d\n", |
Jacob Erlbeck | 69e16b9 | 2014-12-19 19:00:56 +0100 | [diff] [blame] | 76 | gsupc->link->addr, gsupc->link->port); |
| 77 | return 0; |
| 78 | } |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 79 | |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 80 | LOGP(DLGSUP, LOGL_INFO, "GSUP failed to connect to %s:%d: %s\n", |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 81 | gsupc->link->addr, gsupc->link->port, strerror(-rc)); |
| 82 | |
| 83 | if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || |
| 84 | rc == -EINVAL) |
| 85 | return rc; |
| 86 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 87 | osmo_timer_schedule(&gsupc->connect_timer, |
| 88 | GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 89 | |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 90 | LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n", |
Jacob Erlbeck | 3718490 | 2015-01-19 10:56:15 +0100 | [diff] [blame] | 91 | gsupc->link->addr, gsupc->link->port); |
| 92 | |
| 93 | return 0; |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void connect_timer_cb(void *gsupc_) |
| 97 | { |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 98 | struct gsup_client *gsupc = gsupc_; |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 99 | |
| 100 | if (gsupc->is_connected) |
| 101 | return; |
| 102 | |
| 103 | gsup_client_connect(gsupc); |
| 104 | } |
| 105 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 106 | static void client_send(struct gsup_client *gsupc, int proto_ext, |
| 107 | struct msgb *msg_tx) |
Neels Hofmeyr | 06cfe00 | 2015-10-01 15:23:51 +0200 | [diff] [blame] | 108 | { |
| 109 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 110 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 111 | ipa_client_conn_send(gsupc->link, msg_tx); |
| 112 | /* msg_tx is now queued and will be freed. */ |
| 113 | } |
| 114 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 115 | static void gsup_client_oap_register(struct gsup_client *gsupc) |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 116 | { |
| 117 | struct msgb *msg_tx; |
| 118 | int rc; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 119 | rc = oap_client_register(&gsupc->oap_state, &msg_tx); |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 120 | |
| 121 | if ((rc < 0) || (!msg_tx)) { |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 122 | LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 126 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 129 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 130 | { |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 131 | struct gsup_client *gsupc = link->data; |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 132 | |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 133 | LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n", |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 134 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 135 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 136 | gsupc->is_connected = up; |
| 137 | |
| 138 | if (up) { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 139 | start_test_procedure(gsupc); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 140 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 141 | if (gsupc->oap_state.state == OAP_INITIALIZED) |
| 142 | gsup_client_oap_register(gsupc); |
| 143 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 144 | osmo_timer_del(&gsupc->connect_timer); |
| 145 | } else { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 146 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 147 | |
| 148 | osmo_timer_schedule(&gsupc->connect_timer, |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 149 | GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 150 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 153 | static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx) |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 154 | { |
| 155 | int rc; |
| 156 | struct msgb *msg_tx; |
| 157 | |
Neels Hofmeyr | 2fa74fa | 2016-12-08 23:12:17 +0100 | [diff] [blame] | 158 | /* If the oap_state is disabled, this will reject the messages. */ |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 159 | rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 160 | msgb_free(msg_rx); |
| 161 | if (rc < 0) |
| 162 | return rc; |
| 163 | |
| 164 | if (msg_tx) |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 165 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 170 | static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 171 | { |
| 172 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 173 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 174 | struct gsup_client *gsupc = (struct gsup_client *)link->data; |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 175 | int rc; |
Neels Hofmeyr | 4c30529 | 2017-03-04 03:15:53 +0100 | [diff] [blame] | 176 | struct ipaccess_unit ipa_dev = { |
| 177 | /* see gsup_client_create() on const vs non-const */ |
| 178 | .unit_name = (char*)gsupc->unit_name, |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 179 | }; |
| 180 | |
Neels Hofmeyr | 4c30529 | 2017-03-04 03:15:53 +0100 | [diff] [blame] | 181 | OSMO_ASSERT(ipa_dev.unit_name); |
| 182 | |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 183 | msg->l2h = &hh->data[0]; |
| 184 | |
| 185 | rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); |
| 186 | |
| 187 | if (rc < 0) { |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 188 | LOGP(DLGSUP, LOGL_NOTICE, |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 189 | "GSUP received an invalid IPA/CCM message from %s:%d\n", |
| 190 | link->addr, link->port); |
| 191 | /* Link has been closed */ |
| 192 | gsupc->is_connected = 0; |
| 193 | msgb_free(msg); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | if (rc == 1) { |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 198 | uint8_t msg_type = *(msg->l2h); |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 199 | /* CCM message */ |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 200 | if (msg_type == IPAC_MSGT_PONG) { |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 201 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n"); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 202 | gsupc->got_ipa_pong = 1; |
| 203 | } |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 204 | |
| 205 | msgb_free(msg); |
| 206 | return 0; |
| 207 | } |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 208 | |
| 209 | if (hh->proto != IPAC_PROTO_OSMO) |
| 210 | goto invalid; |
| 211 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 212 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 213 | goto invalid; |
| 214 | |
| 215 | msg->l2h = &he->data[0]; |
| 216 | |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 217 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 218 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 219 | gsupc->read_cb(gsupc, msg); |
| 220 | /* expecting read_cb() to free msg */ |
| 221 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 222 | return gsup_client_oap_handle(gsupc, msg); |
| 223 | /* gsup_client_oap_handle frees msg */ |
| 224 | } else |
| 225 | goto invalid; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 226 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 227 | return 0; |
| 228 | |
| 229 | invalid: |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 230 | LOGP(DLGSUP, LOGL_NOTICE, |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 231 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 232 | link->addr, link->port, msgb_length(msg)); |
| 233 | |
| 234 | msgb_free(msg); |
| 235 | return -1; |
| 236 | } |
| 237 | |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 238 | static void ping_timer_cb(void *gsupc_) |
| 239 | { |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 240 | struct gsup_client *gsupc = gsupc_; |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 241 | |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 242 | LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 243 | gsupc->is_connected ? "connected" : "not connected", |
| 244 | gsupc->got_ipa_pong ? "got" : "didn't get"); |
| 245 | |
| 246 | if (gsupc->got_ipa_pong) { |
| 247 | start_test_procedure(gsupc); |
| 248 | return; |
| 249 | } |
| 250 | |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 251 | LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 252 | ipa_client_conn_close(gsupc->link); |
| 253 | gsupc->is_connected = 0; |
| 254 | |
| 255 | gsup_client_connect(gsupc); |
| 256 | } |
| 257 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 258 | static void start_test_procedure(struct gsup_client *gsupc) |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 259 | { |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 260 | osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 261 | |
| 262 | gsupc->got_ipa_pong = 0; |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 263 | osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0); |
Neels Hofmeyr | ef02278 | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 264 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n"); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 265 | gsup_client_send_ping(gsupc); |
| 266 | } |
| 267 | |
Neels Hofmeyr | 4c30529 | 2017-03-04 03:15:53 +0100 | [diff] [blame] | 268 | struct gsup_client *gsup_client_create(const char *unit_name, |
| 269 | const char *ip_addr, |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 270 | unsigned int tcp_port, |
| 271 | gsup_client_read_cb_t read_cb, |
Neels Hofmeyr | 37f9252 | 2016-12-08 23:58:31 +0100 | [diff] [blame] | 272 | struct oap_client_config *oapc_config) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 273 | { |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 274 | struct gsup_client *gsupc; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 275 | int rc; |
| 276 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 277 | gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 278 | OSMO_ASSERT(gsupc); |
| 279 | |
Neels Hofmeyr | 4c30529 | 2017-03-04 03:15:53 +0100 | [diff] [blame] | 280 | /* struct ipaccess_unit has a non-const unit_name, so let's copy to be |
| 281 | * able to have a non-const unit_name here as well. To not taint the |
| 282 | * public gsup_client API, let's store it in a const char* anyway. */ |
| 283 | gsupc->unit_name = talloc_strdup(gsupc, unit_name); |
| 284 | OSMO_ASSERT(gsupc->unit_name); |
| 285 | |
Neels Hofmeyr | 37f9252 | 2016-12-08 23:58:31 +0100 | [diff] [blame] | 286 | /* a NULL oapc_config will mark oap_state disabled. */ |
| 287 | rc = oap_client_init(oapc_config, &gsupc->oap_state); |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 288 | if (rc != 0) |
| 289 | goto failed; |
| 290 | |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 291 | gsupc->link = ipa_client_conn_create(gsupc, |
| 292 | /* no e1inp */ NULL, |
| 293 | 0, |
| 294 | ip_addr, tcp_port, |
| 295 | gsup_client_updown_cb, |
| 296 | gsup_client_read_cb, |
| 297 | /* default write_cb */ NULL, |
| 298 | gsupc); |
| 299 | if (!gsupc->link) |
| 300 | goto failed; |
| 301 | |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 302 | osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 303 | |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 304 | rc = gsup_client_connect(gsupc); |
| 305 | |
Jacob Erlbeck | 3ee67ff | 2015-01-26 09:22:39 +0100 | [diff] [blame] | 306 | if (rc < 0) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 307 | goto failed; |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 308 | |
| 309 | gsupc->read_cb = read_cb; |
| 310 | |
| 311 | return gsupc; |
| 312 | |
| 313 | failed: |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 314 | gsup_client_destroy(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 315 | return NULL; |
| 316 | } |
| 317 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 318 | void gsup_client_destroy(struct gsup_client *gsupc) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 319 | { |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 320 | osmo_timer_del(&gsupc->connect_timer); |
Jacob Erlbeck | 03b4630 | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 321 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 849d0a8 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 322 | |
| 323 | if (gsupc->link) { |
| 324 | ipa_client_conn_close(gsupc->link); |
| 325 | ipa_client_conn_destroy(gsupc->link); |
| 326 | gsupc->link = NULL; |
| 327 | } |
| 328 | talloc_free(gsupc); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 331 | int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 332 | { |
| 333 | if (!gsupc) { |
| 334 | msgb_free(msg); |
| 335 | return -ENOTCONN; |
| 336 | } |
| 337 | |
Jacob Erlbeck | 4188c30 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 338 | if (!gsupc->is_connected) { |
| 339 | msgb_free(msg); |
| 340 | return -EAGAIN; |
| 341 | } |
| 342 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 343 | client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg); |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 348 | struct msgb *gsup_client_msgb_alloc(void) |
Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 349 | { |
| 350 | return msgb_alloc_headroom(4000, 64, __func__); |
| 351 | } |