Implement sock_send_to and sock_recv_from system calls (#1457)
This commit is contained in:
@ -34,6 +34,77 @@ textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr *out)
|
||||
return false;
|
||||
}
|
||||
|
||||
static int
|
||||
sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr, socklen_t socklen,
|
||||
bh_sockaddr_t *bh_sockaddr)
|
||||
{
|
||||
switch (sockaddr->sa_family) {
|
||||
case AF_INET:
|
||||
{
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
|
||||
|
||||
assert(socklen >= sizeof(struct sockaddr_in));
|
||||
|
||||
bh_sockaddr->port = ntohs(addr->sin_port);
|
||||
bh_sockaddr->addr_bufer.ipv4 = ntohl(addr->sin_addr.s_addr);
|
||||
bh_sockaddr->is_ipv4 = true;
|
||||
return BHT_OK;
|
||||
}
|
||||
case AF_INET6:
|
||||
{
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)sockaddr;
|
||||
size_t i;
|
||||
|
||||
assert(socklen >= sizeof(struct sockaddr_in6));
|
||||
|
||||
bh_sockaddr->port = ntohs(addr->sin6_port);
|
||||
|
||||
for (i = 0; i < sizeof(bh_sockaddr->addr_bufer.ipv6)
|
||||
/ sizeof(bh_sockaddr->addr_bufer.ipv6[0]);
|
||||
i++) {
|
||||
uint16 part_addr = addr->sin6_addr.s6_addr[i * 2]
|
||||
| (addr->sin6_addr.s6_addr[i * 2 + 1] << 8);
|
||||
bh_sockaddr->addr_bufer.ipv6[i] = ntohs(part_addr);
|
||||
}
|
||||
|
||||
bh_sockaddr->is_ipv4 = false;
|
||||
return BHT_OK;
|
||||
}
|
||||
default:
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bh_sockaddr_to_sockaddr(const bh_sockaddr_t *bh_sockaddr,
|
||||
struct sockaddr_storage *sockaddr, socklen_t *socklen)
|
||||
{
|
||||
if (bh_sockaddr->is_ipv4) {
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
|
||||
addr->sin_port = htons(bh_sockaddr->port);
|
||||
addr->sin_family = AF_INET;
|
||||
addr->sin_addr.s_addr = htonl(bh_sockaddr->addr_bufer.ipv4);
|
||||
*socklen = sizeof(*addr);
|
||||
}
|
||||
else {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)sockaddr;
|
||||
size_t i;
|
||||
addr->sin6_port = htons(bh_sockaddr->port);
|
||||
addr->sin6_family = AF_INET6;
|
||||
|
||||
for (i = 0; i < sizeof(bh_sockaddr->addr_bufer.ipv6)
|
||||
/ sizeof(bh_sockaddr->addr_bufer.ipv6[0]);
|
||||
i++) {
|
||||
uint16 part_addr = htons(bh_sockaddr->addr_bufer.ipv6[i]);
|
||||
addr->sin6_addr.s6_addr[i * 2] = 0xff & part_addr;
|
||||
addr->sin6_addr.s6_addr[i * 2 + 1] = (0xff00 & part_addr) >> 8;
|
||||
}
|
||||
|
||||
*socklen = sizeof(*addr);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_create(bh_socket_t *sock, bool is_ipv4, bool is_tcp)
|
||||
{
|
||||
@ -168,12 +239,48 @@ os_socket_recv(bh_socket_t socket, void *buf, unsigned int len)
|
||||
return recv(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags,
|
||||
bh_sockaddr_t *src_addr)
|
||||
{
|
||||
struct sockaddr_storage sock_addr = {};
|
||||
socklen_t socklen = sizeof(sock_addr);
|
||||
int ret;
|
||||
|
||||
ret = recvfrom(socket, buf, len, flags, (struct sockaddr *)&sock_addr,
|
||||
&socklen);
|
||||
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (src_addr) {
|
||||
sockaddr_to_bh_sockaddr((struct sockaddr *)&sock_addr, socklen,
|
||||
src_addr);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
|
||||
{
|
||||
return send(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len,
|
||||
int flags, const bh_sockaddr_t *dest_addr)
|
||||
{
|
||||
struct sockaddr_storage sock_addr = {};
|
||||
socklen_t socklen = 0;
|
||||
|
||||
bh_sockaddr_to_sockaddr(dest_addr, &sock_addr, &socklen);
|
||||
|
||||
return sendto(socket, buf, len, 0, (const struct sockaddr *)&sock_addr,
|
||||
socklen);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_close(bh_socket_t socket)
|
||||
{
|
||||
@ -189,8 +296,7 @@ os_socket_shutdown(bh_socket_t socket)
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp,
|
||||
bh_inet_network_output_t *out)
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp, bh_ip_addr_buffer_t *out)
|
||||
{
|
||||
if (!cp)
|
||||
return BHT_ERROR;
|
||||
@ -278,26 +384,8 @@ os_socket_addr_resolve(const char *host, const char *service,
|
||||
continue;
|
||||
}
|
||||
|
||||
if (res->ai_family == AF_INET) {
|
||||
struct sockaddr_in *addr_in =
|
||||
(struct sockaddr_in *)res->ai_addr;
|
||||
|
||||
addr_info[pos].port = addr_in->sin_port;
|
||||
addr_info[pos].is_ipv4 = 1;
|
||||
memcpy(addr_info[pos].addr, &addr_in->sin_addr,
|
||||
sizeof(addr_in->sin_addr));
|
||||
}
|
||||
else {
|
||||
struct sockaddr_in6 *addr_in =
|
||||
(struct sockaddr_in6 *)res->ai_addr;
|
||||
|
||||
addr_info[pos].port = addr_in->sin6_port;
|
||||
addr_info[pos].is_ipv4 = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
((uint16 *)addr_info[pos].addr)[i] =
|
||||
ntohs(((uint16_t *)&addr_in->sin6_addr)[i]);
|
||||
}
|
||||
}
|
||||
sockaddr_to_bh_sockaddr(res->ai_addr, sizeof(struct sockaddr_in),
|
||||
&addr_info[pos].sockaddr);
|
||||
|
||||
addr_info[pos].is_tcp = res->ai_socktype == SOCK_STREAM;
|
||||
}
|
||||
@ -312,45 +400,6 @@ os_socket_addr_resolve(const char *host, const char *service,
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
os_socket_convert_sockaddr(struct sockaddr *addr, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
{
|
||||
assert(buf);
|
||||
assert(is_ipv4);
|
||||
assert(port);
|
||||
|
||||
switch (addr->sa_family) {
|
||||
case AF_INET:
|
||||
{
|
||||
struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
|
||||
|
||||
assert(buflen >= sizeof(addr_in->sin_addr));
|
||||
*port = ntohs(addr_in->sin_port);
|
||||
memcpy(buf, &addr_in->sin_addr, sizeof(addr_in->sin_addr));
|
||||
*is_ipv4 = true;
|
||||
break;
|
||||
}
|
||||
case AF_INET6:
|
||||
{
|
||||
struct sockaddr_in6 *addr_in = (struct sockaddr_in6 *)addr;
|
||||
assert(buflen >= sizeof(addr_in->sin6_addr));
|
||||
*port = ntohs(addr_in->sin6_port);
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
((uint16_t *)buf)[i] =
|
||||
ntohs(((uint16_t *)&addr_in->sin6_addr)[i]);
|
||||
}
|
||||
*is_ipv4 = false;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
@ -400,8 +449,7 @@ os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
os_socket_addr_local(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
struct sockaddr_storage addr_storage = { 0 };
|
||||
socklen_t addr_len = sizeof(addr_storage);
|
||||
@ -413,13 +461,12 @@ os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return os_socket_convert_sockaddr((struct sockaddr *)&addr_storage, buf,
|
||||
buflen, port, is_ipv4);
|
||||
return sockaddr_to_bh_sockaddr((struct sockaddr *)&addr_storage, addr_len,
|
||||
sockaddr);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
struct sockaddr_storage addr_storage = { 0 };
|
||||
socklen_t addr_len = sizeof(addr_storage);
|
||||
@ -431,6 +478,6 @@ os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return os_socket_convert_sockaddr((struct sockaddr *)&addr_storage, buf,
|
||||
buflen, port, is_ipv4);
|
||||
return sockaddr_to_bh_sockaddr((struct sockaddr *)&addr_storage, addr_len,
|
||||
sockaddr);
|
||||
}
|
||||
@ -292,6 +292,18 @@ os_sem_unlink(const char *name);
|
||||
* need to implement these APIs
|
||||
*/
|
||||
|
||||
typedef union {
|
||||
uint32 ipv4;
|
||||
uint16 ipv6[8];
|
||||
uint8 data[1];
|
||||
} bh_ip_addr_buffer_t;
|
||||
|
||||
typedef struct {
|
||||
bh_ip_addr_buffer_t addr_bufer;
|
||||
uint16 port;
|
||||
bool is_ipv4;
|
||||
} bh_sockaddr_t;
|
||||
|
||||
/**
|
||||
* Create a socket
|
||||
*
|
||||
@ -381,6 +393,22 @@ os_socket_connect(bh_socket_t socket, const char *addr, int port);
|
||||
int
|
||||
os_socket_recv(bh_socket_t socket, void *buf, unsigned int len);
|
||||
|
||||
/**
|
||||
* Blocking receive message from a socket.
|
||||
*
|
||||
* @param socket the socket to send message
|
||||
* @param buf the buffer to store the data
|
||||
* @param len length of the buffer, this API does not guarantee that
|
||||
* [len] bytes are received
|
||||
* @param flags control the operation
|
||||
* @param src_addr source address
|
||||
*
|
||||
* @return number of bytes sent if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags,
|
||||
bh_sockaddr_t *src_addr);
|
||||
|
||||
/**
|
||||
* Blocking send message on a socket
|
||||
*
|
||||
@ -393,6 +421,21 @@ os_socket_recv(bh_socket_t socket, void *buf, unsigned int len);
|
||||
int
|
||||
os_socket_send(bh_socket_t socket, const void *buf, unsigned int len);
|
||||
|
||||
/**
|
||||
* Blocking send message on a socket to the target address
|
||||
*
|
||||
* @param socket the socket to send message
|
||||
* @param buf the buffer of data to be sent
|
||||
* @param len length of the buffer
|
||||
* @param flags control the operation
|
||||
* @param dest_addr target address
|
||||
*
|
||||
* @return number of bytes sent if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len,
|
||||
int flags, const bh_sockaddr_t *dest_addr);
|
||||
|
||||
/**
|
||||
* Close a socket
|
||||
*
|
||||
@ -413,12 +456,6 @@ os_socket_close(bh_socket_t socket);
|
||||
int
|
||||
os_socket_shutdown(bh_socket_t socket);
|
||||
|
||||
typedef union {
|
||||
uint32 ipv4;
|
||||
uint16 ipv6[8];
|
||||
uint8_t data[0];
|
||||
} bh_inet_network_output_t;
|
||||
|
||||
/**
|
||||
* converts cp into a number in host byte order suitable for use as
|
||||
* an Internet network address
|
||||
@ -435,13 +472,10 @@ typedef union {
|
||||
* If the input is invalid, -1 is returned
|
||||
*/
|
||||
int
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp,
|
||||
bh_inet_network_output_t *out);
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp, bh_ip_addr_buffer_t *out);
|
||||
|
||||
typedef struct {
|
||||
uint8_t addr[16];
|
||||
uint16_t port;
|
||||
uint8_t is_ipv4;
|
||||
bh_sockaddr_t sockaddr;
|
||||
uint8_t is_tcp;
|
||||
} bh_addr_info_t;
|
||||
|
||||
@ -478,38 +512,24 @@ os_socket_addr_resolve(const char *host, const char *service,
|
||||
*
|
||||
* @param socket the local socket
|
||||
*
|
||||
* @param buf buffer to store the address
|
||||
*
|
||||
* @param buflen length of the buf buffer
|
||||
*
|
||||
* @param port a buffer for storing socket's port
|
||||
*
|
||||
* @param is_ipv4 a buffer for storing information about the address family
|
||||
* @param sockaddr a buffer for storing the address
|
||||
*
|
||||
* @return On success, returns 0; otherwise, it returns -1.
|
||||
*/
|
||||
int
|
||||
os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4);
|
||||
os_socket_addr_local(bh_socket_t socket, bh_sockaddr_t *sockaddr);
|
||||
|
||||
/**
|
||||
* Returns an binary address and a port of the remote socket
|
||||
*
|
||||
* @param socket the remote socket
|
||||
*
|
||||
* @param buf buffer to store the address
|
||||
*
|
||||
* @param buflen length of the buf buffer
|
||||
*
|
||||
* @param port a buffer for storing socket's port
|
||||
*
|
||||
* @param is_ipv4 a buffer for storing information about the address family
|
||||
* @param sockaddr a buffer for storing the address
|
||||
*
|
||||
* @return On success, returns 0; otherwise, it returns -1.
|
||||
*/
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4);
|
||||
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr);
|
||||
|
||||
/**
|
||||
* Set the send timeout until reporting an error
|
||||
|
||||
@ -605,8 +605,7 @@ os_socket_create(bh_socket_t *sock, bool is_ipv4, bool is_tcp)
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp,
|
||||
bh_inet_network_output_t *out)
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp, bh_ip_addr_buffer_t *out)
|
||||
{
|
||||
if (!cp)
|
||||
return BHT_ERROR;
|
||||
@ -662,6 +661,15 @@ os_socket_recv(bh_socket_t socket, void *buf, unsigned int len)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags,
|
||||
bh_sockaddr_t *src_addr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
|
||||
{
|
||||
@ -678,6 +686,15 @@ os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len,
|
||||
int flags, const bh_sockaddr_t *dest_addr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_shutdown(bh_socket_t socket)
|
||||
{
|
||||
@ -696,8 +713,7 @@ os_socket_addr_resolve(const char *host, const char *service,
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
os_socket_addr_local(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
@ -705,8 +721,7 @@ os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
|
||||
@ -143,12 +143,30 @@ os_socket_recv(bh_socket_t socket, void *buf, unsigned int len)
|
||||
return recv(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags,
|
||||
bh_sockaddr_t *src_addr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
|
||||
{
|
||||
return send(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len,
|
||||
int flags, const bh_sockaddr_t *dest_addr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_close(bh_socket_t socket)
|
||||
{
|
||||
@ -164,8 +182,7 @@ os_socket_shutdown(bh_socket_t socket)
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp,
|
||||
bh_inet_network_output_t *out)
|
||||
os_socket_inet_network(bool is_ipv4, const char *cp, bh_ip_addr_buffer_t *out)
|
||||
{
|
||||
if (!cp)
|
||||
return BHT_ERROR;
|
||||
@ -201,8 +218,7 @@ os_socket_addr_resolve(const char *host, const char *service,
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
os_socket_addr_local(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
@ -242,8 +258,7 @@ os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
|
||||
uint16_t *port, uint8_t *is_ipv4)
|
||||
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user