mirror of https://github.com/ARMmbed/mbed-os.git
network-socket: Optionally return peer address from accept().
Fixes issue #2399 by optionally returning a SocketAddress from TCPServer::accept(). This entails changes to underlying NetworkStack and nsapi. This commit deals only with lwip and higher level APIs; other users of NetworkStack and nsapi may be affected. Currently lwip is the only in-tree user of nsapi.pull/2434/head
parent
58d9926c0c
commit
36bf4827c7
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue