Skip to content

Commit 36bf482

Browse files
author
Colin Hogben
committed
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.
1 parent 58d9926 commit 36bf482

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static int lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsap
292292
return lwip_err_remap(err);
293293
}
294294

295-
static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_socket_t server)
295+
static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port)
296296
{
297297
struct lwip_socket *s = (struct lwip_socket *)server;
298298
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
304304
}
305305

306306
*(struct lwip_socket **)handle = ns;
307+
308+
(void) netconn_peer(ns->conn, (ip_addr_t *)addr->bytes, port);
309+
addr->version = NSAPI_IPv4;
310+
307311
return 0;
308312
}
309313

features/net/network-socket/NetworkStack.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,23 @@ class NetworkStackWrapper : public NetworkStack
159159
return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port());
160160
}
161161

162-
virtual int socket_accept(nsapi_socket_t *socket, nsapi_socket_t server)
162+
virtual int socket_accept(nsapi_socket_t *socket, nsapi_socket_t server, SocketAddress *address)
163163
{
164164
if (!_stack_api()->socket_accept) {
165165
return NSAPI_ERROR_UNSUPPORTED;
166166
}
167167

168-
return _stack_api()->socket_accept(_stack(), socket, server);
168+
nsapi_addr_t addr = {NSAPI_IPv4, 0};
169+
uint16_t port = 0;
170+
171+
int err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port);
172+
173+
if (address) {
174+
address->set_addr(addr);
175+
address->set_port(port);
176+
}
177+
178+
return err;
169179
}
170180

171181
virtual int socket_send(nsapi_socket_t socket, const void *data, unsigned size)

features/net/network-socket/NetworkStack.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ class NetworkStack
160160
* This call is non-blocking. If accept would block,
161161
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
162162
*
163-
* @param handle Destination for a handle to the newly created sockey
163+
* @param handle Destination for a handle to the newly created socket
164164
* @param server Socket handle to server to accept from
165+
* @param address Destination for the remote address or NULL
165166
* @return 0 on success, negative error code on failure
166167
*/
167-
virtual int socket_accept(nsapi_socket_t *handle, nsapi_socket_t server) = 0;
168+
virtual int socket_accept(nsapi_socket_t *handle, nsapi_socket_t server, SocketAddress *address=0) = 0;
168169

169170
/** Send data over a TCP socket
170171
*

features/net/network-socket/TCPServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int TCPServer::listen(int backlog)
4747
return ret;
4848
}
4949

50-
int TCPServer::accept(TCPSocket *connection)
50+
int TCPServer::accept(TCPSocket *connection, SocketAddress *address)
5151
{
5252
_lock.lock();
5353
int ret;
@@ -60,7 +60,7 @@ int TCPServer::accept(TCPSocket *connection)
6060

6161
_pending = 0;
6262
void *socket;
63-
ret = _stack->socket_accept(&socket, _socket);
63+
ret = _stack->socket_accept(&socket, _socket, address);
6464

6565
if (0 == ret) {
6666
connection->_lock.lock();

features/net/network-socket/TCPServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ class TCPServer : public Socket {
7878
* immediately.
7979
*
8080
* @param socket TCPSocket instance that will handle the incoming connection.
81+
* @param address Destination for the remote address or NULL
8182
* @return 0 on success, negative error code on failure
8283
*/
83-
int accept(TCPSocket *connection);
84+
int accept(TCPSocket *connection, SocketAddress *address = NULL);
8485

8586
protected:
8687
virtual nsapi_protocol_t get_proto();

features/net/network-socket/nsapi_types.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,13 @@ typedef struct nsapi_stack_api
310310
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
311311
*
312312
* @param stack Stack handle
313-
* @param socket Destination for a handle to the newly created sockey
314313
* @param server Socket handle to server to accept from
314+
* @param socket Destination for a handle to the newly created socket
315+
* @param addr Destination for the address of the remote host
316+
* @param port Destination for the port of the remote host
315317
* @return 0 on success, negative error code on failure
316318
*/
317-
int (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t *socket, nsapi_socket_t server);
319+
int (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port);
318320

319321
/** Send data over a TCP socket
320322
*

0 commit comments

Comments
 (0)