Skip to content

Commit da69629

Browse files
authored
Merge pull request #2434 from infinnovation/socket-accept-address
network-socket: Optionally return peer address from accept().
2 parents c9426e7 + 9180796 commit da69629

File tree

8 files changed

+32
-13
lines changed

8 files changed

+32
-13
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
@@ -293,7 +293,7 @@ static int lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsap
293293
return lwip_err_remap(err);
294294
}
295295

296-
static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_socket_t server)
296+
static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port)
297297
{
298298
struct lwip_socket *s = (struct lwip_socket *)server;
299299
struct lwip_socket *ns = lwip_arena_alloc();
@@ -305,6 +305,10 @@ static int lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t *handle, nsap
305305
}
306306

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

features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ int NanostackInterface::socket_connect(void *handle, const SocketAddress &addr)
846846
return ret;
847847
}
848848

849-
int NanostackInterface::socket_accept(void **handle, void *server)
849+
int NanostackInterface::socket_accept(void *server, void **handle, SocketAddress *address)
850850
{
851851
return NSAPI_ERROR_UNSUPPORTED;
852852
}

features/net/FEATURE_IPV6/nanostack-interface/NanostackInterface.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ class NanostackInterface : public NetworkStack {
9595
* This call is non-blocking. If accept would block,
9696
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
9797
*
98-
* @param handle Destination for a handle to the newly created sockey
9998
* @param server Socket handle to server to accept from
99+
* @param handle Destination for a handle to the newly created socket
100+
* @param address Destination for the remote address or NULL
100101
* @return 0 on success, negative error code on failure
101102
*/
102-
virtual int socket_accept(void **handle, void *server);
103+
virtual int socket_accept(void *handle, void **server, SocketAddress *address);
103104

104105
/** Send data over a TCP socket
105106
*

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 server, nsapi_socket_t *socket, 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
164163
* @param server Socket handle to server to accept from
164+
* @param handle Destination for a handle to the newly created socket
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 server, nsapi_socket_t *handle, 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)