Skip to content

network-socket: Optionally return peer address from accept(). #2434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this is a better argument order. And now is the best time for the change.

{
struct lwip_socket *s = (struct lwip_socket *)server;
struct lwip_socket *ns = lwip_arena_alloc();
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
14 changes: 12 additions & 2 deletions features/net/network-socket/NetworkStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions features/net/network-socket/NetworkStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions features/net/network-socket/TCPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion features/net/network-socket/TCPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions features/net/network-socket/nsapi_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down