Jacob Erlbeck | bb23dc1 | 2014-12-18 12:28:21 +0100 | [diff] [blame] | 1 | /* GPRS Subscriber Update Protocol client */ |
| 2 | |
| 3 | /* (C) 2014 by Sysmocom s.f.m.c. GmbH |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Jacob Erlbeck |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/gprs_gsup_client.h> |
| 24 | |
| 25 | #include <osmocom/abis/ipa.h> |
| 26 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 27 | #include <osmocom/core/msgb.h> |
| 28 | |
| 29 | #include <openbsc/debug.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | |
| 33 | extern void *tall_bsc_ctx; |
| 34 | |
| 35 | static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) |
| 36 | { |
| 37 | LOGP(DGPRS, LOGL_NOTICE, "GSUP link to %s:%d %s\n", |
| 38 | link->addr, link->port, up ? "UP" : "DOWN"); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | static int gsup_client_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
| 43 | { |
| 44 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 45 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 46 | struct gprs_gsup_client *gsupc = (struct gprs_gsup_client *)link->data; |
| 47 | |
| 48 | if (hh->proto != IPAC_PROTO_OSMO) |
| 49 | goto invalid; |
| 50 | |
| 51 | if (!he || msgb_l2len(msg) < sizeof(*he) || |
| 52 | he->proto != IPAC_PROTO_EXT_GSUP) |
| 53 | goto invalid; |
| 54 | |
| 55 | msg->l2h = &he->data[0]; |
| 56 | |
| 57 | OSMO_ASSERT(gsupc->read_cb != NULL); |
| 58 | gsupc->read_cb(gsupc, msg); |
| 59 | |
| 60 | /* Not freeing msg here, because that must be done by the read_cb. */ |
| 61 | return 0; |
| 62 | |
| 63 | invalid: |
| 64 | LOGP(DGPRS, LOGL_NOTICE, |
| 65 | "GSUP received an invalid IPA message from %s:%d, size = %d\n", |
| 66 | link->addr, link->port, msgb_length(msg)); |
| 67 | |
| 68 | msgb_free(msg); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | struct gprs_gsup_client *gprs_gsup_client_create(const char *ip_addr, |
| 73 | unsigned int tcp_port, |
| 74 | gprs_gsup_read_cb_t read_cb) |
| 75 | { |
| 76 | struct gprs_gsup_client *gsupc; |
| 77 | int rc; |
| 78 | |
| 79 | gsupc = talloc_zero(tall_bsc_ctx, struct gprs_gsup_client); |
| 80 | OSMO_ASSERT(gsupc); |
| 81 | |
| 82 | gsupc->link = ipa_client_conn_create(gsupc, |
| 83 | /* no e1inp */ NULL, |
| 84 | 0, |
| 85 | ip_addr, tcp_port, |
| 86 | gsup_client_updown_cb, |
| 87 | gsup_client_read_cb, |
| 88 | /* default write_cb */ NULL, |
| 89 | gsupc); |
| 90 | if (!gsupc->link) |
| 91 | goto failed; |
| 92 | |
| 93 | rc = ipa_client_conn_open(gsupc->link); |
| 94 | |
| 95 | if (rc < 0 && rc != -EINPROGRESS) { |
| 96 | LOGP(DGPRS, LOGL_NOTICE, "GSUP failed to connect to %s:%d\n", |
| 97 | ip_addr, tcp_port); |
| 98 | goto failed; |
| 99 | } |
| 100 | |
| 101 | gsupc->read_cb = read_cb; |
| 102 | |
| 103 | return gsupc; |
| 104 | |
| 105 | failed: |
| 106 | talloc_free(gsupc); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | void gprs_gsup_client_destroy(struct gprs_gsup_client *gsupc) |
| 111 | { |
| 112 | ipa_client_conn_destroy(gsupc->link); |
| 113 | } |
| 114 | |
| 115 | int gprs_gsup_client_send(struct gprs_gsup_client *gsupc, struct msgb *msg) |
| 116 | { |
| 117 | if (!gsupc) { |
| 118 | msgb_free(msg); |
| 119 | return -ENOTCONN; |
| 120 | } |
| 121 | |
| 122 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_GSUP); |
| 123 | ipa_msg_push_header(msg, IPAC_PROTO_OSMO); |
| 124 | ipa_client_conn_send(gsupc->link, msg); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | struct msgb *gprs_gsup_msgb_alloc(void) |
| 130 | { |
| 131 | return msgb_alloc_headroom(4000, 64, __func__); |
| 132 | } |