Filter incoming UDP packets based on connected peer address.

pull/7192/head
Seppo Takalo 2018-06-20 11:42:17 +03:00
parent e7cb4752fa
commit 1ca9ccda73
3 changed files with 40 additions and 6 deletions

View File

@ -42,13 +42,18 @@ public:
*/
virtual nsapi_error_t close() = 0;
/** Connects socket to a remote host.
/** Connects socket to a remote address.
*
* Initiates a connection to a remote server specified by the
* indicated address. In case of connectionless protocol, set
* the remote address for next send() call.
* Attempt to make connection on connection-mode protocol or set or reset
* the peer address on connectionless protocol.
*
* @param address The SocketAddress of the remote host
* Also connectionless protocols use the connected address to filter
* incoming packets for recv() and recvfrom() calls.
*
* To reset the peer address, zero initialised(default constructor) SocketAddress
* object have to be in the address parameter.
*
* @param address The SocketAddress of the remote peer
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t connect(const SocketAddress &address) = 0;
@ -75,6 +80,11 @@ public:
* Receive data from connected socket or in case of connectionless socket
* this is equivalent of calling recvfrom(NULL, data, size).
*
* If socket is connected, only packets coming from connected peer address
* are accepted.
*
* @note recv() is allowed write to data buffer even if error occurs.
*
* By default, recv blocks until some data is received. If socket is set to
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
* indicate no data.
@ -112,6 +122,11 @@ public:
* Receives a data and stores the source address in address if address
* is not NULL. Returns the number of bytes written into the buffer.
*
* If socket is connected, only packets coming from connected peer address
* are accepted.
*
* @note recvfrom() is allowed write to address and data buffers even if error occurs.
*
* By default, recvfrom blocks until a datagram is received. If socket is set to
* non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK
* is returned.

View File

@ -122,6 +122,13 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
_pending = 0;
nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
// Filter incomming packets using connected peer address
if (recv >= 0 && _remote_peer && _remote_peer == *address) {
continue;
}
// Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
ret = recv;
break;

View File

@ -99,6 +99,11 @@ public:
* is not NULL. Returns the number of bytes written into the buffer. If the
* datagram is larger than the buffer, the excess data is silently discarded.
*
* If socket is connected, only packets coming from connected peer address
* are accepted.
*
* @note recvfrom() is allowed write to address and data buffers even if error occurs.
*
* By default, recvfrom blocks until a datagram is received. If socket is set to
* non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
* is returned.
@ -114,7 +119,9 @@ public:
/** Set remote peer address
*
* Set the remote address for next send() call.
* Set the remote address for next send() call and filtering
* for incomming packets. To reset the address, zero initialised
* SocketAddress must be in the address parameter.
*
* @param address The SocketAddress of the remote host
* @return 0 on success, negative error code on failure
@ -141,6 +148,11 @@ public:
*
* This is equivalent of calling recvfrom(NULL, data, size).
*
* If socket is connected, only packets coming from connected peer address
* are accepted.
*
* @note recv() is allowed write to data buffer even if error occurs.
*
* By default, recv blocks until some data is received. If socket is set to
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
* indicate no data.