File size: 3,937 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 |
/*
* 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-sctp.h"
#include "ogs-app.h"
int __ogs_sctp_domain;
static void sctp_write_callback(short when, ogs_socket_t fd, void *data);
int ogs_sctp_recvdata(ogs_sock_t *sock, void *msg, size_t len,
ogs_sockaddr_t *from, ogs_sctp_info_t *sinfo)
{
int size;
int flags = 0;
do {
size = ogs_sctp_recvmsg(sock, msg, len, from, sinfo, &flags);
if (size < 0 || size >= OGS_MAX_SDU_LEN) {
ogs_error("ogs_sctp_recvmsg(%d) failed(%d:%s)",
size, errno, strerror(errno));
return size;
}
if (flags & MSG_NOTIFICATION) {
/* Nothing */
} else if (flags & MSG_EOR) {
break;
} else {
ogs_fatal("ogs_sctp_recvmsg(%d) failed(%d:%s-0x%x)",
size, errno, strerror(errno), flags);
ogs_fatal("Probably MME might be crashed");
ogs_assert_if_reached();
}
} while(1);
return size;
}
int ogs_sctp_senddata(ogs_sock_t *sock,
ogs_pkbuf_t *pkbuf, ogs_sockaddr_t *addr)
{
int sent;
ogs_assert(sock);
ogs_assert(pkbuf);
sent = ogs_sctp_sendmsg(sock, pkbuf->data, pkbuf->len, addr,
ogs_sctp_ppid_in_pkbuf(pkbuf), ogs_sctp_stream_no_in_pkbuf(pkbuf));
if (sent < 0 || sent != pkbuf->len) {
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"ogs_sctp_senddata(len:%d,ssn:%d)",
pkbuf->len, (int)ogs_sctp_stream_no_in_pkbuf(pkbuf));
ogs_pkbuf_free(pkbuf);
return OGS_ERROR;
}
ogs_pkbuf_free(pkbuf);
return OGS_OK;
}
void ogs_sctp_write_to_buffer(ogs_sctp_sock_t *sctp, ogs_pkbuf_t *pkbuf)
{
ogs_assert(sctp);
ogs_assert(pkbuf);
ogs_list_add(&sctp->write_queue, pkbuf);
if (!sctp->poll.write) {
ogs_assert(sctp->sock);
sctp->poll.write = ogs_pollset_add(ogs_app()->pollset,
OGS_POLLOUT, sctp->sock->fd, sctp_write_callback, sctp);
ogs_assert(sctp->poll.write);
}
}
static void sctp_write_callback(short when, ogs_socket_t fd, void *data)
{
ogs_sctp_sock_t *sctp = data;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(sctp);
if (ogs_list_empty(&sctp->write_queue) == true) {
ogs_assert(sctp->poll.write);
ogs_pollset_remove(sctp->poll.write);
sctp->poll.write = NULL;
return;
}
pkbuf = ogs_list_first(&sctp->write_queue);
ogs_assert(pkbuf);
ogs_list_remove(&sctp->write_queue, pkbuf);
ogs_assert(sctp->sock);
ogs_sctp_senddata(sctp->sock, pkbuf, NULL);
}
void ogs_sctp_flush_and_destroy(ogs_sctp_sock_t *sctp)
{
ogs_pkbuf_t *pkbuf = NULL, *next_pkbuf = NULL;
ogs_assert(sctp);
ogs_assert(sctp->addr);
ogs_free(sctp->addr);
if (sctp->type == SOCK_STREAM) {
ogs_assert(sctp->poll.read);
ogs_pollset_remove(sctp->poll.read);
if (sctp->poll.write)
ogs_pollset_remove(sctp->poll.write);
ogs_sctp_destroy(sctp->sock);
ogs_list_for_each_safe(&sctp->write_queue, next_pkbuf, pkbuf) {
ogs_list_remove(&sctp->write_queue, pkbuf);
ogs_pkbuf_free(pkbuf);
}
}
}
|