blob: 6f299d2d6ada67127a644c095bea7adfb7b0f81c [file] [log] [blame]
Neels Hofmeyr996ec1d2015-12-02 15:43:10 +01001/* GTP Hub Implementation */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * gtphub_sock.c.
7 *
8 * This file is kept separate so that these functions can be wrapped for
9 * gtphub_test.c. When a function and its callers are in the same compilational
10 * unit, the wrappability may be optimized away.
11 *
12 * Author: Neels Hofmeyr
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Affero General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Affero General Public License for more details.
23 *
24 * You should have received a copy of the GNU Affero General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27
Pau Espin Pedrol6aad14c2023-01-09 13:27:24 +010028#include <errno.h>
29#include <string.h>
30
31#include <osmocom/core/utils.h>
32
Pau Espin Pedrolf2307c42023-01-09 11:55:18 +010033#include <osmocom/gtphub/gtphub.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020034#include <osmocom/sgsn/debug.h>
Neels Hofmeyr996ec1d2015-12-02 15:43:10 +010035
36/* Convenience makro, note: only within this C file. */
37#define LOG(level, fmt, args...) \
38 LOGP(DGTPHUB, level, fmt, ##args)
39
40int gtphub_write(const struct osmo_fd *to,
Alexander Couzensdd930a22020-07-18 16:45:25 +020041 const struct sgsn_sockaddr *to_addr,
Neels Hofmeyr996ec1d2015-12-02 15:43:10 +010042 const uint8_t *buf, size_t buf_len)
43{
44 errno = 0;
45 ssize_t sent = sendto(to->fd, buf, buf_len, 0,
46 (struct sockaddr*)&to_addr->a, to_addr->l);
Alexander Couzensdd930a22020-07-18 16:45:25 +020047 LOG(LOGL_DEBUG, "to %s\n", sgsn_sockaddr_to_str(to_addr));
Neels Hofmeyr996ec1d2015-12-02 15:43:10 +010048
49 if (sent == -1) {
50 LOG(LOGL_ERROR, "error: %s\n", strerror(errno));
51 return -EINVAL;
52 }
53
54 if (sent != buf_len)
55 LOG(LOGL_ERROR, "sent(%d) != data_len(%d)\n",
56 (int)sent, (int)buf_len);
57 else
Neels Hofmeyr36948bf2015-12-07 13:36:47 +010058 LOG(LOGL_DEBUG, "Sent %d: %s%s\n",
59 (int)sent,
60 osmo_hexdump(buf, sent > 1000? 1000 : sent),
61 sent > 1000 ? "..." : "");
Neels Hofmeyr996ec1d2015-12-02 15:43:10 +010062
63 return 0;
64}
65