summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/libraries/lib-socket/test/tcp_udp.c
blob: 49231de8999178a5e2013717d09b796c75c7ea66 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */
#include <unistd.h>
#include <string.h>
#include <assert.h>
#ifdef __wasi__
#include <wasi/api.h>
#include <sys/socket.h>
#include <wasi_socket_ext.h>
#endif
#include <arpa/inet.h>
#include <pthread.h>
#define SERVER_MSG "Message from server."
#define PORT 8989
pthread_mutex_t mut;
pthread_cond_t cond;
int server_init_complete = 0;
char buffer[sizeof(SERVER_MSG) + 1];

struct socket_info {
    union {
        struct sockaddr_in addr_ipv4;
        struct sockaddr_in6 addr_ipv6;
    } addr;
    int sock;
};

struct thread_args {
    int family;
    int protocol;
};

struct socket_info
init_socket_addr(int family, int protocol)
{
    int sock = socket(family, protocol, 0);
    assert(sock != -1);

    struct socket_info info;
    if (family == AF_INET) {
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT);
        addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        info.addr.addr_ipv4 = addr;
    }
    else if (family == AF_INET6) {
        struct sockaddr_in6 addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin6_family = AF_INET6;
        addr.sin6_port = htons(PORT);
        addr.sin6_addr = in6addr_loopback;
        info.addr.addr_ipv6 = addr;
    }
    info.sock = sock;
    return info;
}

void
assert_thread_args(struct thread_args *args)
{
    assert(args->family == AF_INET || args->family == AF_INET6);
    assert(args->protocol == SOCK_STREAM || args->protocol == SOCK_DGRAM);
}

void *
server(void *arg)
{
    server_init_complete = 0;
    struct thread_args *args = (struct thread_args *)arg;
    assert_thread_args(args);

    struct socket_info init_server_sock =
        init_socket_addr(args->family, args->protocol);

    int server_sock = init_server_sock.sock;
    socklen_t addr_size;
    struct sockaddr_storage client_addr;
    strcpy(buffer, SERVER_MSG);

    struct sockaddr *server_addr = (struct sockaddr *)&init_server_sock.addr;
    int ret = bind(server_sock, server_addr,
                   args->family == AF_INET ? sizeof(struct sockaddr_in)
                                           : sizeof(struct sockaddr_in6));
    assert(ret == 0);

    (args->protocol == SOCK_STREAM) && listen(server_sock, 1);
    pthread_mutex_lock(&mut);
    server_init_complete = 1;
    pthread_mutex_unlock(&mut);
    pthread_cond_signal(&cond);

    addr_size = sizeof(client_addr);
    if (args->protocol == SOCK_STREAM) {
        int client_sock =
            accept(server_sock, (struct sockaddr *)&client_addr, &addr_size);
        assert(client_sock >= 0);
        sendto(client_sock, buffer, strlen(buffer), 0,
               (struct sockaddr *)&client_addr, addr_size);

        assert(close(client_sock) == 0);
    }
    else {
        recvfrom(server_sock, buffer, sizeof(buffer), 0,
                 (struct sockaddr *)&client_addr, &addr_size);
        sendto(server_sock, buffer, strlen(buffer), 0,
               (struct sockaddr *)&client_addr, addr_size);

        assert(close(server_sock) == 0);
    }

    return NULL;
}

void *
client(void *arg)
{
    struct thread_args *args = (struct thread_args *)arg;
    assert_thread_args(args);

    pthread_mutex_lock(&mut);

    while (server_init_complete == 0) {
        pthread_cond_wait(&cond, &mut);
    }

    struct socket_info init_client_sock =
        init_socket_addr(args->family, args->protocol);
    int sock = init_client_sock.sock;
    pthread_mutex_unlock(&mut);

    if (args->family == AF_INET) {
        struct sockaddr_in addr = init_client_sock.addr.addr_ipv4;
        if (args->protocol == SOCK_STREAM) {
            assert(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1);
        }
        else {
            assert(sendto(sock, buffer, strlen(buffer), 0,
                          (struct sockaddr *)&addr, sizeof(addr))
                   != -1);
        }
    }
    else {
        struct sockaddr_in6 addr = init_client_sock.addr.addr_ipv6;
        if (args->protocol == SOCK_STREAM) {
            assert(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1);
        }
        else {
            assert(sendto(sock, buffer, strlen(buffer), 0,
                          (struct sockaddr *)&addr, sizeof(addr))
                   != -1);
        }
    }

    recv(sock, buffer, sizeof(buffer), 0);
    assert(strcmp(buffer, SERVER_MSG) == 0);
    assert(close(sock) == 0);
    return NULL;
}

void
test_protocol(int family, int protocol)
{
    pthread_t server_thread, client_thread;
    assert(pthread_cond_init(&cond, NULL) == 0);
    assert(pthread_mutex_init(&mut, NULL) == 0);

    struct thread_args args = { family, protocol };
    assert(pthread_create(&server_thread, NULL, server, (void *)&args) == 0);
    assert(pthread_create(&client_thread, NULL, client, (void *)&args) == 0);
    assert(pthread_join(server_thread, NULL) == 0);
    assert(pthread_join(client_thread, NULL) == 0);

    assert(pthread_mutex_destroy(&mut) == 0);
    assert(pthread_cond_destroy(&cond) == 0);
}

int
main(int argc, char **argv)
{
    /* test tcp with ipv4 and ipv6 */
    test_protocol(AF_INET, SOCK_STREAM);
    test_protocol(AF_INET6, SOCK_STREAM);

    /* test udp with ipv4 and ipv6 */
    test_protocol(AF_INET, SOCK_DGRAM);
    test_protocol(AF_INET6, SOCK_DGRAM);

    return 0;
}