diff --git a/LWIPInterface.cpp b/LWIPInterface.cpp index 94e72ece7c..21b14cba0e 100644 --- a/LWIPInterface.cpp +++ b/LWIPInterface.cpp @@ -545,6 +545,24 @@ int LWIPInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *buf, return copied; } +int LWIPInterface::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) { + struct lwip_socket *s = (struct lwip_socket *)handle; + + switch (optname) { + case NSAPI_KEEPALIVE: + if (optlen != sizeof(int) || s->proto != NSAPI_TCP) { + return NSAPI_ERROR_UNSUPPORTED; + } + + s->tcp->so_options |= SOF_KEEPALIVE; + s->tcp->keep_intvl = *(int*)optval; + return 0; + + default: + return NSAPI_ERROR_UNSUPPORTED; + } +} + void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *data) { struct lwip_socket *s = (struct lwip_socket *)handle; diff --git a/LWIPInterface.h b/LWIPInterface.h index 139ab45548..e272eb5a54 100644 --- a/LWIPInterface.h +++ b/LWIPInterface.h @@ -139,6 +139,21 @@ protected: */ virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); + /* Set stack-specific socket options + * + * The setsockopt allow an application to pass stack-specific hints + * to the underlying stack. For unsupported options, + * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. + * + * @param handle Socket handle + * @param level Stack-specific protocol level + * @param optname Stack-specific option identifier + * @param optval Option value + * @param optlen Length of the option value + * @return 0 on success, negative error code on failure + */ + virtual int setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen); + /** Register a callback on state change of the socket * @param handle Socket handle * @param callback Function to call on state change