Neels Hofmeyr | afc26a9 | 2016-12-08 21:17:01 +0100 | [diff] [blame] | 1 | /* Generic Subscriber Update Protocol client */ |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 2 | |
Neels Hofmeyr | afc26a9 | 2016-12-08 21:17:01 +0100 | [diff] [blame] | 3 | /* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Jacob Erlbeck |
Neels Hofmeyr | afc26a9 | 2016-12-08 21:17:01 +0100 | [diff] [blame] | 7 | * Author: Neels Hofmeyr |
Jacob Erlbeck | 2b2dc9e | 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 | db1d801 | 2016-12-08 21:22:58 +0100 | [diff] [blame] | 24 | #include <openbsc/gsup_client.h> |
Jacob Erlbeck | 2b2dc9e | 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 | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 29 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 30 | |
| 31 | #include <openbsc/debug.h> |
| 32 | |
| 33 | #include <errno.h> |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 34 | #include <string.h> |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 35 | |
| 36 | extern void *tall_bsc_ctx; |
| 37 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 38 | static void start_test_procedure(struct gsup_client *gsupc); |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 39 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 40 | static void gsup_client_send_ping(struct gsup_client *gsupc) |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 41 | { |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 42 | struct msgb *msg = gsup_client_msgb_alloc(); |
Jacob Erlbeck | a93c91c | 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 | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 50 | static int gsup_client_connect(struct gsup_client *gsupc) |
Jacob Erlbeck | 58f75c2 | 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 | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 58 | LOGP(DLGSUP, LOGL_DEBUG, |
Jacob Erlbeck | 58f75c2 | 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 | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 63 | if (osmo_timer_pending(&gsupc->ping_timer)) { |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 64 | LOGP(DLGSUP, LOGL_DEBUG, |
Jacob Erlbeck | a93c91c | 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 | ad8b7e0 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 69 | if (ipa_client_conn_clear_queue(gsupc->link) > 0) |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 70 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); |
Jacob Erlbeck | ad8b7e0 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 71 | |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 72 | rc = ipa_client_conn_open(gsupc->link); |
| 73 | |
Jacob Erlbeck | 89a3b2b | 2014-12-19 19:00:56 +0100 | [diff] [blame] | 74 | if (rc >= 0) { |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 75 | LOGP(DLGSUP, LOGL_INFO, "GSUP connecting to %s:%d\n", |
Jacob Erlbeck | 89a3b2b | 2014-12-19 19:00:56 +0100 | [diff] [blame] | 76 | gsupc->link->addr, gsupc->link->port); |
| 77 | return 0; |
| 78 | } |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 79 | |
Neels Hofmeyr | 7aef97a | 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 | 58f75c2 | 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 | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 87 | osmo_timer_schedule(&gsupc->connect_timer, |
| 88 | GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 89 | |
Neels Hofmeyr | 7aef97a | 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 | 01c4da8 | 2015-01-19 10:56:15 +0100 | [diff] [blame] | 91 | gsupc->link->addr, gsupc->link->port); |
| 92 | |
| 93 | return 0; |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void connect_timer_cb(void *gsupc_) |
| 97 | { |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 98 | struct gsup_client *gsupc = gsupc_; |
Jacob Erlbeck | 58f75c2 | 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 | 0e5c1e1 | 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 | 427a4af | 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 | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 115 | static void gsup_client_oap_register(struct gsup_client *gsupc) |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 116 | { |
| 117 | struct msgb *msg_tx; |
| 118 | int rc; |
Neels Hofmeyr | 325bdfb | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 119 | rc = oap_client_register(&gsupc->oap_state, &msg_tx); |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 120 | |
| 121 | if ((rc < 0) || (!msg_tx)) { |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 122 | LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 126 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Jacob Erlbeck | 2b2dc9e | 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 | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 131 | struct gsup_client *gsupc = link->data; |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 132 | |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 133 | LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n", |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 134 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 135 | |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 136 | gsupc->is_connected = up; |
| 137 | |
| 138 | if (up) { |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 139 | start_test_procedure(gsupc); |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 140 | |
Neels Hofmeyr | 38b5e27 | 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 | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 144 | osmo_timer_del(&gsupc->connect_timer); |
| 145 | } else { |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 146 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 147 | |
| 148 | osmo_timer_schedule(&gsupc->connect_timer, |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 149 | GSUP_CLIENT_RECONNECT_INTERVAL, 0); |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 150 | } |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Neels Hofmeyr | 0e5c1e1 | 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 | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 154 | { |
| 155 | int rc; |
| 156 | struct msgb *msg_tx; |
| 157 | |
Neels Hofmeyr | e4e1ebd | 2016-12-08 23:12:17 +0100 | [diff] [blame] | 158 | /* If the oap_state is disabled, this will reject the messages. */ |
Neels Hofmeyr | 325bdfb | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 159 | rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); |
Neels Hofmeyr | 38b5e27 | 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 | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 165 | client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Jacob Erlbeck | 2b2dc9e | 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 | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 174 | struct gsup_client *gsupc = (struct gsup_client *)link->data; |
Jacob Erlbeck | edb67a1 | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 175 | int rc; |
| 176 | static struct ipaccess_unit ipa_dev = { |
| 177 | .unit_name = "SGSN" |
| 178 | }; |
| 179 | |
| 180 | msg->l2h = &hh->data[0]; |
| 181 | |
| 182 | rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); |
| 183 | |
| 184 | if (rc < 0) { |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 185 | LOGP(DLGSUP, LOGL_NOTICE, |
Jacob Erlbeck | edb67a1 | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 186 | "GSUP received an invalid IPA/CCM message from %s:%d\n", |
| 187 | link->addr, link->port); |
| 188 | /* Link has been closed */ |
| 189 | gsupc->is_connected = 0; |
| 190 | msgb_free(msg); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | if (rc == 1) { |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 195 | uint8_t msg_type = *(msg->l2h); |
Jacob Erlbeck | edb67a1 | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 196 | /* CCM message */ |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 197 | if (msg_type == IPAC_MSGT_PONG) { |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 198 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n"); |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 199 | gsupc->got_ipa_pong = 1; |
| 200 | } |
Jacob Erlbeck | edb67a1 | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 201 | |
| 202 | msgb_free(msg); |
| 203 | return 0; |
| 204 | } |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 205 | |
| 206 | if (hh->proto != IPAC_PROTO_OSMO) |
| 207 | goto invalid; |
| 208 | |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 209 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 210 | goto invalid; |
| 211 | |
| 212 | msg->l2h = &he->data[0]; |
| 213 | |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 214 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 215 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 216 | gsupc->read_cb(gsupc, msg); |
| 217 | /* expecting read_cb() to free msg */ |
| 218 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 219 | return gsup_client_oap_handle(gsupc, msg); |
| 220 | /* gsup_client_oap_handle frees msg */ |
| 221 | } else |
| 222 | goto invalid; |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 223 | |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 224 | return 0; |
| 225 | |
| 226 | invalid: |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 227 | LOGP(DLGSUP, LOGL_NOTICE, |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 228 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 229 | link->addr, link->port, msgb_length(msg)); |
| 230 | |
| 231 | msgb_free(msg); |
| 232 | return -1; |
| 233 | } |
| 234 | |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 235 | static void ping_timer_cb(void *gsupc_) |
| 236 | { |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 237 | struct gsup_client *gsupc = gsupc_; |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 238 | |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 239 | LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 240 | gsupc->is_connected ? "connected" : "not connected", |
| 241 | gsupc->got_ipa_pong ? "got" : "didn't get"); |
| 242 | |
| 243 | if (gsupc->got_ipa_pong) { |
| 244 | start_test_procedure(gsupc); |
| 245 | return; |
| 246 | } |
| 247 | |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 248 | LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 249 | ipa_client_conn_close(gsupc->link); |
| 250 | gsupc->is_connected = 0; |
| 251 | |
| 252 | gsup_client_connect(gsupc); |
| 253 | } |
| 254 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 255 | static void start_test_procedure(struct gsup_client *gsupc) |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 256 | { |
Pablo Neira Ayuso | 274b384 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 257 | osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc); |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 258 | |
| 259 | gsupc->got_ipa_pong = 0; |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 260 | osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0); |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 261 | LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n"); |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 262 | gsup_client_send_ping(gsupc); |
| 263 | } |
| 264 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 265 | struct gsup_client *gsup_client_create(const char *ip_addr, |
| 266 | unsigned int tcp_port, |
| 267 | gsup_client_read_cb_t read_cb, |
Neels Hofmeyr | f1d15e0 | 2016-12-08 23:58:31 +0100 | [diff] [blame] | 268 | struct oap_client_config *oapc_config) |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 269 | { |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 270 | struct gsup_client *gsupc; |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 271 | int rc; |
| 272 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 273 | gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client); |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 274 | OSMO_ASSERT(gsupc); |
| 275 | |
Neels Hofmeyr | f1d15e0 | 2016-12-08 23:58:31 +0100 | [diff] [blame] | 276 | /* a NULL oapc_config will mark oap_state disabled. */ |
| 277 | rc = oap_client_init(oapc_config, &gsupc->oap_state); |
Neels Hofmeyr | 38b5e27 | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 278 | if (rc != 0) |
| 279 | goto failed; |
| 280 | |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 281 | gsupc->link = ipa_client_conn_create(gsupc, |
| 282 | /* no e1inp */ NULL, |
| 283 | 0, |
| 284 | ip_addr, tcp_port, |
| 285 | gsup_client_updown_cb, |
| 286 | gsup_client_read_cb, |
| 287 | /* default write_cb */ NULL, |
| 288 | gsupc); |
| 289 | if (!gsupc->link) |
| 290 | goto failed; |
| 291 | |
Pablo Neira Ayuso | 274b384 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 292 | osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc); |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 293 | |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 294 | rc = gsup_client_connect(gsupc); |
| 295 | |
Jacob Erlbeck | 4e20fb3 | 2015-01-26 09:22:39 +0100 | [diff] [blame] | 296 | if (rc < 0) |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 297 | goto failed; |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 298 | |
| 299 | gsupc->read_cb = read_cb; |
| 300 | |
| 301 | return gsupc; |
| 302 | |
| 303 | failed: |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 304 | gsup_client_destroy(gsupc); |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 308 | void gsup_client_destroy(struct gsup_client *gsupc) |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 309 | { |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 310 | osmo_timer_del(&gsupc->connect_timer); |
Jacob Erlbeck | a93c91c | 2014-12-19 19:18:54 +0100 | [diff] [blame] | 311 | osmo_timer_del(&gsupc->ping_timer); |
Jacob Erlbeck | 58f75c2 | 2014-12-18 15:00:29 +0100 | [diff] [blame] | 312 | |
| 313 | if (gsupc->link) { |
| 314 | ipa_client_conn_close(gsupc->link); |
| 315 | ipa_client_conn_destroy(gsupc->link); |
| 316 | gsupc->link = NULL; |
| 317 | } |
| 318 | talloc_free(gsupc); |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 321 | int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg) |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 322 | { |
| 323 | if (!gsupc) { |
| 324 | msgb_free(msg); |
| 325 | return -ENOTCONN; |
| 326 | } |
| 327 | |
Jacob Erlbeck | ad8b7e0 | 2014-12-19 18:50:05 +0100 | [diff] [blame] | 328 | if (!gsupc->is_connected) { |
| 329 | msgb_free(msg); |
| 330 | return -EAGAIN; |
| 331 | } |
| 332 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 333 | client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg); |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 338 | struct msgb *gsup_client_msgb_alloc(void) |
Jacob Erlbeck | 2b2dc9e | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 339 | { |
| 340 | return msgb_alloc_headroom(4000, 64, __func__); |
| 341 | } |