File size: 4,117 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2019,2020 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 "test-common.h"

ogs_socknode_t *testsctp_server(const char *ipstr, int port)
{
    int rv;
    ogs_sockaddr_t *addr = NULL;
    ogs_socknode_t *node = NULL;
    ogs_sock_t *sock = NULL;

    rv = ogs_getaddrinfo(&addr, AF_UNSPEC, ipstr, port, 0);
    ogs_assert(rv == OGS_OK);

    node = ogs_socknode_new(addr);
    ogs_assert(node);

    sock = ogs_sctp_server(SOCK_SEQPACKET, node->addr, NULL);
    ogs_assert(sock);

    node->sock = sock;
    node->cleanup = ogs_sctp_destroy;

    return node;
}

ogs_socknode_t *testsctp_client(const char *ipstr, int port)
{
    int rv;
    ogs_sockaddr_t *addr = NULL;
    ogs_socknode_t *node = NULL;
    ogs_sock_t *sock = NULL;

    rv = ogs_getaddrinfo(&addr, AF_UNSPEC, ipstr, port, 0);
    ogs_assert(rv == OGS_OK);

    node = ogs_socknode_new(addr);
    ogs_assert(node);

    sock = ogs_sctp_client(SOCK_STREAM, node->addr, NULL);
    ogs_assert(sock);

    node->sock = sock;
    node->cleanup = ogs_sctp_destroy;

    return node;
}

ogs_socknode_t *tests1ap_client(int family)
{
    int rv;
    ogs_sockaddr_t *addr = NULL;
    ogs_socknode_t *node = NULL;
    ogs_sock_t *sock = NULL;

    if (family == AF_INET6)
        ogs_assert(OGS_OK ==
            ogs_copyaddrinfo(&addr, test_self()->s1ap_addr6));
    else
        ogs_assert(OGS_OK ==
            ogs_copyaddrinfo(&addr, test_self()->s1ap_addr));

    ogs_assert(addr);

    node = ogs_socknode_new(addr);
    ogs_assert(node);

    sock = ogs_sctp_client(SOCK_STREAM, node->addr, NULL);
    ogs_assert(sock);

    node->sock = sock;
    node->cleanup = ogs_sctp_destroy;

    return node;
}

ogs_socknode_t *testngap_client(int family)
{
    int rv;
    ogs_sockaddr_t *addr = NULL;
    ogs_socknode_t *node = NULL;
    ogs_sock_t *sock = NULL;

    if (family == AF_INET6)
        ogs_assert(OGS_OK ==
            ogs_copyaddrinfo(&addr, test_self()->ngap_addr6));
    else
        ogs_assert(OGS_OK ==
            ogs_copyaddrinfo(&addr, test_self()->ngap_addr));

    ogs_assert(addr);

    node = ogs_socknode_new(addr);
    ogs_assert(node);

    sock = ogs_sctp_client(SOCK_STREAM, node->addr, NULL);
    ogs_assert(sock);

    node->sock = sock;
    node->cleanup = ogs_sctp_destroy;

    return node;
}

ogs_sockaddr_t last_addr;

ogs_pkbuf_t *testsctp_read(ogs_socknode_t *node, int type)
{
    int size;
    ogs_pkbuf_t *recvbuf = NULL;

    ogs_assert(node);
    ogs_assert(node->sock);

    recvbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN);
    ogs_assert(recvbuf);
    ogs_pkbuf_put(recvbuf, OGS_MAX_SDU_LEN);

    size = ogs_sctp_recvdata(node->sock, recvbuf->data, OGS_MAX_SDU_LEN,
            type == 1 ? &last_addr : NULL, NULL);
    if (size <= 0) {
        ogs_error("ogs_sctp_recvdata() failed");
        return NULL;
    }

    ogs_pkbuf_trim(recvbuf, size);
    return recvbuf;;
}

int testsctp_send(ogs_socknode_t *node, ogs_pkbuf_t *pkbuf,
        int ppid, uint16_t stream_no, int type)
{
    int sent;

    ogs_assert(node);
    ogs_assert(node->sock);
    ogs_assert(pkbuf);

    sent = ogs_sctp_sendmsg(node->sock, pkbuf->data, pkbuf->len,
            type == 1 ? &last_addr : NULL, ppid, stream_no);
    if (sent < 0 || sent != pkbuf->len) {
        ogs_error("ogs_sctp_sendmsg error (%d:%s)", errno, strerror(errno));
        return OGS_ERROR;
    }
    ogs_pkbuf_free(pkbuf);

    return OGS_OK;
}