diff --git a/include_core/omrporterror.h b/include_core/omrporterror.h index c2768267b2f..ed0617670e8 100644 --- a/include_core/omrporterror.h +++ b/include_core/omrporterror.h @@ -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) /** * @} */ diff --git a/port/unix/omrsock.c b/port/unix/omrsock.c index 811fd0a1d06..74daea2b3b6 100644 --- a/port/unix/omrsock.c +++ b/port/unix/omrsock.c @@ -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 @@ -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