diff --git a/runtime/bcutil/dynload.c b/runtime/bcutil/dynload.c index 350d70afbf4..962988de2fc 100644 --- a/runtime/bcutil/dynload.c +++ b/runtime/bcutil/dynload.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -29,7 +29,6 @@ #include "cfr.h" #include "cfreader.h" #include "j2sever.h" -#include "j9socket.h" #include "j9protos.h" #include "j9consts.h" #include "jimage.h" diff --git a/runtime/exelib/common/libhlp.c b/runtime/exelib/common/libhlp.c index db6446393ef..47b0918f18d 100644 --- a/runtime/exelib/common/libhlp.c +++ b/runtime/exelib/common/libhlp.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2017 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -26,7 +26,6 @@ #include "jni.h" #include "j9user.h" #include "j9port.h" -#include "j9socket.h" #include "cfr.h" #include "j9.h" #include "j9protos.h" diff --git a/runtime/oti/j9port_generated.h b/runtime/oti/j9port_generated.h index 58b3343d48c..fd668e6b4cf 100644 --- a/runtime/oti/j9port_generated.h +++ b/runtime/oti/j9port_generated.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -176,32 +176,10 @@ typedef struct J9PortLibrary { int32_t ( *sock_startup)(struct J9PortLibrary *portLibrary) ; /** see @ref j9sock.c::j9sock_shutdown "j9sock_shutdown"*/ int32_t ( *sock_shutdown)(struct J9PortLibrary *portLibrary) ; - /** see @ref j9sock.c::j9sock_htons "j9sock_htons"*/ - uint16_t ( *sock_htons)(struct J9PortLibrary *portLibrary, uint16_t val) ; - /** see @ref j9sock.c::j9sock_write "j9sock_write"*/ - int32_t ( *sock_write)(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags) ; - /** see @ref j9sock.c::j9sock_sockaddr "j9sock_sockaddr"*/ - int32_t ( *sock_sockaddr)(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, const char *addrStr, uint16_t port) ; - /** see @ref j9sock.c::j9sock_read "j9sock_read"*/ - int32_t ( *sock_read)(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags) ; - /** see @ref j9sock.c::j9sock_socket "j9sock_socket"*/ - int32_t ( *sock_socket)(struct J9PortLibrary *portLibrary, j9socket_t *handle, int32_t family, int32_t socktype, int32_t protocol) ; - /** see @ref j9sock.c::j9sock_close "j9sock_close"*/ - int32_t ( *sock_close)(struct J9PortLibrary *portLibrary, j9socket_t *sock) ; - /** see @ref j9sock.c::j9sock_connect "j9sock_connect"*/ - int32_t ( *sock_connect)(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr) ; /** see @ref j9sock.c::j9sock_inetaddr "j9sock_inetaddr"*/ int32_t ( *sock_inetaddr)(struct J9PortLibrary *portLibrary, const char *addrStr, uint32_t *addr) ; /** see @ref j9sock.c::j9sock_gethostbyname "j9sock_gethostbyname"*/ int32_t ( *sock_gethostbyname)(struct J9PortLibrary *portLibrary, const char *name, j9hostent_t handle) ; - /** see @ref j9sock.c::j9sock_hostent_addrlist "j9sock_hostent_addrlist"*/ - int32_t ( *sock_hostent_addrlist)(struct J9PortLibrary *portLibrary, j9hostent_t handle, uint32_t index) ; - /** see @ref j9sock.c::j9sock_sockaddr_init "j9sock_sockaddr_init"*/ - int32_t ( *sock_sockaddr_init)(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, int16_t family, uint32_t ipAddrNetworkOrder, uint16_t portNetworkOrder) ; - /** see @ref j9sock.c::j9sock_linger_init "j9sock_linger_init"*/ - int32_t ( *sock_linger_init)(struct J9PortLibrary *portLibrary, j9linger_t handle, int32_t enabled, uint16_t timeout) ; - /** see @ref j9sock.c::j9sock_setopt_linger "j9sock_setopt_linger"*/ - int32_t ( *sock_setopt_linger)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval) ; /** see @ref j9gp.c::j9gp_startup "j9gp_startup"*/ int32_t ( *gp_startup)(struct J9PortLibrary *portLibrary) ; /** see @ref j9gp.c::j9gp_shutdown "j9gp_shutdown"*/ @@ -232,80 +210,8 @@ typedef struct J9PortLibrary { uintptr_t ( *sysinfo_DLPAR_max_CPUs)(struct J9PortLibrary *portLibrary) ; /** see @ref j9sysinfo.c::j9sysinfo_weak_memory_consistency "j9sysinfo_weak_memory_consistency"*/ uintptr_t ( *sysinfo_weak_memory_consistency)(struct J9PortLibrary *portLibrary) ; - /** see @ref j9sock.c::j9sock_htonl "j9sock_htonl"*/ - int32_t ( *sock_htonl)(struct J9PortLibrary *portLibrary, int32_t val) ; - /** see @ref j9sock.c::j9sock_bind "j9sock_bind"*/ - int32_t ( *sock_bind)(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr) ; - /** see @ref j9sock.c::j9sock_accept "j9sock_accept"*/ - int32_t ( *sock_accept)(struct J9PortLibrary *portLibrary, j9socket_t serverSock, j9sockaddr_t addrHandle, j9socket_t *sockHandle) ; - /** see @ref j9sock.c::j9sock_shutdown_input "j9sock_shutdown_input"*/ - int32_t ( *sock_shutdown_input)(struct J9PortLibrary *portLibrary, j9socket_t sock) ; - /** see @ref j9sock.c::j9sock_shutdown_output "j9sock_shutdown_output"*/ - int32_t ( *sock_shutdown_output)(struct J9PortLibrary *portLibrary, j9socket_t sock) ; - /** see @ref j9sock.c::j9sock_listen "j9sock_listen"*/ - int32_t ( *sock_listen)(struct J9PortLibrary *portLibrary, j9socket_t sock, int32_t backlog ) ; - /** see @ref j9sock.c::j9sock_ntohl "j9sock_ntohl"*/ - int32_t ( *sock_ntohl)(struct J9PortLibrary *portLibrary, int32_t val) ; - /** see @ref j9sock.c::j9sock_ntohs "j9sock_ntohs"*/ - uint16_t ( *sock_ntohs)(struct J9PortLibrary *portLibrary, uint16_t val) ; - /** see @ref j9sock.c::j9sock_getpeername "j9sock_getpeername"*/ - int32_t ( *sock_getpeername)(struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle) ; - /** see @ref j9sock.c::j9sock_getsockname "j9sock_getsockname"*/ - int32_t ( *sock_getsockname)(struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle) ; - /** see @ref j9sock.c::j9sock_readfrom "j9sock_readfrom"*/ - int32_t ( *sock_readfrom)(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle) ; - /** see @ref j9sock.c::j9sock_select "j9sock_select"*/ - int32_t ( *sock_select)(struct J9PortLibrary *portLibrary, int32_t nfds, j9fdset_t readfds, j9fdset_t writefds, j9fdset_t exceptfds, j9timeval_t timeout) ; - /** see @ref j9sock.c::j9sock_writeto "j9sock_writeto"*/ - int32_t ( *sock_writeto)(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle) ; - /** see @ref j9sock.c::j9sock_inetntoa "j9sock_inetntoa"*/ - int32_t ( *sock_inetntoa)(struct J9PortLibrary *portLibrary, char **addrStr, uint32_t nipAddr) ; /** see @ref j9sock.c::j9sock_gethostbyaddr "j9sock_gethostbyaddr"*/ int32_t ( *sock_gethostbyaddr)(struct J9PortLibrary *portLibrary, char *addr, int32_t length, int32_t type, j9hostent_t handle) ; - /** see @ref j9sock.c::j9sock_gethostname "j9sock_gethostname"*/ - int32_t ( *sock_gethostname)(struct J9PortLibrary *portLibrary, char *buffer, int32_t length) ; - /** see @ref j9sock.c::j9sock_hostent_aliaslist "j9sock_hostent_aliaslist"*/ - int32_t ( *sock_hostent_aliaslist)(struct J9PortLibrary *portLibrary, j9hostent_t handle, char ***aliasList) ; - /** see @ref j9sock.c::j9sock_hostent_hostname "j9sock_hostent_hostname"*/ - int32_t ( *sock_hostent_hostname)(struct J9PortLibrary *portLibrary, j9hostent_t handle, char** hostName) ; - /** see @ref j9sock.c::j9sock_sockaddr_port "j9sock_sockaddr_port"*/ - uint16_t ( *sock_sockaddr_port)(struct J9PortLibrary *portLibrary, j9sockaddr_t handle) ; - /** see @ref j9sock.c::j9sock_sockaddr_address "j9sock_sockaddr_address"*/ - int32_t ( *sock_sockaddr_address)(struct J9PortLibrary *portLibrary, j9sockaddr_t handle) ; - /** see @ref j9sock.c::j9sock_fdset_init "j9sock_fdset_init"*/ - int32_t ( *sock_fdset_init)(struct J9PortLibrary *portLibrary, j9socket_t socketP) ; - /** see @ref j9sock.c::j9sock_fdset_size "j9sock_fdset_size"*/ - int32_t ( *sock_fdset_size)(struct J9PortLibrary *portLibrary, j9socket_t handle) ; - /** see @ref j9sock.c::j9sock_timeval_init "j9sock_timeval_init"*/ - int32_t ( *sock_timeval_init)(struct J9PortLibrary *portLibrary, uint32_t secTime, uint32_t uSecTime, j9timeval_t timeP) ; - /** see @ref j9sock.c::j9sock_getopt_int "j9sock_getopt_int"*/ - int32_t ( *sock_getopt_int)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval) ; - /** see @ref j9sock.c::j9sock_setopt_int "j9sock_setopt_int"*/ - int32_t ( *sock_setopt_int)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval) ; - /** see @ref j9sock.c::j9sock_getopt_bool "j9sock_getopt_bool"*/ - int32_t ( *sock_getopt_bool)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval) ; - /** see @ref j9sock.c::j9sock_setopt_bool "j9sock_setopt_bool"*/ - int32_t ( *sock_setopt_bool)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval) ; - /** see @ref j9sock.c::j9sock_getopt_byte "j9sock_getopt_byte"*/ - int32_t ( *sock_getopt_byte)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval) ; - /** see @ref j9sock.c::j9sock_setopt_byte "j9sock_setopt_byte"*/ - int32_t ( *sock_setopt_byte)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval) ; - /** see @ref j9sock.c::j9sock_getopt_linger "j9sock_getopt_linger"*/ - int32_t ( *sock_getopt_linger)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval) ; - /** see @ref j9sock.c::j9sock_getopt_sockaddr "j9sock_getopt_sockaddr"*/ - int32_t ( *sock_getopt_sockaddr)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval) ; - /** see @ref j9sock.c::j9sock_setopt_sockaddr "j9sock_setopt_sockaddr"*/ - int32_t ( *sock_setopt_sockaddr)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval) ; - /** see @ref j9sock.c::j9sock_setopt_ipmreq "j9sock_setopt_ipmreq"*/ - int32_t ( *sock_setopt_ipmreq)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipmreq_t optval) ; - /** see @ref j9sock.c::j9sock_linger_enabled "j9sock_linger_enabled"*/ - int32_t ( *sock_linger_enabled)(struct J9PortLibrary *portLibrary, j9linger_t handle, BOOLEAN *enabled) ; - /** see @ref j9sock.c::j9sock_linger_linger "j9sock_linger_linger"*/ - int32_t ( *sock_linger_linger)(struct J9PortLibrary *portLibrary, j9linger_t handle, uint16_t *linger) ; - /** see @ref j9sock.c::j9sock_ipmreq_init "j9sock_ipmreq_init"*/ - int32_t ( *sock_ipmreq_init)(struct J9PortLibrary *portLibrary, j9ipmreq_t handle, uint32_t nipmcast, uint32_t nipinterface) ; - /** see @ref j9sock.c::j9sock_setflag "j9sock_setflag"*/ - int32_t ( *sock_setflag)(struct J9PortLibrary *portLibrary, int32_t flag, int32_t *arg) ; /** see @ref j9sock.c::j9sock_freeaddrinfo "j9sock_freeaddrinfo"*/ int32_t ( *sock_freeaddrinfo)(struct J9PortLibrary *portLibrary, j9addrinfo_t handle) ; /** see @ref j9sock.c::j9sock_getaddrinfo "j9sock_getaddrinfo"*/ @@ -320,40 +226,8 @@ typedef struct J9PortLibrary { int32_t ( *sock_getaddrinfo_length)(struct J9PortLibrary *portLibrary, j9addrinfo_t handle, int32_t *length) ; /** see @ref j9sock.c::j9sock_getaddrinfo_name "j9sock_getaddrinfo_name"*/ int32_t ( *sock_getaddrinfo_name)(struct J9PortLibrary *portLibrary, j9addrinfo_t handle, char *name, int index) ; - /** see @ref j9sock.c::j9sock_getnameinfo "j9sock_getnameinfo"*/ - int32_t ( *sock_getnameinfo)(struct J9PortLibrary *portLibrary, j9sockaddr_t in_addr, int32_t sockaddr_size, char *name, int32_t name_length, int flags) ; - /** see @ref j9sock.c::j9sock_ipv6_mreq_init "j9sock_ipv6_mreq_init"*/ - int32_t ( *sock_ipv6_mreq_init)(struct J9PortLibrary *portLibrary, j9ipv6_mreq_t handle, uint8_t *ipmcast_addr, uint32_t ipv6mr_interface) ; - /** see @ref j9sock.c::j9sock_setopt_ipv6_mreq "j9sock_setopt_ipv6_mreq"*/ - int32_t ( *sock_setopt_ipv6_mreq)(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipv6_mreq_t optval) ; - /** see @ref j9sock.c::j9sock_sockaddr_address6 "j9sock_sockaddr_address6"*/ - int32_t ( *sock_sockaddr_address6)(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *address, uint32_t *length, uint32_t* scope_id) ; - /** see @ref j9sock.c::j9sock_sockaddr_family "j9sock_sockaddr_family"*/ - int32_t ( *sock_sockaddr_family)(struct J9PortLibrary *portLibrary, int16_t *family, j9sockaddr_t handle) ; - /** see @ref j9sock.c::j9sock_sockaddr_init6 "j9sock_sockaddr_init6"*/ - int32_t ( *sock_sockaddr_init6)(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *addr, int32_t addrlength, int16_t family, uint16_t portNetworkOrder, uint32_t flowinfo, uint32_t scope_id, j9socket_t sock) ; - /** see @ref j9sock.c::j9sock_socketIsValid "j9sock_socketIsValid"*/ - int32_t ( *sock_socketIsValid)(struct J9PortLibrary *portLibrary, j9socket_t handle) ; - /** see @ref j9sock.c::j9sock_select_read "j9sock_select_read"*/ - int32_t ( *sock_select_read)(struct J9PortLibrary *portLibrary, j9socket_t j9socketP, int32_t secTime, int32_t uSecTime, BOOLEAN accept) ; - /** see @ref j9sock.c::j9sock_set_nonblocking "j9sock_set_nonblocking"*/ - int32_t ( *sock_set_nonblocking)(struct J9PortLibrary *portLibrary, j9socket_t socketP, BOOLEAN nonblocking) ; /** see @ref j9sock.c::j9sock_error_message "j9sock_error_message"*/ const char* ( *sock_error_message)(struct J9PortLibrary *portLibrary) ; - /** see @ref j9sock.c::j9sock_get_network_interfaces "j9sock_get_network_interfaces"*/ - int32_t ( *sock_get_network_interfaces)(struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct *array,BOOLEAN preferIPv4Stack) ; - /** see @ref j9sock.c::j9sock_free_network_interface_struct "j9sock_free_network_interface_struct"*/ - int32_t ( *sock_free_network_interface_struct)(struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct* array) ; - /** see @ref j9sock.c::j9sock_connect_with_timeout "j9sock_connect_with_timeout"*/ - int32_t ( *sock_connect_with_timeout)(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr, uint32_t timeout, uint32_t step, uint8_t** context) ; - /** see @ref j9sock.c::j9sock_fdset_zero "j9sock_fdset_zero"*/ - void ( *sock_fdset_zero)(struct J9PortLibrary *portLibrary, j9fdset_t j9fdset) ; - /** see @ref j9sock.c::j9sock_fdset_set "j9sock_fdset_set"*/ - void ( *sock_fdset_set)(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) ; - /** see @ref j9sock.c::j9sock_fdset_clr "j9sock_fdset_clr"*/ - void ( *sock_fdset_clr)(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) ; - /** see @ref j9sock.c::j9sock_fdset_isset "j9sock_fdset_isset"*/ - BOOLEAN ( *sock_fdset_isset)(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) ; /** see @ref j9shsem.c::j9shsem_params_init "j9shsem_params_init"*/ int32_t ( *shsem_params_init)(struct J9PortLibrary *portLibrary, struct J9PortShSemParameters *params) ; /** see @ref j9shsem.c::j9shsem_startup "j9shsem_startup"*/ @@ -723,19 +597,8 @@ extern J9_CFUNC int32_t j9port_isCompatible(struct J9PortLibraryVersion *expecte #define j9vmem_get_process_memory_size(param1,param2) OMRPORT_FROM_J9PORT(privatePortLibrary)->vmem_get_process_memory_size(OMRPORT_FROM_J9PORT(privatePortLibrary),param1,param2) #define j9sock_startup() privatePortLibrary->sock_startup(privatePortLibrary) #define j9sock_shutdown() privatePortLibrary->sock_shutdown(privatePortLibrary) -#define j9sock_htons(param1) privatePortLibrary->sock_htons(privatePortLibrary,param1) -#define j9sock_write(param1,param2,param3,param4) privatePortLibrary->sock_write(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_sockaddr(param1,param2,param3) privatePortLibrary->sock_sockaddr(privatePortLibrary,param1,param2,param3) -#define j9sock_read(param1,param2,param3,param4) privatePortLibrary->sock_read(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_socket(param1,param2,param3,param4) privatePortLibrary->sock_socket(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_close(param1) privatePortLibrary->sock_close(privatePortLibrary,param1) -#define j9sock_connect(param1,param2) privatePortLibrary->sock_connect(privatePortLibrary,param1,param2) #define j9sock_inetaddr(param1,param2) privatePortLibrary->sock_inetaddr(privatePortLibrary,param1,param2) #define j9sock_gethostbyname(param1,param2) privatePortLibrary->sock_gethostbyname(privatePortLibrary,param1,param2) -#define j9sock_hostent_addrlist(param1,param2) privatePortLibrary->sock_hostent_addrlist(privatePortLibrary,param1,param2) -#define j9sock_sockaddr_init(param1,param2,param3,param4) privatePortLibrary->sock_sockaddr_init(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_linger_init(param1,param2,param3) privatePortLibrary->sock_linger_init(privatePortLibrary,param1,param2,param3) -#define j9sock_setopt_linger(param1,param2,param3,param4) privatePortLibrary->sock_setopt_linger(privatePortLibrary,param1,param2,param3,param4) #define j9gp_startup() privatePortLibrary->gp_startup(privatePortLibrary) #define j9gp_shutdown() privatePortLibrary->gp_shutdown(privatePortLibrary) #define j9gp_protect(param1,param2) privatePortLibrary->gp_protect(privatePortLibrary,param1,param2) @@ -808,43 +671,7 @@ extern J9_CFUNC int32_t j9port_isCompatible(struct J9PortLibraryVersion *expecte #define j9file_lock_bytes(param1,param2,param3,param4) OMRPORT_FROM_J9PORT(privatePortLibrary)->file_lock_bytes(OMRPORT_FROM_J9PORT(privatePortLibrary),param1,param2,param3,param4) #define j9file_convert_native_fd_to_j9file_fd(param1) OMRPORT_FROM_J9PORT(privatePortLibrary)->file_convert_native_fd_to_omrfile_fd(OMRPORT_FROM_J9PORT(privatePortLibrary),param1) #define j9file_convert_j9file_fd_to_native_fd(param1) OMRPORT_FROM_J9PORT(privatePortLibrary)->file_convert_omrfile_fd_to_native_fd(OMRPORT_FROM_J9PORT(privatePortLibrary),param1) -#define j9sock_htonl(param1) privatePortLibrary->sock_htonl(privatePortLibrary,param1) -#define j9sock_bind(param1,param2) privatePortLibrary->sock_bind(privatePortLibrary,param1,param2) -#define j9sock_accept(param1,param2,param3) privatePortLibrary->sock_accept(privatePortLibrary,param1,param2,param3) -#define j9sock_shutdown_input(param1) privatePortLibrary->sock_shutdown_input(privatePortLibrary,param1) -#define j9sock_shutdown_output(param1) privatePortLibrary->sock_shutdown_output(privatePortLibrary,param1) -#define j9sock_listen(param1,param2) privatePortLibrary->sock_listen(privatePortLibrary,param1,param2) -#define j9sock_ntohl(param1) privatePortLibrary->sock_ntohl(privatePortLibrary,param1) -#define j9sock_ntohs(param1) privatePortLibrary->sock_ntohs(privatePortLibrary,param1) -#define j9sock_getpeername(param1,param2) privatePortLibrary->sock_getpeername(privatePortLibrary,param1,param2) -#define j9sock_getsockname(param1,param2) privatePortLibrary->sock_getsockname(privatePortLibrary,param1,param2) -#define j9sock_readfrom(param1,param2,param3,param4,param5) privatePortLibrary->sock_readfrom(privatePortLibrary,param1,param2,param3,param4,param5) -#define j9sock_select(param1,param2,param3,param4,param5) privatePortLibrary->sock_select(privatePortLibrary,param1,param2,param3,param4,param5) -#define j9sock_writeto(param1,param2,param3,param4,param5) privatePortLibrary->sock_writeto(privatePortLibrary,param1,param2,param3,param4,param5) -#define j9sock_inetntoa(param1,param2) privatePortLibrary->sock_inetntoa(privatePortLibrary,param1,param2) #define j9sock_gethostbyaddr(param1,param2,param3,param4) privatePortLibrary->sock_gethostbyaddr(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_gethostname(param1,param2) privatePortLibrary->sock_gethostname(privatePortLibrary,param1,param2) -#define j9sock_hostent_aliaslist(param1,param2) privatePortLibrary->sock_hostent_aliaslist(privatePortLibrary,param1,param2) -#define j9sock_hostent_hostname(param1,param2) privatePortLibrary->sock_hostent_hostname(privatePortLibrary,param1,param2) -#define j9sock_sockaddr_port(param1) privatePortLibrary->sock_sockaddr_port(privatePortLibrary,param1) -#define j9sock_sockaddr_address(param1) privatePortLibrary->sock_sockaddr_address(privatePortLibrary,param1) -#define j9sock_fdset_init(param1) privatePortLibrary->sock_fdset_init(privatePortLibrary,param1) -#define j9sock_fdset_size(param1) privatePortLibrary->sock_fdset_size(privatePortLibrary,param1) -#define j9sock_timeval_init(param1,param2,param3) privatePortLibrary->sock_timeval_init(privatePortLibrary,param1,param2,param3) -#define j9sock_getopt_int(param1,param2,param3,param4) privatePortLibrary->sock_getopt_int(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_setopt_int(param1,param2,param3,param4) privatePortLibrary->sock_setopt_int(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_getopt_bool(param1,param2,param3,param4) privatePortLibrary->sock_getopt_bool(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_setopt_bool(param1,param2,param3,param4) privatePortLibrary->sock_setopt_bool(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_getopt_byte(param1,param2,param3,param4) privatePortLibrary->sock_getopt_byte(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_setopt_byte(param1,param2,param3,param4) privatePortLibrary->sock_setopt_byte(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_getopt_linger(param1,param2,param3,param4) privatePortLibrary->sock_getopt_linger(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_getopt_sockaddr(param1,param2,param3,param4) privatePortLibrary->sock_getopt_sockaddr(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_setopt_sockaddr(param1,param2,param3,param4) privatePortLibrary->sock_setopt_sockaddr(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_setopt_ipmreq(param1,param2,param3,param4) privatePortLibrary->sock_setopt_ipmreq(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_linger_enabled(param1,param2) privatePortLibrary->sock_linger_enabled(privatePortLibrary,param1,param2) -#define j9sock_linger_linger(param1,param2) privatePortLibrary->sock_linger_linger(privatePortLibrary,param1,param2) -#define j9sock_ipmreq_init(param1,param2,param3) privatePortLibrary->sock_ipmreq_init(privatePortLibrary,param1,param2,param3) -#define j9sock_setflag(param1,param2) privatePortLibrary->sock_setflag(privatePortLibrary,param1,param2) #define j9sock_freeaddrinfo(param1) privatePortLibrary->sock_freeaddrinfo(privatePortLibrary,param1) #define j9sock_getaddrinfo(param1,param2,param3) privatePortLibrary->sock_getaddrinfo(privatePortLibrary,param1,param2,param3) #define j9sock_getaddrinfo_address(param1,param2,param3,param4) privatePortLibrary->sock_getaddrinfo_address(privatePortLibrary,param1,param2,param3,param4) @@ -852,23 +679,7 @@ extern J9_CFUNC int32_t j9port_isCompatible(struct J9PortLibraryVersion *expecte #define j9sock_getaddrinfo_family(param1,param2,param3) privatePortLibrary->sock_getaddrinfo_family(privatePortLibrary,param1,param2,param3) #define j9sock_getaddrinfo_length(param1,param2) privatePortLibrary->sock_getaddrinfo_length(privatePortLibrary,param1,param2) #define j9sock_getaddrinfo_name(param1,param2,param3) privatePortLibrary->sock_getaddrinfo_name(privatePortLibrary,param1,param2,param3) -#define j9sock_getnameinfo(param1,param2,param3,param4,param5) privatePortLibrary->sock_getnameinfo(privatePortLibrary,param1,param2,param3,param4,param5) -#define j9sock_ipv6_mreq_init(param1,param2,param3) privatePortLibrary->sock_ipv6_mreq_init(privatePortLibrary,param1,param2,param3) -#define j9sock_setopt_ipv6_mreq(param1,param2,param3,param4) privatePortLibrary->sock_setopt_ipv6_mreq(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_sockaddr_address6(param1,param2,param3,param4) privatePortLibrary->sock_sockaddr_address6(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_sockaddr_family(param1,param2) privatePortLibrary->sock_sockaddr_family(privatePortLibrary,param1,param2) -#define j9sock_sockaddr_init6(param1,param2,param3,param4,param5,param6,param7,param8) privatePortLibrary->sock_sockaddr_init6(privatePortLibrary,param1,param2,param3,param4,param5,param6,param7,param8) -#define j9sock_socketIsValid(param1) privatePortLibrary->sock_socketIsValid(privatePortLibrary,param1) -#define j9sock_select_read(param1,param2,param3,param4) privatePortLibrary->sock_select_read(privatePortLibrary,param1,param2,param3,param4) -#define j9sock_set_nonblocking(param1,param2) privatePortLibrary->sock_set_nonblocking(privatePortLibrary,param1,param2) #define j9sock_error_message() privatePortLibrary->sock_error_message(privatePortLibrary) -#define j9sock_get_network_interfaces(param1,param2) privatePortLibrary->sock_get_network_interfaces(privatePortLibrary,param1,param2) -#define j9sock_free_network_interface_struct(param1) privatePortLibrary->sock_free_network_interface_struct(privatePortLibrary,param1) -#define j9sock_connect_with_timeout(param1,param2,param3,param4,param5) privatePortLibrary->sock_connect_with_timeout(privatePortLibrary,param1,param2,param3,param4,param5) -#define j9sock_fdset_zero(param1) privatePortLibrary->sock_fdset_zero(privatePortLibrary,param1) -#define j9sock_fdset_set(param1,param2) privatePortLibrary->sock_fdset_set(privatePortLibrary,param1,param2) -#define j9sock_fdset_clr(param1,param2) privatePortLibrary->sock_fdset_clr(privatePortLibrary,param1,param2) -#define j9sock_fdset_isset(param1,param2) privatePortLibrary->sock_fdset_isset(privatePortLibrary,param1,param2) #define j9str_ftime(param1,param2,param3,param4) OMRPORT_FROM_J9PORT(privatePortLibrary)->str_ftime(OMRPORT_FROM_J9PORT(privatePortLibrary),param1,param2,param3,param4) #define j9mmap_startup() OMRPORT_FROM_J9PORT(privatePortLibrary)->mmap_startup(OMRPORT_FROM_J9PORT(privatePortLibrary)) #define j9mmap_shutdown() OMRPORT_FROM_J9PORT(privatePortLibrary)->mmap_shutdown(OMRPORT_FROM_J9PORT(privatePortLibrary)) diff --git a/runtime/oti/j9socket.h b/runtime/oti/j9socket.h index cc60d486fb3..43ffdc2d85f 100644 --- a/runtime/oti/j9socket.h +++ b/runtime/oti/j9socket.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1998, 2014 IBM Corp. and others + * Copyright (c) 1998, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -30,12 +30,8 @@ #include "j9porterror.h" /* Socket types, stream & datagram */ -#define J9SOCK_STREAM 0 -#define J9SOCK_DGRAM 1 #define J9SOCK_AFINET 2 -#define J9SOCK_ANY 3 -#define J9SOCK_DEFPROTOCOL 0 -#define J9SOCK_INADDR_ANY (uint32_t)0 +#define J9SOCK_INADDR_ANY (uint32_t)0 #define J9SOCK_NOFLAGS (uint32_t)0 /* The default flag argument value, as in a recv */ #define J9SOCK_INADDR_LEN 4 /* The length in bytes of a binary IPv4 internet address */ #define J9SOCK_INADDR6_LEN 16 /* The length in bytes of a binary IPv6 internet address */ @@ -58,52 +54,10 @@ #define J9PROTOCOL_FAMILY_INET4 J9ADDR_FAMILY_AFINET4 /* IPv6 */ #define J9PROTOCOL_FAMILY_INET6 J9ADDR_FAMILY_AFINET6 /* IPv6 */ -/* Portable defines for socket levels */ -#define J9_SOL_SOCKET 1 -#define J9_IPPROTO_TCP 2 -#define J9_IPPROTO_IP 3 -#define J9_IPPROTO_IPV6 4 - -/* Portable defines for socket options */ -#define J9_SO_LINGER 1 -#define J9_SO_KEEPALIVE 2 -#define J9_TCP_NODELAY 3 -#define J9_MCAST_TTL 4 -#define J9_MCAST_ADD_MEMBERSHIP 5 -#define J9_MCAST_DROP_MEMBERSHIP 6 -#define J9_MCAST_INTERFACE 7 -#define J9_SO_REUSEADDR 8 -#define J9_SO_REUSEPORT 9 - -#define J9_SO_SNDBUF 11 -#define J9_SO_RCVBUF 12 -#define J9_SO_BROADCAST 13 /*[PR1FLSKTU] Support datagram broadcasts */ -#define J9_SO_OOBINLINE 14 -#define J9_IP_MULTICAST_LOOP 15 -#define J9_IP_TOS 16 -#define J9_MCAST_INTERFACE_2 17 -#define J9_IPV6_ADD_MEMBERSHIP 18 -#define J9_IPV6_DROP_MEMBERSHIP 19 - -/* Portable defines for socket read/write options */ -#define J9SOCK_MSG_PEEK 1 -#define J9SOCK_MSG_OOB 2 - /* Platform Constants */ -typedef struct j9socket_struct *j9socket_t; typedef struct j9sockaddr_struct *j9sockaddr_t; typedef struct j9hostent_struct *j9hostent_t; -typedef struct j9fdset_struct *j9fdset_t; -typedef struct j9timeval_struct *j9timeval_t; -typedef struct j9linger_struct *j9linger_t; -typedef struct j9ipmreq_struct *j9ipmreq_t; typedef struct j9addrinfo_struct *j9addrinfo_t; /* IPv6 */ -typedef struct j9ipv6_mreq_struct *j9ipv6_mreq_t; /* IPv6 */ - -/* constants for calling multi-call functions */ -#define J9_PORT_SOCKET_STEP_START 10 -#define J9_PORT_SOCKET_STEP_CHECK 20 -#define J9_PORT_SOCKET_STEP_DONE 30 #endif /* j9socket_h */ diff --git a/runtime/oti/portsock.h b/runtime/oti/portsock.h index 4614d59dc65..830909de02d 100644 --- a/runtime/oti/portsock.h +++ b/runtime/oti/portsock.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1998, 2017 IBM Corp. and others + * Copyright (c) 1998, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -26,13 +26,6 @@ #ifndef portsock_h #define portsock_h -#if defined(LINUX) || defined(AIXPPC) || defined(OSX) -/* needed to pull in "struct timeval" */ -#include -#elif defined(J9ZOS390) -#include -#endif - /* This file did redirect to the port socket header but that sort of reaching back into a private module is very unsafe and breaks with vpath since the header will no longer be in the root of port */ /* As a temporary solution, the structures we were looking for will be stored here. Eventually, this file should be merged into the port_api.h */ @@ -41,7 +34,6 @@ #define UDATA UDATA_win32_ #include #undef UDATA /* this is safe because our UDATA is a typedef, not a macro */ -#define SOCKET_USE_IPV4_MASK '\x4' /* 00000100 - this tells which one to pick when doing an operation */ /* Undefine the winsockapi because winsock2 defines it. Removes warnings. */ #if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_) @@ -57,8 +49,6 @@ #include "j9comp.h" -#define J9SOCK_BADSOCKET -1 - #ifdef NI_MAXHOST #define OSNIMAXHOST NI_MAXHOST #define OSNIMAXSERV NI_MAXSERV @@ -82,126 +72,17 @@ typedef struct j9addrinfo_struct { } j9addrinfo_struct; typedef struct hostent OSHOSTENT; -typedef struct ip_mreq OSIPMREQ; -typedef struct ipv6_mreq OSIPMREQ6; - -typedef struct linger OSLINGER; #if defined(WIN32) -typedef SOCKET OSSOCKET; /* as returned by socket() */ typedef SOCKADDR_IN OSSOCKADDR; /* as used by bind() and friends */ -typedef struct sockaddr_storage OSSOCKADDR_STORAGE; /* IPv6 */ #else /* !WIN32 */ -typedef int OSSOCKET; /* as returned by socket() */ typedef struct sockaddr_in OSSOCKADDR; /* as used by bind() and friends */ -#if !defined(J9ZTPF) /* z/TPF doesn't have IPv6 */ -typedef struct sockaddr_storage OSSOCKADDR_STORAGE; /* IPv6 */ -#endif /* !defined(J9ZTPF) */ -#endif /* !WIN32 */ - -#if defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) || defined(OSX) -#define SOCKET_CAST(x) ((struct j9socket_struct*) x)->sock -#else /* defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) || defined(OSX) */ -/* all the non-unix ports use this variant */ -/* The sockets returned as a j9socket_t (j9socket_struct*) are not actual structs, we just pretend that the pointer is a struct and never dereference it. -*/ -#ifdef NO_LVALUE_CASTING -#define SOCKET_CAST(x) (*((OSSOCKET *) &(x))) -#else /* NO_LVALUE_CASTING */ -#define SOCKET_CAST(x) ((OSSOCKET)x) -#endif /* !NO_LVALUE_CASTING */ -#endif /* defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) || defined(OSX) */ - - -typedef struct j9sockaddr_struct { -#if defined(IPv6_FUNCTION_SUPPORT) || (defined(WIN32)) - OSSOCKADDR_STORAGE addr; -#else - OSSOCKADDR addr; -#endif -} j9sockaddr_struct; - -typedef struct j9fdset_struct { -#if defined(LINUX) || defined(OSX) - intptr_t fd; /* fd == -1 indicates the j9fdset is "empty" fd. */ -#else - fd_set handle; -#endif /* defined(LINUX) || defined(OSX) */ -} j9fdset_struct; - -typedef struct j9timeval_struct { - struct timeval time; -} j9timeval_struct; - -/* -* Socket structure on windows requires 2 sockets due to the fact that microsoft does not -* handle ipv6-mapped ipv6 addresses. Therefore we need to listen to sockets on both -* the ipv6 and ipv4 stacks, when in a mode where we support ipv6. -*/ -typedef struct j9socket_struct { -#if defined(WIN32) - OSSOCKET ipv4; - OSSOCKET ipv6; - uint8_t flags; -#else /* WIN32 */ - OSSOCKET sock; - uint16_t family; #endif /* !WIN32 */ -} j9socket_struct; - -typedef struct j9linger_struct { - OSLINGER linger; -} j9linger_struct; typedef struct j9hostent_struct { OSHOSTENT *entity; } j9hostent_struct; -typedef struct j9ipmreq_struct { - OSIPMREQ addrpair; -} j9ipmreq_struct; - -/** new structure for IPv6 mulitcast requests */ -typedef struct j9ipv6_mreq_struct { -#if defined(IPv6_FUNCTION_SUPPORT) || ((defined(WIN32))) - OSIPMREQ6 mreq; -#else - int dummy; -#endif -} j9ipv6_mreq_struct; - -/* structure for returning either and IPV4 or IPV6 ip address */ -typedef struct j9ipAddress_struct { - union { -#if defined(IPv6_FUNCTION_SUPPORT) || ((defined(WIN32))) - uint8_t bytes[sizeof(struct in6_addr)]; -#else - uint8_t bytes[sizeof(struct in_addr)]; -#endif - struct in_addr inAddr; -#if defined(IPv6_FUNCTION_SUPPORT) || ((defined(WIN32))) - struct in6_addr in6Addr; -#endif - } addr; - uint32_t length; - uint32_t scope; -} j9ipAddress_struct; - -/* structure for returning network interface information */ -typedef struct j9NetworkInterface_struct{ - char *name; - char *displayName; - uint32_t numberAddresses; - uint32_t index; - struct j9ipAddress_struct *addresses; -} j9NetworkInterface_struct ; - -/* array of network interface structures */ -typedef struct j9NetworkInterfaceArray_struct { - uint32_t length; - struct j9NetworkInterface_struct* elements; -} j9NetworkInterfaceArray_struct; - #endif /* portsock_h */ diff --git a/runtime/port/common/j9port.c b/runtime/port/common/j9port.c index e43a94ef4e7..7177b825319 100644 --- a/runtime/port/common/j9port.c +++ b/runtime/port/common/j9port.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -52,19 +52,8 @@ static J9PortLibrary MasterPortLibraryTable = { j9sysinfo_get_cache_info, /* sysinfo_get_cache_info */ j9sock_startup, /* sock_startup */ j9sock_shutdown, /* sock_shutdown */ - j9sock_htons, /* sock_htons */ - j9sock_write, /* sock_write */ - j9sock_sockaddr, /* sock_sockaddr */ - j9sock_read, /* sock_read */ - j9sock_socket, /* sock_socket */ - j9sock_close, /* sock_close */ - j9sock_connect, /* sock_connect */ j9sock_inetaddr, /* sock_inetaddr */ j9sock_gethostbyname, /* sock_gethostbyname */ - j9sock_hostent_addrlist, /* sock_hostent_addrlist */ - j9sock_sockaddr_init, /* sock_sockaddr_init */ - j9sock_linger_init, /* sock_linger_init */ - j9sock_setopt_linger, /* sock_setopt_linger */ j9gp_startup, /* gp_startup */ j9gp_shutdown, /* gp_shutdown */ j9gp_protect, /* gp_protect */ @@ -80,43 +69,7 @@ static J9PortLibrary MasterPortLibraryTable = { j9sysinfo_DLPAR_enabled, /* sysinfo_DLPAR_enabled */ j9sysinfo_DLPAR_max_CPUs, /* sysinfo_DLPAR_max_CPUs */ j9sysinfo_weak_memory_consistency, /* sysinfo_weak_memory_consistency */ - j9sock_htonl, /* sock_htonl */ - j9sock_bind, /* sock_bind */ - j9sock_accept, /* sock_accept */ - j9sock_shutdown_input, /* sock_shutdown_input */ - j9sock_shutdown_output, /* sock_shutdown_output */ - j9sock_listen, /* sock_listen */ - j9sock_ntohl, /* sock_ntohl */ - j9sock_ntohs, /* sock_ntohs */ - j9sock_getpeername, /* sock_getpeername */ - j9sock_getsockname, /* sock_getsockname */ - j9sock_readfrom, /* sock_readfrom */ - j9sock_select, /* sock_select */ - j9sock_writeto, /* sock_writeto */ - j9sock_inetntoa, /* sock_inetntoa */ j9sock_gethostbyaddr, /* sock_gethostbyaddr */ - j9sock_gethostname, /* sock_gethostname */ - j9sock_hostent_aliaslist, /* sock_hostent_aliaslist */ - j9sock_hostent_hostname, /* sock_hostent_hostname */ - j9sock_sockaddr_port, /* sock_sockaddr_port */ - j9sock_sockaddr_address, /* sock_sockaddr_address */ - j9sock_fdset_init, /* sock_fdset_init */ - j9sock_fdset_size, /* sock_fdset_size */ - j9sock_timeval_init, /* sock_timeval_init */ - j9sock_getopt_int, /* sock_getopt_int */ - j9sock_setopt_int, /* sock_setopt_int */ - j9sock_getopt_bool, /* sock_getopt_bool */ - j9sock_setopt_bool, /* sock_setopt_bool */ - j9sock_getopt_byte, /* sock_getopt_byte */ - j9sock_setopt_byte, /* sock_setopt_byte */ - j9sock_getopt_linger, /* sock_getopt_linger */ - j9sock_getopt_sockaddr, /* sock_getopt_sockaddr */ - j9sock_setopt_sockaddr, /* sock_setopt_sockaddr */ - j9sock_setopt_ipmreq, /* sock_setopt_ipmreq */ - j9sock_linger_enabled, /* sock_linger_enabled */ - j9sock_linger_linger, /* sock_linger_linger */ - j9sock_ipmreq_init, /* sock_ipmreq_init */ - j9sock_setflag, /* sock_setflag */ j9sock_freeaddrinfo, /* sock_freeaddrinfo */ j9sock_getaddrinfo, /* sock_getaddrinfo */ j9sock_getaddrinfo_address, /* sock_getaddrinfo_address */ @@ -124,23 +77,7 @@ static J9PortLibrary MasterPortLibraryTable = { j9sock_getaddrinfo_family, /* sock_getaddrinfo_family */ j9sock_getaddrinfo_length, /* sock_getaddrinfo_length */ j9sock_getaddrinfo_name, /* sock_getaddrinfo_name */ - j9sock_getnameinfo, /* sock_getnameinfo */ - j9sock_ipv6_mreq_init, /* sock_ipv6_mreq_init */ - j9sock_setopt_ipv6_mreq, /* sock_setopt_ipv6_mreq */ - j9sock_sockaddr_address6, /* sock_sockaddr_address6 */ - j9sock_sockaddr_family, /* sock_sockaddr_family */ - j9sock_sockaddr_init6, /* sock_sockaddr_init6 */ - j9sock_socketIsValid, /* sock_socketIsValid */ - j9sock_select_read, /* sock_select_read */ - j9sock_set_nonblocking, /* sock_set_nonblocking */ j9sock_error_message, /* sock_error_message */ - j9sock_get_network_interfaces, /* sock_get_network_interfaces */ - j9sock_free_network_interface_struct, /* sock_free_network_interface_struct */ - j9sock_connect_with_timeout, /* sock_connect_with_timeout */ - j9sock_fdset_zero, /* sock_fdset_zero */ - j9sock_fdset_set, /* sock_fdset_set */ - j9sock_fdset_clr, /* sock_fdset_clr */ - j9sock_fdset_isset, /* sock_fdset_isset */ j9shsem_params_init, /* shsem_parameters_init */ j9shsem_startup, /* shsem_startup */ j9shsem_shutdown, /* shsem_shutdown */ diff --git a/runtime/port/common/j9sock.c b/runtime/port/common/j9sock.c index 3fd807d17f3..92441070992 100644 --- a/runtime/port/common/j9sock.c +++ b/runtime/port/common/j9sock.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -28,189 +28,6 @@ #include "j9sock.h" #include "portpriv.h" -int32_t platformSocketLevel( int32_t portableSocketLevel ); -int32_t platformSocketOption( int32_t portableSocketOption ); - - -/** - * @internal - * Map the portable to the platform socket level. Used to resolve the arguments of socket option functions. - * Levels currently in use are: - * \arg SOL_SOCKET, for most options - * \arg IPPROTO_TCP, for the TCP noDelay option - * \arg IPPROTO_IP, for the option operations associated with multicast (join, drop, interface) - * - * @param[in] portableSocketLevel The portable socket level to convert. - * - * @return the platform socket level or a (negative) error code if no equivalent level exists. - */ -int32_t -platformSocketLevel( int32_t portableSocketLevel ) -{ - switch (portableSocketLevel) { - case J9_SOL_SOCKET: - return OS_SOL_SOCKET; - case J9_IPPROTO_TCP: - return OS_IPPROTO_TCP; - case J9_IPPROTO_IP: - return OS_IPPROTO_IP; - } - return J9PORT_ERROR_SOCKET_SOCKLEVELINVALID; -} -/** - * @internal - * Map the portable to the platform socket options. Used to resolve the arguments of socket option functions. - * Options currently in supported are: - * \arg SOL_LINGER, the linger timeout - * \arg TCP_NODELAY, the buffering scheme implementing Nagle's algorithm - * \arg IP_MULTICAST_TTL, the packet Time-To-Live - * \arg IP_ADD_MEMBERSHIP, to join a multicast group - * \arg IP_DROP_MEMBERSHIP, to leave a multicast group - * \arg IP_MULTICAST_IF, the multicast interface - * - * @param[in] portableSocketOption The portable socket option to convert. - * - * @return the platform socket option or a (negative) error code if no equivalent option exists. - */ -/*[PR1FLSKTU] Support datagram broadcasts */ -int32_t -platformSocketOption( int32_t portableSocketOption ) -{ - switch (portableSocketOption) { - case J9_SO_LINGER: - return OS_SO_LINGER; - case J9_SO_KEEPALIVE: - return OS_SO_KEEPALIVE; - case J9_TCP_NODELAY: - return OS_TCP_NODELAY; - case J9_MCAST_TTL: - return OS_MCAST_TTL; - case J9_MCAST_ADD_MEMBERSHIP: - return OS_MCAST_ADD_MEMBERSHIP; - case J9_MCAST_DROP_MEMBERSHIP: - return OS_MCAST_DROP_MEMBERSHIP; - case J9_MCAST_INTERFACE: - return OS_MCAST_INTERFACE; - case J9_SO_REUSEADDR: - return OS_SO_REUSEADDR; - case J9_SO_SNDBUF: - return OS_SO_SNDBUF; - case J9_SO_RCVBUF: - return OS_SO_RCVBUF; - case J9_SO_BROADCAST: - return OS_SO_BROADCAST; - case J9_SO_OOBINLINE: - return OS_SO_OOBINLINE; - case J9_IP_MULTICAST_LOOP: - return OS_MCAST_LOOP; - case J9_IP_TOS: - return OS_IP_TOS; - } - return J9PORT_ERROR_SOCKET_OPTUNSUPP; -} - -/** - * The accept function extracts the first connection on the queue of pending connections - * on socket sock. It then creates a new socket and returns a handle to the new socket. - * The newly created socket is the socket that will handle the actual the connection and - * has the same properties as socket sock. - * - * The accept function can block the caller until a connection is present if no pending - * connections are present on the queue. - * - * @param[in] portLibrary The port library. - * @param[in] serverSock A j9socket_t from which data will be read. - * @param[in] addrHandle An optional pointer to a buffer that receives the address of the connecting - * entity, as known to the communications layer. The exact format of the addr parameter - * is determined by the address family established when the socket was created. - * @param[in] sockHandle A pointer to a j9socket_t which will point to the newly created - * socket once accept returns successfully - * - * @return - * \arg 0 on success - * \arg J9PORT_ERROR_SOCKET_BADSOCKET, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if socket library uninitialized - * \arg J9PORT_ERROR_SOCKET_INTERRUPTED, the call was cancelled - * \arg J9PORT_ERROR_SOCKET_ADDRNOTAVAIL, the addr parameter is not valid - * \arg J9PORT_ERROR_SOCKET_SYSTEMBUSY, if system busy handling other requests - * \arg J9PORT_ERROR_SOCKET_SYSTEMFULL, is too many sockets are active - * \arg J9PORT_ERROR_SOCKET_WOULDBLOCK, the socket is marked as nonblocking and no connections are present to be accepted., - */ -int32_t -j9sock_accept(struct J9PortLibrary *portLibrary, j9socket_t serverSock, j9sockaddr_t addrHandle, j9socket_t *sockHandle) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * The bind function is used on an unconnected socket before subsequent - * calls to the connect or listen functions. When a socket is created with a - * call to the socket function, it exists in a name space (address family), but - * it has no name assigned to it. Use bind to establish the local association - * of the socket by assigning a local name to an unnamed socket. - * - * @param[in] portLibrary The port library. - * @param[in] sock j9socket_t which will be be associated with the specified name. - * @param[in] addr Address to bind to socket. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_BADSOCKET, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if socket library uninitialized - * \arg J9PORT_ERROR_SOCKET_ADDRINUSE A process on the machine is already bound to the same fully-qualified address - * and the socket has not been marked to allow address re-use with SO_REUSEADDR. - * \arg J9PORT_ERROR_SOCKET_ADDRNOTAVAIL The specified address is not a valid address for this machine - * \arg J9PORT_ERROR_SOCKET_SYSTEMBUSY, if system busy handling other requests - * \arg J9PORT_ERROR_SOCKET_SYSTEMFULL, is too many sockets are active - * \arg J9PORT_ERROR_SOCKET_BADADDR, the addr parameter is not a valid part of the user address space, - */ -int32_t -j9sock_bind(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * The close function closes a socket. Use it to release the socket descriptor socket so - * further references to socket will fail. - * - * @param[in] portLibrary The port library. - * @param[in] sock j9socket_t which will be closed. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_BADSOCKET, on generic error - * \arg J9PORT_ERROR_SOCKET_SYSTEMBUSY, if system busy handling other requests - * \arg J9PORT_ERROR_SOCKET_WOULDBLOCK, the socket is marked as nonblocking and SO_LINGER is set to a nonzero time-out value. - */ -int32_t -j9sock_close(struct J9PortLibrary *portLibrary, j9socket_t *sock) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Establish a connection to a peer. - * - * For non-blocking sockets: If a connection cannot be immediately established - * this function will return J9PORT_ERROR_SOCK_EINPROGRESS and the connection - * attempt will continue to take place asynchronously. Subsequent calls to this - * function while a connection attempt is taking place will return - * J9PORT_ERROR_SOCKET_ALREADYINPROGRESS. Once the asynchronous connection attempt - * completes this function will return with J9PORT_ERROR_SOCKET_ISCONNECTED or - * J9PORT_ERROR_SOCKET_ALREADYBOUND. It is not recommended that users call this - * function multiple times to determine when the connection attempt has succeeded. - * Instead, the j9sock_select() function can be used to determine when the socket - * is ready for reading or writing. - * - * @param[in] portLibrary The port library. - * @param[in] sock pointer to the unconnected local socket. - * @param[in] addr pointer to the sockaddr, specifying remote host/port. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_connect(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} /** * Return an error message describing the last OS error that occurred. The last * error returned is not thread safe, it may not be related to the operation that @@ -230,52 +47,7 @@ j9sock_error_message(struct J9PortLibrary *portLibrary) { return "Operation Failed"; } -/** - * Create a file descriptor (FD) set of one element. The call may not be generally useful, - * as it currently only supports a single FD and is assumed to be used in conjunction with the - * j9sock_select function. - * - * @param[in] portLibrary The port library. - * @param[in] socketP pointer to the socket to be added to the FD set. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_fdset_init(struct J9PortLibrary *portLibrary, j9socket_t socketP) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - J9SocketPTB *ptBuffers = j9sock_ptb_get(portLibrary); - if (NULL == ptBuffers) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - - if (NULL == ptBuffers->fdset) { - ptBuffers->fdset = omrmem_allocate_memory(sizeof(struct j9fdset_struct), OMRMEM_CATEGORY_PORT_LIBRARY); - if (NULL == ptBuffers->fdset) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - } - memset(ptBuffers->fdset, 0, sizeof(struct j9fdset_struct)); - return 0; -} -/** - * Answer the size information, as required by @ref j9sock_select(), to act on a j9fdset_t that has "handle" set. - * - * @param[in] portLibrary The port library. - * @param[in] handle - * - * @return the maximum size of the fdset, otherwise the (negative) error code. - * - * @note On Unix, the value was the maximum file descriptor plus one, although - * on many flavors, the value is ignored in the select function. - * On Windows, the value is ignored by the select function. - */ -int32_t -j9sock_fdset_size(struct J9PortLibrary *portLibrary, j9socket_t handle) -{ - return 0; -} /** * Frees the memory created by the call to @ref j9sock_getaddrinfo(). * @@ -438,6 +210,7 @@ j9sock_getaddrinfo_name(struct J9PortLibrary *portLibrary, j9addrinfo_t handle, { return 0; } + /** * Answer information on the host referred to by the address. The information includes name, aliases and * addresses for the nominated host (the latter being relevant on multi-homed hosts). @@ -456,6 +229,7 @@ j9sock_gethostbyaddr(struct J9PortLibrary *portLibrary, char *addr, int32_t leng { return 0; } + /** * Answer information on the host, specified by name. The information includes host name, * aliases and addresses. @@ -471,281 +245,7 @@ j9sock_gethostbyname(struct J9PortLibrary *portLibrary, const char *name, j9host { return 0; } -/** - * Answer the name of the local host machine. - * - * @param[in] portLibrary The port library. - * @param[in,out] buffer The string buffer to receive the name - * @param[in] length The length of the buffer - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_gethostname(struct J9PortLibrary *portLibrary, char *buffer, int32_t length ) -{ - return 0; -} -/** - * Answers the host name of the "in_addr" in a preallocated buffer. - * - * The preallocated buffer for "name" should be the size of the maximum host name: OSNIMAXHOST. - * Currently only AF_INET and AF_INET6 are supported. - * - * @param[in] portLibrary The port library. - * @param[in] in_addr The address we want to do a name lookup on - * @param[in] sockaddr_size The size of "in_addr" - * @param[out] name The hostname of the passed address in a preallocated buffer. - * @param[in] name_length The length of the buffer pointed to by name - * @param[in] flags Flags on how to form the response (see man pages or doc for getnameinfo) - * - * @return 0, if no errors occurred, otherwise the (negative) error code - * - * @note Added for IPv6 support. - * @note "flags" do not affect results on OS's that do not support the IPv6 calls. - */ -int32_t -j9sock_getnameinfo(struct J9PortLibrary *portLibrary, j9sockaddr_t in_addr, int32_t sockaddr_size, char *name, int32_t name_length, int flags) -{ - return 0; -} -/** - * Answer the value of the nominated boolean socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the boolean to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_bool(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the value of the nominated byte socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the byte to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_byte(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the value of the nominated integer socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the integer to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_int(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the value of the socket linger option. - * See the @ref j9sock_linger_init for details of the linger behavior. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value - * @param[in] optlevel The level within the IP stack at which the option is defined - * @param[in] optname The name of the option to retrieve - * @param[out] optval Pointer to the linger struct to update with the option value - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_getopt_linger(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the value of the socket option, an address struct. - * Currently only used to retrieve the interface of multicast sockets, - * but the more general call style has been used. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the sockaddr struct to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_sockaddr(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the remote name for the socket. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the socket to get the address of. - * @param[out] addrHandle Pointer to the sockaddr struct to update with the address. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getpeername(struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the local name for the socket. Note, the stack getsockname function - * actually answers a sockaddr structure, not a string name as the function name - * might imply. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the socket to get the address of. - * @param[out] addrHandle Pointer to the sockaddr struct to update with the address. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getsockname(struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the nominated element of the address list within the argument hostent struct. - * The address is in network order. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the hostent struct, in which to access the addr_list. - * @param[in] index The index of the element within the addr_list to retrieve. - * - * @return the address, in network order. - */ -int32_t -j9sock_hostent_addrlist(struct J9PortLibrary *portLibrary, j9hostent_t handle, uint32_t index) -{ - return 0; -} -/** - * Answer a reference to the list of alternative names for the host within the argument hostent struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the hostent struct, in which to access the addr_list - * @param[out] aliasList Pointer to the list of alternative names, to be updated - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_hostent_aliaslist(struct J9PortLibrary *portLibrary, j9hostent_t handle, char ***aliasList) -{ - return 0; -} -/** - * Answer the host name (string) within the argument hostent struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the hostent struct, in which to access the hostName. - * @param[out] hostName Host name string. - * - * @return 0, the function does not validate the name access. - */ -int32_t -j9sock_hostent_hostname(struct J9PortLibrary *portLibrary, j9hostent_t handle, char** hostName) -{ - return 0; -} -/** - * Answer the 32 bit host ordered argument, in network byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 32 bit host ordered number. - * - * @return the 32 bit network ordered number. - */ -int32_t -j9sock_htonl(struct J9PortLibrary *portLibrary, int32_t val) -{ - return val; -} -/** - * Answer the 16 bit host ordered argument, in network byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 16 bit host ordered number. - * - * @return the 16 bit network ordered number. - */ -uint16_t -j9sock_htons(struct J9PortLibrary *portLibrary, uint16_t val) -{ - return val; -} /** * Answer the dotted IP string as an Internet address. * @@ -760,890 +260,38 @@ j9sock_inetaddr(struct J9PortLibrary *portLibrary, const char *addrStr, uint32_t { return 0; } + /** - * Answer the Internet address as a dotted IP string. + * Terminates use of the socket library. No sockets should be in use or the results + * of this operation are unpredictable. Frees any resources held by the socket library. * * @param[in] portLibrary The port library. - * @param[out] addrStr The dotted IP string. - * @param[in] nipAddr The Internet address. * - * @return 0, if no errors occurred, otherwise the (negative) error code. + * @return 0, if no errors occurred, otherwise the (negative) error code */ int32_t -j9sock_inetntoa(struct J9PortLibrary *portLibrary, char **addrStr, uint32_t nipAddr) +j9sock_shutdown(struct J9PortLibrary *portLibrary ) { + j9sock_ptb_shutdown(portLibrary); return 0; } + /** - * Initializes a new multicast membership structure. The membership structure is used to join & leave - * multicast groups @see j9sock_setopt_ipmreq. The group may be joined using 0 (J9SOCK_INADDR_ANY) - * as the local interface, in which case the default local address will be used. + * Initiate the use of sockets by a process. This function must be called before any other socket calls. * * @param[in] portLibrary The port library. - * @param[out] handle Pointer to the multicast membership struct. - * @param[in] nipmcast The address, in network order, of the multicast group to join. - * @param[in] nipinterface The address, in network order, of the local machine interface to join on. * - * @return 0, if no errors occurred, otherwise the (negative) error code. + * @return 0 on success, negative error code on failure. Error code values returned are + * \arg J9PORT_ERROR_STARTUP_SOCK + * \arg J9PORT_ERROR_SOCKET_OPFAILED + * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED */ int32_t -j9sock_ipmreq_init(struct J9PortLibrary *portLibrary, j9ipmreq_t handle, uint32_t nipmcast, uint32_t nipinterface) +j9sock_startup(struct J9PortLibrary *portLibrary) { - memset(handle, 0, sizeof(struct j9ipmreq_struct)); - handle->addrpair.imr_multiaddr.s_addr = nipmcast; - handle->addrpair.imr_interface.s_addr = nipinterface; - - return 0; + int32_t rc = 0; + if (0 != j9sock_ptb_init(portLibrary)) { + rc = J9PORT_ERROR_STARTUP_SOCK; + } + return rc; } -/** - * Fills in a preallocated j9ipv6_mreq_struct - * - * @param[in] portLibrary The port library. - * @param[out] handle A pointer to the j9ipv6_mreq_struct to populate. - * @param[in] ipmcast_addr The ip mulitcast address. - * @param[in] ipv6mr_interface The ip mulitcast interface. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_ipv6_mreq_init(struct J9PortLibrary *portLibrary, j9ipv6_mreq_t handle, uint8_t *ipmcast_addr, uint32_t ipv6mr_interface) -{ - return 0; -} -/** - * Answer true if the linger is enabled in the argument linger struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the linger struct to be accessed. - * @param[out] enabled Pointer to the boolean to be updated with the linger status. - * - * @return 0, the function does not validate the access. - */ -int32_t -j9sock_linger_enabled(struct J9PortLibrary *portLibrary, j9linger_t handle, BOOLEAN *enabled) -{ - return 0; -} -/** - * Initializes a new linger structure, enabled or disabled, with the timeout as specified. - * Linger defines the behavior when unsent messages exist for a socket that has been sent close. - * If linger is disabled, the default, close returns immediately and the stack attempts to deliver unsent messages. - * If linger is enabled: - * \arg if the timeout is 0, the close will block indefinitely until the messages are sent - * \arg if the timeout is set, the close will return after the messages are sent or the timeout period expired - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the linger struct to be accessed. - * @param[in] enabled Aero to disable, a non-zero value to enable linger. - * @param[in] timeout 0 to linger indefinitely or a positive timeout value (in seconds). - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_linger_init(struct J9PortLibrary *portLibrary, j9linger_t handle, int32_t enabled, uint16_t timeout) -{ - memset(handle, 0, sizeof (struct j9linger_struct)); - handle->linger.l_onoff = enabled; - handle->linger.l_linger = timeout; - return 0; -} -/** - * Answer the linger timeout value in the argument linger struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the linger struct to be accessed. - * @param[out] linger Pointer to the integer, to be updated with the linger value (in seconds). - * - * @return 0, the function does not validate the access. - */ -int32_t -j9sock_linger_linger(struct J9PortLibrary *portLibrary, j9linger_t handle, uint16_t *linger) -{ - return 0; -} -/** - * Set the socket to listen for incoming connection requests. This call is made prior to accepting requests, - * via the @ref j9sock_accept function. The backlog specifies the maximum length of the queue of pending connections, - * after which further requests are rejected. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to modify. - * @param[in] backlog The maximum number of queued requests. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_listen(struct J9PortLibrary *portLibrary, j9socket_t sock, int32_t backlog) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Answer the 32 bit network ordered argument, in host byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 32 bit network ordered number. - * - * @return the 32 bit host ordered number. - */ -int32_t -j9sock_ntohl(struct J9PortLibrary *portLibrary, int32_t val) -{ - return val; -} -/** - * Answer the 16-bit network ordered argument, in host byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 16-bit network ordered number. - * - * @return the 16-bit host ordered number. - */ -uint16_t -j9sock_ntohs(struct J9PortLibrary *portLibrary, uint16_t val) -{ - return val; -} -/** - * Receives data from a connected socket. - * - * This will return as much information as is currently available up to the size of the buffer supplied. - * - * Behaviour depending on the blocking characteristic of the socket, as specified by @ref j9sock_set_nonblocking(). - * - * Blocking socket: - * - If no incoming data is available, the call blocks and waits for data to arrive. - * - * Non-blocking socket: - * - If no incoming data is immediately available, the negative portable error code J9PORT_ERROR_SOCKET_WOULDBLOCK is returned. - * - J9PORT_ERROR_SOCKET_WOULDBLOCK is non-fatal and it is normal practice for the caller to retry @ref j9sock_read() upon receiving it. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to read on - * @param[out] buf Pointer to the buffer where input bytes are written - * @param[in] nbyte The length of buf - * @param[in] flags The flags, to influence this read (in addition to the socket options) - * - * @return - * \arg If no error occurs, return the number of bytes received. - * \arg If the connection has been gracefully closed, return 0. - * \arg Otherwise return the (negative) error code. - */ -int32_t -j9sock_read(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * The read function receives data from a possibly connected socket. Calling read will return as much - * information as is currently available up to the size of the buffer supplied. If the information is too large - * for the buffer, the excess will be discarded. If no incoming data is available at the socket, the read call - * blocks and waits for data to arrive. It the address argument is not null, the address will be updated with - * address of the message sender. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to read on. - * @param[out] buf Pointer to the buffer where input bytes are written. - * @param[in] nbyte The length of buf. - * @param[in] flags Tthe flags, to influence this read. - * @param[out] addrHandle if provided, the address to be updated with the sender information. - * - * @return - * \arg If no error occurs, return the number of bytes received. - * \arg If the connection has been gracefully closed, return 0. - * \arg Otherwise return the (negative) error code. - */ -int32_t -j9sock_readfrom(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * The select function allows the state of sockets for read & write operations and exceptional conditions to be tested. - * The function is used prior to a j9sock_read/readfrom, to control the period the operation may block for. - * Depending upon the timeout specified: - * \arg 0, return immediately with the status of the descriptors - * \arg timeout, return when one of the descriptors is ready or after the timeout period has expired - * \arg null, block indefinitely for a ready descriptor - * - * Note 1: j9fdset_t does not support multiple entries. - * Note 2: one of readfd or writefd must contain a valid entry. - * - * @param[in] portLibrary The port library. - * @param[in] nfds For all the sockets that are set in readfds, writefds and exceptfds, this is the maximum value returned - * by @ref j9sock_fdset_size(). - * @param[in] readfd The j9fdset_t representing the descriptor to be checked to see if data can be read without blocking. - * @param[in] writefd The j9fdset_t representing the descriptor to be checked to see if data can be written without blocking. - * @param[in] exceptfd_notSupported Not supported, must either by NULL or empty - * @param[in] timeout Pointer to the timeout (a j9timeval struct). - * - * @return Number of socket descriptors that are ready, otherwise return the (negative) error code. - */ - -int32_t -j9sock_select(struct J9PortLibrary *portLibrary, int32_t nfds, j9fdset_t readfd, j9fdset_t writefd, j9fdset_t exceptfd_notSupported, j9timeval_t timeout) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * A helper method, to ensure a read operation can be performed without blocking. - * The portable version of a read operation is a blocking call (will wait indefinitely for data). - * This function should be called prior to a read operation, to provide a read timeout. - * If the result is 1, the caller is guaranteed to be able to complete a read on the socket without blocking. - * The actual contents of the fdset are not available for inspection (as provided in the more general 'select' function). - * The timeout is specified in seconds and microseconds. - * - * @param[in] portLibrary The port library. - * @param[in] j9socketP Pointer to the j9socket to query for available read data. - * @param[in] secTime The integer component of the timeout period, in seconds. - * @param[in] uSecTime The fractional component of the timeout period, in microSeconds. - * @param[in] isNonBlocking Only used when timeout is zero. If TRUE, then method - * will not block, if FALSE then may block if waiting for data - * - * @return - * \arg 1, If a read operation can be performed on the socket without blocking - * Note that this does not necessarily mean there is data available to be read. - * For instance, a read operation on a closed socket will not block therefore - * j9sock_select_read() will return 1 even though the read will fail. - * \arg J9PORT_ERROR_SOCKET_TIMEOUT if the call timed out - * \arg otherwise return the (negative) error code. - */ -int32_t -j9sock_select_read(struct J9PortLibrary *portLibrary, j9socket_t j9socketP, int32_t secTime, int32_t uSecTime, BOOLEAN isNonBlocking) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the nonblocking state of the socket. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to read on - * @param[in] nonblocking Set true for nonblocking, false for blocking - * - * @return 0 if no error occurs, otherwise return the (negative) error code. - */ -int32_t -j9sock_set_nonblocking(struct J9PortLibrary *portLibrary, j9socket_t socketP, BOOLEAN nonblocking) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Ensure the flag designated is set in the argument. This is used to construct arguments for the - * j9sock_read/readfrom/write/writeto calls with optional flags, such as J9SOCK_MSG_PEEK. - * - * @param[in] portLibrary The port library. - * @param[in] flag The operation flag to set in the argument. - * @param[in] arg Pointer to the argument to set the flag bit in. - * - * @return 0 if no error occurs, otherwise return the (negative) error code. - */ -int32_t -j9sock_setflag(struct J9PortLibrary *portLibrary, int32_t flag, int32_t *arg) -{ - return 0; -} -/** - * Set the value of the nominated boolean socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the boolean to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_bool(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the value of the nominated byte socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the byte to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_byte(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the value of the nominated integer socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the integer to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_int(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the multicast request on this socket. - * Currently this is used to join or leave the nominated multicast group on the local interface. - * It may be more generally useful, so a generic 'setop' function has been defined. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the ipmreq struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_ipmreq(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipmreq_t optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the multicast request on this socket for IPv6 sockets. - * Currently this is used to join or leave the nominated multicast group on the local interface. - * It may be more generally useful, so a generic 'setop' function has been defined.t. - * - * Supported families are OS_AF_INET and OS_AF_INET6 - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the ipmreq struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_setopt_ipv6_mreq(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipv6_mreq_t optval) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the linger value on the socket. - * See the @ref j9sock_linger_init for details of the linger behavior. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the linger struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_linger(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Set the sockaddr for the socket. - * Currently used to set the interface of multicast sockets, but the more general call style is used, - * in case it is more generally useful. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the j9sockaddr struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_sockaddr(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval) -{ - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel( optlevel ); - int32_t platformOption = platformSocketOption( optname ); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Terminates use of the socket library. No sockets should be in use or the results - * of this operation are unpredictable. Frees any resources held by the socket library. - * - * @param[in] portLibrary The port library. - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_shutdown(struct J9PortLibrary *portLibrary ) -{ - j9sock_ptb_shutdown(portLibrary); - return 0; -} -/** - * The shutdown_input function disables the input stream on a socket. Any subsequent reads from the socket - * will fail. - * - * @param[in] portLibrary The port library. - * @param[in] sock Socket to close input stream on. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_OPFAILED, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if the library is not initialized -*/ -int32_t -j9sock_shutdown_input(struct J9PortLibrary *portLibrary, j9socket_t sock) -{ - return J9PORT_ERROR_SOCKET_OPFAILED; -} -/** - * The shutdown_output function disables the output stream on a socket. Any subsequent writes to the socket - * will fail. - * - * @param[in] portLibrary The port library. - * @param[in] sock Socket to close output stream on. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_OPFAILED, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if the library is not initialized - */ -int32_t -j9sock_shutdown_output(struct J9PortLibrary *portLibrary, j9socket_t sock) -{ - return J9PORT_ERROR_SOCKET_OPFAILED; -} -/** - * Creates a new j9sockaddr, referring to the specified port and address. The only address family currently supported - * is AF_INET. - * - * @param[in] portLibrary The port library. - * @param[out] handle Pointer to the j9sockaddr struct, to be allocated. - * @param[in] addrStr The target host, as either a name or dotted ip string. - * @param[in] portNetworkOrder The target port, in network byte order. Use @ref j9sock_htons() to convert from to host to network order. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_sockaddr(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, const char *addrStr, uint16_t portNetworkOrder) -{ - int32_t rc = 0; - uint32_t addr = 0; - j9hostent_struct host_t; - - if(0 != portLibrary->sock_inetaddr(portLibrary, addrStr, &addr)) { - memset(&host_t, 0, sizeof (struct j9hostent_struct)); - if (0 != (rc = portLibrary->sock_gethostbyname(portLibrary, addrStr, &host_t))) { - return rc; - } else { - addr = portLibrary->sock_hostent_addrlist(portLibrary, &host_t, 0); - } - } - rc = portLibrary->sock_sockaddr_init(portLibrary, handle, J9SOCK_AFINET, addr, portNetworkOrder); - return rc; -} - -/** - * Answer the address, in network order, of the j9sockaddr argument. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the j9sockaddr struct to access. - * - * @return the address (there is no validation on the access). - */ -int32_t -j9sock_sockaddr_address(struct J9PortLibrary *portLibrary, j9sockaddr_t handle) -{ - return handle->addr.sin_addr.s_addr; -} -/** - * Answers the IP address of a structure and its length, in a preallocated buffer. - * - * Preallocated buffer "address" should be 16 bytes. "length" tells you how many bytes were used 4 or 16. - * - * @param[in] portLibrary The port library. - * @param[in] handle A populated j9sockaddr_t. - * @param[out] address The IPv4 or IPv6 address in network byte order. - * @param[out] length The number of bytes of the address (4 or 16). - * @param[out] scope_id the scope id for the address if appropriate - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_sockaddr_address6(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *address, uint32_t *length, uint32_t* scope_id) -{ - return 0; -} -/** - * Answers the family name of a j9sockaddr_struct. - * - * Supported families are OS_AF_INET and OS_AF_INET6 - * - * @param[in] portLibrary The port library. - * @param[out] family The family name of the address. - * @param[in] handle A populated j9sockaddr_t. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_sockaddr_family(struct J9PortLibrary *portLibrary, int16_t *family, j9sockaddr_t handle) -{ - *family = handle->addr.sin_family; - return 0; -} - -/** - * Creates a new j9sockaddr, referring to the specified port and address. The only address family currently supported - * is AF_INET. - * - * @param[in] portLibrary The port library. - * @param[out] handle Pointer pointer to the j9sockaddr struct, to be allocated. - * @param[in] family The address family. - * @param[in] ipAddrNetworkOrder The target host address, in network order. Use @ref j9sock_htonl() to convert the ip address from host to network byte order. - * @param[in] portNetworkOrder The target port, in network byte order. Use @ref j9sock_htons() to convert the port from host to network byte order. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_sockaddr_init(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, int16_t family, uint32_t ipAddrNetworkOrder, uint16_t portNetworkOrder) -{ - memset(handle, 0, sizeof (struct j9sockaddr_struct)); - handle->addr.sin_family = family; - handle->addr.sin_addr.s_addr = ipAddrNetworkOrder; - handle->addr.sin_port = portNetworkOrder; - - return 0; -} - -/** - * Answers an initialized j9sockaddr_struct structure. - * - * Pass in a j9sockaddr_struct with some initial parameters to initialize it appropriately. - * Currently the only address families supported are OS_AF_INET6 and OS_AF_INET, which - * will be determined by addrlength. (4 bytes for IPv4 addresses and 16 bytes for IPv6 addresses). - * - * @param[in] portLibrary The port library. - * @param[out] handle Pointer pointer to the j9sockaddr struct, to be allocated. - * @param[in] addr The IPv4 or IPv6 address in network byte order. - * @param[in] addrlength The number of bytes of the address (4 or 16). - * @param[in] family The address family. - * @param[in] portNetworkOrder The target port, in network order. Use @ref j9sock_htons() to convert the port from host to network byte order. - * @param[in] flowinfo The flowinfo value for IPv6 addresses in HOST order. Set to 0 for IPv4 addresses or if no flowinfo needs to be set for IPv6 address - * @param[in] scope_id The scope id for an IPv6 address in HOST order. Set to 0 for IPv4 addresses and for non-scoped IPv6 addresses - * @param[in] sock The socket that this address will be used with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_sockaddr_init6(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *addr, int32_t addrlength, int16_t family, uint16_t portNetworkOrder,uint32_t flowinfo, uint32_t scope_id, j9socket_t sock) -{ - return 0; -} -/** - * Answer the port, in network order, of the j9sockaddr argument. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the j9sockaddr struct to access. - * - * @return the port (there is no validation on the access). - */ -uint16_t -j9sock_sockaddr_port(struct J9PortLibrary *portLibrary, j9sockaddr_t handle) -{ - return handle->addr.sin_port; -} -/** - * Creates a new socket descriptor and any related resources. - * - * @param[in] portLibrary The port library. - * @param[out] handle Pointer pointer to the j9socket struct, to be allocated - * @param[in] family The address family (currently, only J9SOCK_AFINET is supported) - * @param[in] socktype Specifies what type of socket is created - * \arg J9SOCK_STREAM, for a stream socket - * \arg J9SOCK_DGRAM, for a datagram socket - * @param[in] protocol Type/family specific creation parameter (currently, only J9SOCK_DEFPROTOCOL supported). - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_socket(struct J9PortLibrary *portLibrary, j9socket_t *handle, int32_t family, int32_t socktype, int32_t protocol) -{ - return J9PORT_ERROR_SOCKET_OPFAILED; -} -/** - * Determines whether or not the socket is valid. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the j9socket struct, to be allocated. - * - * @return 0 if invalid, non-zero for valid. - */ - -int32_t -j9sock_socketIsValid(struct J9PortLibrary *portLibrary, j9socket_t handle) -{ - return 0; -} -/** - * Initiate the use of sockets by a process. This function must be called before any other socket calls. - * - * @param[in] portLibrary The port library. - * - * @return 0 on success, negative error code on failure. Error code values returned are - * \arg J9PORT_ERROR_STARTUP_SOCK - * \arg J9PORT_ERROR_SOCKET_OPFAILED - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED - */ -int32_t -j9sock_startup(struct J9PortLibrary *portLibrary) -{ - int32_t rc = 0; - if (0 != j9sock_ptb_init(portLibrary)) { - rc = J9PORT_ERROR_STARTUP_SOCK; - } - return rc; -} -/** - * Create a time structure, representing the timeout period defined in seconds & microSeconds. - * Timeval's are used as timeout arguments in the @ref j9sock_select function. - * - * @param[in] portLibrary The port library. - * @param[in] secTime The integer component of the timeout value (in seconds). - * @param[in] uSecTime The fractional component of the timeout value (in microseconds). - * @param[out] timeP Pointer pointer to the j9timeval_struct to be allocated. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_timeval_init(struct J9PortLibrary *portLibrary, uint32_t secTime, uint32_t uSecTime, j9timeval_t timeP) -{ - memset(timeP, 0, sizeof (struct j9timeval_struct)); - timeP->time.tv_sec = secTime; - timeP->time.tv_usec = uSecTime; - - return 0; -} -/** - * The write function writes data to a connected socket. The successful completion of a write - * does not indicate that the data was successfully delivered. If no buffer space is available - * within the transport system to hold the data to be transmitted, send will block. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to send on - * @param[in] buf The bytes to be sent - * @param[in] nbyte The number of bytes to send - * @param[in] flags The flags to modify the send behavior - * - * @return If no error occur, return the total number of bytes sent, which can be less than the - * 'nbyte' for nonblocking sockets, otherwise the (negative) error code - */ -int32_t -j9sock_write(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * The writeto function writes data to a datagram socket. The successful completion of a writeto - * does not indicate that the data was successfully delivered. If no buffer space is available - * within the transport system to hold the data to be transmitted, writeto will block. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to send on - * @param[in] buf The bytes to be sent - * @param[in] nbyte The number of bytes to send - * @param[in] flags The flags to modify the send behavior - * @param [in] addrHandle The network address to send the datagram to - * - * @return If no error occur, return the total number of bytes sent, otherwise the (negative) error code. - */ -int32_t -j9sock_writeto(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} -/** - * Queries and returns the information for the network interfaces that are currently active within the system. - * Applications are responsible for freeing the memory returned via the handle. - * - * @param[in] portLibrary The port library. - * @param[in,out] array Pointer to structure with array of network interface entries - * @param[in] boolean which indicates if we should prefer the IPv4 stack or not - * - * @return 0 on success, negative portable error code on failure. - J9PORT_ERROR_SOCKET_NORECOVERY if system calls required to get the info fail, J9PORT_ERROR_SOCKET_NOBUFFERS if memory allocation fails - */ -int32_t -j9sock_get_network_interfaces(struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct *array,BOOLEAN preferIPv4Stack) -{ - array->elements =NULL; - array->length = 0; - return 0; -} -/** - * Frees the memory allocated for the j9NetworkInterface_struct array passed in - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to array of network interface structures to be freed - * - * @return 0 on success -*/ -int32_t -j9sock_free_network_interface_struct(struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct* array) -{ - return 0; -} -/** - * Establish a connection to a peer with a timeout. This function is called repeatedly in order to carry out the connect and to allow - * other tasks to proceed on certain platforms. The caller must first call with step = J9_SOCK_STEP_START, if the result is J9_ERROR_SOCKET_NOTCONNECTED - * it will then call it with step = CHECK until either another error or 0 is returned to indicate the connect is complete. Each time the function should sleep for no more than - * timeout milliseconds. If the connect succeeds or an error occurs, the caller must always end the process by calling the function with step = J9_SOCK_STEP_DONE - * - * @param[in] portLibrary The port library. - * @param[in] sock pointer to the unconnected local socket. - * @param[in] addr pointer to the sockaddr, specifying remote host/port. - * @param[in] timeout timeout in milliseconds - * @param[in,out] pointer to context pointer. Filled in on first call and then to be passed into each subsequent call - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_connect_with_timeout(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr, uint32_t timeout, uint32_t step, uint8_t** context) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} - -/** - * Zero a j9 file descriptor set - * - * Note: j9fdset_t does not support multiple entries. - * - * @param[in] portLibrary The port library. - * @param[in] j9fdset a j9 file descriptor set. - */ -void -j9sock_fdset_zero(struct J9PortLibrary *portLibrary, j9fdset_t j9fdset) -{ - return; -} - -/** - * Add a j9 socket to a j9 file descriptor set. - * - * Note: j9fdset_t does not support multiple entries. - * - * @param[in] portLibrary The port library. - * @param[in] aSocket j9 socket to be added to j9fdset - * @param[in] j9fdset a j9 file descriptor set. - */ -void -j9sock_fdset_set(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) -{ - return; -} - -/** - * Remove a j9 socket from a j9 file descriptor set - * - * Note: j9fdset_t does not support multiple entries. - * - * @param[in] portLibrary The port library. - * @param[in] aSocket j9 socket to be removed from j9fdset - * @param[in] j9fdset a j9 file descriptor set. - */ -void -j9sock_fdset_clr(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) -{ - return; -} - -/** - * Returns TRUE if a j9 socket is part of the j9 file descriptor set. - * This is designed to be used after a call to @ref j9socket_select to determine if the j9 socket is ready for a read/write. - * - * Note: j9fdset_t does not support multiple entries. - * - * @param[in] portLibrary the port library. - * @param[in] aSocket j9 socket to test for in j9fdset - * @param[in] j9fdset a j9 file descriptor set. - * - * @return TRUE if fd is part of j9fdset. - */ -BOOLEAN -j9sock_fdset_isset(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) -{ - return FALSE; -} - diff --git a/runtime/port/common/j9sockptb.c b/runtime/port/common/j9sockptb.c index e8d63c9b3e3..539ecbf8110 100644 --- a/runtime/port/common/j9sockptb.c +++ b/runtime/port/common/j9sockptb.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -88,9 +88,6 @@ j9sock_ptb_free(void *socketPTBVoidP) OMRPORT_ACCESS_FROM_J9PORT(socketPTB->portLibrary); /* Free the socketPTB */ - if (NULL != socketPTB->fdset) { - omrmem_free_memory(socketPTB->fdset); - } #if defined(WIN32) if (NULL != socketPTB->addr_info_hints.addr_info) { omrmem_free_memory(socketPTB->addr_info_hints.addr_info); diff --git a/runtime/port/portpriv.h b/runtime/port/portpriv.h index 6a368b360de..051d5e3f868 100644 --- a/runtime/port/portpriv.h +++ b/runtime/port/portpriv.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2018 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -273,157 +273,31 @@ j9port_create_library (struct J9PortLibrary *portLibrary, struct J9PortLibraryVe /* J9SourceSockets*/ extern J9_CFUNC int32_t -j9sock_hostent_addrlist (struct J9PortLibrary *portLibrary, j9hostent_t handle, uint32_t index); -extern J9_CFUNC int32_t j9sock_getaddrinfo (struct J9PortLibrary *portLibrary, char *name, j9addrinfo_t hints, j9addrinfo_t result); extern J9_CFUNC int32_t -j9sock_setopt_int (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval); -extern J9_CFUNC int32_t j9sock_shutdown (struct J9PortLibrary *portLibrary ); extern J9_CFUNC int32_t -j9sock_hostent_aliaslist (struct J9PortLibrary *portLibrary, j9hostent_t handle, char ***aliasList); -extern J9_CFUNC int32_t -j9sock_fdset_init (struct J9PortLibrary *portLibrary, j9socket_t socketP); -extern J9_CFUNC int32_t j9sock_getaddrinfo_create_hints (struct J9PortLibrary *portLibrary, j9addrinfo_t *result, int16_t family, int32_t socktype, int32_t protocol, int32_t flags); -extern J9_CFUNC int32_t -j9sock_connect_with_timeout (struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr, uint32_t timeout, uint32_t step, uint8_t** context); -extern J9_CFUNC int32_t -j9sock_getpeername (struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle); extern J9_CFUNC const char* j9sock_error_message (struct J9PortLibrary *portLibrary); extern J9_CFUNC int32_t -j9sock_getnameinfo (struct J9PortLibrary *portLibrary, j9sockaddr_t in_addr, int32_t sockaddr_size, char *name, int32_t name_length, int flags); -extern J9_CFUNC int32_t -j9sock_setopt_ipv6_mreq (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipv6_mreq_t optval); -extern J9_CFUNC int32_t j9sock_inetaddr (struct J9PortLibrary *portLibrary, const char *addrStr, uint32_t *addr); extern J9_CFUNC int32_t -j9sock_bind (struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr); -extern J9_CFUNC int32_t -j9sock_connect (struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr); -extern J9_CFUNC int32_t -j9sock_listen (struct J9PortLibrary *portLibrary, j9socket_t sock, int32_t backlog); -extern J9_CFUNC int32_t -j9sock_setopt_bool (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval); -extern J9_CFUNC int32_t -j9sock_readfrom (struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle); -extern J9_CFUNC int32_t -j9sock_getopt_bool (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval); -extern J9_CFUNC int32_t -j9sock_fdset_size (struct J9PortLibrary *portLibrary, j9socket_t handle); -extern J9_CFUNC int32_t -j9sock_setopt_ipmreq (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipmreq_t optval); -extern J9_CFUNC int32_t -j9sock_writeto (struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle); -extern J9_CFUNC int32_t -j9sock_linger_init (struct J9PortLibrary *portLibrary, j9linger_t handle, int32_t enabled, uint16_t timeout); -extern J9_CFUNC int32_t -j9sock_select_read (struct J9PortLibrary *portLibrary, j9socket_t j9socketP, int32_t secTime, int32_t uSecTime, BOOLEAN accept); -extern J9_CFUNC int32_t -j9sock_sockaddr (struct J9PortLibrary *portLibrary, j9sockaddr_t handle, const char *addrStr, uint16_t portNetworkOrder); -extern J9_CFUNC int32_t j9sock_gethostbyaddr (struct J9PortLibrary *portLibrary, char *addr, int32_t length, int32_t type, j9hostent_t handle); extern J9_CFUNC int32_t -j9sock_inetntoa (struct J9PortLibrary *portLibrary, char **addrStr, uint32_t nipAddr); -extern J9_CFUNC int32_t -j9sock_linger_enabled (struct J9PortLibrary *portLibrary, j9linger_t handle, BOOLEAN *enabled); -extern J9_CFUNC int32_t -j9sock_select (struct J9PortLibrary *portLibrary, int32_t nfds, j9fdset_t readfds, j9fdset_t writefds, j9fdset_t exceptfds, j9timeval_t timeout); -extern J9_CFUNC int32_t j9sock_startup (struct J9PortLibrary *portLibrary); extern J9_CFUNC int32_t -j9sock_linger_linger (struct J9PortLibrary *portLibrary, j9linger_t handle, uint16_t *linger); -extern J9_CFUNC int32_t j9sock_getaddrinfo_name (struct J9PortLibrary *portLibrary, j9addrinfo_t handle, char *name, int index); extern J9_CFUNC int32_t -j9sock_sockaddr_address (struct J9PortLibrary *portLibrary, j9sockaddr_t handle); -extern J9_CFUNC int32_t -j9sock_sockaddr_address6 (struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *address, uint32_t *length, uint32_t* scope_id); -extern J9_CFUNC int32_t -j9sock_write (struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags); -extern J9_CFUNC int32_t -j9sock_shutdown_input (struct J9PortLibrary *portLibrary, j9socket_t sock); -extern J9_CFUNC int32_t -j9sock_free_network_interface_struct (struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct* array); -extern J9_CFUNC int32_t -j9sock_read (struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags); -extern J9_CFUNC int32_t -j9sock_socket (struct J9PortLibrary *portLibrary, j9socket_t *handle, int32_t family, int32_t socktype, int32_t protocol); -extern J9_CFUNC int32_t -j9sock_close (struct J9PortLibrary *portLibrary, j9socket_t *sock); -extern J9_CFUNC int32_t -j9sock_gethostname (struct J9PortLibrary *portLibrary, char *buffer, int32_t length ); -extern J9_CFUNC uint16_t -j9sock_sockaddr_port (struct J9PortLibrary *portLibrary, j9sockaddr_t handle); -extern J9_CFUNC int32_t j9sock_gethostbyname (struct J9PortLibrary *portLibrary, const char *name, j9hostent_t handle); extern J9_CFUNC int32_t -j9sock_get_network_interfaces (struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct *array,BOOLEAN preferIPv4Stack); -extern J9_CFUNC int32_t j9sock_getaddrinfo_length (struct J9PortLibrary *portLibrary, j9addrinfo_t handle, int32_t *length); extern J9_CFUNC int32_t -j9sock_ipv6_mreq_init (struct J9PortLibrary *portLibrary, j9ipv6_mreq_t handle, uint8_t *ipmcast_addr, uint32_t ipv6mr_interface); -extern J9_CFUNC int32_t -j9sock_htonl (struct J9PortLibrary *portLibrary, int32_t val); -extern J9_CFUNC int32_t -j9sock_getsockname (struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle); -extern J9_CFUNC int32_t j9sock_freeaddrinfo (struct J9PortLibrary *portLibrary, j9addrinfo_t handle); -extern J9_CFUNC uint16_t -j9sock_htons (struct J9PortLibrary *portLibrary, uint16_t val); -extern J9_CFUNC int32_t -j9sock_sockaddr_init (struct J9PortLibrary *portLibrary, j9sockaddr_t handle, int16_t family, uint32_t ipAddrNetworkOrder, uint16_t portNetworkOrder); extern J9_CFUNC int32_t j9sock_getaddrinfo_address (struct J9PortLibrary *portLibrary, j9addrinfo_t handle, uint8_t *address, int index, uint32_t* scope_id); extern J9_CFUNC int32_t -j9sock_socketIsValid (struct J9PortLibrary *portLibrary, j9socket_t handle); -extern J9_CFUNC uint16_t -j9sock_ntohs (struct J9PortLibrary *portLibrary, uint16_t val); -extern J9_CFUNC int32_t -j9sock_ntohl (struct J9PortLibrary *portLibrary, int32_t val); -extern J9_CFUNC int32_t -j9sock_sockaddr_init6 (struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *addr, int32_t addrlength, int16_t family, uint16_t portNetorkOrder,uint32_t flowinfo, uint32_t scope_id, j9socket_t sock); -extern J9_CFUNC int32_t j9sock_getaddrinfo_family (struct J9PortLibrary *portLibrary, j9addrinfo_t handle, int32_t *family, int index ); -extern J9_CFUNC int32_t -j9sock_setopt_sockaddr (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval); -extern J9_CFUNC int32_t -j9sock_setopt_linger (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval); -extern J9_CFUNC int32_t -j9sock_getopt_linger (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval); -extern J9_CFUNC int32_t -j9sock_getopt_sockaddr (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval); -extern J9_CFUNC int32_t -j9sock_setflag (struct J9PortLibrary *portLibrary, int32_t flag, int32_t *arg); -extern J9_CFUNC int32_t -j9sock_shutdown_output (struct J9PortLibrary *portLibrary, j9socket_t sock); -extern J9_CFUNC int32_t -j9sock_hostent_hostname (struct J9PortLibrary *portLibrary, j9hostent_t handle, char** hostName); -extern J9_CFUNC int32_t -j9sock_accept (struct J9PortLibrary *portLibrary, j9socket_t serverSock, j9sockaddr_t addrHandle, j9socket_t *sockHandle); -extern J9_CFUNC int32_t -j9sock_sockaddr_family (struct J9PortLibrary *portLibrary, int16_t *family, j9sockaddr_t handle); -extern J9_CFUNC int32_t -j9sock_ipmreq_init (struct J9PortLibrary *portLibrary, j9ipmreq_t handle, uint32_t nipmcast, uint32_t nipinterface); -extern J9_CFUNC int32_t -j9sock_timeval_init (struct J9PortLibrary *portLibrary, uint32_t secTime, uint32_t uSecTime, j9timeval_t timeP); -extern J9_CFUNC int32_t -j9sock_set_nonblocking (struct J9PortLibrary *portLibrary, j9socket_t socketP, BOOLEAN nonblocking); -extern J9_CFUNC int32_t -j9sock_getopt_int (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval); -extern J9_CFUNC int32_t -j9sock_setopt_byte (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval); -extern J9_CFUNC int32_t -j9sock_getopt_byte (struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval); -extern J9_CFUNC void -j9sock_fdset_zero(struct J9PortLibrary *portLibrary, j9fdset_t j9fdset); -extern J9_CFUNC void -j9sock_fdset_set(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset); -extern J9_CFUNC void -j9sock_fdset_clr(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset); -extern J9_CFUNC BOOLEAN -j9sock_fdset_isset(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset); /* Per-thread buffer for platform-dependent socket information */ struct J9SocketPTB; diff --git a/runtime/port/unix/j9sock.c b/runtime/port/unix/j9sock.c index 25e3c308e73..07a81c03db9 100644 --- a/runtime/port/unix/j9sock.c +++ b/runtime/port/unix/j9sock.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -55,109 +55,16 @@ #include "atoe.h" #endif -#if defined(LINUX) || defined(OSX) -#include -#if defined(LINUX) -#define IPV6_FLOWINFO_SEND 33 -#endif /* defined(LINUX) */ -#endif /* defined(LINUX) || defined(OSX) */ - -#define INVALID_SOCKET (j9socket_t) -1 - -/*the number of times to retry a gethostby* call if the return code is TRYAGAIN*/ -#define MAX_RETRIES 50 - -/* needed for connect_with_timeout */ -typedef struct selectFDSet_struct { - int nfds; - int sock; - fd_set writeSet; - fd_set readSet; - fd_set exceptionSet; -} selectFDSet_strut; - -static int32_t platformSocketOption(int32_t portableSocketOption); -static int32_t platformSocketLevel(int32_t portableSocketLevel); static int32_t findError (int32_t errorCode); static int32_t map_protocol_family_J9_to_OS( int32_t addr_family); static int32_t map_addr_family_J9_to_OS( int32_t addr_family); static int32_t map_sockettype_J9_to_OS( int32_t socket_type); static int32_t findHostError (int herr); -#if defined(IPv6_FUNCTION_SUPPORT) -static int32_t getAddrLength(j9sockaddr_t addrHandle); -#endif - -static int32_t disconnectSocket(struct J9PortLibrary *portLibrary, j9socket_t sock, socklen_t fromlen); -static int32_t connectSocket(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr, socklen_t fromlen); - -static int32_t lookupIPv6AddressFromIndex(struct J9PortLibrary *portLibrary, uint8_t index, j9sockaddr_t handle); - -/** - * @internal - * Given a pointer to a sockaddr_struct, look at the address family, and - * return the length of sockaddr to be passed to bind/connect/writeto methods - * - * @param[in] pointer to j9sockaddr_t structure - * - * @return the length of the structure - */ -static int32_t getAddrLength(j9sockaddr_t addrHandle){ - - OSSOCKADDR * sockAddr = ((OSSOCKADDR *)&addrHandle->addr); - int32_t rc = 0; - - if (sockAddr->sin_family == OS_AF_INET4) { - rc = sizeof(OSSOCKADDR); - } -#if defined(IPv6_FUNCTION_SUPPORT) - else { - rc = sizeof(OSSOCKADDR_IN6); - } -#endif /* defined(IPv6_FUNCTION_SUPPORT) */ - return rc; - -} - -/** - * @internal Given a pointer to a j9socket_struct, determine the address family of the socket. - * - * @param[in] pointer to j9socket_t structure - * - * @return the portable address family of the socket or the (negative) portable error code - */ -static int32_t -getSocketFamily(struct J9PortLibrary *portLibrary, j9socket_t handle) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = J9ADDR_FAMILY_UNSPEC; - - if ((handle->family == J9ADDR_FAMILY_AFINET4) || (handle->family == J9ADDR_FAMILY_AFINET6)) { - rc = handle->family; - } else { - j9sockaddr_struct addr; - socklen_t addrlen = sizeof(addr.addr); - - if (getsockname(SOCKET_CAST(handle), (struct sockaddr *) &addr.addr, &addrlen) != 0) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - rc = omrerror_set_last_error(err, findError(err)); - } else { - int32_t family = ((struct sockaddr_in *) &addr.addr)->sin_family; - if (family == AF_INET) { - rc = J9ADDR_FAMILY_AFINET4; - } -#if defined(IPv6_FUNCTION_SUPPORT) - else if (family == AF_INET6) { - rc = J9ADDR_FAMILY_AFINET6; - } -#endif /* defined(IPv6_FUNCTION_SUPPORT) */ - } - } +/*the number of times to retry a gethostby* call if the return code is TRYAGAIN*/ +#define MAX_RETRIES 50 - return rc; -} /** * @internal @@ -357,347 +264,6 @@ map_sockettype_J9_to_OS( int32_t socket_type) -/** - * @internal - * Map the portable to the platform socket level. Used to resolve the arguments of socket option functions. - * Levels currently in use are: - * \arg SOL_SOCKET, for most options - * \arg IPPROTO_TCP, for the TCP noDelay option - * \arg IPPROTO_IP, for the option operations associated with multicast (join, drop, interface) - * - * @param[in] portableSocketLevel The portable socket level to convert. - * - * @return the platform socket level or a (negative) error code if no equivalent level exists. - */ -static int32_t -platformSocketLevel(int32_t portableSocketLevel) -{ - switch (portableSocketLevel) - { - case J9_SOL_SOCKET: - return OS_SOL_SOCKET; - case J9_IPPROTO_TCP: - return OS_IPPROTO_TCP; - case J9_IPPROTO_IP: - return OS_IPPROTO_IP; -#ifdef IPv6_FUNCTION_SUPPORT - case J9_IPPROTO_IPV6: - return OS_IPPROTO_IPV6; -#endif - } - return J9PORT_ERROR_SOCKET_SOCKLEVELINVALID; -} - - - - - -/** - * @internal - * Map the portable to the platform socket options. Used to resolve the arguments of socket option functions. - * Options currently in supported are: - * \arg SOL_LINGER, the linger timeout - * \arg TCP_NODELAY, the buffering scheme implementing Nagle's algorithm - * \arg IP_MULTICAST_TTL, the packet Time-To-Live - * \arg IP_ADD_MEMBERSHIP, to join a multicast group - * \arg IP_DROP_MEMBERSHIP, to leave a multicast group - * \arg IP_MULTICAST_IF, the multicast interface - * - * @param[in] portableSocketOption The portable socket option to convert. - * - * @return the platform socket option or a (negative) error code if no equivalent option exists. - */ -/** - * @internal Map the portable to the platform socket options. Used to resolve the arguments of socket option functions. - * Options currently in supported are: - * SOL_LINGER, the linger timeout - * TCP_NODELAY, the buffering scheme implementing Nagle's algorithm - * IP_MULTICAST_TTL, the packet Time-To-Live - * IP_ADD_MEMBERSHIP, to join a multicast group - * IP_DROP_MEMBERSHIP, to leave a multicast group - * IP_MULTICAST_IF, the multicast interface - * - * @param portlib pointer to the VM port library - * @param portableSocketOption the portable socket option to convert - * - * @return the platform socket option or a J9 error if no equivalent option exists - */ -/*[PR1FLSKTU] Support datagram broadcasts */ -static int32_t -platformSocketOption(int32_t portableSocketOption) -{ - switch (portableSocketOption) - { - case J9_SO_LINGER: - return OS_SO_LINGER; - case J9_SO_KEEPALIVE: - return OS_SO_KEEPALIVE; - case J9_TCP_NODELAY: - return OS_TCP_NODELAY; - case J9_MCAST_TTL: - return OS_MCAST_TTL; - case J9_MCAST_ADD_MEMBERSHIP: - return OS_MCAST_ADD_MEMBERSHIP; - case J9_MCAST_DROP_MEMBERSHIP: - return OS_MCAST_DROP_MEMBERSHIP; - case J9_MCAST_INTERFACE: - return OS_MCAST_INTERFACE; - case J9_SO_REUSEADDR: - return OS_SO_REUSEADDR; - case J9_SO_SNDBUF: - return OS_SO_SNDBUF; - case J9_SO_RCVBUF: - return OS_SO_RCVBUF; - case J9_SO_BROADCAST: - return OS_SO_BROADCAST; -#if defined(AIXPPC) || defined(J9ZOS390) - case J9_SO_REUSEPORT: - return OS_SO_REUSEPORT; -#endif - case J9_SO_OOBINLINE: - return OS_SO_OOBINLINE; - case J9_IP_MULTICAST_LOOP: - return OS_MCAST_LOOP; - case J9_IP_TOS: - return OS_IP_TOS; -#ifdef IPv6_FUNCTION_SUPPORT - case J9_MCAST_INTERFACE_2: - return OS_MCAST_INTERFACE_2; - case J9_IPV6_ADD_MEMBERSHIP: - return OS_IPV6_ADD_MEMBERSHIP; - case J9_IPV6_DROP_MEMBERSHIP: - return OS_IPV6_DROP_MEMBERSHIP; -#endif - } - return J9PORT_ERROR_SOCKET_OPTUNSUPP; -} - - - - - - -/** - * The accept function extracts the first connection on the queue of pending connections - * on socket sock. It then creates a new socket and returns a handle to the new socket. - * The newly created socket is the socket that will handle the actual the connection and - * has the same properties as socket sock. - * - * The accept function can block the caller until a connection is present if no pending - * connections are present on the queue. - * - * @param[in] portLibrary The port library. - * @param[in] serverSock A j9socket_t from which data will be read. - * @param[in] addrHandle An optional pointer to a buffer that receives the address of the connecting - * entity, as known to the communications layer. The exact format of the addr parameter - * is determined by the address family established when the socket was created. - * @param[in] sockHandle A pointer to a j9socket_t which will point to the newly created - * socket once accept returns successfully - * - * @return - * \arg 0 on success - * \arg J9PORT_ERROR_SOCKET_BADSOCKET, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if socket library uninitialized - * \arg J9PORT_ERROR_SOCKET_INTERRUPTED, the call was cancelled - * \arg J9PORT_ERROR_SOCKET_ADDRNOTAVAIL, the addr parameter is not valid - * \arg J9PORT_ERROR_SOCKET_SYSTEMBUSY, if system busy handling other requests - * \arg J9PORT_ERROR_SOCKET_SYSTEMFULL, is too many sockets are active - * \arg J9PORT_ERROR_SOCKET_WOULDBLOCK, the socket is marked as nonblocking and no connections are present to be accepted., - */ -int32_t -j9sock_accept(struct J9PortLibrary *portLibrary, j9socket_t serverSock, j9sockaddr_t addrHandle, j9socket_t *sockHandle) -{ -#if defined(LINUX) || defined(OSX) -#define ACCEPTCAST (socklen_t *) -#else -#define ACCEPTCAST -#endif - - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int sc; - socklen_t fromlen = sizeof(addrHandle->addr); - - Trc_PRT_sock_j9sock_accept_Entry(serverSock); - - *sockHandle = INVALID_SOCKET; - - sc = accept(SOCKET_CAST(serverSock), (struct sockaddr *) &addrHandle->addr, ACCEPTCAST &fromlen); - if (sc < 0) { - rc = errno; - Trc_PRT_sock_j9sock_accept_failure(errno); - J9SOCKDEBUG( "\n", rc); - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_ADDRNOTAVAIL); - } - - if (0 == rc) { - *sockHandle = omrmem_allocate_memory(sizeof(struct j9socket_struct), OMRMEM_CATEGORY_PORT_LIBRARY); - if (*sockHandle == NULL) { - close(sc); - *sockHandle = INVALID_SOCKET ; - rc = J9PORT_ERROR_SOCKET_NOBUFFERS; - Trc_PRT_sock_j9sock_accept_failure_oom(); - Trc_PRT_sock_j9sock_accept_Exit(rc); - return rc; - } - - Trc_PRT_sock_j9sock_accept_socket_created(*sockHandle); - SOCKET_CAST(*sockHandle) = sc; - (*sockHandle)->family = serverSock->family; - } - Trc_PRT_sock_j9sock_accept_Exit(rc); - return rc; -} - -#undef ACCEPTCAST - - - - - -/** - * The bind function is used on an unconnected socket before subsequent - * calls to the connect or listen functions. When a socket is created with a - * call to the socket function, it exists in a name space (address family), but - * it has no name assigned to it. Use bind to establish the local association - * of the socket by assigning a local name to an unnamed socket. - * - * @param[in] portLibrary The port library. - * @param[in] sock j9socket_t which will be be associated with the specified name. - * @param[in] addr Address to bind to socket. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_BADSOCKET, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if socket library uninitialized - * \arg J9PORT_ERROR_SOCKET_ADDRINUSE A process on the machine is already bound to the same fully-qualified address - * and the socket has not been marked to allow address re-use with SO_REUSEADDR. - * \arg J9PORT_ERROR_SOCKET_ADDRNOTAVAIL The specified address is not a valid address for this machine - * \arg J9PORT_ERROR_SOCKET_SYSTEMBUSY, if system busy handling other requests - * \arg J9PORT_ERROR_SOCKET_SYSTEMFULL, is too many sockets are active - * \arg J9PORT_ERROR_SOCKET_BADADDR, the addr parameter is not a valid part of the user address space, - */ -int32_t -j9sock_bind(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; -#if defined(IPv6_FUNCTION_SUPPORT) - socklen_t length = getAddrLength(addr); -#else - socklen_t length = sizeof(addr->addr); -#endif - - Trc_PRT_sock_j9sock_bind_Entry(sock, SOCKET_CAST(sock), addr); - - if (bind(SOCKET_CAST(sock), (struct sockaddr *) &addr->addr, length) < 0) { - int addrLength = 0; - int port = 0; - char ipstr[INET6_ADDRSTRLEN]; - rc = errno; - port = j9sock_ntohs(portLibrary, j9sock_sockaddr_port(portLibrary, addr)); - - J9SOCKDEBUG("\n", rc); - - if (((OSSOCKADDR *)&addr->addr)->sin_family == OS_AF_INET4) { - addrLength = INET_ADDRSTRLEN; - } -#if defined(IPv6_FUNCTION_SUPPORT) - else { - addrLength = INET6_ADDRSTRLEN; - } -#endif -#if !defined(J9ZTPF) - if (0 == j9sock_getnameinfo(portLibrary, addr, length, ipstr, addrLength, NI_NUMERICHOST)) { - Trc_PRT_sock_j9sock_bind_failed2(rc, ipstr, port); - } else { - Trc_PRT_sock_j9sock_bind_failed(rc); - } -#else /* !defined(J9ZTPF) */ - Trc_PRT_sock_j9sock_bind_failed(rc); -#endif /* !defined(J9ZTPF) */ - rc = omrerror_set_last_error(rc, findError(rc)); - } - - Trc_PRT_sock_j9sock_bind_Exit(rc); - return rc; -} - - -/** - * The close function closes a socket. Use it to release the socket descriptor socket so - * further references to socket will fail. - * - * @param[in] portLibrary The port library. - * @param[in] sock j9socket_t which will be closed. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_BADSOCKET, on generic error - * \arg J9PORT_ERROR_SOCKET_SYSTEMBUSY, if system busy handling other requests - * \arg J9PORT_ERROR_SOCKET_WOULDBLOCK, the socket is marked as nonblocking and SO_LINGER is set to a nonzero time-out value. - */ -int32_t -j9sock_close(struct J9PortLibrary *portLibrary, j9socket_t *sock) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformErrorCode = 0; - - Trc_PRT_sock_j9sock_close_Entry(*sock); - - if (*sock == INVALID_SOCKET) { - platformErrorCode = J9PORT_ERROR_SOCKET_UNIX_EBADF; - } else { - if( 0 != close(SOCKET_CAST(*sock)) ) { - platformErrorCode = errno; - } - - omrmem_free_memory(*sock); - *sock = INVALID_SOCKET; - } - - if (0 != platformErrorCode) { - Trc_PRT_sock_j9sock_close_failed(platformErrorCode); - J9SOCKDEBUG( "\n", platformErrorCode); - rc = omrerror_set_last_error(platformErrorCode, J9PORT_ERROR_SOCKET_BADSOCKET); - } - - Trc_PRT_sock_j9sock_close_Exit(rc); - return rc; -} - - - - - -int32_t -j9sock_connect(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr) -{ - int32_t rc = 0; - -#if defined(IPv6_FUNCTION_SUPPORT) - socklen_t fromlen = getAddrLength(addr); -#else - socklen_t fromlen = sizeof(addr->addr); -#endif - OSSOCKADDR * sockAddr = ((OSSOCKADDR *)&addr->addr); - Trc_PRT_sock_j9sock_connect_Entry(sock, addr); - - /* if AF_UNSPEC, then its a request to disconnect */ - if (sockAddr->sin_family == AF_UNSPEC) { - rc = disconnectSocket(portLibrary, sock, fromlen); - } else { - rc = connectSocket(portLibrary, sock, addr, fromlen); - } - Trc_PRT_sock_j9sock_connect_Exit(rc); - return rc; -} - - - - - /** * Return an error message describing the last OS error that occurred. The last * error returned is not thread safe, it may not be related to the operation that @@ -723,74 +289,6 @@ j9sock_error_message(struct J9PortLibrary *portLibrary) - -/** - * Create a file descriptor (FD) set of one element. The call may not be generally useful, - * as it currently only supports a single FD and is assumed to be used in conjunction with the - * j9sock_select function. - * - * @param[in] portLibrary The port library. - * @param[in] socketP pointer to the socket to be added to the FD set. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_fdset_init(struct J9PortLibrary *portLibrary, j9socket_t socketP) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - j9fdset_t fdset; - J9SocketPTB *ptBuffers = j9sock_ptb_get(portLibrary); - - if (NULL == ptBuffers) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - - if (NULL == ptBuffers->fdset) { - ptBuffers->fdset = omrmem_allocate_memory(sizeof(struct j9fdset_struct), OMRMEM_CATEGORY_PORT_LIBRARY); - if (NULL == ptBuffers->fdset) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - } - fdset = ptBuffers->fdset; - memset(fdset, 0, sizeof(struct j9fdset_struct)); - -#if defined(LINUX) || defined(OSX) - portLibrary->sock_fdset_zero(portLibrary, ptBuffers->fdset); - portLibrary->sock_fdset_set(portLibrary, socketP, ptBuffers->fdset); -#else - FD_ZERO(&fdset->handle); - FD_SET(SOCKET_CAST(socketP), &fdset->handle); -#endif /* defined(LINUX) || defined(OSX) */ - - return 0; -} - - - - - -/** - * Answer the size information, as required by @ref j9sock_select(), to act on a j9fdset_t that has "handle" set. - * - * @param[in] portLibrary The port library. - * @param[in] handle - * - * @return the maximum size of the fdset, otherwise the (negative) error code. - * - * @note On Unix, the value was the maximum file descriptor plus one, although - * on many flavors, the value is ignored in the select function. - * On Windows, the value is ignored by the select function. - */ -int32_t -j9sock_fdset_size(struct J9PortLibrary *portLibrary, j9socket_t handle) -{ - return SOCKET_CAST(handle) + 1; -} - - - - - /** * Frees the memory created by the call to @ref j9sock_getaddrinfo(). * @@ -1416,24 +914,28 @@ j9sock_gethostbyname(struct J9PortLibrary *portLibrary, const char *name, j9host /** - * Answer the name of the local host machine. + * Answer the dotted IP string as an Internet address. * * @param[in] portLibrary The port library. - * @param[in,out] buffer The string buffer to receive the name - * @param[in] length The length of the buffer + * @param[in] addrStr The dotted IP string. + * @param[out] addr Pointer to the Internet address. * - * @return 0, if no errors occurred, otherwise the (negative) error code + * @return 0, if no errors occurred, otherwise the (negative) error code. */ int32_t -j9sock_gethostname(struct J9PortLibrary *portLibrary, char *buffer, int32_t length) +j9sock_inetaddr(struct J9PortLibrary *portLibrary, const char *addrStr, uint32_t *addr) { - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - if (0 != gethostname(buffer, length)) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - return omrerror_set_last_error(err, findError(err)); + int32_t rc = 0; + uint32_t val; + + val = inet_addr((char *)addrStr); + if (-1 == val) { + J9SOCKDEBUGPRINT( "\n" ); + rc = J9PORT_ERROR_SOCKET_ADDRNOTAVAIL; + } else { + *addr = val; } - return 0; + return rc; } @@ -1441,2156 +943,18 @@ j9sock_gethostname(struct J9PortLibrary *portLibrary, char *buffer, int32_t leng /** - * Answers the host name of the "in_addr" in a preallocated buffer. - * - * The preallocated buffer for "name" should be the size of the maximum host name: OSNIMAXHOST. - * Currently only AF_INET and AF_INET6 are supported. + * Terminates use of the socket library. No sockets should be in use or the results + * of this operation are unpredictable. Frees any resources held by the socket library. * * @param[in] portLibrary The port library. - * @param[in] in_addr The address we want to do a name lookup on - * @param[in] sockaddr_size The size of "in_addr" - * @param[out] name The hostname of the passed address in a preallocated buffer. - * @param[in] name_length The length of the buffer pointed to by name - * @param[in] flags Flags on how to form the response (see man pages or doc for getnameinfo) * * @return 0, if no errors occurred, otherwise the (negative) error code - * - * @note Added for IPv6 support. - * @note "flags" do not affect results on OS's that do not support the IPv6 calls. */ int32_t -j9sock_getnameinfo(struct J9PortLibrary *portLibrary, j9sockaddr_t in_addr, int32_t sockaddr_size, char *name, int32_t name_length, int flags) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - /* On z/TPF we don't support this option of returning the host name from the in_addr */ -#if defined(J9ZTPF) || defined(OSX) /* TODO: OSX: Revisit this after java -version works */ - int herr = NO_RECOVERY; - J9SOCKDEBUGH( "\n", herr); - return omrerror_set_last_error(herr, findHostError(herr)); -#else /* defined(J9ZTPF) || defined(OSX) */ -/* If we have the IPv6 functions available we will call them, otherwise we'll call the IPv4 function */ -#ifdef IPv6_FUNCTION_SUPPORT - int rc = 0; - - rc = getnameinfo( (OSADDR *) &in_addr->addr, sockaddr_size, name, name_length, NULL, 0, flags ); - if (0 != rc) { - rc = errno; - J9SOCKDEBUG( "\n", rc); - return omrerror_set_last_error(rc, findError(rc)); - } - - return 0; - -#else /* IPv6_FUNCTION_SUPPORT */ - -#if !HOSTENT_DATA_R - OSHOSTENT *result; -#endif -#if GLIBC_R || OTHER_R - BOOLEAN allocBuffer = FALSE; -#endif - int herr = 0; - int i = 0; - int rc = 0; - int length; - OSSOCKADDR *addr; - J9SocketPTB *ptBuffers = NULL; - - addr = (OSSOCKADDR *) &in_addr->addr; - if (addr->sin_family == OS_AF_INET4) { - length = 4; - } else { - length = 16; - } - - ptBuffers = j9sock_ptb_get(portLibrary); - if (NULL == ptBuffers) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } -#define hostentBuffer (&ptBuffers->hostent) - - /* PR 94621 - one of several threadsafe gethostbyaddr calls must be made depending on the current platform */ - /* if there is a transient error (J9PORT_ERROR_SOCKET_UNIX_TRYAGAIN), try making the call again */ - for (i = 0; i < MAX_RETRIES; i ++) { -#if HOSTENT_DATA_R -#define dataBuffer (ptBuffers->hostent_data) - if (NULL == dataBuffer) { - dataBuffer = omrmem_allocate_memory(sizeof(OSHOSTENT_DATA), OMRMEM_CATEGORY_PORT_LIBRARY); - if (NULL == dataBuffer) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - } -#if defined (RS6000) - herr = gethostbyaddr_r((const char*)&addr->sin_addr,length,addr->sin_family, hostentBuffer, dataBuffer); -#else - herr = gethostbyaddr_r(&addr->sin_addr,length,addr->sin_family, hostentBuffer, dataBuffer); -#endif -#undef dataBuffer -#elif GLIBC_R || OTHER_R -#define buffer (ptBuffers->gethostBuffer) -#define bufferSize (ptBuffers->gethostBufferSize) - if (!buffer) { - bufferSize = GET_HOST_BUFFER_SIZE; - } - - while (TRUE) { - if (allocBuffer == TRUE || !buffer) { - /* The buffer is allocated bufferSize + EXTRA_SPACE, while gethostby*_r is only aware of bufferSize - * because there seems to be a bug on Linux 386 where gethostbyname_r writes past the end of the - * buffer. This bug has not been observed on other platforms, but EXTRA_SPACE is added anyway as a precaution.*/ - buffer = omrmem_allocate_memory(bufferSize + EXTRA_SPACE, OMRMEM_CATEGORY_PORT_LIBRARY); - if (NULL == buffer) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - allocBuffer = FALSE; - } -#if GLIBC_R - gethostbyaddr_r((char *)&addr->sin_addr,length,addr->sin_family, hostentBuffer, buffer, bufferSize, &result, &herr); -#elif OTHER_R - result = gethostbyaddr_r((char *)&addr->sin_addr,length,addr->sin_family, hostentBuffer, buffer, bufferSize, &herr); -#endif /* GLIBC_R */ - /* allocate more space if the buffer is too small */ - if (ERANGE == herr) { - omrmem_free_memory(buffer); - bufferSize *= 2; - allocBuffer = TRUE; - } else { - break; - } - } -#undef buffer -#undef bufferSize -#endif - if (herr != J9PORT_ERROR_SOCKET_UNIX_TRYAGAIN) { - break; - } - } - -#if HOSTENT_DATA_R - if (0 != herr) { - herr = h_errno; - J9SOCKDEBUGH( "\n", herr); - return omrerror_set_last_error(herr, findHostError(herr)); - } -#else - if (NULL == result) { - J9SOCKDEBUGH( "\n", herr); - return omrerror_set_last_error(herr, findHostError(herr)); - } -#endif - else { - memset( name, 0, sizeof(char) * name_length ); -#if HOSTENT_DATA_R - strcpy( name, hostentBuffer->h_name ); -#else - strcpy( name, result->h_name ); -#endif - } -#undef hostentBuffer - - return 0; -#endif /* IPv6_FUNCTION_SUPPORT */ - -#endif /* defined(J9ZTPF) */ -} - - -#define MAP_LEVEL_OPTION_AND_FAMILY(optlevel, optname, socketP) \ - int32_t platformLevel = platformSocketLevel((optlevel)); \ - int32_t platformOption = platformSocketOption((optname)); \ - int32_t portableFamily = getSocketFamily(portLibrary, (socketP)) - -#define CHECK_LEVEL_OPTION_AND_FAMILY(variant) \ - if (0 > platformLevel) { \ - Trc_PRT_sock_j9sock_##variant##_invalid_level_Exit(); \ - return platformLevel; \ - } \ - if (0 > platformOption) { \ - Trc_PRT_sock_j9sock_##variant##_invalid_option_Exit(); \ - return platformOption; \ - } \ - if (0 > portableFamily) { \ - Trc_PRT_sock_j9sock_##variant##_invalid_family_Exit(); \ - return portableFamily; \ - } \ - do {} while (0) - - -/** - * Answer the value of the nominated boolean socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the boolean to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_bool(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - socklen_t optlen = sizeof(*optval); - MAP_LEVEL_OPTION_AND_FAMILY(optlevel, optname, socketP); - - Trc_PRT_sock_j9sock_getopt_bool_Entry(socketP, optlevel, optname); - - CHECK_LEVEL_OPTION_AND_FAMILY(getopt_bool); - - if ((IP_MULTICAST_LOOP == platformOption) && (IPPROTO_IP == platformLevel)) { - if (J9ADDR_FAMILY_AFINET6 == portableFamily) { - /* Attempt to get an IPv4 option on an IPv6 socket - map the option to the IPv6 variant */ - platformLevel = IPPROTO_IPV6; - platformOption = IPV6_MULTICAST_LOOP; - } else if (J9ADDR_FAMILY_AFINET4 == portableFamily) { - /* Some platforms, such as AIX and QNX, return the IP_MULTICAST_LOOP value as a byte */ - optlen = sizeof(uint8_t); - } else { - Trc_PRT_sock_j9sock_getopt_bool_invalid_socket_family_Exit(portableFamily); - return J9PORT_ERROR_SOCKET_BADAF; - } - } - - if (0 != getsockopt(SOCKET_CAST(socketP), platformLevel, platformOption, (void*)optval, &optlen)) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - Trc_PRT_sock_j9sock_getopt_bool_getsockopt_failed_Exit(err); - return omrerror_set_last_error(err, findError(err)); - } - - /* - * Some platforms return values other than 1 or 0 for some options, such as AIX for SO_OOBINLINE, and - * the IP_MULTICAST_LOOP return value is stored at the lowest address of optval. Map non-zero values to 1. - */ - if (*optval != 0) { - *optval = 1; - } - - Trc_PRT_sock_j9sock_getopt_bool_Exit(*optval); - return 0; -} - - - - - -/** - * Answer the value of the nominated byte socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the byte to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_byte(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - socklen_t optlen = sizeof(*optval); - MAP_LEVEL_OPTION_AND_FAMILY(optlevel, optname, socketP); - - Trc_PRT_sock_j9sock_getopt_byte_Entry(socketP, SOCKET_CAST(socketP), optlevel, optname); - - CHECK_LEVEL_OPTION_AND_FAMILY(getopt_byte); - - if (0 != getsockopt(SOCKET_CAST(socketP), platformLevel, platformOption, (void*)optval, &optlen)) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - Trc_PRT_sock_j9sock_getopt_byte_getsockopt_failed_Exit(err); - return omrerror_set_last_error(err, findError(err)); - } - Trc_PRT_sock_j9sock_getopt_byte_Exit(); - return 0; -} - - - - - -/** - * Answer the value of the nominated integer socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the integer to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_int(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - MAP_LEVEL_OPTION_AND_FAMILY(optlevel, optname, socketP); - socklen_t optlen = sizeof(*optval); - Trc_PRT_sock_j9sock_getopt_int_Entry(socketP, SOCKET_CAST(socketP), optlevel, optname); - - CHECK_LEVEL_OPTION_AND_FAMILY(getopt_int); - -#if defined(AIXPPC) || defined(J9ZOS390) - if((IPPROTO_IP == platformLevel) && (IP_MULTICAST_TTL == platformOption) && (J9ADDR_FAMILY_AFINET6 == portableFamily)){ - platformLevel = IPPROTO_IPV6; - platformOption = IPV6_MULTICAST_HOPS; - } -#endif - - if (0 != getsockopt(SOCKET_CAST(socketP), platformLevel, platformOption, (void*)optval, &optlen)) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - Trc_PRT_sock_j9sock_getopt_int_getsockopt_failed_Exit(err); - return omrerror_set_last_error(err, findError(err)); - } - Trc_PRT_sock_j9sock_getopt_int_Exit(); - return 0; -} - - - - - -/** - * Answer the value of the socket linger option. - * See the @ref j9sock_linger_init for details of the linger behavior. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value - * @param[in] optlevel The level within the IP stack at which the option is defined - * @param[in] optname The name of the option to retrieve - * @param[out] optval Pointer to the linger struct to update with the option value - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_getopt_linger(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - socklen_t optlen = sizeof(optval->linger); - - if (0 > platformLevel) { - return platformLevel; - } - if (0 > platformOption) { - return platformOption; - } - - if (0 != getsockopt(SOCKET_CAST(socketP), platformLevel, platformOption, (char*)(&optval->linger), &optlen)) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - return omrerror_set_last_error(err, findError(err)); - } - return 0; -} - - - - - -/** - * Answer the value of the socket option, an address struct. - * Currently only used to retrieve the interface of multicast sockets, - * but the more general call style has been used. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to query for the option value. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to retrieve. - * @param[out] optval Pointer to the sockaddr struct to update with the option value. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getopt_sockaddr(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - MAP_LEVEL_OPTION_AND_FAMILY(optlevel, optname, socketP); - - /* if IPv4 the OS returns in_addr, if IPv6, value of interface index is returned */ - typedef union byte_or_int { - uint8_t byte_val; - uint32_t integer_val; - } value; - value val; - socklen_t optlen = sizeof(val); - OSSOCKADDR *sockaddr; - - Trc_PRT_sock_j9sock_getopt_sockaddr_Entry(socketP, SOCKET_CAST(socketP), platformLevel, platformOption, optval); - - CHECK_LEVEL_OPTION_AND_FAMILY(getopt_sockaddr); - -#if defined(AIXPPC) || defined(J9ZOS390) - if ((IPPROTO_IP == platformLevel) && (IP_MULTICAST_IF == platformOption) && (J9ADDR_FAMILY_AFINET6 == portableFamily)){ - platformLevel = IPPROTO_IPV6; - platformOption = IPV6_MULTICAST_IF; - } -#endif - - if(0 != getsockopt(SOCKET_CAST(socketP), platformLevel, platformOption, (char*)&val, &optlen)) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - Trc_PRT_sock_j9sock_getopt_sockaddr_getsockopt_failed_Exit(err); - return omrerror_set_last_error(err, findError(err)); - } - sockaddr = (OSSOCKADDR*) &optval->addr; - /* if socket family is IPv6, and getsockopt can return an interface index (length 1), - * which means an IPv6 address needs to be looked up, or getsockopt can return an in_addr structure, - * for the case of an IPv4 address */ - if (J9ADDR_FAMILY_AFINET6 == portableFamily) { - if (optlen == sizeof(uint8_t)) { - /* lookup the address with the interface index */ - int32_t result = lookupIPv6AddressFromIndex(portLibrary, val.byte_val, (j9sockaddr_t) sockaddr); - if (0 != result){ - return result; - } - } else if (optlen == sizeof(struct in_addr)){ - /* if optlen is 4, address is IPv4 */ - sockaddr->sin_addr.s_addr = val.integer_val; - } else { - Trc_PRT_sock_j9sock_getopt_sockaddr_address_length_invalid_Exit(portableFamily); - return J9PORT_ERROR_SOCKET_OPTARGSINVALID; - } - } else if (J9ADDR_FAMILY_AFINET4 == portableFamily) { - /* portableFamily is AFINET4 when preferIPv4Stack=true */ - if (optlen == sizeof(struct in_addr)) { - sockaddr->sin_addr.s_addr = val.integer_val; - } else { - Trc_PRT_sock_j9sock_getopt_sockaddr_address_length_invalid_Exit(portableFamily); - return J9PORT_ERROR_SOCKET_OPTARGSINVALID; - } - /* A temporary fix as Linux ARM (maybe others) returns 0 for the family. */ - sockaddr->sin_family = AF_INET; - } else { - Trc_PRT_sock_j9sock_getopt_sockaddr_socket_family_invalid_Exit(portableFamily); - return J9PORT_ERROR_SOCKET_BADAF; - } - - Trc_PRT_sock_j9sock_getopt_sockaddr_Exit(); - return 0; -} - - - - - -/** - * Answer the remote name for the socket. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the socket to get the address of. - * @param[out] addrHandle Pointer to the sockaddr struct to update with the address. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getpeername(struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - socklen_t addrlen = sizeof(addrHandle->addr); - - if (getpeername(SOCKET_CAST(handle), (struct sockaddr*) &addrHandle->addr, &addrlen) != 0) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - return omrerror_set_last_error(err, findError(err)); - } - return 0; -} - - - - - -/** - * Answer the local name for the socket. Note, the stack getsockname function - * actually answers a sockaddr structure, not a string name as the function name - * might imply. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the socket to get the address of. - * @param[out] addrHandle Pointer to the sockaddr struct to update with the address. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_getsockname(struct J9PortLibrary *portLibrary, j9socket_t handle, j9sockaddr_t addrHandle) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - socklen_t addrlen = sizeof(addrHandle->addr); - int32_t rc = 0; - - Trc_PRT_sock_j9sock_getsockname_Entry(handle); - - if (getsockname(SOCKET_CAST(handle), (struct sockaddr*) &addrHandle->addr, &addrlen) != 0) { - int32_t err = errno; - J9SOCKDEBUG( "\n", err); - rc = omrerror_set_last_error(err, findError(err)); - Trc_PRT_sock_j9sock_getsockname_failure(err); - } - Trc_PRT_sock_j9sock_getsockname_Exit(addrHandle, rc); - - return rc; -} - - - - - -/** - * Answer the nominated element of the address list within the argument hostent struct. - * The address is in network order. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the hostent struct, in which to access the addr_list. - * @param[in] index The index of the element within the addr_list to retrieve. - * - * @return the address, in network order. - */ -int32_t -j9sock_hostent_addrlist(struct J9PortLibrary *portLibrary, j9hostent_t handle, uint32_t index) -{ - return *((int32_t*)handle->entity->h_addr_list[index]); -} - - - - - -/** - * Answer a reference to the list of alternative names for the host within the argument hostent struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the hostent struct, in which to access the addr_list - * @param[out] aliasList Pointer to the list of alternative names, to be updated - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_hostent_aliaslist(struct J9PortLibrary *portLibrary, j9hostent_t handle, char ***aliasList) -{ - *aliasList = handle->entity->h_addr_list; - return 0; -} - - - - - -/** - * Answer the host name (string) within the argument hostent struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the hostent struct, in which to access the hostName. - * @param[out] hostName Host name string. - * - * @return 0, the function does not validate the name access. - */ -int32_t -j9sock_hostent_hostname(struct J9PortLibrary *portLibrary, j9hostent_t handle, char** hostName) -{ - *hostName = handle->entity->h_name; - return 0; -} - - - - - -/** - * Answer the 32 bit host ordered argument, in network byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 32 bit host ordered number. - * - * @return the 32 bit network ordered number. - */ -int32_t -j9sock_htonl(struct J9PortLibrary *portLibrary, int32_t val) -{ - return htonl(val); -} - - - - - -/** - * Answer the 16 bit host ordered argument, in network byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 16 bit host ordered number. - * - * @return the 16 bit network ordered number. - */ -uint16_t -j9sock_htons(struct J9PortLibrary *portLibrary, uint16_t val) -{ - return htons(val); -} - - - - - -/** - * Answer the dotted IP string as an Internet address. - * - * @param[in] portLibrary The port library. - * @param[in] addrStr The dotted IP string. - * @param[out] addr Pointer to the Internet address. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_inetaddr(struct J9PortLibrary *portLibrary, const char *addrStr, uint32_t *addr) -{ - int32_t rc = 0; - uint32_t val; - - val = inet_addr((char *)addrStr); - if (-1 == val) { - J9SOCKDEBUGPRINT( "\n" ); - rc = J9PORT_ERROR_SOCKET_ADDRNOTAVAIL; - } else { - *addr = val; - } - return rc; -} - - - - - -/** - * Answer the Internet address as a dotted IP string. - * - * @param[in] portLibrary The port library. - * @param[out] addrStr The dotted IP string. - * @param[in] nipAddr The Internet address. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_inetntoa(struct J9PortLibrary *portLibrary, char **addrStr, uint32_t nipAddr) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - uint8_t *addr = (uint8_t *)&nipAddr; - J9SocketPTB *ptBuffers = j9sock_ptb_get(portLibrary); - if (NULL == ptBuffers) { - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - omrstr_printf(ptBuffers->ntoa, NTOA_SIZE, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); - *addrStr = ptBuffers->ntoa; - return 0; -} - - - - - -/** - * Initializes a new multicast membership structure. The membership structure is used to join & leave - * multicast groups @see j9sock_setopt_ipmreq. The group may be joined using 0 (J9SOCK_INADDR_ANY) - * as the local interface, in which case the default local address will be used. - * - * @param[in] portLibrary The port library. - * @param[out] handle Pointer to the multicast membership struct. - * @param[in] nipmcast The address, in network order, of the multicast group to join. - * @param[in] nipinterface The address, in network order, of the local machine interface to join on. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_ipmreq_init(struct J9PortLibrary *portLibrary, j9ipmreq_t handle, uint32_t nipmcast, uint32_t nipinterface) -{ - memset(handle, 0, sizeof(struct j9ipmreq_struct)); - - handle->addrpair.imr_multiaddr.s_addr = nipmcast; - handle->addrpair.imr_interface.s_addr = nipinterface; - return 0; - -} - - - - - -/** - * Fills in a preallocated j9ipv6_mreq_struct - * - * @param[in] portLibrary The port library. - * @param[out] handle A pointer to the j9ipv6_mreq_struct to populate. - * @param[in] ipmcast_addr The ip mulitcast address. - * @param[in] ipv6mr_interface The ip mulitcast interface. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_ipv6_mreq_init(struct J9PortLibrary *portLibrary, j9ipv6_mreq_t handle, uint8_t *ipmcast_addr, uint32_t ipv6mr_interface) -{ -#ifdef IPv6_FUNCTION_SUPPORT - memset(handle, 0, sizeof(struct j9ipmreq_struct)); - memcpy( handle->mreq.ipv6mr_multiaddr.s6_addr, ipmcast_addr, 16 ); - handle->mreq.ipv6mr_interface = ipv6mr_interface; -#endif - return 0; -} - - - - - -/** - * Answer true if the linger is enabled in the argument linger struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the linger struct to be accessed. - * @param[out] enabled Pointer to the boolean to be updated with the linger status. - * - * @return 0, the function does not validate the access. - */ -int32_t -j9sock_linger_enabled(struct J9PortLibrary *portLibrary, j9linger_t handle, BOOLEAN *enabled) -{ - *enabled = (BOOLEAN)(handle->linger.l_onoff); - return 0; -} - - - - - -/** - * Initializes a new linger structure, enabled or disabled, with the timeout as specified. - * Linger defines the behavior when unsent messages exist for a socket that has been sent close. - * If linger is disabled, the default, close returns immediately and the stack attempts to deliver unsent messages. - * If linger is enabled: - * \arg if the timeout is 0, the close will block indefinitely until the messages are sent - * \arg if the timeout is set, the close will return after the messages are sent or the timeout period expired - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the linger struct to be accessed. - * @param[in] enabled Aero to disable, a non-zero value to enable linger. - * @param[in] timeout 0 to linger indefinitely or a positive timeout value (in seconds). - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_linger_init(struct J9PortLibrary *portLibrary, j9linger_t handle, int32_t enabled, uint16_t timeout) -{ - memset(handle, 0, sizeof (struct j9linger_struct)); - handle->linger.l_onoff = enabled; - handle->linger.l_linger = (int32_t) timeout; - return 0; -} - - - - - -/** - * Answer the linger timeout value in the argument linger struct. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the linger struct to be accessed. - * @param[out] linger Pointer to the integer, to be updated with the linger value (in seconds). - * - * @return 0, the function does not validate the access. - */ -int32_t -j9sock_linger_linger(struct J9PortLibrary *portLibrary, j9linger_t handle, uint16_t *linger) -{ - *linger = (uint16_t)(handle->linger.l_linger); - return 0; -} - - - - - -/** - * Set the socket to listen for incoming connection requests. This call is made prior to accepting requests, - * via the @ref j9sock_accept function. The backlog specifies the maximum length of the queue of pending connections, - * after which further requests are rejected. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to modify. - * @param[in] backlog The maximum number of queued requests. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_listen(struct J9PortLibrary *portLibrary, j9socket_t sock, int32_t backlog) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - - if (listen(SOCKET_CAST(sock), backlog) < 0) { - rc = errno; - J9SOCKDEBUG( "\n", rc); - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPFAILED); - } - return rc; -} - - - - - -/** - * Answer the 32 bit network ordered argument, in host byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 32 bit network ordered number. - * - * @return the 32 bit host ordered number. - */ -int32_t -j9sock_ntohl(struct J9PortLibrary *portLibrary, int32_t val) -{ - return ntohl(val); -} - - - - - -/** - * Answer the 16-bit network ordered argument, in host byte order. - * - * @param[in] portLibrary The port library. - * @param[in] val The 16-bit network ordered number. - * - * @return the 16-bit host ordered number. - */ -uint16_t -j9sock_ntohs(struct J9PortLibrary *portLibrary, uint16_t val) -{ - uint16_t rc; - - Trc_PRT_sock_j9sock_ntohs_Entry(val); - - rc = ntohs(val); - - Trc_PRT_sock_j9sock_ntohs_Exit(rc); - - return rc; -} - -int32_t -j9sock_read(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t bytesRec = 0; - - Trc_PRT_sock_j9sock_read_Entry(sock, nbyte, flags); - - bytesRec = recv(SOCKET_CAST(sock), buf, nbyte, flags ); - if (-1 == bytesRec) { - int32_t err = errno; - Trc_PRT_sock_j9sock_read_failure(errno); - J9SOCKDEBUG( "\n", err); - err = omrerror_set_last_error(err, findError(err)); - Trc_PRT_sock_j9sock_read_Exit(err); - return err; - } else { - Trc_PRT_sock_j9sock_read_Exit(bytesRec); - return bytesRec; - } -} - -/** - * The read function receives data from a possibly connected socket. Calling read will return as much - * information as is currently available up to the size of the buffer supplied. If the information is too large - * for the buffer, the excess will be discarded. If no incoming data is available at the socket, the read call - * blocks and waits for data to arrive. It the address argument is not null, the address will be updated with - * address of the message sender. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to read on. - * @param[out] buf Pointer to the buffer where input bytes are written. - * @param[in] nbyte The length of buf. - * @param[in] flags Tthe flags, to influence this read. - * @param[out] addrHandle if provided, the address to be updated with the sender information. - * - * @return - * \arg If no error occurs, return the number of bytes received. - * \arg If the connection has been gracefully closed, return 0. - * \arg Otherwise return the (negative) error code. - */ -int32_t -j9sock_readfrom(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t bytesRec = 0; - socklen_t addrlen; - - Trc_PRT_sock_j9sock_readfrom_Entry(sock, nbyte, flags, addrHandle); - - if (NULL == addrHandle) { - addrlen = sizeof(*addrHandle); - bytesRec = recvfrom(SOCKET_CAST(sock), buf, nbyte, flags, NULL, &addrlen); - } else { - addrlen = sizeof(addrHandle->addr); - bytesRec = recvfrom(SOCKET_CAST(sock), buf, nbyte, flags, (struct sockaddr*) &addrHandle->addr, &addrlen); - } - if (-1 == bytesRec) { - int32_t err = errno; - Trc_PRT_sock_j9sock_readfrom_failure(errno); - J9SOCKDEBUG( "\n", err); - err = omrerror_set_last_error(err, findError(err)); - Trc_PRT_sock_j9sock_readfrom_Exit(err); - return err; - } else { - Trc_PRT_sock_j9sock_readfrom_Exit(bytesRec); - return bytesRec; - } -} - - -/** - * The select function allows the state of sockets for read & write operations and exceptional conditions to be tested. - * The function is used prior to a j9sock_read/readfrom, to control the period the operation may block for. - * Depending upon the timeout specified: - * \arg 0, return immediately with the status of the descriptors - * \arg timeout, return when one of the descriptors is ready or after the timeout period has expired - * \arg null, block indefinitely for a ready descriptor - * - * Note 1: j9fdset_t does not support multiple entries. - * Note 2: one of readfd or writefd must contain a valid entry. - * - * @param[in] portLibrary The port library. - * @param[in] nfds For all the sockets that are set in readfds, writefds and exceptfds, this is the maximum value returned - * by @ref j9sock_fdset_size(). - * @param[in] readfd The j9fdset_t representing the descriptor to be checked to see if data can be read without blocking. - * @param[in] writefd The j9fdset_t representing the descriptor to be checked to see if data can be written without blocking. - * @param[in] exceptfd_notSupported Not supported, must either by NULL or empty. Must be NULL on AIX or z/OS. - * @param[in] timeout Pointer to the timeout (a j9timeval struct). - * - * @return Number of socket descriptors that are ready, otherwise return the (negative) error code. - */ -int32_t -j9sock_select(struct J9PortLibrary *portLibrary, int32_t nfds, j9fdset_t readfd, j9fdset_t writefd, j9fdset_t exceptfd_notSupported, j9timeval_t timeout) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; -#if !defined(LINUX) && !defined(OSX) - int32_t result = 0; -#endif /* !defined(LINUX) && !defined(OSX) */ - - Trc_PRT_sock_j9sock_select_Entry(nfds, readfd, writefd, exceptfd_notSupported, timeout == NULL ? 0 : timeout->time.tv_sec, timeout == NULL ? 0 : timeout->time.tv_usec); - - /** - * CMVC 201051 - * Checking whether nfds >= FD_SETSIZE(1024) on Linux is unnecessary in that - * 1) it is using poll(..) which doesn't use fdset at all. - * 2) nfds also gets ignored when using poll(..) on Linux. - * The checking code should be kept active on other platforms (zOS/AIX) since they still use select(..) - * and the default value of FD_SETSIZE varies with platforms. - */ -#if defined(LINUX) || defined(OSX) - if (NULL == timeout) { -#else - if ((nfds >= FD_SETSIZE) || (NULL == timeout)) { -#endif - rc = J9PORT_ERROR_SOCKET_ARGSINVALID; - } else { - - if ((NULL != exceptfd_notSupported) -#if defined(LINUX) || defined(OSX) - && (-1 != exceptfd_notSupported->fd) -#endif - ) { - rc = omrerror_set_last_error_with_message(J9PORT_ERROR_SOCKET_ARGSINVALID, "exceptfd_notSupported cannot contain a valid fd"); - Trc_PRT_sock_j9sock_select_Exit(rc); - return rc; - } - -#if defined(LINUX) || defined(OSX) - int timeoutms; - int pollrc = 0; - struct pollfd pfds[2]; - uintptr_t numEntriesInPfd = 0; - BOOLEAN readfdHasValidFD = FALSE; - BOOLEAN writefdHasValidFD = FALSE; - - memset(pfds, 0, sizeof(pfds)); - - timeoutms = timeout->time.tv_usec/1000 + timeout->time.tv_sec*1000; - - if (NULL != readfd) { - if (-1 != readfd->fd) { - readfdHasValidFD = TRUE; - } - } - - if (NULL != writefd) { - if (-1 != writefd->fd) { - writefdHasValidFD = TRUE; - } - } - - /* at least one of the fds must be valid */ - if (TRUE != (readfdHasValidFD || writefdHasValidFD)) { - rc = omrerror_set_last_error_with_message(J9PORT_ERROR_SOCKET_ARGSINVALID, "One of readfd and writefd must contain a valid socket"); - Trc_PRT_sock_j9sock_select_Exit(rc); - return rc; - } - - /* so, we have at least one valid fd, fill out the poll pfds array */ - - if (readfdHasValidFD) { - pfds[0].fd = readfd->fd; - pfds[0].events |= POLLIN | POLLPRI; - portLibrary->sock_fdset_zero(portLibrary, readfd); - numEntriesInPfd++; - - if (writefdHasValidFD) { - if (writefd->fd == readfd->fd) { - /* same fd, can store both read/write events in the first pollfd entry */ - pfds[0].events |= POLLOUT; - portLibrary->sock_fdset_zero(portLibrary, writefd); - } else { - pfds[1].fd = writefd->fd; - pfds[1].events |= POLLOUT; - portLibrary->sock_fdset_zero(portLibrary, writefd); - numEntriesInPfd++; - } - } - - } else { - pfds[0].fd = writefd->fd; - pfds[0].events |= POLLOUT; - portLibrary->sock_fdset_zero(portLibrary, writefd); - numEntriesInPfd++; - } - - /** - * JAZZ103 80030: PMR 46171,001,866 : JDWP module failed to initialize due to socket accept failed with J9PORT_ERROR_SOCKET_OPFAILED - * - * In the case of poll fails with EINTR, continue trying to poll since EINTR happens randomly for no reason. - */ - do { - /* Make sure revents is initialized to 0 before poll call. revents might not be initialized to 0 if this is not the first try of calling poll */ - pfds[0].revents = 0; - pfds[1].revents = 0; - pollrc = poll(pfds, numEntriesInPfd, timeoutms); - } while ((-1 == pollrc) && (EINTR == errno)); - - if (0 < pollrc) { - /* an event occurred */ - - uintptr_t i; - for (i = 0; i < numEntriesInPfd; i++){ - - if (0 != (pfds[i].revents & (POLLIN | POLLPRI))) { - /* there's data to read */ - readfd->fd = pfds[i].fd; - rc++; - } else if (0 != (POLLOUT & pfds[i].revents)) { - /* writing won't block */ - writefd->fd = pfds[i].fd; - rc++; - } - } - } else if (0 == pollrc) { - /* timeout */ - Trc_PRT_sock_j9sock_select_timeout(); - rc = J9PORT_ERROR_SOCKET_TIMEOUT; - } else { - /* something went wrong in the call to poll */ - Trc_PRT_sock_j9sock_select_failure(errno); - rc = omrerror_set_last_error(pollrc, findError(pollrc)); - } - } -#else - result = select( nfds, readfd == NULL ? NULL : &readfd->handle, writefd == NULL ? NULL : &writefd->handle, NULL, - timeout == NULL ? NULL : &timeout->time); - if (-1 == result) { - rc = errno; - Trc_PRT_sock_j9sock_select_failure(errno); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_INVALIDTIMEOUT); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } else { - if(0 == result) { - rc = J9PORT_ERROR_SOCKET_TIMEOUT; - Trc_PRT_sock_j9sock_select_timeout(); - } else { - rc = result; - } - } - - Trc_PRT_sock_j9sock_select_Exit(rc); - return rc; -} - - -int32_t -j9sock_select_read(struct J9PortLibrary *portLibrary, j9socket_t j9socketP, int32_t secTime, int32_t uSecTime, BOOLEAN isNonBlocking) -{ - /* IPv6 - If both IPv4 and IPv6 sockets are open, the call to this function - * will alternate selecting between the sockets. - */ - j9timeval_struct timeP; - int32_t result =0; - int32_t size = 0; - J9SocketPTB *ptBuffers = NULL; - - Trc_PRT_sock_j9sock_select_read_Entry(j9socketP, secTime, uSecTime, isNonBlocking); - - ptBuffers = j9sock_ptb_get(portLibrary); - if (NULL == ptBuffers) { - Trc_PRT_sock_j9sock_select_read_failure("NULL == ptBuffers"); - Trc_PRT_sock_j9sock_select_read_Exit(J9PORT_ERROR_SOCKET_SYSTEMFULL); - return J9PORT_ERROR_SOCKET_SYSTEMFULL; - } - -/* The max fdset size per process is always expected to be less than a 32bit integer value. - * Is this valid on a 64bit platform? - */ - - /*[PR 99074] Removed check for zero timeout */ - - if (0 == secTime && 0 == uSecTime) { - /* NOTE: empirical evidence indicates that select() on windows always returns zero if timeout is set to zero */ - /* [PR 134527] - win j9sock_select_read reports socket ready */ - if (isNonBlocking){ - /* add these checks, so that if only one socket is open return right away and avoid the loop below */ - if( ((j9socketP->flags & SOCKET_IPV4_OPEN_MASK) != 0) && ((j9socketP->flags & SOCKET_IPV6_OPEN_MASK)==0) ) { - j9socketP->flags = j9socketP->flags | SOCKET_USE_IPV4_MASK; - /* return value of 1 means there is data to be read */ - Trc_PRT_sock_j9sock_select_read_Exit(1); - return 1; - } else if( ((j9socketP->flags & SOCKET_IPV6_OPEN_MASK) != 0) && ((j9socketP->flags & SOCKET_IPV4_OPEN_MASK)==0) ) { - j9socketP->flags = j9socketP->flags & ~SOCKET_USE_IPV4_MASK; - /* return value of 1 means there is data to be read */ - Trc_PRT_sock_j9sock_select_read_Exit(1); - return 1; - } - /* poll every 100 ms */ - j9sock_timeval_init( portLibrary, 0, 100*1000, &timeP ); - } else { - uSecTime = 1; - /* set time to smallest value, without setting to zero which returns 0 on Windows */ - j9sock_timeval_init( portLibrary, 0, uSecTime, &timeP ); - } - } else { - j9sock_timeval_init( portLibrary, secTime, uSecTime, &timeP ); - } - - for(;;) { - result = j9sock_fdset_init( portLibrary, j9socketP ); - if ( 0 != result) { - Trc_PRT_sock_j9sock_select_read_failure("0 != j9sock_fdset_init( portLibrary, j9socketP )"); - Trc_PRT_sock_j9sock_select_read_Exit(result); - return result; - } - size = j9sock_fdset_size( portLibrary, j9socketP ); - if ( 0 > size) { - result = J9PORT_ERROR_SOCKET_FDSET_SIZEBAD; - } else { - result = j9sock_select(portLibrary, size, ptBuffers->fdset, NULL, NULL, &timeP); - } - /* break out of the loop if we should not be looping (timeout is zero) or if an error occured or data ready to be read */ - if ((J9PORT_ERROR_SOCKET_TIMEOUT != result) || (0 != secTime) || (0 !=uSecTime) ) { - /* check which socket has activity after select call and set the appropriate flags */ - if (FD_ISSET(j9socketP->ipv6, ptBuffers->fdset)) { - j9socketP->flags = j9socketP->flags & ~SOCKET_USE_IPV4_MASK; - } - /* update IPv4 last, so it will be used in the event both sockets had activity */ - if (FD_ISSET(j9socketP->ipv4, ptBuffers->fdset)) { - j9socketP->flags = j9socketP->flags | SOCKET_USE_IPV4_MASK; - } - - break; - } - } - - Trc_PRT_sock_j9sock_select_read_Exit(result); - - return result; -} - -int32_t -j9sock_set_nonblocking(struct J9PortLibrary *portLibrary, j9socket_t socketP, BOOLEAN nonblocking) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - uint32_t param = (uint32_t)nonblocking; - - Trc_PRT_sock_j9sock_setnonblocking_Entry(socketP, nonblocking); - - /* If both the IPv4 and IPv6 socket are open then we want to set the option on both. If only one is open, - then we set it just on that one. */ - - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - rc = ioctlsocket( socketP->ipv4, FIONBIO, ¶m); - } - - if( rc == 0 && socketP->flags & SOCKET_IPV6_OPEN_MASK ) { - rc = ioctlsocket( socketP->ipv6, FIONBIO, ¶m); - } - - if( rc != 0 ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setnonblocking_failure(rc); - J9SOCKDEBUG( "\n", rc ); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - default: - rc = omrerror_set_last_error(rc, findError(rc)); - } - } - - Trc_PRT_sock_j9sock_setnonblocking_Exit(rc); - return rc; -} - -/** - * Ensure the flag designated is set in the argument. This is used to construct arguments for the - * j9sock_read/readfrom/write/writeto calls with optional flags, such as J9SOCK_MSG_PEEK. - * - * @param[in] portLibrary The port library. - * @param[in] flag The operation flag to set in the argument. - * @param[in] arg Pointer to the argument to set the flag bit in. - * - * @return 0 if no error occurs, otherwise return the (negative) error code. - */ -int32_t -j9sock_setflag(struct J9PortLibrary *portLibrary, int32_t flag, int32_t *arg) -{ - int32_t rc = 0; - if(flag == J9SOCK_MSG_PEEK) { - *arg |= MSG_PEEK; - } - else if (flag == J9SOCK_MSG_OOB) { - *arg |= MSG_OOB; - } - else { - rc = J9PORT_ERROR_SOCKET_UNKNOWNFLAG; - } - return rc; -} - - -/** - * Set the value of the nominated boolean socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the boolean to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_bool(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, BOOLEAN *optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - BOOL option; - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_bool_Entry(socketP, optlevel, optname, optval == NULL ? 0 : *optval); - - if (NULL == optval) { - rc = J9PORT_ERROR_SOCKET_OPTARGSINVALID; - Trc_PRT_sock_j9sock_setopt_bool_Exit(rc); - return rc; - } - - option = (BOOL)*optval; - optlen = sizeof(option); - - if(0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("bool", "0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_bool_Exit(platformLevel); - return platformLevel; - } - if(0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("bool", "0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_bool_Exit(platformOption); - return platformOption; - } - - /* If both the IPv4 and IPv6 socket are open then we want to set the option on both. If only one is open, - then we set it just on that one. */ - - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - rc = setsockopt( socketP->ipv4, platformLevel, platformOption, (char*)&option, optlen); - } - if( rc == 0 && socketP->flags & SOCKET_IPV6_OPEN_MASK ) { - if((platformOption == IP_MULTICAST_LOOP)&&(platformLevel == OS_IPPROTO_IP)) { - platformLevel = IPPROTO_IPV6; - platformOption = IPV6_MULTICAST_LOOP; - } - rc = setsockopt( socketP->ipv6, platformLevel, platformOption, (char*)&option, optlen); - } - if( rc != 0 ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("bool", rc); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_bool_Exit(rc); - - return rc; -} - - -/** - * Set the value of the nominated byte socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the byte to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_byte(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, uint8_t *optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - int32_t result = 0; - uint32_t optTemp; - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_byte_Entry(socketP, optlevel, optname, optval == NULL ? 0 : *optval); - - if(0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("byte","0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_byte_Exit(platformLevel); - return platformLevel; - } - if(0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("byte", "0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_byte_Exit(platformOption); - return platformOption; - } - - /* If both the IPv4 and IPv6 socket are open then we want to set the option on both. If only one is open, - then we set it just on that one. */ - - if(platformOption == IP_MULTICAST_TTL) { - optTemp = (uint32_t)(*optval); - optlen = sizeof(optTemp); - - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - result = setsockopt(socketP->ipv4, platformLevel, platformOption, (char*)&optTemp, optlen); - } - if( result == 0 && socketP->flags & SOCKET_IPV6_OPEN_MASK ) { - platformLevel = IPPROTO_IPV6; - platformOption = IPV6_MULTICAST_HOPS; - result = setsockopt(socketP->ipv6, platformLevel, platformOption, (char*)&optTemp, optlen); - } - } else { - optlen = sizeof(*optval); - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - result = setsockopt(socketP->ipv4, platformLevel, platformOption, (char*)optval, optlen); - } - if( result == 0 && socketP->flags & SOCKET_IPV6_OPEN_MASK ) { - result = setsockopt(socketP->ipv6, platformLevel, platformOption, (char*)optval, optlen); - } - } - if(0 != result) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("byte", rc); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_byte_Exit(rc); - - return rc; -} - - -/** - * Set the value of the nominated integer socket option. - * Refer to the private platformSocketLevel & platformSocketOption functions for details of the options - * supported. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the integer to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_int(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, int32_t *optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_int_Entry(socketP, optlevel, optname, optval == NULL ? 0 : *optval); - - optlen = sizeof(*optval); - - if(0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("int","0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_int_Exit(platformLevel); - return platformLevel; - } - if(0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("int","0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_int_Exit(platformOption); - return platformOption; - } - - /* If both the IPv4 and IPv6 socket are open then we want to set the option on both. If only one is open, - then we set it just on that one. Also if the option is at the IPV6 level we only set it if the - IPV6 socket is open */ - if(( socketP->flags & SOCKET_IPV4_OPEN_MASK )&&( OS_IPPROTO_IPV6 != platformLevel)){ - rc = setsockopt( socketP->ipv4, platformLevel, platformOption, (char*)optval, optlen); - } - - if( rc == 0 && socketP->flags & SOCKET_IPV6_OPEN_MASK ){ - /* set the option on the IPv6 socket unless it is IP_TOS which is not supported on IPv6 sockets */ - if( !(( OS_IPPROTO_IP == platformLevel)&&(OS_IP_TOS==platformOption))){ - rc = setsockopt( socketP->ipv6, platformLevel, platformOption, (char*)optval, optlen); - } - } - - if(rc != 0) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("int", rc); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_int_Exit(rc); - - return rc; -} - - -/** - * Set the multicast request on this socket. - * Currently this is used to join or leave the nominated multicast group on the local interface. - * It may be more generally useful, so a generic 'setop' function has been defined. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the ipmreq struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_ipmreq(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipmreq_t optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_ipmreq_Entry(socketP, optlevel, optname); - - optlen = sizeof(optval->addrpair); - - if(0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("ipmreq","0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_ipmreq_Exit(platformLevel); - return platformLevel; - } - if(0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("ipmreq","0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_ipmreq_Exit(platformOption); - return platformOption; - } - - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - rc = setsockopt(socketP->ipv4, platformLevel, platformOption, (char*)(&optval->addrpair), optlen); - } - - if( rc != 0 ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("ipmreq",rc); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_ipmreq_Exit(rc); - - return rc; -} - - -/** - * Set the multicast request on this socket for IPv6 sockets. - * Currently this is used to join or leave the nominated multicast group on the local interface. - * It may be more generally useful, so a generic 'setop' function has been defined.t. - * - * Supported families are OS_AF_INET and OS_AF_INET6 - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the ipmreq struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_setopt_ipv6_mreq(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9ipv6_mreq_t optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_ipv6_mreq_Entry(socketP, optlevel, optname); - - optlen = sizeof(optval->mreq); - - if(0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("ipv6_mreq","0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_ipv6_mreq_Exit(platformLevel); - return platformLevel; - } - if(0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("ipv6_mreq","0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_ipv6_mreq_Exit(platformOption); - return platformOption; - } - - if (socketP->flags & SOCKET_IPV6_OPEN_MASK ) { - rc = setsockopt(socketP->ipv6, platformLevel, platformOption, (char*)(&optval->mreq), optlen); - } - else { - /* this option is not supported on this socket */ - Trc_PRT_sock_j9sock_setopt_failure_cause("ipv6_mreq","this option is not supported on this socket"); - Trc_PRT_sock_j9sock_setopt_ipv6_mreq_Exit(J9PORT_ERROR_SOCKET_SOCKLEVELINVALID); - return J9PORT_ERROR_SOCKET_SOCKLEVELINVALID; - } - - if( rc != 0 ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("ipv6_mreq",rc); - J9SOCKDEBUG( "\n",rc ); - switch(rc) { - case WSAEINVAL: - rc = J9PORT_ERROR_SOCKET_OPTARGSINVALID; - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_ipv6_mreq_Exit(rc); - - return rc; -} - - -/** - * Set the linger value on the socket. - * See the @ref j9sock_linger_init for details of the linger behavior. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the linger struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_linger(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9linger_t optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_linger_Entry(socketP, optlevel, optname); - - optlen = sizeof(optval->linger); - - if(0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("linger","0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_linger_Exit(platformLevel); - return platformLevel; - } - if(0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("linger","0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_linger_Exit(platformOption); - return platformOption; - } - - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - rc = setsockopt( socketP->ipv4, platformLevel, platformOption, (char*)(&optval->linger), optlen); - } - - if( rc == 0 && socketP->flags & SOCKET_IPV6_OPEN_MASK ) { - rc = setsockopt( socketP->ipv6, platformLevel, platformOption, (char*)(&optval->linger), optlen); - } - - if( rc != 0 ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("linger", rc); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_linger_Exit(rc); - - return rc; -} - - -/** - * Set the sockaddr for the socket. - * Currently used to set the interface of multicast sockets, but the more general call style is used, - * in case it is more generally useful. - * - * @param[in] portLibrary The port library. - * @param[in] socketP Pointer to the socket to set the option in. - * @param[in] optlevel The level within the IP stack at which the option is defined. - * @param[in] optname The name of the option to set. - * @param[out] optval Pointer to the j9sockaddr struct to update the socket option with. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_setopt_sockaddr(struct J9PortLibrary *portLibrary, j9socket_t socketP, int32_t optlevel, int32_t optname, j9sockaddr_t optval) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t platformLevel = platformSocketLevel(optlevel); - int32_t platformOption = platformSocketOption(optname); - int32_t optlen; - - Trc_PRT_sock_j9sock_setopt_sockaddr_Entry(socketP, optlevel, optname); - - /* It is safe to cast to this as this method is only used with IPv4 addresses */ - optlen = sizeof(((OSSOCKADDR*)&optval->addr)->sin_addr); - - if (0 > platformLevel) { - Trc_PRT_sock_j9sock_setopt_failure_cause("sockaddr","0 > platformLevel"); - Trc_PRT_sock_j9sock_setopt_sockaddr_Exit(platformLevel); - return platformLevel; - } - if (0 > platformOption) { - Trc_PRT_sock_j9sock_setopt_failure_cause("sockaddr","0 > platformOption"); - Trc_PRT_sock_j9sock_setopt_sockaddr_Exit(platformOption); - return platformOption; - } - - if( socketP->flags & SOCKET_IPV4_OPEN_MASK ) { - rc = setsockopt( socketP->ipv4, platformLevel, platformOption, (char *) &((OSSOCKADDR*)&optval->addr)->sin_addr, optlen); - } - - if( rc != 0 ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_setopt_failure_code("sockaddr", rc); - J9SOCKDEBUG("\n", rc); - switch(rc) - { - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_OPTARGSINVALID); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } - - Trc_PRT_sock_j9sock_setopt_sockaddr_Exit(rc); - - return rc; -} - - -/** - * Terminates use of the socket library. No sockets should be in use or the results - * of this operation are unpredictable. Frees any resources held by the socket library. - * - * @param[in] portLibrary The port library. - * - * @return 0, if no errors occurred, otherwise the (negative) error code - */ -int32_t -j9sock_shutdown(struct J9PortLibrary *portLibrary) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t result = 0; - - result = WSACleanup(); - if(SOCKET_ERROR == result ) { - int32_t errorCode = WSAGetLastError(); - - J9SOCKDEBUG( "\n", errorCode ); - return omrerror_set_last_error(errorCode, findError(errorCode)); - } - j9sock_ptb_shutdown(portLibrary); - return 0; -} - - -/** - * The shutdown_input function disables the input stream on a socket. Any subsequent reads from the socket - * will fail. - * - * @param[in] portLibrary The port library. - * @param[in] sock Socket to close input stream on. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_OPFAILED, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if the library is not initialized -*/ -/* IPv6 - If we still have 2 sockets open, then close the input on both. May happen with ::0 and 0.0.0.0. - */ -int32_t -j9sock_shutdown_input(struct J9PortLibrary *portLibrary, j9socket_t sock) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - /* If IPv4 is open or IPv6 is not open. Previously we called it every time, even if the socket was closed. */ - if( sock->flags & SOCKET_USE_IPV4_MASK || ! (sock->flags & SOCKET_IPV6_OPEN_MASK) ) { - rc =shutdown(sock->ipv4, 0); - } - - if( sock->flags & SOCKET_IPV6_OPEN_MASK ) { - rc =shutdown(sock->ipv6, 0); - } - - if( rc == SOCKET_ERROR ) { - int32_t errorCode = WSAGetLastError(); - - J9SOCKDEBUG( "\n", errorCode); - return omrerror_set_last_error(errorCode, findError(errorCode)); - } - return 0; -} - - -/** - * The shutdown_output function disables the output stream on a socket. Any subsequent writes to the socket - * will fail. - * - * @param[in] portLibrary The port library. - * @param[in] sock Socket to close output stream on. - * - * @return - * \arg 0, on success - * \arg J9PORT_ERROR_SOCKET_OPFAILED, on generic error - * \arg J9PORT_ERROR_SOCKET_NOTINITIALIZED, if the library is not initialized - */ -int32_t -j9sock_shutdown_output(struct J9PortLibrary *portLibrary, j9socket_t sock) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - - if( sock->flags & SOCKET_USE_IPV4_MASK || ! (sock->flags & SOCKET_IPV6_OPEN_MASK) ) { - rc = shutdown( sock->ipv4 , 1); - } - - if( sock->flags & SOCKET_IPV6_OPEN_MASK) { - rc = shutdown( sock->ipv6 , 1); - } - - if( rc == SOCKET_ERROR) { - int32_t errorCode = WSAGetLastError(); - - J9SOCKDEBUG( "\n", errorCode); - return omrerror_set_last_error(errorCode, findError(errorCode)); - } - return 0; -} - - -/** - * Creates a new j9sockaddr, referring to the specified port and address. The only address family currently supported - * is AF_INET. - * - * @param[in] portLibrary The port library. - * @param[out] handle Pointer to the j9sockaddr struct, to be allocated. - * @param[in] addrStr The target host, as either a name or dotted ip string. - * @param[in] port The target port, in host order. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_sockaddr(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, const char *addrStr, uint16_t port) -{ - int32_t rc = 0; - uint32_t addr = 0; - j9hostent_struct host_t; - - if(0 != portLibrary->sock_inetaddr(portLibrary, addrStr, &addr)) { - memset(&host_t, 0, sizeof (struct j9hostent_struct)); - if (0 != (rc = portLibrary->sock_gethostbyname(portLibrary, addrStr, &host_t))) { - return rc; - } else { - addr = portLibrary->sock_hostent_addrlist(portLibrary, &host_t, 0); - } - } - rc = portLibrary->sock_sockaddr_init(portLibrary, handle, J9SOCK_AFINET, addr, port); - return rc; -} - - - -/** - * Answer the address, in network order, of the j9sockaddr argument. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the j9sockaddr struct to access. - * - * @return the address (there is no validation on the access). - */ -int32_t -j9sock_sockaddr_address(struct J9PortLibrary *portLibrary, j9sockaddr_t handle) -{ - return ((OSSOCKADDR*)&handle->addr)->sin_addr.s_addr; -} - - -/** - * Answers the IP address of a structure and its length, in a preallocated buffer. - * - * Preallocated buffer "address" should be 16 bytes. "length" tells you how many bytes were used 4 or 16. - * - * @param[in] portLibrary The port library. - * @param[in] handle A populated j9sockaddr_t. - * @param[out] address The IPv4 or IPv6 address in network byte order. - * @param[out] length The number of bytes of the address (4 or 16). - * @param[out] scope_id the scope id for the address if appropriate - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_sockaddr_address6(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *address, uint32_t *length, uint32_t* scope_id) -{ - OSSOCKADDR *ipv4; - OSSOCKADDR_IN6 *ipv6; - - ipv4 = (OSSOCKADDR *) &handle->addr; - if( ipv4->sin_family == OS_AF_INET4 ) { - memcpy( address, &ipv4->sin_addr, 4 ); - *length = 4; - *scope_id = 0; - } - else { - ipv6 = (OSSOCKADDR_IN6 *) &handle->addr; - memcpy( address, &ipv6->sin6_addr, 16 ); - *length = 16; - *scope_id = ipv6->sin6_scope_id; - } - - return 0; -} - - -/** - * Answers the family name of a j9sockaddr_struct. - * - * Supported families are OS_AF_INET and OS_AF_INET6 - * - * @param[in] portLibrary The port library. - * @param[out] family The family name of the address. - * @param[in] handle A populated j9sockaddr_t. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - * - * @note Added for IPv6 support. - */ -int32_t -j9sock_sockaddr_family(struct J9PortLibrary *portLibrary, int16_t *family, j9sockaddr_t handle) -{ - OSSOCKADDR *ipv4; - - ipv4 = (OSSOCKADDR *) &handle->addr; - if( ipv4->sin_family == OS_AF_INET4 ) { - *family = J9ADDR_FAMILY_AFINET4; - } else { - *family = J9ADDR_FAMILY_AFINET6; - } - - return 0; -} - -int32_t -j9sock_sockaddr_init(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, int16_t family, uint32_t nipAddr, uint16_t portNetworkOrder) -{ - OSSOCKADDR *sockaddr; - memset(handle, 0, sizeof (struct j9sockaddr_struct)); - sockaddr = (OSSOCKADDR*)&handle->addr; - sockaddr->sin_family = family; - sockaddr->sin_addr.s_addr = nipAddr ; - sockaddr->sin_port = portNetworkOrder; - - return 0; -} - -int32_t -j9sock_sockaddr_init6(struct J9PortLibrary *portLibrary, j9sockaddr_t handle, uint8_t *addr, int32_t addrlength, int16_t family, uint16_t nPort,uint32_t flowinfo, uint32_t scope_id, j9socket_t sock) -{ - OSSOCKADDR *sockaddr; - OSSOCKADDR_IN6 *sockaddr_6; - memset(handle, 0, sizeof (struct j9sockaddr_struct)); - - if( family == J9ADDR_FAMILY_AFINET4 ) { - sockaddr = (OSSOCKADDR*)&handle->addr; - memcpy( &sockaddr->sin_addr.s_addr, addr, addrlength) ; - sockaddr->sin_port = nPort; - sockaddr->sin_family = OS_AF_INET4; - } - else if ( family == J9ADDR_FAMILY_AFINET6 ) { - sockaddr_6 = (OSSOCKADDR_IN6*)&handle->addr; - memcpy( &sockaddr_6->sin6_addr.s6_addr, addr, addrlength) ; - sockaddr_6->sin6_port = nPort; - sockaddr_6->sin6_family = OS_AF_INET6; - sockaddr_6->sin6_scope_id = scope_id; - sockaddr_6->sin6_flowinfo = htonl(flowinfo); - } - else { - sockaddr = (OSSOCKADDR*)&handle->addr; - sockaddr->sin_family = map_addr_family_J9_to_OS(family); - } - - return 0; -} - - -/** - * Answer the port, in network order, of the j9sockaddr argument. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the j9sockaddr struct to access. - * - * @return the port (there is no validation on the access). - */ -uint16_t -j9sock_sockaddr_port(struct J9PortLibrary *portLibrary, j9sockaddr_t handle) -{ - if( ((OSSOCKADDR*)&handle->addr)->sin_family == OS_AF_INET4 ) { - return ((OSSOCKADDR*)&handle->addr)->sin_port; - } else { - return ((OSSOCKADDR_IN6*)&handle->addr)->sin6_port; - } - -} - - -/** - * Creates a new socket descriptor and any related resources. + * Terminates use of the socket library. No sockets should be in use or the results + * of this operation are unpredictable. Frees any resources held by the socket library. * * @param[in] portLibrary The port library. - * @param[out] handle Pointer pointer to the j9socket struct, to be allocated - * @param[in] family The address family (currently, only J9SOCK_AFINET is supported) - * @param[in] socktype Specifies what type of socket is created - * \arg J9SOCK_STREAM, for a stream socket - * \arg J9SOCK_DGRAM, for a datagram socket - * @param[in] protocol Type/family specific creation parameter (currently, only J9SOCK_DEFPROTOCOL supported). * - * @return 0, if no errors occurred, otherwise the (negative) error code. + * @return 0, if no errors occurred, otherwise the (negative) error code */ int32_t -j9sock_socket(struct J9PortLibrary *portLibrary, j9socket_t *handle, int32_t family, int32_t socktype, int32_t protocol) +j9sock_shutdown(struct J9PortLibrary *portLibrary) { OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - OSSOCKET ipv4 = INVALID_SOCKET; - OSSOCKET ipv6 = INVALID_SOCKET; - - - Trc_PRT_sock_j9sock_socket_Entry(handle, family, socktype, protocol); - - /* Initialize the handle to invalid */ - *handle = (j9socket_t) -1; - - if(family != J9ADDR_FAMILY_AFINET6 && family != J9ADDR_FAMILY_AFINET4 && family != J9ADDR_FAMILY_UNSPEC) { - Trc_PRT_sock_j9sock_socket_failure_cause("family != J9ADDR_FAMILY_AFINET6 && family != J9ADDR_FAMILY_AFINET4 && family != J9ADDR_FAMILY_UNSPEC"); - rc = J9PORT_ERROR_SOCKET_BADAF; - } - if((socktype != J9SOCK_STREAM) && (socktype != J9SOCK_DGRAM) ) { - Trc_PRT_sock_j9sock_socket_failure_cause("(socktype != J9SOCK_STREAM) && (socktype != J9SOCK_DGRAM)"); - rc = J9PORT_ERROR_SOCKET_BADTYPE; - } - if(protocol != J9SOCK_DEFPROTOCOL ) { - Trc_PRT_sock_j9sock_socket_failure_cause("protocol != J9SOCK_DEFPROTOCOL"); - rc = J9PORT_ERROR_SOCKET_BADPROTO; - } - if (rc == 0) { - if( family == J9ADDR_FAMILY_AFINET4 || family == J9ADDR_FAMILY_UNSPEC ) { - ipv4 = socket(AF_INET, ((socktype == J9SOCK_STREAM) ? SOCK_STREAM : SOCK_DGRAM), 0); - if( ipv4 == INVALID_SOCKET ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_socket_creation_failure(rc); - J9SOCKDEBUG( "\n", rc ); - switch(rc) { - case WSAENOBUFS: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_SYSTEMFULL); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - } - } - } - if( rc ==0 && ( family == J9ADDR_FAMILY_AFINET6 || family == J9ADDR_FAMILY_UNSPEC ) ) { - - ipv6 = socket(AF_INET6, ((socktype == J9SOCK_STREAM) ? SOCK_STREAM : SOCK_DGRAM), 0); - - if( ipv6 == INVALID_SOCKET ) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_socket_creation_failure(rc); - J9SOCKDEBUG( "\n", rc); - switch(rc) { - case WSAENOBUFS: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_SYSTEMFULL); - break; - case WSAEAFNOSUPPORT: - rc = 0; - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - } - if (rc != 0) { - /* close the open IPv4 socket because socket() call fails - * and the user would never get to use this socket as we - * would return in error from IPv6 socket creation failure */ - internalCloseSocket(portLibrary, *handle, TRUE); - } - } - } - } - - if (0 == rc) { - *handle = omrmem_allocate_memory(sizeof( struct j9socket_struct ), OMRMEM_CATEGORY_PORT_LIBRARY); - - if (NULL == *handle) { - - *handle = (j9socket_t)INVALID_SOCKET; - - if (INVALID_SOCKET != ipv4) { - shutdown(ipv4, 1); - closesocket(ipv4); - ipv4 = INVALID_SOCKET; - } - if (INVALID_SOCKET != ipv6) { - shutdown(ipv6, 1); - closesocket(ipv6); - ipv6 = INVALID_SOCKET; - } - rc = J9PORT_ERROR_SOCKET_NOBUFFERS; - Trc_PRT_sock_j9sock_socket_failure_oom(); - Trc_PRT_sock_j9sock_socket_Exit(rc); - return rc; - } - - /* Initialize the new structure to show that the IPv4 structure is to be used, and the 2 sockets are invalid */ - (*handle)->ipv4 = INVALID_SOCKET; - (*handle)->ipv6 = INVALID_SOCKET; - (*handle)->flags = SOCKET_USE_IPV4_MASK; - - - if( ipv4 != INVALID_SOCKET ) { - /* adjust flags to show IPv4 socket is open for business */ - (*handle)->flags = (*handle)->flags | SOCKET_IPV4_OPEN_MASK; - (*handle)->ipv4 = ipv4; - } - - if( ipv6 != INVALID_SOCKET ) { - (*handle)->ipv6 = ipv6; - if( family == J9ADDR_FAMILY_AFINET6 ) { - /* set flags to show use IPV6 and IPv6 socket open */ - (*handle)->flags = ~SOCKET_USE_IPV4_MASK & SOCKET_IPV6_OPEN_MASK; - } else { - /* adjust flags to show IPv6 is open for business */ - (*handle)->flags = (*handle)->flags | SOCKET_IPV6_OPEN_MASK; - } - } - - Trc_PRT_sock_j9sock_socket_created(*handle); - } - - Trc_PRT_sock_j9sock_socket_Exit(rc); - - return rc; -} + int32_t result = 0; + result = WSACleanup(); + if(SOCKET_ERROR == result ) { + int32_t errorCode = WSAGetLastError(); -/** - * Determines whether or not the socket is valid. - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to the j9socket struct, to be allocated. - * - * @return 0 if invalid, non-zero for valid. - */ -int32_t -j9sock_socketIsValid(struct J9PortLibrary *portLibrary, j9socket_t handle) -{ - if( handle == (void *) NULL || handle == (void *) INVALID_SOCKET ) { - return FALSE; + J9SOCKDEBUG( "\n", errorCode ); + return omrerror_set_last_error(errorCode, findError(errorCode)); } - - /* If either socket is open, then return TRUE, otherwise return FALSE */ - return handle->flags & SOCKET_BOTH_OPEN_MASK; + j9sock_ptb_shutdown(portLibrary); + return 0; } @@ -3584,500 +921,3 @@ j9sock_startup(struct J9PortLibrary *portLibrary) } return rc; } - - -/** - * Create a time structure, representing the timeout period defined in seconds & microSeconds. - * Timeval's are used as timeout arguments in the @ref j9sock_select function. - * - * @param[in] portLibrary The port library. - * @param[in] secTime The integer component of the timeout value (in seconds). - * @param[in] uSecTime The fractional component of the timeout value (in microseconds). - * @param[out] timeP Pointer pointer to the j9timeval_struct to be allocated. - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_timeval_init(struct J9PortLibrary *portLibrary, uint32_t secTime, uint32_t uSecTime, j9timeval_t timeP) -{ - memset(timeP, 0, sizeof (struct j9timeval_struct)); - timeP->time.tv_sec = secTime; - timeP->time.tv_usec = uSecTime; - - return 0; -} - - -/** - * The write function writes data to a connected socket. The successful completion of a write - * does not indicate that the data was successfully delivered. If no buffer space is available - * within the transport system to hold the data to be transmitted, send will block. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to send on - * @param[in] buf The bytes to be sent - * @param[in] nbyte The number of bytes to send - * @param[in] flags The flags to modify the send behavior - * - * @return If no error occur, return the total number of bytes sent, which can be less than the - * 'nbyte' for nonblocking sockets, otherwise the (negative) error code - */ -int32_t -j9sock_write(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t rc = 0; - int32_t bytesSent = 0; - - Trc_PRT_sock_j9sock_write_Entry(sock, buf, nbyte, flags); - - if( sock->flags & SOCKET_USE_IPV4_MASK ) { - bytesSent = send(sock->ipv4, (char *)buf, nbyte, flags); - } else { - bytesSent = send(sock->ipv6, (char *)buf, nbyte, flags); - } - if(SOCKET_ERROR == bytesSent) { - rc = WSAGetLastError(); - Trc_PRT_sock_j9sock_write_failure(rc); - J9SOCKDEBUG( "\n", rc); - switch(rc){ - case WSAEINVAL: - rc = omrerror_set_last_error(rc, J9PORT_ERROR_SOCKET_NOTBOUND); - break; - default: - rc = omrerror_set_last_error(rc, findError(rc)); - break; - } - } else { - rc = bytesSent; - } - - Trc_PRT_sock_j9sock_write_Exit(rc); - - return rc; -} - - -/** - * The writeto function writes data to a datagram socket. The successful completion of a writeto - * does not indicate that the data was successfully delivered. If no buffer space is available - * within the transport system to hold the data to be transmitted, writeto will block. - * - * @param[in] portLibrary The port library. - * @param[in] sock Pointer to the socket to send on - * @param[in] buf The bytes to be sent - * @param[in] nbyte The number of bytes to send - * @param[in] flags The flags to modify the send behavior - * @param [in] addrHandle The network address to send the datagram to - * - * @return If no error occur, return the total number of bytes sent, otherwise the (negative) error code. - */ -int32_t -j9sock_writeto(struct J9PortLibrary *portLibrary, j9socket_t sock, uint8_t *buf, int32_t nbyte, int32_t flags, j9sockaddr_t addrHandle) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - int32_t bytesSent = 0; -#if defined(J9_USE_CONNECTION_MANAGER) - BOOLEAN firstAttempt = TRUE; - Trc_PRT_sock_j9sock_writeto_Entry(sock, buf, nbyte, flags, addrHandle); - -retry: -#else - Trc_PRT_sock_j9sock_writeto_Entry(sock, buf, nbyte, flags, addrHandle); -#endif - if( ((((OSSOCKADDR *) &addrHandle->addr)->sin_family == OS_AF_INET4) && (sock->flags & SOCKET_IPV4_OPEN_MASK) ) || - ((((OSSOCKADDR *) &addrHandle->addr)->sin_family == OS_AF_INET6) && !(sock->flags & SOCKET_IPV6_OPEN_MASK)) ){ - bytesSent = sendto( sock->ipv4, (char *)buf, nbyte, flags, (const struct sockaddr *) &(addrHandle->addr), sizeof(addrHandle->addr)); - } else { - bytesSent = sendto( sock->ipv6, (char *)buf, nbyte, flags, (const struct sockaddr *) &(addrHandle->addr), sizeof(addrHandle->addr)); - } - - if(SOCKET_ERROR == bytesSent) { - int32_t errorCode = WSAGetLastError(); - Trc_PRT_sock_j9sock_writeto_failure(errorCode); - J9SOCKDEBUG( "\n", errorCode ); -#if defined(J9_USE_CONNECTION_MANAGER) - if (firstAttempt && (errorCode == WSAENETDOWN || errorCode == WSAEHOSTUNREACH)) { - firstAttempt = FALSE; - if (ensureConnected(portLibrary)) goto retry; - } - firstAttempt = FALSE; -#endif - errorCode = omrerror_set_last_error(errorCode, findError(errorCode)); - Trc_PRT_sock_j9sock_writeto_Exit(errorCode); - return errorCode; - } else { - Trc_PRT_sock_j9sock_writeto_Exit(bytesSent); - return bytesSent; - } -} - - -static int32_t -addAdapterIpv6(struct J9PortLibrary *portLibrary, struct j9NetworkInterface_struct *interfaces, uint32_t currentAdapterIndex, - IP_ADAPTER_ADDRESSES *currentAdapter, IP_ADAPTER_ADDRESSES *adaptersList, char *baseName, uintptr_t adapterIndex) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - uintptr_t nameLength; - uint32_t numAddresses =0; - IP_ADAPTER_UNICAST_ADDRESS *currentIPAddress = NULL; - uint32_t pseudoLoopbackFound = 0; - IP_ADAPTER_ADDRESSES *tempAdapter = NULL; - uint32_t currentIPAddressIndex = 0; - - /* set the index for the interface */ - /* return non-zero Ipv6IfIndex for Ipv6 enabled host, otherwise return IfIndex for Ipv4 only host */ - if (currentAdapter->Ipv6IfIndex != 0) { - interfaces[currentAdapterIndex].index = currentAdapter->Ipv6IfIndex; - } else { - interfaces[currentAdapterIndex].index = currentAdapter->IfIndex; - } - - /* get the name and display name for the adapter */ - if (adapterIndex != -1) { - nameLength = omrstr_printf(NULL, 0, "%s%d", baseName, adapterIndex); - } else { - nameLength = strlen(baseName) + 1; - } - interfaces[currentAdapterIndex].name = omrmem_allocate_memory(nameLength, OMRMEM_CATEGORY_PORT_LIBRARY); -#if (defined(VALIDATE_ALLOCATIONS)) - if (NULL == interfaces[currentAdapterIndex].name) { - return J9PORT_ERROR_SOCKET_NOBUFFERS; - } -#endif - if (-1 != adapterIndex) { - omrstr_printf(interfaces[currentAdapterIndex].name, nameLength, "%s%d", baseName, adapterIndex); - } else { - strcpy(interfaces[currentAdapterIndex].name, baseName); - } - - nameLength = WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS,currentAdapter->Description, -1,NULL,0,NULL,NULL); - - if (0 != nameLength) { - interfaces[currentAdapterIndex].displayName = omrmem_allocate_memory(nameLength + 1, OMRMEM_CATEGORY_PORT_LIBRARY); - } else { - interfaces[currentAdapterIndex].displayName = omrmem_allocate_memory(1, OMRMEM_CATEGORY_PORT_LIBRARY); - interfaces[currentAdapterIndex].displayName[0] = 0; - } -#if (defined(VALIDATE_ALLOCATIONS)) - if (NULL == interfaces[currentAdapterIndex].displayName) { - return J9PORT_ERROR_SOCKET_NOBUFFERS; - } -#endif - if (nameLength != 0) { - WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS,currentAdapter->Description, -1, interfaces[currentAdapterIndex].displayName , (int)nameLength, NULL,NULL); - interfaces[currentAdapterIndex].displayName[nameLength] = 0; - } - - /* now get the interface information */ - - /* first count the number of IP addresses and allocate the memory required for the ip address info that will be returned*/ - numAddresses = 0; - currentIPAddress = currentAdapter->FirstUnicastAddress; - while(currentIPAddress) { - numAddresses = numAddresses + 1; - currentIPAddress = currentIPAddress->Next; - } - - /* if this is the loopback address then we need to add the addresses from the Loopback Pseudo-Interface */ - pseudoLoopbackFound = 0; - if ((NULL != currentAdapter->FirstUnicastAddress)&& - (AF_INET == ((struct sockaddr_in*)(currentAdapter->FirstUnicastAddress->Address.lpSockaddr))->sin_family ) && - ( 127 == ((struct sockaddr_in*)(currentAdapter->FirstUnicastAddress->Address.lpSockaddr))->sin_addr.S_un.S_un_b.s_b1) - ){ - /* find the pseudo interface and get the first unicast address */ - tempAdapter = adaptersList; - pseudoLoopbackFound = 0; - while((tempAdapter)&&(0==pseudoLoopbackFound)) { - if (strcmp(tempAdapter->AdapterName,pseudoLoopbackGUID) == 0) { - pseudoLoopbackFound = 1; - } else{ - tempAdapter = tempAdapter->Next; - } - } - - if (1 == pseudoLoopbackFound){ - /* now if we found the adapter add the count for the addresses on it */ - currentIPAddress = tempAdapter->FirstUnicastAddress; - while(currentIPAddress) { - numAddresses = numAddresses + 1; - currentIPAddress = currentIPAddress->Next; - } - - /* also if we found the pseudo interface we must have to use the interface id associated with this interface */ - interfaces[currentAdapterIndex].index = tempAdapter->Ipv6IfIndex; - } - } - - interfaces[currentAdapterIndex].addresses = omrmem_allocate_memory(numAddresses * sizeof(j9ipAddress_struct), OMRMEM_CATEGORY_PORT_LIBRARY); -#if (defined(VALIDATE_ALLOCATIONS)) - if (NULL == interfaces[currentAdapterIndex].addresses) { - return J9PORT_ERROR_SOCKET_NOBUFFERS; - } -#endif - interfaces[currentAdapterIndex].numberAddresses = numAddresses; - - /* now get the actual ip address info */ - currentIPAddressIndex =0; - currentIPAddress = currentAdapter->FirstUnicastAddress; - while(currentIPAddress) { - if (currentIPAddress->Address.iSockaddrLength == sizeof(struct sockaddr_in6)) { - memcpy(interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.bytes, - &(((struct sockaddr_in6*) currentIPAddress->Address.lpSockaddr)->sin6_addr.u.Byte), - sizeof(struct in6_addr)); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in6_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = ((struct sockaddr_in6*) currentIPAddress->Address.lpSockaddr)->sin6_scope_id; - } else if (currentIPAddress->Address.iSockaddrLength == sizeof(struct sockaddr_in6_old)) { - memcpy(interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.bytes, - &(((struct sockaddr_in6_old*) currentIPAddress->Address.lpSockaddr)->sin6_addr.u.Byte), - sizeof(struct in6_addr)); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in6_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = 0; - } else { - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.inAddr.S_un.S_addr = - ((struct sockaddr_in*) currentIPAddress->Address.lpSockaddr)->sin_addr.S_un.S_addr; - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = 0; - } - - currentIPAddress = currentIPAddress->Next; - currentIPAddressIndex = currentIPAddressIndex + 1; - } - - /* now add in the addresses from the loopback pseudo-interface if appropriate */ - if (1 == pseudoLoopbackFound) { - currentIPAddress = tempAdapter->FirstUnicastAddress; - while(currentIPAddress) { - if (currentIPAddress->Address.iSockaddrLength == sizeof(struct sockaddr_in6)) { - memcpy(interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.bytes, - &(((struct sockaddr_in6*) currentIPAddress->Address.lpSockaddr)->sin6_addr.u.Byte), - sizeof(struct in6_addr)); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in6_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = ((struct sockaddr_in6*) currentIPAddress->Address.lpSockaddr)->sin6_scope_id; - } else if (currentIPAddress->Address.iSockaddrLength == sizeof(struct sockaddr_in6_old)) { - memcpy(interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.bytes, - &(((struct sockaddr_in6_old*) currentIPAddress->Address.lpSockaddr)->sin6_addr.u.Byte), - sizeof(struct in6_addr)); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in6_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = 0; - } else { - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.inAddr.S_un.S_addr = - ((struct sockaddr_in*) currentIPAddress->Address.lpSockaddr)->sin_addr.S_un.S_addr; - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = 0; - } - - currentIPAddress = currentIPAddress->Next; - currentIPAddressIndex = currentIPAddressIndex + 1; - } - } - return 0; -} - -static int32_t -addAdapterIpv4(struct J9PortLibrary *portLibrary, struct j9NetworkInterface_struct *interfaces, uint32_t currentAdapterIndex, - IP_ADAPTER_INFO *currentAdapter, IP_ADAPTER_INFO *adaptersList, char *baseName, uintptr_t adapterIndex) -{ - OMRPORT_ACCESS_FROM_J9PORT(portLibrary); - uintptr_t nameLength; - uint32_t numAddresses =0; - IP_ADDR_STRING *currentIPAddress = NULL; - uint32_t pseudoLoopbackFound = 0; - IP_ADAPTER_ADDRESSES *tempAdapter = NULL; - uint32_t currentIPAddressIndex = 0; - - /* Harmony code rely on index to check network interface status */ - interfaces[currentAdapterIndex].index = currentAdapter->Index; - - /* get the name and display name for the adapter */ - if (-1 != adapterIndex) { - nameLength = omrstr_printf(NULL, 0, "%s%d", baseName, adapterIndex); - } else { - nameLength = strlen(baseName) + 1; - } - interfaces[currentAdapterIndex].name = omrmem_allocate_memory(nameLength, OMRMEM_CATEGORY_PORT_LIBRARY); -#if (defined(VALIDATE_ALLOCATIONS)) - if (NULL == interfaces[currentAdapterIndex].name) { - return J9PORT_ERROR_SOCKET_NOBUFFERS; - } -#endif - - if (-1 != adapterIndex) { - omrstr_printf(interfaces[currentAdapterIndex].name, nameLength, "%s%d", baseName, adapterIndex); - } else { - strcpy(interfaces[currentAdapterIndex].name, baseName); - } - - nameLength = strlen(currentAdapter->Description); - interfaces[currentAdapterIndex].displayName = omrmem_allocate_memory(nameLength+1, OMRMEM_CATEGORY_PORT_LIBRARY); -#if (defined(VALIDATE_ALLOCATIONS)) - if (NULL == interfaces[currentAdapterIndex].displayName) { - return J9PORT_ERROR_SOCKET_NOBUFFERS; - } -#endif - strncpy(interfaces[currentAdapterIndex].displayName,currentAdapter->Description,nameLength); - interfaces[currentAdapterIndex].displayName[nameLength] = 0; - - /* now get the interface information */ - - /* first count the number of IP addresses and allocate the memory required for the ip address info that will be returned*/ - numAddresses = 0; - currentIPAddress = &(currentAdapter->IpAddressList); - while(currentIPAddress) { - unsigned long addr = inet_addr(currentIPAddress->IpAddress.String); - - /* don't count the any address which seems to be returned as the first address for interfaces with no addresses */ - if ( (addr != INADDR_NONE) && (addr != INADDR_ANY) ) { - numAddresses = numAddresses + 1; - } - currentIPAddress = currentIPAddress->Next; - } - interfaces[currentAdapterIndex].addresses = omrmem_allocate_memory(numAddresses * sizeof(j9ipAddress_struct), OMRMEM_CATEGORY_PORT_LIBRARY); -#if (defined(VALIDATE_ALLOCATIONS)) - if (NULL == interfaces[currentAdapterIndex].addresses) { - return J9PORT_ERROR_SOCKET_NOBUFFERS; - } -#endif - interfaces[currentAdapterIndex].numberAddresses = numAddresses; - - /* now get the actual ip address info */ - currentIPAddressIndex =0; - currentIPAddress = &(currentAdapter->IpAddressList); - while(currentIPAddress) { - unsigned long addr = inet_addr(currentIPAddress->IpAddress.String); - - /* don't count the any address which seems to be returned as the first address for interfaces with no addresses */ - if ( (addr != INADDR_NONE) && (addr != INADDR_ANY) ) { - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].addr.inAddr.S_un.S_addr = inet_addr(currentIPAddress->IpAddress.String); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].length = sizeof(struct in_addr); - interfaces[currentAdapterIndex].addresses[currentIPAddressIndex].scope = 0; - currentIPAddressIndex = currentIPAddressIndex + 1; - } - currentIPAddress = currentIPAddress->Next; - } - return 0; -} - -/** - * Queries and returns the information for the network interfaces that are currently active within the system. - * Applications are responsible for freeing the memory returned via the handle. - * - * @param[in] portLibrary The port library. - * @param[in,out] array Pointer to structure with array of network interface entries - * @param[in] boolean which indicates if we should prefer the IPv4 stack or not - * - * @return 0 on success, negative portable error code on failure. - J9PORT_ERROR_SOCKET_NORECOVERY if system calls required to get the info fail, J9PORT_ERROR_SOCKET_NOBUFFERS if memory allocation fails - */ -int32_t -j9sock_get_network_interfaces(struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct *array,BOOLEAN preferIPv4Stack) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} - - -/** - * Frees the memory allocated for the j9NetworkInterface_struct array passed in - * - * @param[in] portLibrary The port library. - * @param[in] handle Pointer to array of network interface structures to be freed - * - * @return 0 on success -*/ -int32_t -j9sock_free_network_interface_struct(struct J9PortLibrary *portLibrary, struct j9NetworkInterfaceArray_struct* array ) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} - - -/** - * Establish a connection to a peer with a timeout. This function is called repeatedly in order to carry out the connect and to allow - * other tasks to proceed on certain platforms. The caller must first call with step = J9_SOCK_STEP_START, if the result is J9_ERROR_SOCKET_NOTCONNECTED - * it will then call it with step = CHECK until either another error or 0 is returned to indicate the connect is complete. Each time the function should sleep for no more than - * timeout milliseconds. If the connect succeeds or an error occurs, the caller must always end the process by calling the function with step = J9_SOCK_STEP_DONE - * - * @param[in] portLibrary The port library. - * @param[in] sock pointer to the unconnected local socket. - * @param[in] addr pointer to the sockaddr, specifying remote host/port. - * @param[in] timeout timeout in milliseconds - * @param[in,out] pointer to context pointer. Filled in on first call and then to be passed into each subsequent call - * - * @return 0, if no errors occurred, otherwise the (negative) error code. - */ -int32_t -j9sock_connect_with_timeout(struct J9PortLibrary *portLibrary, j9socket_t sock, j9sockaddr_t addr, uint32_t timeout, uint32_t step, uint8_t** context) -{ - return J9PORT_ERROR_SOCKET_BADSOCKET; -} - -/* Note: j9fdset_t does not support multiple entries. */ -void -j9sock_fdset_zero(struct J9PortLibrary *portLibrary, j9fdset_t j9fdset) -{ - - FD_ZERO(&j9fdset->handle); - return; - -} - -/* Note: j9fdset_t does not support multiple entries. */ -void -j9sock_fdset_set(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) -{ - - if( aSocket->flags & SOCKET_IPV4_OPEN_MASK ) { - FD_SET( aSocket->ipv4, &j9fdset->handle); - } - if( aSocket->flags & SOCKET_IPV6_OPEN_MASK ) { - FD_SET( aSocket->ipv6, &j9fdset->handle); - } - - return; -} - -/* Note: j9fdset_t does not support multiple entries. */ -void -j9sock_fdset_clr(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) -{ - - if( aSocket->flags & SOCKET_IPV4_OPEN_MASK ) { - FD_CLR( aSocket->ipv4, &j9fdset->handle); - } - if( aSocket->flags & SOCKET_IPV6_OPEN_MASK ) { - FD_CLR( aSocket->ipv6, &j9fdset->handle); - } - - return; -} - -/* @internal Check if the ipv4 or ipv6 sockets in the fdset are set if they are open - * If both are set (as in FD_ISSET returned non-zero for both), defer to the ipv4 socket is it takes precedence - * - * Note: j9fdset_t does not support multiple entries. - */ -BOOLEAN -j9sock_fdset_isset(struct J9PortLibrary *portLibrary, j9socket_t aSocket, j9fdset_t j9fdset) -{ - - BOOLEAN rc = FALSE; - int ipv6FdIsSet, ipv4FdIsSet = 0; - - if( aSocket->flags & SOCKET_IPV6_OPEN_MASK ) { - ipv6FdIsSet = FD_ISSET(aSocket->ipv6, &j9fdset->handle); - if (0 != ipv6FdIsSet) { - rc = TRUE; - aSocket->flags = aSocket->flags & ~SOCKET_USE_IPV4_MASK; - } - } - if( aSocket->flags & SOCKET_IPV4_OPEN_MASK ) { - /* update IPv4 last, so it will be used in the event both sockets had activity */ - ipv4FdIsSet = FD_ISSET(aSocket->ipv4, &j9fdset->handle); - if (0 != ipv4FdIsSet) { - rc = TRUE; - aSocket->flags = aSocket->flags | SOCKET_USE_IPV4_MASK; - } - } - - return rc; -} diff --git a/runtime/port/win32_include/j9sock.h b/runtime/port/win32_include/j9sock.h index ceb85f860df..633e16f03f4 100644 --- a/runtime/port/win32_include/j9sock.h +++ b/runtime/port/win32_include/j9sock.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2015 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -46,36 +46,10 @@ extern J9_CFUNC char * os_error_message (struct J9PortLibrary *portLibrary, int32_t errorNum ); /* os types */ -typedef SOCKADDR OSADDR; - -typedef struct ip_mreq OSIPMREQ; -#define OSSOMAXCONN SOMAXCONN -#define OS_BADSOCKET INVALID_SOCKET /*[PR 1FJBLB9] Provide bad socket constant */ - typedef struct addrinfoW OSADDRINFO; /* IPv6 - Unicode */ typedef SOCKADDR_IN6 OSSOCKADDR_IN6; /* IPv6 */ -/* constant for identifying the pseudo looback interface */ -#define pseudoLoopbackGUID "{6BD113CC-5EC2-7638-B953-0B889DA72014}" - -/* defines for socket levels */ -#define OS_SOL_SOCKET SOL_SOCKET -#define OS_IPPROTO_TCP IPPROTO_TCP -#define OS_IPPROTO_IP IPPROTO_IP -#define OS_IPPROTO_IPV6 IPPROTO_IPV6 - -/* defines for socket options */ -#define OS_SO_LINGER SO_LINGER -#define OS_SO_KEEPALIVE SO_KEEPALIVE -#define OS_TCP_NODELAY TCP_NODELAY -#define OS_SO_REUSEADDR SO_REUSEADDR -#define OS_SO_SNDBUF SO_SNDBUF -#define OS_SO_RCVBUF SO_RCVBUF -#define OS_SO_BROADCAST SO_BROADCAST /*[PR1FLSKTU] Support datagram broadcasts */ -#define OS_SO_OOBINLINE SO_OOBINLINE -#define OS_IP_TOS IP_TOS - /* defines added for IPv6 */ #define OS_AF_INET4 AF_INET #define OS_AF_UNSPEC AF_UNSPEC @@ -86,20 +60,6 @@ typedef SOCKADDR_IN6 OSSOCKADDR_IN6; /* IPv6 */ #define OS_PF_INET6 PF_INET6 #define OS_INET4_ADDRESS_LENGTH INET_ADDRSTRLEN #define OS_INET6_ADDRESS_LENGTH INET6_ADDRSTRLEN -#define OSNIMAXSERV NI_MAXSERV - -/* defines for socket options, multicast */ -#define OS_MCAST_TTL IP_MULTICAST_TTL -#define OS_MCAST_ADD_MEMBERSHIP IP_ADD_MEMBERSHIP -#define OS_MCAST_DROP_MEMBERSHIP IP_DROP_MEMBERSHIP -#define OS_MCAST_INTERFACE IP_MULTICAST_IF -#define OS_MCAST_INTERFACE_2 IPV6_MULTICAST_IF -#define OS_IPV6_ADD_MEMBERSHIP IPV6_ADD_MEMBERSHIP -#define OS_IPV6_DROP_MEMBERSHIP IPV6_DROP_MEMBERSHIP -#define OS_MCAST_LOOP IP_MULTICAST_LOOP - -/* platform constants */ -#define J9SOCK_MAXCONN OSSOMAXCONN /* * Socket Types @@ -112,15 +72,9 @@ typedef SOCKADDR_IN6 OSSOCKADDR_IN6; /* IPv6 */ #define OSSOCK_SEQPACKET SOCK_SEQPACKET /* sequenced packet stream */ -/* socket structure flags */ -#define SOCKET_IPV4_OPEN_MASK '\x1' /* 00000001 */ -#define SOCKET_IPV6_OPEN_MASK '\x2' /* 00000010 */ -#define SOCKET_BOTH_OPEN_MASK '\x3' /* 00000011 */ - /* Per-thread buffer for platform-dependent socket information */ typedef struct J9SocketPTB { struct J9PortLibrary *portLibrary; - j9fdset_t fdset; /**< file descriptor set */ j9addrinfo_struct addr_info_hints; } J9SocketPTB; diff --git a/runtime/tests/port/CMakeLists.txt b/runtime/tests/port/CMakeLists.txt index 57fe5e15d81..01a3c48207a 100644 --- a/runtime/tests/port/CMakeLists.txt +++ b/runtime/tests/port/CMakeLists.txt @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2017, 2018 IBM Corp. and others +# Copyright (c) 2017, 2020 IBM Corp. and others # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License 2.0 which accompanies this @@ -46,7 +46,6 @@ add_executable(pltest shmem.c si_numcpusTest.c si.c - socket.c testHelpers.c testProcessHelpers.c ) diff --git a/runtime/tests/port/j9sockTest.c b/runtime/tests/port/j9sockTest.c index e30b7b750c3..6e0fcfdb480 100644 --- a/runtime/tests/port/j9sockTest.c +++ b/runtime/tests/port/j9sockTest.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -51,42 +51,12 @@ #include "testProcessHelpers.h" #include "j9fileTest.h" -static I_32 connectClientToServerNonBlocking(struct J9PortLibrary *portLibrary, const char* testName, char *addrStr, U_16 port, j9socket_t *session_client_socket, j9sockaddr_t session_client_addr); - #if 0 #define SOCKTEST_DBG #endif -#ifdef IPv6_FUNCTION_SUPPORT -void printAddress(struct J9PortLibrary *portLibrary, const struct sockaddr *address) { - char ipstr[INET_ADDRSTRLEN]; - PORT_ACCESS_FROM_PORT(portLibrary); - - switch(address->sa_family) { - case AF_INET: - /* - inet_ntop(AF_INET, &(((struct sockaddr_in *)address)->sin_addr), ipstr, sizeof ipstr); - j9tty_printf(portLibrary, "AF_INET %s\n", ipstr); - */ - break; - - case AF_INET6: - /* - inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)address)->sin6_addr), ipstr, sizeof ipstr); - j9tty_printf(portLibrary, "AF_INET6 %s\n", ipstr); - */ - break; - - default: - j9tty_printf(portLibrary, "Unknown address family: %s\n", ipstr); - } - -} -#endif /* IPv6_FUNCTION_SUPPORT */ - - /** * Verify port library properly setup to run j9sock tests * @@ -110,1214 +80,15 @@ j9sock_verify_function_slots(struct J9PortLibrary *portLibrary) outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_shutdown is NULL\n"); } - if (NULL == portLibrary->sock_htons) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_htons is NULL\n"); - } - - if (NULL == portLibrary->sock_write) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_write is NULL\n"); - } - - if (NULL == portLibrary->sock_sockaddr) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_sockaddr is NULL\n"); - } - - if (NULL == portLibrary->sock_read) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_read is NULL\n"); - } - - if (NULL == portLibrary->sock_socket) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_socket is NULL\n"); - } - - if (NULL == portLibrary->sock_close) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_close is NULL\n"); - } - - if (NULL == portLibrary->sock_connect) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_connect is NULL\n"); - } - - if (NULL == portLibrary->sock_inetaddr) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_inetaddr is NULL\n"); - } - if (NULL == portLibrary->sock_gethostbyname) { outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_gethostbyname is NULL\n"); } - - if (NULL == portLibrary->sock_hostent_addrlist) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_hostent_addrlist is NULL\n"); - } - - if (NULL == portLibrary->sock_sockaddr_init) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_sockaddr_init is NULL\n"); - } - - if (NULL == portLibrary->sock_linger_init) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_linger_init is NULL\n"); - } - - if (NULL == portLibrary->sock_setopt_linger) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->sock_setopt_linger is NULL\n"); - } - - - return reportTestExit(portLibrary, testName); - -} - - -/* - * Start a server: create socket, bind to an address/port and listen for clients - * - * @param[in] portLibrary - * @param[in] testname the name of the calling test - * @param[in] addrStr the address of the server - * @param[in] port the server port - * @param[in] server_socket a pointer to the server socket. - * @param[in] server_addr - * - * @return 0 on success, non-zero otherwise - */ -I_32 -startServer(struct J9PortLibrary *portLibrary, const char* testName, char *addrStr, U_16 port, j9socket_t *server_socket, j9sockaddr_t server_addr) { - - PORT_ACCESS_FROM_PORT(portLibrary); - BOOLEAN value = 1; - - /* server_socket: create, bind */ - if (0 != j9sock_socket(server_socket, J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_sockaddr(server_addr, addrStr, j9sock_htons(port))) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating address: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_setopt_bool(*server_socket, J9_SOL_SOCKET, J9_SO_REUSEADDR, &value)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket option: %s\n", j9error_last_error_message()); - return -1; - - } - - if (0 != j9sock_bind(*server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error binding socket: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_listen(*server_socket, 1)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error binding listening to socket: %s\n", j9error_last_error_message()); - return -1; - } - - return 0; -} - -/* - * Create the client socket and sock address, then connect to the server - * - * @param[in] portLibrary - * @param[in] testname the name of the calling test - * @param[in] addrStr the address of the server - * @param[in] port the server port - * @param[in] session_client_socket a pointer to the client socket. - * @param[in] server_addr - * - * @return 0 on success, non-zero otherwise - */ -I_32 -connectClientToServer(struct J9PortLibrary *portLibrary, const char* testName, char *addrStr, U_16 port, j9socket_t *session_client_socket, j9sockaddr_t session_client_addr) { - - PORT_ACCESS_FROM_PORT(portLibrary); - BOOLEAN value = 1; - - /* server_socket: create, bind */ - if (0 != j9sock_socket(session_client_socket, J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_sockaddr(session_client_addr, addrStr, j9sock_htons(port))) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating address: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_setopt_bool(*session_client_socket, J9_SOL_SOCKET, J9_SO_REUSEADDR, &value)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket option: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_connect(*session_client_socket, session_client_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error connecting to socket: %s\n", j9error_last_error_message()); - return -1; - } - - return 0; -} - -/* - * Create the client socket and sock address, set the client - * socket to non-blocking then connect to the server - * - * @param[in] portLibrary - * @param[in] testname the name of the calling test - * @param[in] addrStr the address of the server - * @param[in] port the server port - * @param[in] session_client_socket a pointer to the client socket. - * @param[in] server_addr - * - * @return 0 on success, non-zero otherwise - */ -static I_32 -connectClientToServerNonBlocking(struct J9PortLibrary *portLibrary, const char* testName, char *addrStr, U_16 port, j9socket_t *session_client_socket, j9sockaddr_t session_client_addr) { -#define MAX_LOOP_TIMES 300 - PORT_ACCESS_FROM_PORT(portLibrary); - BOOLEAN value = 1; - I_32 rc = 0; - I_32 count = 0; - I_32 expectedRC = 0; - - /* server_socket: create, bind */ - if (0 != j9sock_socket(session_client_socket, J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - return -1; - } - - if ( 0 != j9sock_set_nonblocking(*session_client_socket, TRUE) ) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket to non-blocking: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_sockaddr(session_client_addr, addrStr, j9sock_htons(port))) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating address: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_setopt_bool(*session_client_socket, J9_SOL_SOCKET, J9_SO_REUSEADDR, &value)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket option: %s\n", j9error_last_error_message()); - return -1; - } - - while ( 0 != (rc = j9sock_connect(*session_client_socket, session_client_addr)) ) { - count++; - switch (count) { - case 1: - expectedRC = J9PORT_ERROR_SOCKET_EINPROGRESS; - break; - default: - expectedRC = J9PORT_ERROR_SOCKET_ALREADYINPROGRESS; - break; - } - - if ( count > 1 ) { - /*After the first try if j9sock_connect returns J9PORT_ERROR_SOCKET_ISCONNECTED - * or J9PORT_ERROR_SOCKET_ALREADYBOUND we can stop trying - */ - if (J9PORT_ERROR_SOCKET_ISCONNECTED == rc) { - outputComment(portLibrary, "j9sock_connect returned socket_isconnected\n"); - break; - } else if (J9PORT_ERROR_SOCKET_ALREADYBOUND == rc) { - outputComment(portLibrary, "j9sock_connect returned socket_alreadybound\n"); - break; - } - } - - if ( rc != expectedRC ) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error connecting to socket (expectedRC=%d, rc=%d, count=%d): %s\n", expectedRC, rc, count, j9error_last_error_message()); - return -1; - } else if ( (2 <= count) && (MAX_LOOP_TIMES > count) ) { - /* After the second try we have already tested what we want to test so - * just give the connect some time to complete - */ - omrthread_sleep((I_64)100); /* 100 ms*/ - } else if ( MAX_LOOP_TIMES <= count ) { - /* Connection attempt timed out */ - outputErrorMessage(PORTTEST_ERROR_ARGS, "error connecting to socket, reached MAX_LOOP_TIMES limit (=%d)\n", MAX_LOOP_TIMES); - return -1; - } - } - - outputComment(portLibrary, "Connected after %d attempt(s).\n", count); - - return 0; -#undef MAX_LOOP_TIMES -} - -/** - * Open two sockets and talk to eachother. - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test1_basic(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test1_basic"; - char *address = "localhost"; - char *message = "hey, what's up?"; - U_8 *cursor = NULL; - U_16 port = 8888; - int lenMessagePlusTerminator; - int bytesLeft, bytesWritten, bytesRead; - BOOLEAN value = 1; - char bufferA[EsMaxPath], bufferB[EsMaxPath]; - IDATA selectReadRC = -1; - - j9socket_struct server_socketStruct, session_client_socketStruct, session_server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9socket_t session_client_socket = &session_client_socketStruct; - j9socket_t session_server_socket = &session_server_socketStruct; - - j9sockaddr_struct server_socket_addrStruct, session_client_addrStruct, session_server_addrStruct; - j9sockaddr_t server_addr = &server_socket_addrStruct; - j9sockaddr_t session_client_addr = &session_client_addrStruct; - j9sockaddr_t session_server_addr = &session_server_addrStruct; - - reportTestEntry(portLibrary, testName); - - /* printf("\nj9sock_test1 : session_server_addr->addr: %p \n", session_server_addr->addr);fflush(stdout); */ - - lenMessagePlusTerminator = (int)strlen(message) +1; - memset(bufferA, 0, sizeof(bufferA)); - memset(bufferB, 0, sizeof(bufferB)); - - if (0 != startServer(portLibrary, testName, address, port, &server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error starting server: %s\n", j9error_last_error_message()); - } - - if (0 != connectClientToServer(portLibrary, testName, address, port, &session_client_socket, session_client_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating/connecting client: %s\n", j9error_last_error_message()); - } - - /* TODO: DEFECT it appears that the address is not optional as win32 does not test for NULL in sock_accept. Fix or DOC */ - if (0 != j9sock_accept(server_socket, session_server_addr, &session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error accepting connection: %s\n", j9error_last_error_message()); - goto exit; - } - - /***************************************************** - * - * We have two sockets that can talk to each other - * - *****************************************************/ - - /* nothing to read, this should timeout */ - selectReadRC = j9sock_select_read(session_client_socket, 0, 1, FALSE); - if (J9PORT_ERROR_SOCKET_TIMEOUT != selectReadRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select_read: expected rc = %i, received rc: %i : %s\n", J9PORT_ERROR_SOCKET_TIMEOUT, selectReadRC, j9error_last_error_message()); - goto exit; - } - - /* Write full message to session_server_socket */ - cursor = (U_8*)message; - bytesLeft = lenMessagePlusTerminator; - bytesWritten = 0; - for (;;) { - bytesWritten = j9sock_write(session_server_socket, cursor, bytesLeft, 0); - if (bytesWritten < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "full message was not written to server_socket %s\n", j9error_last_error_message()); - goto exit; - } - - bytesLeft -=bytesWritten; - cursor += bytesWritten; - if (0 == bytesLeft) { - break; - } - } - /* - * we should be able to read from this socket. - * CMVC 182679: 1 us appears too short. Increase to 1 ms - */ - selectReadRC = j9sock_select_read(session_client_socket, 0, 1000, FALSE); - if (1 != selectReadRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select_read: expected rc = 1, received rc: %i : %s\n", selectReadRC, j9error_last_error_message()); - goto exit; - } - - /* Read full message from session_client_socket */ - cursor = (U_8*)bufferB; - bytesLeft = lenMessagePlusTerminator; - bytesRead = 0; - for (;;) { - bytesRead = j9sock_read(session_client_socket, cursor, EsMaxPath - bytesRead, 0); - if (bytesRead < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "full message not received on session_client_socket%s\n", j9error_last_error_message()); - goto exit; - } - bytesLeft -=bytesRead; - cursor +=bytesRead; - if (0 == bytesLeft) { - break; - } - } - - j9tty_printf(portLibrary, "\t session_client_socket received: \"%s\"\n ",bufferB); - - /* Close the sockets */ - if (0 != j9sock_close(&session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_server_socket_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&session_client_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_client_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing server_socket: %s\n", j9error_last_error_message()); - } return reportTestExit(portLibrary, testName); - - exit: - /* cleanup on failure, there should be no harm in calling close on a socket that's already closed */ - j9sock_close(&session_server_socket); - j9sock_close(&session_client_socket); - j9sock_close(&server_socket); - return reportTestExit(portLibrary, testName); } -/** - * Basic test of an fdset with only one socket; - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test2_fdset_basic(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test2_fdset_basic"; - j9socket_struct sock_struct1; - j9socket_t sock1; - j9fdset_struct fdsetStruct; - j9fdset_t fdset = &fdsetStruct; - - sock1 = &sock_struct1; - - reportTestEntry(portLibrary, testName); - - - j9sock_fdset_zero(fdset); - - if (0 != j9sock_socket(&sock1, J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - goto exit; - } - - if (TRUE == j9sock_fdset_isset(sock1, fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should not include sock1: %s\n", j9error_last_error_message()); - goto exit; - } - - j9sock_fdset_set(sock1, fdset); - - if (FALSE == j9sock_fdset_isset(sock1, fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should include sock1: %s\n", j9error_last_error_message()); - goto exit; - } - - j9sock_fdset_clr(sock1, fdset); - - if (TRUE == j9sock_fdset_isset(sock1, fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should not include sock1: %s\n", j9error_last_error_message()); - goto exit; - } - - j9sock_fdset_set(sock1, fdset); - j9sock_fdset_zero(fdset); - - if (TRUE == j9sock_fdset_isset(sock1, fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should be empty: %s\n", j9error_last_error_message()); - goto exit; - } - - exit: - return reportTestExit(portLibrary, testName); - -} - -/** - * Basic test of an fdset with only one socket; - * NOTE: Windows and Linux (?unix?) behave differently for select: - * If the timeout is set to zero: - * Windows: returns 0, regardless of whether any sockets are ready for reads/writes - * Linux: returns the number of sockets that are ready, as expected. - * - * 1. Establish a connection between the server and client sockets - * 2. Once the connection has been made, a write should not block, but a read should, check this - * 3. Write some data to the server socket - * 4. The client socket should not block on a read, check this - * 5. Read and validate the data - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test3_fdset_basic_select(struct J9PortLibrary *portLibrary) { - - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test3_fdset_basic_select"; - char *address = "localhost"; - char *message = "hey, what's up?"; - U_8 *cursor = NULL; - U_16 port = 8889; - int lenMessagePlusTerminator; - int bytesLeft, bytesWritten, bytesRead; - BOOLEAN value = 1; - char bufferA[EsMaxPath], bufferB[EsMaxPath]; - j9fdset_struct readFdsetStruct, writeFdsetStruct; - j9fdset_t readFdset = &readFdsetStruct; - j9fdset_t writeFdset = &writeFdsetStruct; - - I_32 nfds; - struct j9timeval_struct timeP; - I_32 selectRC = 0; - - j9socket_struct server_socketStruct, session_client_socketStruct, session_server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9socket_t session_client_socket = &session_client_socketStruct; - j9socket_t session_server_socket = &session_server_socketStruct; - - j9sockaddr_struct server_socket_addrStruct, session_client_addrStruct, session_server_addrStruct; - j9sockaddr_t server_addr = &server_socket_addrStruct; - j9sockaddr_t session_client_addr = &session_client_addrStruct; - j9sockaddr_t session_server_addr = &session_server_addrStruct; - - reportTestEntry(portLibrary, testName); - - /* printf("\nj9sock_test1 : session_server_addr->addr: %p \n", session_server_addr->addr);fflush(stdout); */ - - lenMessagePlusTerminator = (int)strlen(message) +1; - memset(bufferA, 0, sizeof(bufferA)); - memset(bufferB, 0, sizeof(bufferB)); - - if (0 != startServer(portLibrary, testName, address, port, &server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error starting server: %s\n", j9error_last_error_message()); - } - - if (0 != connectClientToServer(portLibrary, testName, address, port, &session_client_socket, session_client_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating/connecting client: %s\n", j9error_last_error_message()); - } - - /* TODO: DEFECT it appears that the address is not optional as win32 does not test for NULL in sock_accept. Fix or DOC */ - if (0 != j9sock_accept(server_socket, session_server_addr, &session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error accepting connection: %s\n", j9error_last_error_message()); - goto exit; - } - - /***************************************************** - * - * We have two sockets that can talk to eachother - * - *****************************************************/ - /* Note, Windows select() returns "0" regardless of state of sockets when timeout is set to 0 */ - j9sock_timeval_init(0, 1, &timeP); - - /* we should be able to write without blocking, test it */ - j9sock_fdset_zero(writeFdset); - j9sock_fdset_set(session_server_socket, writeFdset); - nfds = j9sock_fdset_size(session_server_socket); - - selectRC = j9sock_select(nfds, NULL, writeFdset, NULL, &timeP); - - if (selectRC <= 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error in j9sock_select: %s\n", j9error_last_error_message()); - goto exit; - } else if (1 != selectRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select should have returned exactly one socket in the read fdset: %s\n", j9error_last_error_message()); - goto exit; - } - if (TRUE != j9sock_fdset_isset(session_server_socket, writeFdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "session_server_socket should be set %s\n", j9error_last_error_message()); - goto exit; - } - if (FALSE != j9sock_fdset_isset(session_client_socket, writeFdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "session_client_socket should NOT be set %s\n", j9error_last_error_message()); - goto exit; - } - - /* test that a read would block as we haven't written anything yet, we should get a timeout from any call to select at this point */ - j9sock_fdset_zero(readFdset); - j9sock_fdset_set(session_client_socket, readFdset); - nfds = j9sock_fdset_size(session_client_socket); - selectRC = j9sock_select(nfds, readFdset, NULL, NULL, &timeP); - - if (selectRC != J9PORT_ERROR_SOCKET_TIMEOUT) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "expected J9PORT_ERROR_SOCKET_TIMEOUT from j9sock_select, instead got: %i, %s\n", selectRC, j9error_last_error_message()); - goto exit; - } - - if (TRUE == j9sock_fdset_isset(session_client_socket, readFdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "session_client_socket should NOT be set %s\n", j9error_last_error_message()); - - /* Read message from session_client_socket */ - bytesRead = j9sock_read(session_client_socket, cursor, EsMaxPath, 0); - if (bytesRead > 0) { - j9tty_printf(portLibrary, "\t session_client_socket received: \"%s\"\n ",bufferB); - } - - goto exit; - } - - /* Write full message to session_server_socket */ - cursor = (U_8*)message; - bytesLeft = lenMessagePlusTerminator; - bytesWritten = 0; - for (;;) { - bytesWritten = j9sock_write(session_server_socket, cursor, bytesLeft , 0); - if (bytesWritten < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "full message was not written to server_socket %s\n", j9error_last_error_message()); - goto exit; - } - - bytesLeft -=bytesWritten; - cursor += bytesWritten; - if (0 == bytesLeft) { - break; - } - } - j9sock_fdset_set(session_client_socket, readFdset); - - /* Note, Windows select() returns "0" regardless of state of sockets when timeout is set to 0 */ - j9sock_timeval_init(30, 0, &timeP); - - j9sock_fdset_zero(readFdset); - j9sock_fdset_set(session_client_socket, readFdset); - nfds = j9sock_fdset_size(session_client_socket); - - /* we've written to the server socket so the client should have something to read, this j9sock_select should return right away */ - selectRC = j9sock_select(nfds, readFdset, NULL, NULL, &timeP/*NULL */); - - if (selectRC < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error in j9sock_select: %s\n", j9error_last_error_message()); - goto exit; - } else if (1 != selectRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select should not have set more than one socket: %s\n", j9error_last_error_message()); - goto exit; - } - - if (TRUE != j9sock_fdset_isset(session_client_socket, readFdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "session_client_socket should be set %s\n", j9error_last_error_message()); - goto exit; - } - - if (FALSE!= j9sock_fdset_isset(session_server_socket, readFdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "session_server_socket should NOT be set %s\n", j9error_last_error_message()); - goto exit; - } - - /* Read full message from session_client_socket */ - cursor = (U_8*)bufferB; - bytesLeft = lenMessagePlusTerminator; - bytesRead = 0; - for (;;) { - bytesRead = j9sock_read(session_client_socket, cursor, EsMaxPath - bytesRead, 0); - if (bytesRead < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "full message not received on session_client_socket%s\n", j9error_last_error_message()); - goto exit; - } - bytesLeft -=bytesRead; - cursor +=bytesRead; - if (0 == bytesLeft) { - break; - } - } - - j9tty_printf(portLibrary, "\t session_client_socket received: \"%s\"\n ",bufferB); - - /* Close the sockets */ - if (0 != j9sock_close(&session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_server_socket_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&session_client_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_client_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing server_socket: %s\n", j9error_last_error_message()); - } - return reportTestExit(portLibrary, testName); - - exit: - /* cleanup on failure, there should be no harm in calling close on a socket that's already closed */ - j9sock_close(&session_server_socket); - j9sock_close(&session_client_socket); - j9sock_close(&server_socket); - - return reportTestExit(portLibrary, testName); - -} - -/** - * More advanced tests of fdset support with only one socket; - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test4_fdset_advanced(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test4_fdset_advanced"; -#if defined WIN32 -#define NUM_SOCKS FD_SETSIZE /* FD_SETSIZE is defined in Winsock2.h and is something like 64*/ -#else -#define NUM_SOCKS 1 /* FD_SETSIZE is much bigger on linux, so much so that it can't handle creating so many file descriptor's for the sockets */ -#endif - j9socket_struct sockStructArray[NUM_SOCKS]; - j9socket_t sockArray[NUM_SOCKS]; - IDATA i; - j9fdset_struct fdsetStruct; - j9fdset_t fdset = &fdsetStruct; - - reportTestEntry(portLibrary, testName); - -#if defined(SOCKTEST_DBG) - j9tty_printf(portLibrary, "\n FD_SETSIZE = %u\n", FD_SETSIZE); -#endif - - j9sock_fdset_zero(fdset); - - for (i=0; i< NUM_SOCKS; i++) { - sockArray[i] = &sockStructArray[i]; - } - - for (i=0; i< NUM_SOCKS; i++) { - if (0 != j9sock_socket(&sockArray[i], J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating sock[%i]: %s\n", i, j9error_last_error_message()); - goto exit; - } - } - - for (i=0; i< NUM_SOCKS; i++) { - if (TRUE == j9sock_fdset_isset(sockArray[i], fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should not include sock[%i]: %s\n", i, j9error_last_error_message()); - goto exit; - } - } - - for (i=0; i< NUM_SOCKS; i++) { - j9sock_fdset_set(sockArray[i], fdset); - } - - for (i=0; i< NUM_SOCKS; i++) { - if (FALSE == j9sock_fdset_isset(sockArray[i], fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should include sock[%i]: %s\n", i, j9error_last_error_message()); - goto exit; - } - } - - for (i=0; i< NUM_SOCKS; i++) { - j9sock_fdset_clr(sockArray[i], fdset); - } - - for (i=0; i< NUM_SOCKS; i++) { - if (TRUE == j9sock_fdset_isset(sockArray[i], fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should not include sock[%i]: %s\n", i, j9error_last_error_message()); - goto exit; - } - } - - for (i=0; i< NUM_SOCKS; i++) { - j9sock_fdset_set(sockArray[i], fdset); - } - - j9sock_fdset_zero(fdset); - - for (i=0; i< NUM_SOCKS; i++) { - if (TRUE == j9sock_fdset_isset(sockArray[i], fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should not include sock[%i]: %s\n", i, j9error_last_error_message()); - goto exit; - } - } - - /* we should be able to set the same socket twice */ - j9sock_fdset_set(sockArray[0], fdset); - j9sock_fdset_set(sockArray[0], fdset); - if (FALSE == j9sock_fdset_isset(sockArray[0], fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should include sock[0]: %s\n", j9error_last_error_message()); - goto exit; - } - - j9sock_fdset_zero(fdset); - if (TRUE == j9sock_fdset_isset(sockArray[0], fdset)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error: fdset should not include sock[0]: %s\n", j9error_last_error_message()); - goto exit; - } - - - exit: - return reportTestExit(portLibrary, testName); -#undef NUM_SOCKS - -} - -/** - * Basic testing of setting socket options. - * - * Check that we don't fail when setting certain options, - * then get the value of the option and check that it's what we just set it to. - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test5_basic_options(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test5_basic_options"; - char *address = "localhost"; - U_16 port = 8890; - BOOLEAN setValue = 1; - BOOLEAN getValue = 0; - int i=0; -#define LEVEL 0 -#define OPTION 1 -#define NUM_TEST_FIVE_OPTIONS 3 - -#if defined(J9ZOS390) -#define NUM_TEST_FIVE_OPTIONS_TO_TEST 1 -#elif defined(WIN32) -#define NUM_TEST_FIVE_OPTIONS_TO_TEST 2 -#else -#define NUM_TEST_FIVE_OPTIONS_TO_TEST NUM_TEST_FIVE_OPTIONS -#endif - - I_32 options[NUM_TEST_FIVE_OPTIONS][2] = { {J9_SOL_SOCKET, J9_SO_REUSEADDR}, - /* {J9_IPPROTO_IP, J9_TCP_NODELAY}, */ - {J9_IPPROTO_TCP, J9_TCP_NODELAY}, - {J9_IPPROTO_IP, J9_IP_MULTICAST_LOOP}}; - - j9socket_struct server_socketStruct, session_server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9socket_t session_server_socket = &session_server_socketStruct; - - j9sockaddr_struct server_socket_addrStruct, session_server_addrStruct; - j9sockaddr_t server_addr = &server_socket_addrStruct; - j9sockaddr_t session_server_addr = &session_server_addrStruct; - - reportTestEntry(portLibrary, testName); - - j9tty_printf(PORTLIB, "\t%u options will be tested on this platform\n", NUM_TEST_FIVE_OPTIONS_TO_TEST ); - - for (i = 0; i < NUM_TEST_FIVE_OPTIONS_TO_TEST; i++) { - /* server_socket: create, bind */ - if (0 != j9sock_socket(&server_socket, J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_sockaddr(server_addr, address, j9sock_htons(port))) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating address: %s\n", j9error_last_error_message()); - return -1; - } - - /* set the option */ - if (0 != j9sock_setopt_bool(server_socket, options[i][LEVEL], options[i][OPTION], &setValue)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket option index %i: %s\n", i, j9error_last_error_message()); - return -1; - - } - - /* check that the value is what we just set it to */ - if (0 != j9sock_getopt_bool(server_socket, options[i][LEVEL], options[i][OPTION], &getValue)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket option index %i: %s\n", i, j9error_last_error_message()); - return -1; - - } - - if (setValue != getValue) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_getopt_bool() was not returned the value set by j9sock_setopt_bool(): index %i, setValue %i, getValue %i: %s\n", i, setValue, getValue, j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_bind(server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error binding socket, index %i: %s, \n", i, j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_listen(server_socket, 1)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error listening to socket, index %i: %s\n", i, j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_close(&server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing server_socket, index %i: %s\n", i, j9error_last_error_message()); - } - } - - return reportTestExit(portLibrary, testName); -#undef NUM_TEST_FIVE_OPTIONS -#undef LEVEL -#undef OPTION -} - -/** - * Tests that j9sock_connect on a non-blocking socket will return - * J9PORT_ERROR_SOCKET_EINPROGRESS if it cannot immediately establish - * a connection and will return J9PORT_ERROR_SOCKET_ALREADYINPROGRESS - * if j9sock_connect is called again while it is trying to connect. - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test6_nonblocking_connect(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test6_nonblocking_connect"; - char *address = "localhost"; - U_16 port = 8888; - - j9socket_struct server_socketStruct, session_client_socketStruct, session_server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9socket_t session_client_socket = &session_client_socketStruct; - j9socket_t session_server_socket = &session_server_socketStruct; - - j9sockaddr_struct server_socket_addrStruct, session_client_addrStruct, session_server_addrStruct; - j9sockaddr_t server_addr = &server_socket_addrStruct; - j9sockaddr_t session_client_addr = &session_client_addrStruct; - j9sockaddr_t session_server_addr = &session_server_addrStruct; - - reportTestEntry(portLibrary, testName); - - - if (0 != startServer(portLibrary, testName, address, port, &server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error starting server: %s\n", j9error_last_error_message()); - } else { - if (0 != connectClientToServerNonBlocking(portLibrary, testName, address, port, &session_client_socket, session_client_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating/connecting client: %s\n", j9error_last_error_message()); - } - - if (0 != j9sock_close(&session_client_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_client_socket: %s\n", j9error_last_error_message()); - } - } - - if (0 != j9sock_close(&server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing server_socket: %s\n", j9error_last_error_message()); - } - - return reportTestExit(portLibrary, testName); -} - -/** - * Tests that - * For nonblocking sockets: - * 1. calling j9sock_read when there is nothing to read returns J9PORT_ERROR_SOCKET_WOULDBLOCK - * 2. calling j9sock_read when there is something to read works fine. - * - * This implicitly test j9sock_set_nonblocking() - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test7_nonblocking_read(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test7_nonblocking_read"; - char *address = "localhost"; - char *message = "hey, what's up?"; - U_8 *cursor = NULL; - U_16 port = 8888; - int lenMessagePlusTerminator; - int bytesLeft, bytesWritten, bytesRead; - BOOLEAN value = 1; - char bufferA[EsMaxPath], bufferB[EsMaxPath]; - - j9socket_struct server_socketStruct, session_client_socketStruct, session_server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9socket_t session_client_socket = &session_client_socketStruct; - j9socket_t session_server_socket = &session_server_socketStruct; - - j9sockaddr_struct server_socket_addrStruct, session_client_addrStruct, session_server_addrStruct; - j9sockaddr_t server_addr = &server_socket_addrStruct; - j9sockaddr_t session_client_addr = &session_client_addrStruct; - j9sockaddr_t session_server_addr = &session_server_addrStruct; - - reportTestEntry(portLibrary, testName); - - /* printf("\nj9sock_test1 : session_server_addr->addr: %p \n", session_server_addr->addr);fflush(stdout); */ - - lenMessagePlusTerminator = (int)strlen(message) +1; - memset(bufferA, 0, sizeof(bufferA)); - memset(bufferB, 0, sizeof(bufferB)); - - if (0 != startServer(portLibrary, testName, address, port, &server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error starting server: %s\n", j9error_last_error_message()); - } - - if (0 != connectClientToServer(portLibrary, testName, address, port, &session_client_socket, session_client_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating/connecting client: %s\n", j9error_last_error_message()); - } - - if (0 != j9sock_accept(server_socket, session_server_addr, &session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error accepting connection: %s\n", j9error_last_error_message()); - goto exit; - } - - /***************************************************** - * - * We have two sockets that can talk to eachother - * - *****************************************************/ - - /* TEST: - * Set the client socket to blocking, - * Don't write anything to the server - * When we call j9sock_read() on the client we should get J9PORT_ERROR_SOCKET_WOULDBLOCK */ - - /* Set client to nonblocking */ - if ( 0 != j9sock_set_nonblocking(session_client_socket, TRUE) ) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_set_nonblocking: %s\n", j9error_last_error_message()); - return -1; - } - - /* Try to read when nothing had been written to the server */ - cursor = (U_8*)bufferB; - bytesRead = j9sock_read(session_client_socket, cursor, EsMaxPath, 0); - if (J9PORT_ERROR_SOCKET_WOULDBLOCK != bytesRead) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_read on nonblocking socket (with no queued data) did not return J9PORT_ERROR_SOCKET_WOULDBLOCK. " - "\tInstead it returned: %i. Expected: %i", bytesRead, J9PORT_ERROR_SOCKET_WOULDBLOCK); - } - - /* Write full message to session_server_socket */ - cursor = (U_8*)message; - bytesLeft = lenMessagePlusTerminator; - bytesWritten = 0; - for (;;) { - bytesWritten = j9sock_write(session_server_socket, cursor, bytesLeft , 0); - if (bytesWritten < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "full message was not written to server_socket %s\n", j9error_last_error_message()); - goto exit; - } - - bytesLeft -=bytesWritten; - cursor += bytesWritten; - if (0 == bytesLeft) { - break; - } - } - - /* Read full message from session_client_socket */ - cursor = (U_8*)bufferB; - bytesLeft = lenMessagePlusTerminator; - bytesRead = 0; - for (;;) { - bytesRead = j9sock_read(session_client_socket, cursor, EsMaxPath - bytesRead, 0); - if (bytesRead < 0) { - if (J9PORT_ERROR_SOCKET_WOULDBLOCK == bytesRead) { - /* just because we wrote to the server doesn't mean the data was queued on the client in time for this call to j9sock_read */ - continue; - } else { - outputErrorMessage(PORTTEST_ERROR_ARGS, "Error reading from socket. j9sock_read returned: %i", bytesRead); - goto exit; - } - } - bytesLeft -=bytesRead; - cursor +=bytesRead; - if (0 == bytesLeft) { - break; - } - - } - - j9tty_printf(portLibrary, "\t session_client_socket received: \"%s\"\n ",bufferB); - - /* Close the sockets */ - if (0 != j9sock_close(&session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_server_socket_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&session_client_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_client_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing server_socket: %s\n", j9error_last_error_message()); - } - exit: - - return reportTestExit(portLibrary, testName); -} - - -/** - * Tests that j9sock_select() and j9sock_select_read() on a closed socket will - * return that the socket is ready and that a subsequent j9sock_read() will fail - * - * @ref j9sock.c - * - * @param[in] portLibrary The port library under test - * - * @return TEST_PASS on success, TEST_FAIL on error - */ -int -j9sock_test8_select_read_closed_socket(struct J9PortLibrary *portLibrary) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - const char* testName = "j9sock_test8_select_read_closed_socket"; - char *address = "localhost"; - U_16 port = 8888; - j9fdset_struct readFdsetStruct; - j9fdset_t readFdset = &readFdsetStruct; - I_32 nfds, selectRC, readRC; - U_8 buffer = 0; - struct j9timeval_struct timeP; - - j9socket_struct server_socketStruct, session_client_socketStruct, session_server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9socket_t session_client_socket = &session_client_socketStruct; - j9socket_t session_server_socket = &session_server_socketStruct; - - j9sockaddr_struct server_socket_addrStruct, session_client_addrStruct, session_server_addrStruct; - j9sockaddr_t server_addr = &server_socket_addrStruct; - j9sockaddr_t session_client_addr = &session_client_addrStruct; - j9sockaddr_t session_server_addr = &session_server_addrStruct; - - reportTestEntry(portLibrary, testName); - - if (0 != startServer(portLibrary, testName, address, port, &server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error starting server: %s\n", j9error_last_error_message()); - } - - if (0 != connectClientToServer(portLibrary, testName, address, port, &session_client_socket, session_client_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating/connecting client: %s\n", j9error_last_error_message()); - } - - if (0 != j9sock_accept(server_socket, session_server_addr, &session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error accepting connection: %s\n", j9error_last_error_message()); - goto exit; - } - - - /* Set client to nonblocking */ - if ( 0 != j9sock_set_nonblocking(session_client_socket, TRUE) ) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_set_nonblocking: %s\n", j9error_last_error_message()); - return -1; - } - - - /* do a j9sock_select_read with a timeout before any data is written to - * the client to ensure that j9sock_select_read times out - */ - selectRC = j9sock_select_read(session_client_socket, 0, (I_32) 10000, TRUE); - - j9tty_printf(portLibrary, "\t j9sock_select_read(session_client_socket) returned %d \n ", selectRC); - - if (J9PORT_ERROR_SOCKET_TIMEOUT != selectRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select_read should have returned J9PORT_ERROR_SOCKET_TIMEOUT\n", j9error_last_error_message()); - } - - - /* Close session_server_socket */ - if (0 != j9sock_close(&session_server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_server_socket_socket: %s\n", j9error_last_error_message()); - } - - /* create an fdset and add the session_client to it */ - j9sock_fdset_zero(readFdset); - j9sock_fdset_set(session_client_socket, readFdset); - nfds = j9sock_fdset_size(session_client_socket); - j9sock_timeval_init(30, 0, &timeP); - - /* The session_server_socket was closed above, test that j9sock_select() and j9sock_select_read() - * return 1 indicating that operations on the client socket will not block - */ - selectRC = j9sock_select(nfds, readFdset, NULL, NULL, &timeP/*NULL */); - j9tty_printf(portLibrary, "\t j9sock_select(session_client_socket) returned %d \n ", selectRC); - if (selectRC < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error in j9sock_select: %s\n", j9error_last_error_message()); - } else if (1 != selectRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select should not have set more than one socket: %s\n", j9error_last_error_message()); - } - - selectRC = j9sock_select_read(session_client_socket, 0, (I_32) 100000, TRUE); - j9tty_printf(portLibrary, "\t j9sock_select_read(session_client_socket) returned %d \n ", selectRC); - if (selectRC < 0) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error in j9sock_select_read: %s\n", j9error_last_error_message()); - } else if (1 != selectRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_select_read should not have set more than one socket: %s\n", j9error_last_error_message()); - } - - /* check that j9sock_read() returns 0 */ - readRC = j9sock_read(session_client_socket, &buffer, 1, 0); - if (0 != readRC) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "j9sock_read should have returned 0 but returned %d instead\n", readRC); - } - - /* Close the sockets */ - if (0 != j9sock_close(&session_client_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing session_client_socket: %s\n", j9error_last_error_message()); - } - if (0 != j9sock_close(&server_socket)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error closing server_socket: %s\n", j9error_last_error_message()); - } - -exit: - return reportTestExit(portLibrary, testName); -} - -I_32 -startServerForIPv6(struct J9PortLibrary *portLibrary, const char* testName, j9socket_t *server_socket, unsigned char *sa, I_32 family, U_32 scopeID, j9addrinfo_struct *res, I_32 index) { - - PORT_ACCESS_FROM_PORT(portLibrary); - BOOLEAN value = 1; - - if (0 != j9sock_socket(server_socket, family, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_setopt_bool(*server_socket, J9_SOL_SOCKET, J9_SO_REUSEADDR, &value)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error setting socket option: %s\n", j9error_last_error_message()); - return -1; - } -/* - if (0 != j9sock_getaddrinfo_address(&res, sa, index, &scopeID)){ - outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_getaddrinfo_address: %s\n", j9error_last_error_message()); - return -1; - } - - if (0 != j9sock_bind(*server_socket, server_addr)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error binding socket: %s\n", j9error_last_error_message()); - return -1; - } - -#if defined(SOCKTEST_DBG) && defined (IPv6_FUNCTION_SUPPORT) - printAddress(portLibrary, (struct sockaddr *) server_addr); -#endif - if (0 != j9sock_listen(*server_socket, 1)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error binding listening to socket: %s\n", j9error_last_error_message()); - return -1; - } -*/ - return 0; -} I_32 j9sock_test10_get_addrinfo(struct J9PortLibrary *portLibrary) @@ -1325,11 +96,8 @@ j9sock_test10_get_addrinfo(struct J9PortLibrary *portLibrary) PORT_ACCESS_FROM_PORT(portLibrary); const char* testName = "j9sock_test10_get_addrinfo"; #ifdef IPv6_FUNCTION_SUPPORT - j9socket_struct server_socketStruct; - j9socket_t server_socket = &server_socketStruct; j9addrinfo_struct res; j9addrinfo_t hints; - unsigned char *sa; struct addrinfo *addrinf; BOOLEAN value = 1; I_32 length = 0; @@ -1382,10 +150,6 @@ j9sock_test10_get_addrinfo(struct J9PortLibrary *portLibrary) j9tty_printf(portLibrary, "protocol: %d\n", &addrinf.ai_protocol); } #endif -/* if (0 != startServerForIPv6(portLibrary, testName, &server_socket, sa, family, scopeID, &res, i)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error starting server: %s\n", j9error_last_error_message()); - } -*/ } if (0 != j9sock_freeaddrinfo(&res)){ @@ -1402,12 +166,6 @@ j9sock_test11_getnameinfo(struct J9PortLibrary *portLibrary) PORT_ACCESS_FROM_PORT(portLibrary); const char* testName = "j9sock_test11_getnameinfo"; #ifdef IPv6_FUNCTION_SUPPORT - j9socket_struct server_socketStruct; - j9socket_t server_socket = &server_socketStruct; - j9sockaddr_struct local_addrStruct; - j9sockaddr_t local_addr = &local_addrStruct; - char hostname[1025]; - char hostname2[1025]; char hostname3[1025]; j9addrinfo_struct res; j9addrinfo_t hints; @@ -1464,46 +222,11 @@ j9sock_test11_getnameinfo(struct J9PortLibrary *portLibrary) } } - if (0 != j9sock_socket(&server_socket, J9SOCK_AFINET, J9SOCK_STREAM, J9SOCK_DEFPROTOCOL)) { - outputErrorMessage(PORTTEST_ERROR_ARGS, "error creating socket: %s\n", j9error_last_error_message()); - return reportTestExit(portLibrary, testName); - } - - if (0 != j9sock_sockaddr_init6(local_addr, nipAddr, 4, family, 0, 0, 0, server_socket)){ - outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_sockaddr_init6: %s\n", j9error_last_error_message()); - return reportTestExit(portLibrary, testName); - } - if (0 != j9sock_freeaddrinfo(&res)){ outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_freeaddrinfo: %s\n", j9error_last_error_message()); return reportTestExit(portLibrary, testName); } - if((0 != j9sock_getnameinfo(local_addr, sizeof(struct sockaddr), hostname, sizeof(hostname), 0))){ - outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_getnameinfo: %s\n", j9error_last_error_message()); - return reportTestExit(portLibrary, testName); - } - if (0 != j9sock_gethostname(hostname2, sizeof(hostname2))){ - outputErrorMessage(PORTTEST_ERROR_ARGS, "error calling j9sock_gethostname: %s\n", j9error_last_error_message()); - return reportTestExit(portLibrary, testName); - } - - /* compare the output of getnameinfo with that of j9sock_gethostname expecting same results, - * depending on the platform and configuration, can return a number of different results */ - - /* use the shortest name for a generous compare */ - nameLength = strlen(hostname); - if (strlen(hostname2) < nameLength){ - nameLength = strlen(hostname2); - } - if ( (0 == strncmp(hostname, hostname2, nameLength)) - || (0 == strncmp(hostname, "localhost", strlen("localhost"))) || (0 == strncmp(hostname, "loopback", strlen("loopback"))) - || (0 == strncmp(hostname, "127.0.0.1", strlen("127.0.0.1")))){ - } else { - outputErrorMessage(PORTTEST_ERROR_ARGS, "test fails: getnameinfo returns %s, gethostname returns %s\n", hostname, hostname2); - return reportTestExit(portLibrary, testName); - } - #endif return reportTestExit(portLibrary, testName); } @@ -1534,14 +257,6 @@ j9sock_runTests(struct J9PortLibrary *portLibrary) rc = j9sock_verify_function_slots(portLibrary); if (TEST_PASS == rc) { - rc |= j9sock_test1_basic(portLibrary); - rc |= j9sock_test2_fdset_basic(portLibrary); - rc |= j9sock_test3_fdset_basic_select(portLibrary); - rc |= j9sock_test4_fdset_advanced(portLibrary); - rc |= j9sock_test5_basic_options(portLibrary); - rc |= j9sock_test6_nonblocking_connect(portLibrary); - rc |= j9sock_test7_nonblocking_read(portLibrary); - /* j9sock_test8_select_read_closed_socket doesn't work on Windows and applies only to Harmony port library. */ rc |= j9sock_test10_get_addrinfo(portLibrary); rc |= j9sock_test11_getnameinfo(portLibrary); } diff --git a/runtime/tests/port/main.c b/runtime/tests/port/main.c index 2ddb4fe6127..9a5602afd35 100644 --- a/runtime/tests/port/main.c +++ b/runtime/tests/port/main.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 1991, 2017 IBM Corp. and others + * Copyright (c) 1991, 2020 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -219,7 +219,6 @@ signalProtectedMain(struct J9PortLibrary *portLibrary, void *arg) BOOLEAN isChild = 0; BOOLEAN shsem_child = 0; BOOLEAN negative = FALSE; - char serverName[99]=""; char testName[99]=""; char *hypervisor = NULL; char *hvOptions = NULL; @@ -240,8 +239,6 @@ signalProtectedMain(struct J9PortLibrary *portLibrary, void *arg) for (i=1; i,,[,] - * shouldDisclaim is set from the +/- prefix, numIterations is optional and defaults to 50 */ - rc |= j9vmem_disclaimPerfTests(PORTLIB, disclaimPerfTestArg, shouldDisclaim); - } else { - rc |= j9vmem_runTests(PORTLIB); - } - } - if (J9PORT_TEST_J9DUMP == (areasToTest & J9PORT_TEST_J9DUMP)) { - rc |= j9dump_runTests(PORTLIB); - } - if (J9PORT_TEST_J9SOCK == (areasToTest & J9PORT_TEST_J9SOCK)) { - rc |= j9sock_runTests(PORTLIB); - } - if (J9PORT_TEST_J9PROCESS == (areasToTest & J9PORT_TEST_J9PROCESS)) { - rc |= j9process_runTests(PORTLIB, argv[0], NULL); + if (J9PORT_TEST_J9SHSEM == (areasToTest & J9PORT_TEST_J9SHSEM)) { + rc |= j9shsem_runTests(PORTLIB, argv[0], NULL); } - if (J9PORT_TEST_J9SL == (areasToTest & J9PORT_TEST_J9SL)) { - rc |= j9sl_runTests(PORTLIB); - } - if (J9PORT_TEST_J9NLS == (areasToTest & J9PORT_TEST_J9NLS)) { - rc |= j9nls_runTests(PORTLIB); - } - /* j9hypervisor Tests are run only when specified by -include */ - if(J9PORT_TEST_J9HYPERVISOR == (areasToTest & J9PORT_TEST_J9HYPERVISOR)) { - rc |= j9hypervisor_runTests(PORTLIB, hypervisor, hvOptions, negative); - } - if (J9PORT_TEST_NUMCPUS == (areasToTest & J9PORT_TEST_NUMCPUS)) { - rc |= j9sysinfo_numcpus_runTests(PORTLIB, boundTest); - } - if (J9PORT_TEST_INSTRUMENTATION == (areasToTest & J9PORT_TEST_INSTRUMENTATION)) { - rc |= j9ri_runTests(PORTLIB); - } - if (J9PORT_TEST_J9CUDA == (areasToTest & J9PORT_TEST_J9CUDA)) { - rc |= j9cuda_runTests(PORTLIB); + if (J9PORT_TEST_J9SHSEM_DEPRECATED == (areasToTest & J9PORT_TEST_J9SHSEM_DEPRECATED)) { + rc |= j9shsem_deprecated_runTests(PORTLIB, argv[0], NULL); } + } else { + j9tty_printf(PORTLIB,"\nINFO SKIPPING TESTS: The following tests will not be run if the current userid is 'root': \n\t" \ + "j9shmem_runTests(), j9shsem_runTests(), and j9shsem_deprecated_runTests().\n\n"); + } +#endif /* LINUX | J9ZOS390 | AIXPPC | WIN32 */ - if (rc) { - dumpTestFailuresToConsole(portLibrary); + if (J9PORT_TEST_J9MMAP ==(areasToTest & J9PORT_TEST_J9MMAP)) { + rc |= j9mmap_runTests(PORTLIB, argv[0], NULL); + } + if (J9PORT_TEST_J9VMEM ==(areasToTest & J9PORT_TEST_J9VMEM)) { + if (NULL != disclaimPerfTestArg){ + /* disclaimPerfTestArg is the string after the ':' from +/-disclaimPerfTest:,,[,] + * shouldDisclaim is set from the +/- prefix, numIterations is optional and defaults to 50 */ + rc |= j9vmem_disclaimPerfTests(PORTLIB, disclaimPerfTestArg, shouldDisclaim); } else { - j9tty_printf(PORTLIB,"\nALL TESTS COMPLETED AND PASSED\n"); + rc |= j9vmem_runTests(PORTLIB); } } + if (J9PORT_TEST_J9DUMP == (areasToTest & J9PORT_TEST_J9DUMP)) { + rc |= j9dump_runTests(PORTLIB); + } + if (J9PORT_TEST_J9SOCK == (areasToTest & J9PORT_TEST_J9SOCK)) { + rc |= j9sock_runTests(PORTLIB); + } + if (J9PORT_TEST_J9PROCESS == (areasToTest & J9PORT_TEST_J9PROCESS)) { + rc |= j9process_runTests(PORTLIB, argv[0], NULL); + } + if (J9PORT_TEST_J9SL == (areasToTest & J9PORT_TEST_J9SL)) { + rc |= j9sl_runTests(PORTLIB); + } + if (J9PORT_TEST_J9NLS == (areasToTest & J9PORT_TEST_J9NLS)) { + rc |= j9nls_runTests(PORTLIB); + } + /* j9hypervisor Tests are run only when specified by -include */ + if(J9PORT_TEST_J9HYPERVISOR == (areasToTest & J9PORT_TEST_J9HYPERVISOR)) { + rc |= j9hypervisor_runTests(PORTLIB, hypervisor, hvOptions, negative); + } + if (J9PORT_TEST_NUMCPUS == (areasToTest & J9PORT_TEST_NUMCPUS)) { + rc |= j9sysinfo_numcpus_runTests(PORTLIB, boundTest); + } + if (J9PORT_TEST_INSTRUMENTATION == (areasToTest & J9PORT_TEST_INSTRUMENTATION)) { + rc |= j9ri_runTests(PORTLIB); + } + if (J9PORT_TEST_J9CUDA == (areasToTest & J9PORT_TEST_J9CUDA)) { + rc |= j9cuda_runTests(PORTLIB); + } + + if (rc) { + dumpTestFailuresToConsole(portLibrary); + } else { + j9tty_printf(PORTLIB,"\nALL TESTS COMPLETED AND PASSED\n"); + } return 0; } diff --git a/runtime/tests/port/socket.c b/runtime/tests/port/socket.c deleted file mode 100644 index 506edd06137..00000000000 --- a/runtime/tests/port/socket.c +++ /dev/null @@ -1,541 +0,0 @@ -/******************************************************************************* - * Copyright (c) 1991, 2019 IBM Corp. and others - * - * This program and the accompanying materials are made available under - * the terms of the Eclipse Public License 2.0 which accompanies this - * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ - * or the Apache License, Version 2.0 which accompanies this distribution and - * is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * This Source Code may also be made available under the following - * Secondary Licenses when the conditions for such availability set - * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU - * General Public License, version 2 with the GNU Classpath - * Exception [1] and GNU General Public License, version 2 with the - * OpenJDK Assembly Exception [2]. - * - * [1] https://www.gnu.org/software/classpath/license.html - * [2] http://openjdk.java.net/legal/assembly-exception.html - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception - *******************************************************************************/ - -/* - * $RCSfile: socket.c,v $ - * $Revision: 1.29 $ - * $Date: 2008-09-18 17:28:51 $ - */ - -#include "testHelpers.h" -#include "portsock.h" - -#if 0 - -static const char CHALLENGE[]="Object Technology International"; -static const char RESPONSE[]="OTI"; -static U_8 buf[1024]; -/*U_8>>char?*//*when [>50000], client can't pause*/ - /*=32708 OK. must <32709 though ELSE -11!!!!!!!!!(client)*/ - -static char LOCAL[]="localhost"; -/*no const?*/ -extern char serverName[99]; -/* - * I_32 (*sock_gethostbyaddr) ( struct J9PortLibrary *portLibrary, char *ad - - * I_16 (*sock_htons) ( struct J9PortLibrary *portLibrary, I_16 val ) ; - * I_32 (*sock_htonl) ( struct J9PortLibrary *portLibrary, I_32 val ) ; - * I_32 (*sock_shutdown_input) ( struct J9PortLibrary *portLibrary, j9socke - * I_32 (*sock_shutdown_output) ( struct J9PortLibrary *portLibrary, j9sock - * I_16 (*sock_ntohs) ( struct J9PortLibrary *portLibrary, I_16 val ) ; - * I_32 (*sock_getsockname) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_readfrom) ( struct J9PortLibrary *portLibrary, j9socket_t so - * I_32 (*sock_writeto) ( struct J9PortLibrary *portLibrary, j9socket_t soc - * I_32 (*sock_inetaddr) ( struct J9PortLibrary *portLibrary, char *addrStr - - * I_32 (*sock_sockaddr_create) ( struct J9PortLibrary *portLibrary, j9sock - * I_16 (*sock_sockaddr_port) ( struct J9PortLibrary *portLibrary, j9sockad - * I_32 (*sock_sockaddr_address) ( struct J9PortLibrary *portLibrary, j9soc - - * I_32 (*sock_linger_create) ( struct J9PortLibrary *portLibrary, j9linger - * I_32 (*sock_linger_free) ( struct J9PortLibrary *portLibrary, j9linger_t - * I_32 (*sock_linger_enabled) ( struct J9PortLibrary *portLibrary, j9linge - * I_32 (*sock_linger_linger) ( struct J9PortLibrary *portLibrary, j9linger - - * I_32 (*sock_getopt_int) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_setopt_int) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_getopt_bool) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_setopt_bool) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_getopt_byte) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_setopt_byte) ( struct J9PortLibrary *portLibrary, j9socket_t - * I_32 (*sock_getopt_linger) ( struct J9PortLibrary *portLibrary, j9socket - * I_32 (*sock_setopt_linger) ( struct J9PortLibrary *portLibrary, j9socket - * I_32 (*sock_getopt_sockaddr) ( struct J9PortLibrary *portLibrary, j9sock - * I_32 (*sock_setopt_sockaddr) ( struct J9PortLibrary *portLibrary, j9sock - * I_32 (*sock_setopt_ipmreq) ( struct J9PortLibrary *portLibrary, j9socket - - * I_32 (*sock_ipmreq_create) ( struct J9PortLibrary *portLibrary, j9ipmreq - * I_32 (*sock_ipmreq_free) ( struct J9PortLibrary *portLibrary, j9ipmreq_t - - * I_32 (*sock_setflag_read) ( struct J9PortLibrary *portLibrary, I_32 flag - */ - -/* - * Test and report on DNs characteristics of host {byname? byaddr??} - */ -static I_32 reportDNS(struct J9PortLibrary *portLibrary,char *host) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - I_32 rc,addr; - char* hostname; /*now.. are there mem leak concerns??*/ - char** aliasList; - char* dottedIP; - U_32 i=0,num; - j9hostent_struct handle; - - HEADING(PORTLIB,"DNS report:"); - - if(strlen(host)>0){ - /*gethost, by NAME*/ - rc=j9sock_gethostbyname(host,&handle); /*what if buf 2short? trunc?*/ - if(rc!=0){ - j9tty_printf(PORTLIB," Error getting host [%s] by name: %d\n",host,rc); - goto ERROR; - } - j9tty_printf(PORTLIB," Got host [%s] by name.\n",host); - } else { - /*gethost, by addr??*/ - } - - rc=j9sock_hostent_hostname(&handle, &hostname); /*ret 0, in win, anyway*/ - if(rc!=0) - strcpy(hostname,"(error getting hostname)"); - - rc=j9sock_hostent_aliaslist(&handle,&aliasList); /*ret 0, in win*/ - if(rc!=0) - strcpy(hostname,"(error getting aliasList)"); - - - j9tty_printf(PORTLIB," [%s]\n\n",hostname); - - while(aliasList[i] !=NULL){ - addr=j9sock_hostent_addrlist(&handle,i); - rc=j9sock_inetntoa(&dottedIP,(U_32)addr, NULL); - if(rc!=0){ - j9tty_printf(PORTLIB,"inetntoa failed: %d\n",rc); - goto ERROR; - } - - num=j9sock_ntohl(*(U_32*)(aliasList)[i]); /*get bytes in host order*/ - j9tty_printf(PORTLIB," 0x%X [%s]\n",num,dottedIP); /*%d i*/ - ++i; - } - if(i==0) j9tty_printf(PORTLIB,"No alias\n"); - - /*********/ - /*cleanup*/ - /*********/ - HEADING(PORTLIB,"END of DNS report"); - return 0; - -ERROR: - /*********/ - /*cleanup*/ - /*********/ - j9tty_printf(PORTLIB,"reportDNS failed\n"); - return -1; -} -/******************************* - * startup routine for the tests - ******************************/ -static I_32 -startupIP(struct J9PortLibrary *portLibrary,j9socket_ *socket, j9socket_t *netPipe, j9sockaddr_t addr, char *host, I_32 sockType, U_16 port) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - I_32 rc; - - /* create/open socket [get socket number] - * note: J9SOCK_AFINET is only addr family supported] - * sockTypes: J9SOCK_STREAM (stream socket), or J9SOCK_DGRAM (datagram) - * protocol: creation param [only J9SOCK_DEFPROTOCOL supported] - */ - rc=j9sock_socket(socket, J9SOCK_AFINET, sockType, J9SOCK_DEFPROTOCOL); - if(rc!=0){ - j9tty_printf(PORTLIB," Error creating new socket descriptor: %d\n",rc); - /*maybe more desc later*/ - return rc; - } - /*j9tty_printf(PORTLIB," Created new socket descriptor\n");*/ - - /* create new j9sockaddr [with host:port] - * note: AF_INET only address family supported now - */ - rc=j9sock_sockaddr(addr,host,port); - if(rc!=0){ - j9tty_printf(PORTLIB," Error creating new sockaddr struct: %d\n",rc); - /*maybe more desc later*/ - return rc; - } - /*j9tty_printf(PORTLIB," Created new sockaddr struct\n");*/ - - if(netPipe!=NULL){ /*not when client, or udp, etc..*/ - /* [associate Inet addr+port# with socket] - * bind socket to sockaddr struct {Inet addr+port#}. - */ - rc=j9sock_bind(*socket, *addr); /*rc gets J9PORT_ERROR_SOCKET_xxxx*/ - if(rc!=0){ - j9tty_printf(PORTLIB," Error binding unconnected socket: %d\n",rc); - /*maybe more desc later*/ - return rc; - } - j9tty_printf(PORTLIB," Bound unconnected socket, done startup\n"); - } - return 0; -} -/******************************** - * shutdown routine for the tests - ********************************/ -static I_32 -shutdownIP(struct J9PortLibrary *portLibrary,j9socket_t *socket, j9socket_t *netPipe, j9sockaddr_t *addr) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - I_32 rc; - - /* close network socket */ - if(netPipe != NULL && *netPipe==NULL) - j9tty_printf(PORTLIB," network socket *netPipe==NULL!\n"); - if(netPipe !=NULL && *netPipe!=NULL && j9sock_isSocketValid( *netPipe ) ){ - rc=j9sock_close(netPipe); - if(rc!=0){ - j9tty_printf(PORTLIB," Error closing network socket: %d\n",rc); - return rc; - } - /*j9tty_printf(PORTLIB," Closed network socket\n");*/ - } - /*close socket new j9sockaddr */ - if( j9sock_socketIsValid( *socket ) ) { - rc=j9sock_close(socket); - - if(rc!=0){ - j9tty_printf(PORTLIB," Error closing socket: %d\n",rc); - return rc; - } - } - /*j9tty_printf(PORTLIB," Closed socket\n");*/ - - j9tty_printf(PORTLIB," Freed sockaddr struct, done shutdown\n"); - return 0; -} -/******************************** - * waitForRead - ********************************/ -static I_32 -waitForRead(struct J9PortLibrary *portLibrary, j9socket_t socket, U_32 wait_seconds) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - I_32 rc; - - rc=j9sock_select_read(socket, wait_seconds,0, FALSE); - if(rc!=0){ - j9tty_printf(PORTLIB," Error on select: %d\n",rc); - /*maybe more desc later*/ - /*return rc;*/ /*what does +1 mean??!?!?!*/ - } - return 0; -} -/******************************** - * waitForWrite, similar to REad - ********************************/ -static I_32 -waitForWrite(struct J9PortLibrary *portLibrary, j9socket_t socket, U_32 wait_seconds) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - I_32 rc; - - /* TODO: create a select_write call - rc=j9sock_select_write(socket, wait_seconds,0, FALSE); - */ - rc = 0; - if(rc!=0){ - j9tty_printf(PORTLIB," Error on select: %d\n",rc); - /*maybe more desc later*/ - /*return rc;*/ /*what does +1 mean??!?!?!*/ - } - return 0; -} -/****************************** - * server half of the TCP test - ******************************/ -static I_32 -testTCPserver(struct J9PortLibrary *portLibrary,char *host, U_16 port) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - j9socket_t socket, netPipe; - j9sockaddr_struct addr; - I_32 rc; - - HEADING(PORTLIB,"TCP server"); - - rc=startupIP(PORTLIB,&socket, &netPipe, &addr, host, J9SOCK_STREAM, port); - if(rc!=0){ - j9tty_printf(PORTLIB," Error starting up server: %d\n",rc); - goto shutdown; /*return rc;*/ /*should still shutdown, you know*/ - } - - j9tty_printf(PORTLIB," Server waiting on port %d...\n",port); - - /*listen before accept*/ - rc=j9sock_listen(socket, 3); /*accept backlog of 3, others refused*/ - if(rc!=0){ - j9tty_printf(PORTLIB," Error on j9sock_listen: %d\n",rc); - return rc; - } - /*j9tty_printf(PORTLIB," done j9sock_listen\n");*/ - - /*wait for client*/ - netPipe=NULL; - rc=j9sock_accept(socket, &addr, &netPipe); - if(rc!=0){ - j9tty_printf(PORTLIB," Error on j9sock_accept: %d\n",rc); - return rc; - } - /*j9tty_printf(PORTLIB," done j9sock_accept\n");*/ - - if(netPipe==NULL){ - j9tty_printf(PORTLIB," No client connection actually received... :(\n"); - goto shutdown; - } - - /* wait for CHALLENGE */ - rc=waitForRead(PORTLIB,netPipe, 10); - if(rc!=0){ - j9tty_printf(PORTLIB," Error on waitForRead: %d\n",rc); - return rc; - } - /* read: CHALLENGE +ve=#bytes read, 0=conn. gracefully closed, -ve=ERR*/ - rc=j9sock_read(netPipe, buf, sizeof(buf), 0); /*Flag=0?!*/ - if(rc<0){ - j9tty_printf(PORTLIB," Error on read: %d\n",rc); - return rc; - } - j9tty_printf(PORTLIB," read %d bytes [0=conn. closed]\n",rc); - - if(strcmp(buf,CHALLENGE)==0){ - j9tty_printf(PORTLIB," -challenge matched!\n"); - } else { - j9tty_printf(PORTLIB," Server got: %s\n",buf); - j9tty_printf(PORTLIB," [Expected : %s]",CHALLENGE); - j9tty_printf(PORTLIB," -challenge not matched!\n"); - } - - /*send back RESPONSE*/ - j9tty_printf(PORTLIB," sending back response: %s\n",RESPONSE); - rc=waitForWrite(PORTLIB,netPipe, 10); - if(rc!=0){ - j9tty_printf(PORTLIB," Error on waitForWrite: %d\n",rc); - return rc; - } - - strcpy(buf,RESPONSE); /*need to?*/ - - /* write: RESPONSE +ve=#bytes written, -ve=ERR*/ - rc=j9sock_write(netPipe, buf, sizeof(buf), J9SOCK_NOFLAGS); /*Flag=0?!*/ - if(rc<0){ - j9tty_printf(PORTLIB," Error on write: %d\n",rc); - return rc; - } - j9tty_printf(PORTLIB," wrote %d bytes [0=conn. closed]\n",rc); rc=0;/*!error!*/ - - -shutdown: - rc=shutdownIP(PORTLIB,&socket, &netPipe, &(&addr)); - if(rc!=0){ - j9tty_printf(PORTLIB," Error shuting down server: %d\n",rc); - return rc; - } - return 0; -} -/****************************** - * client half of the TCP test - *****************************/ -static I_32 -testTCPclient(struct J9PortLibrary *portLibrary,char *host, U_16 port) -{ - PORT_ACCESS_FROM_PORT(portLibrary); - j9socket_t socket; - j9sockaddr_struct addr; - I_32 rc; - - HEADING(PORTLIB,"TCP client"); - - rc=startupIP(PORTLIB,&socket, NULL, &addr, host, J9SOCK_STREAM, port); - if(rc!=0){ - j9tty_printf(PORTLIB," Error starting up client: %d\n",rc); - goto shutdown; /*return rc;*/ /*should still shutdown, you know*/ - } - - rc=j9sock_connect(socket, addr); - if(rc!=0){ - j9tty_printf(PORTLIB," Error connecting to %s:%d (%d)\n",host,port,rc); - goto shutdown; /*return rc;*/ /*should still shutdown, you know*/ - } - j9tty_printf(PORTLIB," connected to %s on port %d...\n",host,port); - - /*send CHALLENGE*/ - j9tty_printf(PORTLIB," sending challenge: %s\n","",CHALLENGE); - rc=waitForWrite(PORTLIB,socket, 10); - if(rc!=0){ - j9tty_printf(PORTLIB," Error on waitForWrite: %d\n",rc); - return rc; - } - - strcpy(buf,CHALLENGE); /*need to?*/ - - /* write: CHALLENGE +ve=#bytes written, -ve=ERR*/ - rc=j9sock_write(socket, buf, sizeof(buf), J9SOCK_NOFLAGS); /* NEED size buf + 1???*/ - if(rc<0){ - j9tty_printf(PORTLIB," Error on write: %d\n",rc); - return rc; - } - j9tty_printf(PORTLIB," wrote %d bytes [0=conn. closed?]\n",rc); - - - - /* wait for RESPONSE */ - rc=waitForRead(PORTLIB,socket, 10); - if(rc!=0){ - j9tty_printf(PORTLIB," Error on waitForRead: %d\n",rc); - goto shutdown; /*return rc;*/ - } - /* read: RESPONSE +ve=#bytes read, 0=conn. gracefully closed, -ve=ERR*/ - rc=j9sock_read(socket, buf, sizeof(buf), 0); /*Flag=0?!*/ - if(rc<0){ - j9tty_printf(PORTLIB," Error on read: %d\n",rc); - goto shutdown; /*return rc;*/ - } - j9tty_printf(PORTLIB," read %d bytes [0=conn. closed]\n",rc); rc=0;/*!error!*/ - j9tty_printf(PORTLIB," We recv'ed: %s\n",buf); - - if(strcmp(buf,RESPONSE)==0){ - j9tty_printf(PORTLIB," -matched!\n"); - } else { - j9tty_printf(PORTLIB," [Expected : %s]",RESPONSE); - j9tty_printf(PORTLIB," -no match!\n"); - } - - -shutdown: - if(shutdownIP(PORTLIB,&socket, NULL, &(&addr)) || rc){ /*if shutdown fail, YOU KNOW*/ - j9tty_printf(PORTLIB," Error shuting down client: rc=%d\n",rc); - return rc; - } - return 0; -} - -#endif - -/* NOT DONE - need to test UDP */ - - -/*********************************************************** - * sock_test, the entry point to the socket testing module - ***********************************************************/ -int -socket_test(struct J9PortLibrary *portLibrary,BOOLEAN isClient,char* serverName) -{ - - PORT_ACCESS_FROM_PORT(portLibrary); -#if 0 - I_32 rc; - char buf[64]; - U_16 i,portMin=45000, portMax=45003; /*[50000,60000)*/ -#endif - /*fancy heading*/ - HEADING(PORTLIB,"Socket test"); - -#if 0 - - j9tty_printf(PORTLIB,"IN SOCKET TEST %d %d %d check..\n",1,2,3); - - /*initialize socket library*/ - rc=j9sock_startup(); - if(rc!=0){ - j9tty_printf(PORTLIB,"Error initializing sockets: %d\n",rc); - return rc; - } - j9tty_printf(PORTLIB,"Sockets initialized!\n"); - - - - /*gethostname*/ - rc=j9sock_gethostname(buf,(I_32)sizeof(buf));/*whatif buf too short?trunc?*/ - if(rc!=0){ - j9tty_printf(PORTLIB,"Error on gethostname of local machine: %d\n",rc); - return rc; - } - j9tty_printf(PORTLIB,"(local) Hostname=[%s]\n",buf); - - - -if(strlen(serverName)==0){ - j9tty_printf(PORTLIB,"defaulting to localhost\n"); - strcpy(serverName,LOCAL); /*defaults to using localhost*/ -} - -#if 0 - - if(isClient){ - if(rc= testUDPclient(PORTLIB,serverName,5140)){ /*4499*/ - j9tty_printf(PORTLIB,"Failed to test UDP! (%d)\n",rc); - return rc; - } - goto shutdown; /*return 0;*/ - }else{ - if(rc= testUDPserver(PORTLIB,LOCAL,5140)){ - j9tty_printf(PORTLIB,"Failed to test UDP! (%d)\n",rc); - return rc; - } - goto shutdown; /*return 0;*/ - } - -#else -/*********************************************************************/ - if(isClient){ - for(i=portMin;i