diff --git a/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c b/features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c index f6f7cbb8ae..44d6a5ecc6 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/network-socket/NetworkStack.cpp b/features/net/network-socket/NetworkStack.cpp index 701324a8dd..1951566f93 100644 --- a/features/net/network-socket/NetworkStack.cpp +++ b/features/net/network-socket/NetworkStack.cpp @@ -159,13 +159,23 @@ protected: 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 *socket, nsapi_socket_t server, 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 7faab703b6..d948e6f9ba 100644 --- a/features/net/network-socket/NetworkStack.h +++ b/features/net/network-socket/NetworkStack.h @@ -160,11 +160,12 @@ protected: * 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 handle Destination for a handle to the newly created socket * @param server Socket handle to server to accept from + * @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 *handle, nsapi_socket_t server, 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 c3c23b0bcd..78ef775a9e 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 f57c5f5ad9..ff9f177f06 100644 --- a/features/net/network-socket/TCPServer.h +++ b/features/net/network-socket/TCPServer.h @@ -78,9 +78,10 @@ public: * 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 176e42f35c..669ef40311 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 *