File size: 3,848 Bytes
258fb37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
/*
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
*
* 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/>.
*/
#include "ogs-gtp.h"
ogs_sock_t *ogs_gtp_server(ogs_socknode_t *node)
{
char buf[OGS_ADDRSTRLEN];
ogs_sock_t *gtp;
ogs_assert(node);
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;
}
return gtp;
}
int ogs_gtp_connect(ogs_sock_t *ipv4, ogs_sock_t *ipv6, ogs_gtp_node_t *gnode)
{
ogs_sockaddr_t *addr;
char buf[OGS_ADDRSTRLEN];
ogs_assert(ipv4 || ipv6);
ogs_assert(gnode);
ogs_assert(gnode->sa_list);
addr = gnode->sa_list;
while (addr) {
ogs_sock_t *sock = NULL;
if (addr->ogs_sa_family == AF_INET)
sock = ipv4;
else if (addr->ogs_sa_family == AF_INET6)
sock = ipv6;
else
ogs_assert_if_reached();
if (sock) {
ogs_info("gtp_connect() [%s]:%d",
OGS_ADDR(addr, buf), OGS_PORT(addr));
gnode->sock = sock;
memcpy(&gnode->addr, addr, sizeof gnode->addr);
break;
}
addr = addr->next;
}
if (addr == NULL) {
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));
return OGS_ERROR;
}
return OGS_OK;
}
int ogs_gtp_send(ogs_gtp_node_t *gnode, ogs_pkbuf_t *pkbuf)
{
ssize_t sent;
ogs_sock_t *sock = NULL;
ogs_assert(gnode);
ogs_assert(pkbuf);
sock = gnode->sock;
ogs_assert(sock);
sent = ogs_send(sock->fd, pkbuf->data, pkbuf->len, 0);
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");
}
return OGS_ERROR;
}
return OGS_OK;
}
int ogs_gtp_sendto(ogs_gtp_node_t *gnode, ogs_pkbuf_t *pkbuf)
{
ssize_t sent;
ogs_sock_t *sock = NULL;
ogs_sockaddr_t *addr = NULL;
ogs_assert(gnode);
ogs_assert(pkbuf);
sock = gnode->sock;
ogs_assert(sock);
addr = &gnode->addr;
ogs_assert(addr);
sent = ogs_sendto(sock->fd, pkbuf->data, pkbuf->len, 0, addr);
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));
}
return OGS_ERROR;
}
return OGS_OK;
}
void ogs_gtp_send_error_message(
ogs_gtp_xact_t *xact, uint32_t teid, uint8_t type, uint8_t cause_value)
{
switch (xact->gtp_version) {
case 1:
ogs_gtp1_send_error_message(xact, teid, type, cause_value);
break;
case 2:
ogs_gtp2_send_error_message(xact, teid, type, cause_value);
break;
}
}
|