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