blob: 99fae93cea3c8d85816b5058e66612e8daf770e2 [file] [log] [blame]
Neels Hofmeyr9d307ec2018-05-04 16:06:32 +02001/* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20/* This is kept separate to be able to override the actual sending functions from unit tests. */
21
22#include <errno.h>
23
Neels Hofmeyr2f758032019-11-20 00:37:07 +010024#include <osmocom/hlr/gsup_server.h>
25#include <osmocom/hlr/gsup_router.h>
Neels Hofmeyr9d307ec2018-05-04 16:06:32 +020026
27#include <osmocom/core/logging.h>
28
Oliver Smithf7d32512019-04-08 15:00:43 +020029/*! Send a msgb to a given address using routing.
30 * \param[in] gs gsup server
31 * \param[in] addr IPA name of the client (SGSN, MSC/VLR). Although this is passed like a blob, together with the
32 * length, it must be nul-terminated! This is for legacy reasons, see the discussion here:
33 * https://gerrit.osmocom.org/#/c/osmo-hlr/+/13048/
34 * \param[in] addrlen length of addr, *including the nul-byte* (strlen(addr) + 1).
35 * \param[in] msg message buffer
36 */
Neels Hofmeyr9d307ec2018-05-04 16:06:32 +020037int osmo_gsup_addr_send(struct osmo_gsup_server *gs,
38 const uint8_t *addr, size_t addrlen,
39 struct msgb *msg)
40{
41 struct osmo_gsup_conn *conn;
42
43 conn = gsup_route_find(gs, addr, addrlen);
44 if (!conn) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010045 LOGP(DLGSUP, LOGL_ERROR,
46 "Cannot find route for addr %s\n", osmo_quote_str((const char*)addr, addrlen));
Neels Hofmeyr9d307ec2018-05-04 16:06:32 +020047 msgb_free(msg);
48 return -ENODEV;
49 }
50
51 return osmo_gsup_conn_send(conn, msg);
52}
53
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010054/*! Send a msgb to a given address using routing.
55 * \param[in] gs gsup server
56 * \param[in] ipa_name IPA unit name of the client (SGSN, MSC/VLR, proxy).
57 * \param[in] msg message buffer
58 */
59int osmo_gsup_send_to_ipa_name(struct osmo_gsup_server *gs, const struct osmo_ipa_name *ipa_name, struct msgb *msg)
60{
61 if (ipa_name->val[ipa_name->len - 1]) {
62 /* Is not nul terminated. But for legacy reasons we (still) require that. */
63 if (ipa_name->len >= sizeof(ipa_name->val)) {
64 LOGP(DLGSUP, LOGL_ERROR, "IPA unit name is too long: %s\n",
65 osmo_ipa_name_to_str(ipa_name));
66 return -EINVAL;
67 }
68 struct osmo_ipa_name ipa_name2 = *ipa_name;
69 ipa_name2.val[ipa_name->len] = '\0';
70 ipa_name2.len++;
71 return osmo_gsup_addr_send(gs, ipa_name2.val, ipa_name2.len, msg);
72 }
73 return osmo_gsup_addr_send(gs, ipa_name->val, ipa_name->len, msg);
74}
75
76int osmo_gsup_enc_send_to_ipa_name(struct osmo_gsup_server *gs, const struct osmo_ipa_name *ipa_name,
77 const struct osmo_gsup_message *gsup)
78{
79 struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx");
80 int rc;
81 rc = osmo_gsup_encode(msg, gsup);
82 if (rc) {
83 LOGP(DLGSUP, LOGL_ERROR, "IMSI-%s: Cannot encode GSUP: %s\n",
84 gsup->imsi, osmo_gsup_message_type_name(gsup->message_type));
85 msgb_free(msg);
86 return -EINVAL;
87 }
88
89 LOGP(DLGSUP, LOGL_DEBUG, "IMSI-%s: Tx: %s\n", gsup->imsi, osmo_gsup_message_type_name(gsup->message_type));
90 return osmo_gsup_send_to_ipa_name(gs, ipa_name, msg);
91}