Skip to content

Commit

Permalink
Added Unix Implementation for sendto and recvfrom
Browse files Browse the repository at this point in the history
Added omrsock_sendto and omrsock_recvfrom unix implementation

- Both functions uses function calls to system sendto and recvfrom
  socket api functions.
- Both functions checks for invalid arguments and saves errno using
  set_last_error and returns the bytes sent/received.
- recvfrom function can take NULL for addrHandle argument when user
  does not want to know who the message was from.

Issue: eclipse-omr#5128

Signed-off-by: Haley Cao <haleycao88@hotmail.com>
  • Loading branch information
Haley Cao committed May 3, 2020
1 parent 4a681a6 commit 1459658
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include_core/omrporterror.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@
#define OMRPORT_ERROR_SOCK_ACCEPT_FAILED (OMRPORT_ERROR_SOCK_BASE - 9)
#define OMRPORT_ERROR_SOCK_SEND_FAILED (OMRPORT_ERROR_SOCK_BASE - 10)
#define OMRPORT_ERROR_SOCK_RECV_FAILED (OMRPORT_ERROR_SOCK_BASE - 11)
#define OMRPORT_ERROR_SOCK_SENDTO_FAILED (OMRPORT_ERROR_SOCK_BASE - 12)
#define OMRPORT_ERROR_SOCK_RECVFROM_FAILED (OMRPORT_ERROR_SOCK_BASE - 13)
/**
* @}
*/
Expand Down
47 changes: 45 additions & 2 deletions port/unix/omrsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,31 @@ omrsock_send(struct OMRPortLibrary *portLibrary, omrsock_socket_t sock, uint8_t
int32_t
omrsock_sendto(struct OMRPortLibrary *portLibrary, omrsock_socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, omrsock_sockaddr_t addrHandle)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;

int32_t bytesSent = 0;
socklen_t addrLength = 0;
int32_t err;

if (NULL == sock || 0 >= nbyte || NULL == addrHandle) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}

if (OS_SOCK_AF_INET == (addrHandle->data).ss_family) {
addrLength = sizeof(omr_os_sockaddr_in);
}
else {
addrLength = sizeof(omr_os_sockaddr_in6);
}

bytesSent = sendto(sock->data, buf, nbyte, flags, (omr_os_sockaddr *)&addrHandle->data, addrLength);

if (-1 == bytesSent) {
err = errno;
portLibrary->error_set_last_error(portLibrary, err, OMRPORT_ERROR_SOCK_SENDTO_FAILED);
return OMRPORT_ERROR_SOCK_SENDTO_FAILED;
}

return bytesSent;
}

int32_t
Expand All @@ -590,7 +614,26 @@ omrsock_recv(struct OMRPortLibrary *portLibrary, omrsock_socket_t sock, uint8_t
int32_t
omrsock_recvfrom(struct OMRPortLibrary *portLibrary, omrsock_socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, omrsock_sockaddr_t addrHandle)
{
return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
int32_t bytesRecv = 0;
socklen_t addrlen = 0;

if (NULL == sock || 0 >= nbyte) {
return OMRPORT_ERROR_INVALID_ARGUMENTS;
}

if (NULL == addrHandle) {
/* If addrHandle is NULL, the source address will not be filled in by recvfrom call. */
bytesRecv = recvfrom(sock->data, buf, nbyte, flags, NULL, NULL);
} else {
addrlen = sizeof(omr_os_sockaddr_storage);
bytesRecv = recvfrom(sock->data, buf, nbyte, flags, (struct sockaddr*)&addrHandle->data, &addrlen);
}
if (-1 == bytesRecv) {
portLibrary->error_set_last_error(portLibrary, errno, OMRPORT_ERROR_SOCK_RECVFROM_FAILED);
return OMRPORT_ERROR_SOCK_RECVFROM_FAILED;
}

return bytesRecv;
}

int32_t
Expand Down

0 comments on commit 1459658

Please sign in to comment.