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 Jun 16, 2020
1 parent 840e14b commit 48a8cc0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
10 changes: 6 additions & 4 deletions include_core/omrporterror.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,12 @@
#define OMRPORT_ERROR_SOCK_ACCEPT_FAILED (OMRPORT_ERROR_SOCK_BASE - 10)
#define OMRPORT_ERROR_SOCK_SEND_FAILED (OMRPORT_ERROR_SOCK_BASE - 11)
#define OMRPORT_ERROR_SOCK_RECV_FAILED (OMRPORT_ERROR_SOCK_BASE - 12)
#define OMRPORT_ERROR_SOCK_LEVEL_UNSUPPORTED (OMRPORT_ERROR_SOCK_BASE - 13)
#define OMRPORT_ERROR_SOCK_OPTION_UNSUPPORTED (OMRPORT_ERROR_SOCK_BASE - 14)
#define OMRPORT_ERROR_SOCK_SETSOCKOPT_FAILED (OMRPORT_ERROR_SOCK_BASE - 15)
#define OMRPORT_ERROR_SOCK_GETSOCKOPT_FAILED (OMRPORT_ERROR_SOCK_BASE - 16)
#define OMRPORT_ERROR_SOCK_SENDTO_FAILED (OMRPORT_ERROR_SOCK_BASE - 13)
#define OMRPORT_ERROR_SOCK_RECVFROM_FAILED (OMRPORT_ERROR_SOCK_BASE - 14)
#define OMRPORT_ERROR_SOCK_LEVEL_UNSUPPORTED (OMRPORT_ERROR_SOCK_BASE - 15)
#define OMRPORT_ERROR_SOCK_OPTION_UNSUPPORTED (OMRPORT_ERROR_SOCK_BASE - 16)
#define OMRPORT_ERROR_SOCK_SETSOCKOPT_FAILED (OMRPORT_ERROR_SOCK_BASE - 17)
#define OMRPORT_ERROR_SOCK_GETSOCKOPT_FAILED (OMRPORT_ERROR_SOCK_BASE - 18)
/**
* @}
*/
Expand Down
47 changes: 45 additions & 2 deletions port/unix/omrsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,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 @@ -658,7 +682,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 48a8cc0

Please sign in to comment.