diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index f6f7cbb8ae8..44d6a5ecc69 100644 --- a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c +++ b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c @@ -292,7 +292,7 @@ static int lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsap return lwip_err_remap(err); } -static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_socket_t server) +static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port) { struct lwip_socket *s = (struct lwip_socket *)server; struct lwip_socket *ns = lwip_arena_alloc(); @@ -304,6 +304,10 @@ static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t *handle, nsap } *(struct lwip_socket **)handle = ns; + + (void) netconn_peer(ns->conn, (ip_addr_t *)addr->bytes, port); + addr->version = NSAPI_IPv4; + return 0; } diff --git a/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.cpp b/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.cpp index 23dbc886ba2..9cd5688c42b 100644 --- a/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.cpp +++ b/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.cpp @@ -846,7 +846,7 @@ int NanostackInterface::socket_connect(void *handle, const SocketAddress &addr) return ret; } -int NanostackInterface::socket_accept(void **handle, void *server) +int NanostackInterface::socket_accept(void *server, void **handle, SocketAddress *address) { return NSAPI_ERROR_UNSUPPORTED; } diff --git a/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.h b/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.h index e9560070dee..47126c4a1f1 100644 --- a/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.h +++ b/features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.h @@ -95,11 +95,12 @@ class NanostackInterface : public NetworkStack { * This call is non-blocking. If accept would block, * NSAPI_ERROR_WOULD_BLOCK is returned immediately. * - * @param handle Destination for a handle to the newly created sockey * @param server Socket handle to server to accept from + * @param handle Destination for a handle to the newly created socket + * @param address Destination for the remote address or NULL * @return 0 on success, negative error code on failure */ - virtual int socket_accept(void **handle, void *server); + virtual int socket_accept(void *handle, void **server, SocketAddress *address); /** Send data over a TCP socket * diff --git a/features/net/network-socket/NetworkStack.cpp b/features/net/network-socket/NetworkStack.cpp index 701324a8dd1..2198507d9e2 100644 --- a/features/net/network-socket/NetworkStack.cpp +++ b/features/net/network-socket/NetworkStack.cpp @@ -159,13 +159,23 @@ class NetworkStackWrapper : public NetworkStack return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port()); } - virtual int socket_accept(nsapi_socket_t *socket, nsapi_socket_t server) + virtual int socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address) { if (!_stack_api()->socket_accept) { return NSAPI_ERROR_UNSUPPORTED; } - return _stack_api()->socket_accept(_stack(), socket, server); + nsapi_addr_t addr = {NSAPI_IPv4, 0}; + uint16_t port = 0; + + int err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port); + + if (address) { + address->set_addr(addr); + address->set_port(port); + } + + return err; } virtual int socket_send(nsapi_socket_t socket, const void *data, unsigned size) diff --git a/features/net/network-socket/NetworkStack.h b/features/net/network-socket/NetworkStack.h index 7faab703b69..aff3541c6f7 100644 --- a/features/net/network-socket/NetworkStack.h +++ b/features/net/network-socket/NetworkStack.h @@ -160,11 +160,12 @@ class NetworkStack * This call is non-blocking. If accept would block, * NSAPI_ERROR_WOULD_BLOCK is returned immediately. * - * @param handle Destination for a handle to the newly created sockey * @param server Socket handle to server to accept from + * @param handle Destination for a handle to the newly created socket + * @param address Destination for the remote address or NULL * @return 0 on success, negative error code on failure */ - virtual int socket_accept(nsapi_socket_t *handle, nsapi_socket_t server) = 0; + virtual int socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address=0) = 0; /** Send data over a TCP socket * diff --git a/features/net/network-socket/TCPServer.cpp b/features/net/network-socket/TCPServer.cpp index c3c23b0bcd7..a56372032f4 100644 --- a/features/net/network-socket/TCPServer.cpp +++ b/features/net/network-socket/TCPServer.cpp @@ -47,7 +47,7 @@ int TCPServer::listen(int backlog) return ret; } -int TCPServer::accept(TCPSocket *connection) +int TCPServer::accept(TCPSocket *connection, SocketAddress *address) { _lock.lock(); int ret; @@ -60,7 +60,7 @@ int TCPServer::accept(TCPSocket *connection) _pending = 0; void *socket; - ret = _stack->socket_accept(&socket, _socket); + ret = _stack->socket_accept(_socket, &socket, address); if (0 == ret) { connection->_lock.lock(); diff --git a/features/net/network-socket/TCPServer.h b/features/net/network-socket/TCPServer.h index f57c5f5ad96..ff9f177f06a 100644 --- a/features/net/network-socket/TCPServer.h +++ b/features/net/network-socket/TCPServer.h @@ -78,9 +78,10 @@ class TCPServer : public Socket { * immediately. * * @param socket TCPSocket instance that will handle the incoming connection. + * @param address Destination for the remote address or NULL * @return 0 on success, negative error code on failure */ - int accept(TCPSocket *connection); + int accept(TCPSocket *connection, SocketAddress *address = NULL); protected: virtual nsapi_protocol_t get_proto(); diff --git a/features/net/network-socket/nsapi_types.h b/features/net/network-socket/nsapi_types.h index 176e42f35cf..669ef40311e 100644 --- a/features/net/network-socket/nsapi_types.h +++ b/features/net/network-socket/nsapi_types.h @@ -310,11 +310,13 @@ typedef struct nsapi_stack_api * NSAPI_ERROR_WOULD_BLOCK is returned immediately. * * @param stack Stack handle - * @param socket Destination for a handle to the newly created sockey * @param server Socket handle to server to accept from + * @param socket Destination for a handle to the newly created socket + * @param addr Destination for the address of the remote host + * @param port Destination for the port of the remote host * @return 0 on success, negative error code on failure */ - int (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t *socket, nsapi_socket_t server); + int (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port); /** Send data over a TCP socket *