From 34077cae40f486b22c367f3711c279da026d7d60 Mon Sep 17 00:00:00 2001 From: Haley Cao Date: Wed, 20 May 2020 16:29:20 +0000 Subject: [PATCH] Setting Up Support for Socket Options Setting up socket option definitions and function prototype - Set up omrsock_linger_t pointer, OMRLinger struct to be used in the linger socket option. - Set up omrsock_timeval_t pointer, OMRTimeval struct to be used in the timeval socket option. - Set up omr public socket option definitions and private os socket option definitions. - Set up functions prototypes for the socket option related functions. Issue: #5158 Signed-off-by: Haley Cao --- include_core/omrport.h | 12 ++++++ include_core/omrportsock.h | 21 ++++++++-- include_core/omrportsocktypes.h | 16 +++++++- port/common/omrport.c | 4 ++ port/common/omrsock.c | 73 +++++++++++++++++++++++++++++++++ port/omrportpriv.h | 8 ++++ port/unix/omrsock.c | 20 +++++++++ port/unix_include/omrsock.h | 9 ++++ port/win32/omrsock.c | 21 ++++++++++ 9 files changed, 180 insertions(+), 4 deletions(-) diff --git a/include_core/omrport.h b/include_core/omrport.h index d50c2ed100f..6f81a4014b0 100644 --- a/include_core/omrport.h +++ b/include_core/omrport.h @@ -2160,6 +2160,14 @@ typedef struct OMRPortLibrary { uint32_t (*sock_htonl)(struct OMRPortLibrary *portLibrary, uint32_t val) ; /** see @ref omrsock.c::omrsock_inet_pton "omrsock_inet_pton"*/ int32_t (*sock_inet_pton)(struct OMRPortLibrary *portLibrary, int32_t addrFamily, const char *addr, uint8_t *addrNetworkOrder) ; + /** see @ref omrsock.c::omrsock_timeval_init "omrsock_timeval_init"*/ + int32_t (*sock_timeval_init)(struct OMRPortLibrary *portLibrary, omrsock_timeval_t handle, uint32_t secTime, uint32_t uSecTime) ; + /** see @ref omrsock.c::omrsock_linger_init "omrsock_linger_init"*/ + int32_t (*sock_linger_init)(struct OMRPortLibrary *portLibrary, omrsock_linger_t handle, int32_t enabled, uint32_t timeout) ; + /** see @ref omrsock.c::omrsock_setsockopt "omrsock_setsockopt"*/ + int32_t (*sock_setsockopt)(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) ; + /** see @ref omrsock.c::omrsock_getsockopt "omrsock_getsockopt"*/ + int32_t (*sock_getsockopt)(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) ; #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */ #if defined(OMR_OPT_CUDA) /** CUDA configuration data */ @@ -2631,6 +2639,10 @@ extern J9_CFUNC int32_t omrport_getVersion(struct OMRPortLibrary *portLibrary); #define omrsock_htons(param1) privateOmrPortLibrary->sock_htons(privateOmrPortLibrary, (param1)) #define omrsock_htonl(param1) privateOmrPortLibrary->sock_htonl(privateOmrPortLibrary, (param1)) #define omrsock_inet_pton(param1) privateOmrPortLibrary->sock_inet_pton(privateOmrPortLibrary, (param1)) +#define omrsock_timeval_init(param1,param2,param3) privateOmrPortLibrary->sock_timeval_init(privateOmrPortLibrary, (param1), (param2), (param3)) +#define omrsock_linger_init(param1,param2,param3) privateOmrPortLibrary->sock_linger_init(privateOmrPortLibrary, (param1), (param2), (param3)) +#define omrsock_setsockopt(param1,param2,param3,param4) privateOmrPortLibrary->sock_setsockopt(privateOmrPortLibrary, (param1), (param2), (param3), (param4)) +#define omrsock_getsockopt(param1,param2,param3,param4) privateOmrPortLibrary->sock_getsockopt(privateOmrPortLibrary, (param1), (param2), (param3), (param4)) #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */ #if defined(OMR_OPT_CUDA) diff --git a/include_core/omrportsock.h b/include_core/omrportsock.h index 19b18d9bca8..e811f8cc97a 100644 --- a/include_core/omrportsock.h +++ b/include_core/omrportsock.h @@ -34,6 +34,12 @@ typedef struct OMRSockAddrStorage *omrsock_sockaddr_t; /* Pointer to OMRSocket, a struct that contains socket descriptor. */ typedef struct OMRSocket *omrsock_socket_t; +/* Pointer to OMRTimeval, a struct that contains struct timeval. */ +typedef struct OMRTimeval *omrsock_timeval_t; + +/* Pointer to OMRLinger, a struct that contains struct linger.*/ +typedef struct OMRLinger *omrsock_linger_t; + /* Bind to all available interfaces */ #define OMRSOCK_INADDR_ANY ((uint32_t)0) @@ -47,9 +53,18 @@ typedef struct OMRSocket *omrsock_socket_t; #define OMRSOCK_STREAM 1 #define OMRSOCK_DGRAM 2 -/* Protocol Family */ +/* Protocol Family and Socket Levels */ #define OMRSOCK_IPPROTO_DEFAULT 0 -#define OMRSOCK_IPPROTO_TCP 1 -#define OMRSOCK_IPPROTO_UDP 2 +#define OMRSOCK_SOL_SOCKET 1 +#define OMRSOCK_IPPROTO_TCP 2 +#define OMRSOCK_IPPROTO_UDP 3 + +/* Socket Options */ +#define OMRSOCK_SO_REUSEADDR 1 +#define OMRSOCK_SO_KEEPALIVE 2 +#define OMRSOCK_SO_LINGER 3 +#define OMRSOCK_SO_RCVTIMEO 4 +#define OMRSOCK_SO_SNDTIMEO 5 +#define OMRSOCK_TCP_NODELAY 6 #endif /* !defined(OMRPORTSOCK_H_) */ diff --git a/include_core/omrportsocktypes.h b/include_core/omrportsocktypes.h index b19faa48c51..b0133f0e241 100644 --- a/include_core/omrportsocktypes.h +++ b/include_core/omrportsocktypes.h @@ -86,7 +86,7 @@ typedef struct OMRSocket { } OMRSocket; /** - * A node in a linked-list of addrinfo. Filled in using @ref omr_getaddrinfo. + * A node in a linked-list of addrinfo. Filled in using @ref omrsock_getaddrinfo. */ typedef struct OMRAddrInfoNode { /** @@ -100,4 +100,18 @@ typedef struct OMRAddrInfoNode { uint32_t length; } OMRAddrInfoNode; +/** + * A struct for storing timeval values. Filled in using @ref omrsock_timeval_init. + */ +typedef struct OMRTimeval { + struct timeval data; +} OMRTimeval; + +/** + * A struct for storing linger values. Filled in using @ref omrsock_linger_init. + */ +typedef struct OMRLinger { + struct linger data; +} OMRLinger; + #endif /* !defined(OMRPORTSOCKTYPES_H_) */ diff --git a/port/common/omrport.c b/port/common/omrport.c index d6b3d8b7110..259c655f971 100644 --- a/port/common/omrport.c +++ b/port/common/omrport.c @@ -329,6 +329,10 @@ static OMRPortLibrary MasterPortLibraryTable = { omrsock_htons, /* sock_htons */ omrsock_htonl, /* sock_htonl */ omrsock_inet_pton, /* sock_inet_pton */ + omrsock_timeval_init, /* sock_timeval_init */ + omrsock_linger_init, /* sock_linger_init */ + omrsock_setsockopt, /* sock_setsockopt */ + omrsock_getsockopt, /* sock_getsockopt */ #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */ #if defined(OMR_OPT_CUDA) NULL, /* cuda_configData */ diff --git a/port/common/omrsock.c b/port/common/omrsock.c index 53fef7691e5..c81b681884d 100644 --- a/port/common/omrsock.c +++ b/port/common/omrsock.c @@ -517,4 +517,77 @@ omrsock_inet_pton(struct OMRPortLibrary *portLibrary, int32_t addrFamily, const return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; } +/** + * Create a time structure, representing the timeout period defined in seconds & microSeconds. Timeval's could be + * used as timeout arguments in the @ref omrsock_setsockopt function. + * + * @param[in] portLibrary The port library. + * @param[out] handle Pointer pointer to the OMRTimeval to be allocated. + * @param[in] secTime The integer component of the timeout value (in seconds). + * @param[in] uSecTime The fractional component of the timeout value (in microseconds). + * + * @return 0, if no errors occurred, otherwise return an error. + */ +int32_t +omrsock_timeval_init(struct OMRPortLibrary *portLibrary, omrsock_timeval_t handle, uint32_t secTime, uint32_t uSecTime) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +/** + * 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 A positive timeout value from 0 to linger indefinitely (in seconds). + * + * @return 0, if no errors occurred, otherwise return an error. + */ +int32_t +omrsock_linger_init(struct OMRPortLibrary *portLibrary, omrsock_linger_t handle, int32_t enabled, uint32_t timeout) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +/** + * Set the value of the socket option. + * Refer to the private platform_socket_level & platform_socket_option functions for details of the options + * supported. + * + * @param[in] portLibrary The port library. + * @param[in] handle 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[in] optval Pointer to the option value to update the socket option with. + * + * @return 0, if no errors occurred, otherwise return an error. + */ +int32_t +omrsock_setsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +/** + * Answer the value of the socket option. + * Refer to the private platform_socket_level & platform_socket_option functions for details of the options + * supported. + * + * @param[in] portLibrary The port library. + * @param[in] handle 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 pre-allocated space to update with the option value. + * + * @return 0, if no errors occurred, otherwise return an error. + */ +int32_t +omrsock_getsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */ diff --git a/port/omrportpriv.h b/port/omrportpriv.h index 3d90a88a0fb..0b7fd081574 100644 --- a/port/omrportpriv.h +++ b/port/omrportpriv.h @@ -665,6 +665,14 @@ extern J9_CFUNC uint32_t omrsock_htonl(struct OMRPortLibrary *portLibrary, uint32_t val); extern J9_CFUNC int32_t omrsock_inet_pton(struct OMRPortLibrary *portLibrary, int32_t addrFamily, const char *addr, uint8_t *addrNetworkOrder); +extern J9_CFUNC int32_t +omrsock_timeval_init(struct OMRPortLibrary *portLibrary, omrsock_timeval_t handle, uint32_t secTime, uint32_t uSecTime); +extern J9_CFUNC int32_t +omrsock_linger_init(struct OMRPortLibrary *portLibrary, omrsock_linger_t handle, int32_t enabled, uint32_t timeout); +extern J9_CFUNC int32_t +omrsock_setsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval); +extern J9_CFUNC int32_t +omrsock_getsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval); #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */ /* J9SourceJ9Str*/ diff --git a/port/unix/omrsock.c b/port/unix/omrsock.c index 727df994ce0..dac71dacecd 100644 --- a/port/unix/omrsock.c +++ b/port/unix/omrsock.c @@ -705,4 +705,24 @@ omrsock_inet_pton(struct OMRPortLibrary *portLibrary, int32_t addrFamily, const } +int32_t +omrsock_timeval_init(struct OMRPortLibrary *portLibrary, omrsock_timeval_t handle, uint32_t secTime, uint32_t uSecTime) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +int32_t +omrsock_linger_init(struct OMRPortLibrary *portLibrary, omrsock_linger_t handle, int32_t enabled, uint32_t timeout) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +int32_t +omrsock_setsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +int32_t +omrsock_getsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */ diff --git a/port/unix_include/omrsock.h b/port/unix_include/omrsock.h index 8df90d052b8..d92ec9af4c4 100644 --- a/port/unix_include/omrsock.h +++ b/port/unix_include/omrsock.h @@ -57,7 +57,16 @@ typedef struct sockaddr_in6 omr_os_sockaddr_in6; /* IPv6 */ /* Protocol */ #define OS_SOCK_IPPROTO_DEFAULT 0 +#define OS_SOL_SOCKET SOL_SOCKET #define OS_SOCK_IPPROTO_TCP IPPROTO_TCP #define OS_SOCK_IPPROTO_UDP IPPROTO_UDP +/* Socket options*/ +#define OSK_SO_REUSEADDR SO_REUSEADDR +#define OS_SO_KEEPALIVE SO_KEEPALIVE +#define OS_SO_LINGER SO_LINGER +#define OS_SO_RCVTIMEO SO_RCVTIMEO +#define OS_SO_SNDTIMEO SO_SNDTIMEO +#define OS_TCP_NODELAY TCP_NODELAY + #endif /* !defined(OMRSOCK_H_) */ diff --git a/port/win32/omrsock.c b/port/win32/omrsock.c index 47517190743..b4c0fceee87 100644 --- a/port/win32/omrsock.c +++ b/port/win32/omrsock.c @@ -182,4 +182,25 @@ omrsock_inet_pton(struct OMRPortLibrary *portLibrary, int32_t addrFamily, const return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; } +int32_t +omrsock_timeval_init(struct OMRPortLibrary *portLibrary, omrsock_timeval_t handle, uint32_t secTime, uint32_t uSecTime) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +int32_t +omrsock_linger_init(struct OMRPortLibrary *portLibrary, omrsock_linger_t handle, int32_t enabled, uint32_t timeout) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +int32_t +omrsock_setsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + +int32_t +omrsock_getsockopt(struct OMRPortLibrary *portLibrary, omrsock_socket_t handle, int32_t optlevel, int32_t optname, void *optval) { + return OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM; +} + + #endif /* defined(OMR_PORT_SOCKET_SUPPORT) */