open5gs/lib/gtp/path.c

131 lines
3.4 KiB
C
Raw Normal View History

2019-04-27 14:54:30 +00:00
/*
* Copyright (C) 2019-2024 by Sukchan Lee <acetcom@gmail.com>
2019-04-27 14:54:30 +00:00
*
* This file is part of Open5GS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2017-11-30 11:13:15 +00:00
2019-09-13 12:07:47 +00:00
#include "ogs-gtp.h"
2017-11-30 11:13:15 +00:00
2019-09-13 12:07:47 +00:00
ogs_sock_t *ogs_gtp_server(ogs_socknode_t *node)
2017-11-30 11:13:15 +00:00
{
2019-04-27 14:54:30 +00:00
char buf[OGS_ADDRSTRLEN];
2019-05-31 14:22:22 +00:00
ogs_sock_t *gtp;
ogs_assert(node);
2017-12-02 05:17:32 +00:00
gtp = ogs_udp_server(node->addr, node->option);
if (gtp) {
ogs_info("gtp_server() [%s]:%d",
OGS_ADDR(node->addr, buf), OGS_PORT(node->addr));
node->sock = gtp;
}
2017-11-30 11:13:15 +00:00
2019-05-31 14:22:22 +00:00
return gtp;
2017-12-02 05:17:32 +00:00
}
2019-09-13 12:07:47 +00:00
int ogs_gtp_connect(ogs_sock_t *ipv4, ogs_sock_t *ipv6, ogs_gtp_node_t *gnode)
2018-01-09 07:37:05 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_sockaddr_t *addr;
char buf[OGS_ADDRSTRLEN];
2018-01-09 07:37:05 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(ipv4 || ipv6);
ogs_assert(gnode);
ogs_assert(gnode->sa_list);
2018-01-09 07:37:05 +00:00
addr = gnode->sa_list;
2019-06-08 14:42:12 +00:00
while (addr) {
2019-04-27 14:54:30 +00:00
ogs_sock_t *sock = NULL;
2018-01-09 07:37:05 +00:00
if (addr->ogs_sa_family == AF_INET)
sock = ipv4;
else if (addr->ogs_sa_family == AF_INET6)
sock = ipv6;
2018-01-09 07:37:05 +00:00
else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2018-01-09 07:37:05 +00:00
2019-06-08 14:42:12 +00:00
if (sock) {
2019-04-27 14:54:30 +00:00
ogs_info("gtp_connect() [%s]:%d",
OGS_ADDR(addr, buf), OGS_PORT(addr));
gnode->sock = sock;
2020-04-26 19:36:05 +00:00
memcpy(&gnode->addr, addr, sizeof gnode->addr);
2019-04-27 14:54:30 +00:00
break;
2018-01-09 07:37:05 +00:00
}
addr = addr->next;
}
2019-06-08 14:42:12 +00:00
if (addr == NULL) {
2019-04-27 14:54:30 +00:00
ogs_log_message(OGS_LOG_WARN, ogs_socket_errno,
"gtp_connect() [%s]:%d failed",
OGS_ADDR(gnode->sa_list, buf), OGS_PORT(gnode->sa_list));
2019-04-27 14:54:30 +00:00
return OGS_ERROR;
2018-01-09 07:37:05 +00:00
}
2019-04-27 14:54:30 +00:00
return OGS_OK;
2018-01-09 07:37:05 +00:00
}
2019-09-13 12:07:47 +00:00
int ogs_gtp_send(ogs_gtp_node_t *gnode, ogs_pkbuf_t *pkbuf)
2017-03-23 11:57:55 +00:00
{
ssize_t sent;
2019-04-27 14:54:30 +00:00
ogs_sock_t *sock = NULL;
2017-03-23 11:57:55 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(gnode);
ogs_assert(pkbuf);
sock = gnode->sock;
2019-04-27 14:54:30 +00:00
ogs_assert(sock);
sent = ogs_send(sock->fd, pkbuf->data, pkbuf->len, 0);
2019-06-08 14:42:12 +00:00
if (sent < 0 || sent != pkbuf->len) {
if (ogs_socket_errno != OGS_EAGAIN) {
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"ogs_gtp_send() failed");
}
2019-04-27 14:54:30 +00:00
return OGS_ERROR;
}
return OGS_OK;
}
2019-09-13 12:07:47 +00:00
int ogs_gtp_sendto(ogs_gtp_node_t *gnode, ogs_pkbuf_t *pkbuf)
2019-04-27 14:54:30 +00:00
{
ssize_t sent;
ogs_sock_t *sock = NULL;
ogs_sockaddr_t *addr = NULL;
2017-03-23 11:57:55 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(gnode);
ogs_assert(pkbuf);
2017-12-07 04:47:07 +00:00
sock = gnode->sock;
2019-04-27 14:54:30 +00:00
ogs_assert(sock);
2020-04-26 19:36:05 +00:00
addr = &gnode->addr;
ogs_assert(addr);
2017-12-07 04:47:07 +00:00
sent = ogs_sendto(sock->fd, pkbuf->data, pkbuf->len, 0, addr);
2019-06-08 14:42:12 +00:00
if (sent < 0 || sent != pkbuf->len) {
if (ogs_socket_errno != OGS_EAGAIN) {
char buf[OGS_ADDRSTRLEN];
int err = ogs_socket_errno;
ogs_log_message(OGS_LOG_ERROR, err,
"ogs_gtp_sendto(%u, %p, %u, 0, %s:%u) failed",
sock->fd, pkbuf->data, pkbuf->len,
OGS_ADDR(addr, buf), OGS_PORT(addr));
}
2019-04-27 14:54:30 +00:00
return OGS_ERROR;
2017-03-23 11:57:55 +00:00
}
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-03-23 11:57:55 +00:00
}