Skip to content

Commit 1ca9ccd

Browse files
author
Seppo Takalo
committed
Filter incoming UDP packets based on connected peer address.
1 parent e7cb475 commit 1ca9ccd

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

features/netsocket/Socket.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ class Socket {
4242
*/
4343
virtual nsapi_error_t close() = 0;
4444

45-
/** Connects socket to a remote host.
45+
/** Connects socket to a remote address.
4646
*
47-
* Initiates a connection to a remote server specified by the
48-
* indicated address. In case of connectionless protocol, set
49-
* the remote address for next send() call.
47+
* Attempt to make connection on connection-mode protocol or set or reset
48+
* the peer address on connectionless protocol.
5049
*
51-
* @param address The SocketAddress of the remote host
50+
* Also connectionless protocols use the connected address to filter
51+
* incoming packets for recv() and recvfrom() calls.
52+
*
53+
* To reset the peer address, zero initialised(default constructor) SocketAddress
54+
* object have to be in the address parameter.
55+
*
56+
* @param address The SocketAddress of the remote peer
5257
* @return 0 on success, negative error code on failure
5358
*/
5459
virtual nsapi_error_t connect(const SocketAddress &address) = 0;
@@ -75,6 +80,11 @@ class Socket {
7580
* Receive data from connected socket or in case of connectionless socket
7681
* this is equivalent of calling recvfrom(NULL, data, size).
7782
*
83+
* If socket is connected, only packets coming from connected peer address
84+
* are accepted.
85+
*
86+
* @note recv() is allowed write to data buffer even if error occurs.
87+
*
7888
* By default, recv blocks until some data is received. If socket is set to
7989
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
8090
* indicate no data.
@@ -112,6 +122,11 @@ class Socket {
112122
* Receives a data and stores the source address in address if address
113123
* is not NULL. Returns the number of bytes written into the buffer.
114124
*
125+
* If socket is connected, only packets coming from connected peer address
126+
* are accepted.
127+
*
128+
* @note recvfrom() is allowed write to address and data buffers even if error occurs.
129+
*
115130
* By default, recvfrom blocks until a datagram is received. If socket is set to
116131
* non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK
117132
* is returned.

features/netsocket/UDPSocket.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
122122

123123
_pending = 0;
124124
nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
125+
126+
// Filter incomming packets using connected peer address
127+
if (recv >= 0 && _remote_peer && _remote_peer == *address) {
128+
continue;
129+
}
130+
131+
// Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK
125132
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
126133
ret = recv;
127134
break;

features/netsocket/UDPSocket.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class UDPSocket : public InternetSocket {
9999
* is not NULL. Returns the number of bytes written into the buffer. If the
100100
* datagram is larger than the buffer, the excess data is silently discarded.
101101
*
102+
* If socket is connected, only packets coming from connected peer address
103+
* are accepted.
104+
*
105+
* @note recvfrom() is allowed write to address and data buffers even if error occurs.
106+
*
102107
* By default, recvfrom blocks until a datagram is received. If socket is set to
103108
* non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
104109
* is returned.
@@ -114,7 +119,9 @@ class UDPSocket : public InternetSocket {
114119

115120
/** Set remote peer address
116121
*
117-
* Set the remote address for next send() call.
122+
* Set the remote address for next send() call and filtering
123+
* for incomming packets. To reset the address, zero initialised
124+
* SocketAddress must be in the address parameter.
118125
*
119126
* @param address The SocketAddress of the remote host
120127
* @return 0 on success, negative error code on failure
@@ -141,6 +148,11 @@ class UDPSocket : public InternetSocket {
141148
*
142149
* This is equivalent of calling recvfrom(NULL, data, size).
143150
*
151+
* If socket is connected, only packets coming from connected peer address
152+
* are accepted.
153+
*
154+
* @note recv() is allowed write to data buffer even if error occurs.
155+
*
144156
* By default, recv blocks until some data is received. If socket is set to
145157
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
146158
* indicate no data.

0 commit comments

Comments
 (0)