Add support for IPv6 in WAMR (#1411)
For now this implementation only covers posix platforms, as defined in MVP #1336
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
#include "tcp_utils.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
@ -14,28 +15,50 @@
|
||||
#include <wasi_socket_ext.h>
|
||||
#endif
|
||||
|
||||
static void
|
||||
init_sockaddr_inet(struct sockaddr_in *addr)
|
||||
{
|
||||
/* 127.0.0.1:1234 */
|
||||
addr->sin_family = AF_INET;
|
||||
addr->sin_port = htons(1234);
|
||||
addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
}
|
||||
|
||||
static void
|
||||
init_sockaddr_inet6(struct sockaddr_in6 *addr)
|
||||
{
|
||||
/* [::1]:1234 */
|
||||
addr->sin6_family = AF_INET6;
|
||||
addr->sin6_port = htons(1234);
|
||||
addr->sin6_addr = in6addr_loopback;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int socket_fd, ret, total_size = 0;
|
||||
int socket_fd, ret, total_size = 0, af;
|
||||
char buffer[1024] = { 0 };
|
||||
char ip_string[16] = { 0 };
|
||||
struct sockaddr_in server_address = { 0 };
|
||||
struct sockaddr_in local_address = { 0 };
|
||||
char ip_string[64] = { 0 };
|
||||
socklen_t len;
|
||||
struct sockaddr_storage server_address = { 0 };
|
||||
struct sockaddr_storage local_address = { 0 };
|
||||
|
||||
if (argc > 1 && strcmp(argv[1], "inet6") == 0) {
|
||||
af = AF_INET6;
|
||||
init_sockaddr_inet6((struct sockaddr_in6 *)&server_address);
|
||||
}
|
||||
else {
|
||||
af = AF_INET;
|
||||
init_sockaddr_inet((struct sockaddr_in *)&server_address);
|
||||
}
|
||||
|
||||
printf("[Client] Create socket\n");
|
||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
socket_fd = socket(af, SOCK_STREAM, 0);
|
||||
if (socket_fd == -1) {
|
||||
perror("Create socket failed");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* 127.0.0.1:1234 */
|
||||
server_address.sin_family = AF_INET;
|
||||
server_address.sin_port = htons(1234);
|
||||
server_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
|
||||
printf("[Client] Connect socket\n");
|
||||
if (connect(socket_fd, (struct sockaddr *)&server_address,
|
||||
sizeof(server_address))
|
||||
@ -53,11 +76,15 @@ main(int argc, char *argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET, &local_address.sin_addr, ip_string,
|
||||
sizeof(ip_string) / sizeof(ip_string[0]));
|
||||
if (sockaddr_to_string((struct sockaddr *)&local_address, ip_string,
|
||||
sizeof(ip_string) / sizeof(ip_string[0]))
|
||||
!= 0) {
|
||||
printf("[Client] failed to parse local address\n");
|
||||
close(socket_fd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("[Client] Local address is: %s:%d\n", ip_string,
|
||||
ntohs(local_address.sin_port));
|
||||
printf("[Client] Local address is: %s\n", ip_string);
|
||||
|
||||
printf("[Client] Client receive\n");
|
||||
while (1) {
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
#include "tcp_utils.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
@ -41,28 +43,50 @@ run(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
init_sockaddr_inet(struct sockaddr_in *addr)
|
||||
{
|
||||
/* 0.0.0.0:1234 */
|
||||
addr->sin_family = AF_INET;
|
||||
addr->sin_port = htons(1234);
|
||||
addr->sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
}
|
||||
|
||||
static void
|
||||
init_sockaddr_inet6(struct sockaddr_in6 *addr)
|
||||
{
|
||||
/* [::]:1234 */
|
||||
addr->sin6_family = AF_INET6;
|
||||
addr->sin6_port = htons(1234);
|
||||
addr->sin6_addr = in6addr_any;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int socket_fd = -1, addrlen = 0;
|
||||
struct sockaddr_in addr = { 0 };
|
||||
int socket_fd = -1, addrlen = 0, af;
|
||||
struct sockaddr_storage addr = { 0 };
|
||||
unsigned connections = 0;
|
||||
pthread_t workers[WORKER_NUM] = { 0 };
|
||||
int client_sock_fds[WORKER_NUM] = { 0 };
|
||||
char ip_string[16];
|
||||
char ip_string[64];
|
||||
|
||||
if (argc > 1 && strcmp(argv[1], "inet6") == 0) {
|
||||
af = AF_INET6;
|
||||
init_sockaddr_inet6((struct sockaddr_in6 *)&addr);
|
||||
}
|
||||
else {
|
||||
af = AF_INET;
|
||||
init_sockaddr_inet((struct sockaddr_in *)&addr);
|
||||
}
|
||||
|
||||
printf("[Server] Create socket\n");
|
||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
socket_fd = socket(af, SOCK_STREAM, 0);
|
||||
if (socket_fd < 0) {
|
||||
perror("Create socket failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* 0.0.0.0:1234 */
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(1234);
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
printf("[Server] Bind socket\n");
|
||||
addrlen = sizeof(addr);
|
||||
if (bind(socket_fd, (struct sockaddr *)&addr, addrlen) < 0) {
|
||||
@ -85,11 +109,14 @@ main(int argc, char *argv[])
|
||||
break;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET, &addr.sin_addr, ip_string,
|
||||
sizeof(ip_string) / sizeof(ip_string[0]));
|
||||
if (sockaddr_to_string((struct sockaddr *)&addr, ip_string,
|
||||
sizeof(ip_string) / sizeof(ip_string[0]))
|
||||
!= 0) {
|
||||
printf("[Server] failed to parse client address\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
printf("[Server] Client connected (%s:%d)\n", ip_string,
|
||||
ntohs(addr.sin_port));
|
||||
printf("[Server] Client connected (%s)\n", ip_string);
|
||||
if (pthread_create(&workers[connections], NULL, run,
|
||||
&client_sock_fds[connections])) {
|
||||
perror("Create a worker thread failed");
|
||||
|
||||
48
samples/socket-api/wasm-src/tcp_utils.h
Normal file
48
samples/socket-api/wasm-src/tcp_utils.h
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef TCP_UTILS_H
|
||||
#define TCP_UTILS_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
sockaddr_to_string(struct sockaddr *addr, char *str, size_t len)
|
||||
{
|
||||
uint16_t port;
|
||||
char ip_string[64];
|
||||
void *addr_buf;
|
||||
int ret;
|
||||
|
||||
switch (addr->sa_family) {
|
||||
case AF_INET:
|
||||
{
|
||||
struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
|
||||
port = addr_in->sin_port;
|
||||
addr_buf = &addr_in->sin_addr;
|
||||
break;
|
||||
}
|
||||
case AF_INET6:
|
||||
{
|
||||
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
|
||||
port = addr_in6->sin6_port;
|
||||
addr_buf = &addr_in6->sin6_addr;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
inet_ntop(addr->sa_family, addr_buf, ip_string,
|
||||
sizeof(ip_string) / sizeof(ip_string[0]));
|
||||
|
||||
ret = snprintf(str, len, "%s:%d", ip_string, ntohs(port));
|
||||
|
||||
return ret > 0 && (size_t)ret < len ? 0 : -1;
|
||||
}
|
||||
|
||||
#endif /* TCP_UTILS_H */
|
||||
Reference in New Issue
Block a user