Skip to content

Commit ec3f437

Browse files
Code improvements for the unresolved comments
This patch contains improvements mentioned in the unresolved PR comments: - function names were changed from socket_sendmsg/socket_recvmsg to socket_sendto_control/socket_recvfrom_control. - default implementation of this functions was provided in the NetworkStack class. - MsgHeaderIterator accesses elements on the aligned addresses.
1 parent bdfd98e commit ec3f437

File tree

23 files changed

+297
-133
lines changed

23 files changed

+297
-133
lines changed

connectivity/cellular/include/cellular/framework/AT/AT_CellularStack.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ class AT_CellularStack : public NetworkStack {
8989
virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
9090

9191

92-
nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
93-
const void *data, nsapi_size_t size,
94-
nsapi_msghdr_t *control, nsapi_size_t control_size) override
92+
nsapi_size_or_error_t socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address,
93+
const void *data, nsapi_size_t size,
94+
nsapi_msghdr_t *control, nsapi_size_t control_size) override
9595
{
9696
return NSAPI_ERROR_UNSUPPORTED;
9797
}
9898

99-
nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
100-
void *data, nsapi_size_t size,
101-
nsapi_msghdr_t *control, nsapi_size_t control_size) override
99+
nsapi_size_or_error_t socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address,
100+
void *data, nsapi_size_t size,
101+
nsapi_msghdr_t *control, nsapi_size_t control_size) override
102102
{
103103
return NSAPI_ERROR_UNSUPPORTED;
104104
}

connectivity/lwipstack/include/lwipstack/LWIPStack.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,9 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
472472
* @return Number of sent bytes on success, negative error
473473
* code on failure
474474
*/
475-
nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
476-
const void *data, nsapi_size_t size,
477-
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
475+
nsapi_size_or_error_t socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address,
476+
const void *data, nsapi_size_t size,
477+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
478478

479479
/** Receive data over a TCP socket
480480
*
@@ -519,7 +519,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
519519
* This call is non-blocking. If recvfrom would block,
520520
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
521521
*
522-
* It uses socket_recvmsg with zero ancillary data.
522+
* It uses socket_recvfrom_control with zero ancillary data.
523523
* @param handle Socket handle
524524
* @param address Destination for the source address or NULL
525525
* @param buffer Destination buffer for data received from the host
@@ -547,9 +547,9 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
547547
* @return Number of received bytes on success, negative error
548548
* code on failure
549549
*/
550-
nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
551-
void *data, nsapi_size_t size,
552-
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
550+
nsapi_size_or_error_t socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address,
551+
void *data, nsapi_size_t size,
552+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
553553

554554
/** Register a callback on state change of the socket
555555
*

connectivity/lwipstack/source/LWIPStack.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,18 @@ nsapi_size_or_error_t LWIP::socket_recv(nsapi_socket_t handle, void *data, nsapi
443443

444444
nsapi_size_or_error_t LWIP::socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size)
445445
{
446-
return socket_sendmsg(handle, address, data, size, NULL, 0);
446+
return socket_sendto_control(handle, address, data, size, NULL, 0);
447447
}
448448

449449
nsapi_size_or_error_t LWIP::socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size)
450450
{
451-
return socket_recvmsg(handle, address, data, size, NULL, 0);
451+
return socket_recvfrom_control(handle, address, data, size, NULL, 0);
452452

453453
}
454454

455-
nsapi_size_or_error_t LWIP::socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
456-
void *data, nsapi_size_t size,
457-
nsapi_msghdr_t *control, nsapi_size_t control_size)
455+
nsapi_size_or_error_t LWIP::socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address, void *data,
456+
nsapi_size_t size, nsapi_msghdr_t *control,
457+
nsapi_size_t control_size)
458458
{
459459
struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle;
460460
struct netbuf *buf;
@@ -491,9 +491,9 @@ nsapi_size_or_error_t LWIP::socket_recvmsg(nsapi_socket_t handle, SocketAddress
491491
return recv;
492492
}
493493

494-
nsapi_size_or_error_t LWIP::socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
495-
const void *data, nsapi_size_t size,
496-
nsapi_msghdr_t *control, nsapi_size_t control_size)
494+
nsapi_size_or_error_t LWIP::socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address,
495+
const void *data, nsapi_size_t size, nsapi_msghdr_t *control,
496+
nsapi_size_t control_size)
497497
{
498498
struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle;
499499
ip_addr_t ip_addr = {};

connectivity/nanostack/include/nanostack-interface/Nanostack.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,16 @@ class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostac
302302
*/
303303
nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) override;
304304

305-
// FIXME: Implement
306-
nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
307-
const void *data, nsapi_size_t size,
308-
nsapi_msghdr_t *control, nsapi_size_t control_size) override
305+
nsapi_size_or_error_t socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address,
306+
const void *data, nsapi_size_t size,
307+
nsapi_msghdr_t *control, nsapi_size_t control_size) override
309308
{
310309
return NSAPI_ERROR_UNSUPPORTED;
311310
}
312311

313-
nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
314-
void *data, nsapi_size_t size,
315-
nsapi_msghdr_t *control, nsapi_size_t control_size) override
312+
nsapi_size_or_error_t socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address,
313+
void *data, nsapi_size_t size,
314+
nsapi_msghdr_t *control, nsapi_size_t control_size) override
316315
{
317316
return NSAPI_ERROR_UNSUPPORTED;
318317
}

connectivity/netsocket/include/netsocket/CellularNonIPSocket.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ class CellularNonIPSocket : public Socket {
133133
nsapi_size_or_error_t recvfrom(SocketAddress *address,
134134
void *data, nsapi_size_t size) override;
135135
/// NOT APPLICABLE
136-
nsapi_size_or_error_t sendmsg(const SocketAddress &address,
137-
const void *data, nsapi_size_t size,
138-
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
136+
nsapi_size_or_error_t sendto_control(const SocketAddress &address,
137+
const void *data, nsapi_size_t size,
138+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
139139
/// NOT APPLICABLE
140-
nsapi_size_or_error_t recvmsg(SocketAddress *address,
141-
void *data, nsapi_size_t size,
142-
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
140+
nsapi_size_or_error_t recvfrom_control(SocketAddress *address,
141+
void *data, nsapi_size_t size,
142+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
143143
/// NOT APPLICABLE
144144
nsapi_error_t bind(const SocketAddress &address) override;
145145

connectivity/netsocket/include/netsocket/InternetDatagramSocket.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class InternetDatagramSocket : public InternetSocket {
3636
* nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
3737
* immediately.
3838
*
39-
* It uses sendmsg with zero ancillary data
39+
* It uses sendto_control with zero ancillary data
4040
* @param address The SocketAddress of the remote host.
4141
* @param data Buffer of data to send to the host.
4242
* @param size Size of the buffer in bytes.
@@ -61,7 +61,7 @@ class InternetDatagramSocket : public InternetSocket {
6161
* are accepted.
6262
*
6363
* @note recvfrom() is allowed write to address and data buffers even if error occurs.
64-
* It uses recvmsg with zero ancillary data
64+
* It uses recvfrom_control with zero ancillary data
6565
* @param address Destination for the source address or NULL.
6666
* @param data Destination buffer for RAW data to be received from the host.
6767
* @param size Size of the buffer in bytes.
@@ -81,7 +81,7 @@ class InternetDatagramSocket : public InternetSocket {
8181
* nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
8282
* immediately.
8383
*
84-
* It uses sendmsg with zero ancillary data
84+
* It uses sendto_control with zero ancillary data
8585
* @param address The SocketAddress of the remote host.
8686
* @param data Buffer of data to send to the host.
8787
* @param size Size of the buffer in bytes.
@@ -93,9 +93,9 @@ class InternetDatagramSocket : public InternetSocket {
9393
* @retval int Other negative error codes for stack-related failures.
9494
* See \ref NetworkStack::socket_send.
9595
*/
96-
nsapi_size_or_error_t sendmsg(const SocketAddress &address,
97-
const void *data, nsapi_size_t size,
98-
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
96+
nsapi_size_or_error_t sendto_control(const SocketAddress &address,
97+
const void *data, nsapi_size_t size,
98+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
9999

100100

101101
/** Receive a datagram with ancillary data and store the source address in address if it's not NULL.
@@ -109,7 +109,7 @@ class InternetDatagramSocket : public InternetSocket {
109109
* @note If socket is connected, only packets coming from connected peer address
110110
* are accepted.
111111
*
112-
* @note recvmsg() is allowed write to address and data buffers even if error occurs.
112+
* @note recvfrom_control() is allowed write to address and data buffers even if error occurs.
113113
*
114114
* @param address Destination for the source address or NULL.
115115
* @param data Destination buffer for RAW data to be received from the host.
@@ -123,9 +123,9 @@ class InternetDatagramSocket : public InternetSocket {
123123
* @retval int Other negative error codes for stack-related failures.
124124
* See \ref NetworkStack::socket_recv.
125125
*/
126-
nsapi_size_or_error_t recvmsg(SocketAddress *address,
127-
void *data, nsapi_size_t size,
128-
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
126+
nsapi_size_or_error_t recvfrom_control(SocketAddress *address,
127+
void *data, nsapi_size_t size,
128+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
129129

130130
/** Set the remote address for next send() call and filtering
131131
* of incoming packets. To reset the address, zero initialized

connectivity/netsocket/include/netsocket/MsgHeader.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@
2020

2121
#include "netsocket/nsapi_types.h"
2222

23+
/**
24+
* Allows iteration through the list of message headers received in the control parameter of the
25+
* socket_sendto_control / socket_recvfrom_control methods.
26+
*/
27+
2328
struct MsgHeaderIterator {
29+
// Constructor takes pointer to the first header element and size of the whole list.
2430
MsgHeaderIterator(nsapi_msghdr_t *hdr, nsapi_size_t size) :
2531
start(hdr),
2632
current(nullptr),
2733
size(size)
2834
{}
2935

36+
// Checks if the next address of the iterator is a valid list member.
3037
bool has_next()
3138
{
3239
if (current == nullptr) {
33-
if (start != nullptr) {
40+
if (start != nullptr && start->len <= size && start->len >= sizeof(*start)) {
3441
return true;
3542
} else {
3643
return false;
@@ -41,13 +48,15 @@ struct MsgHeaderIterator {
4148
return false;
4249
}
4350

44-
if ((reinterpret_cast<uint8_t *>(current) + current->len) >= (reinterpret_cast<uint8_t *>(start) + size)) {
51+
if (get_next_aligned_addr() >= (reinterpret_cast<uint8_t *>(start) + size)) {
4552
return false;
4653
}
4754

4855
return true;
4956
}
5057

58+
// Returns pointer to the next member of the list.
59+
// If next member doesn't exist nullptr is returned.
5160
nsapi_msghdr_t *next()
5261
{
5362
if (!has_next()) {
@@ -57,13 +66,29 @@ struct MsgHeaderIterator {
5766
if (current == nullptr) {
5867
current = start;
5968
} else {
60-
current = reinterpret_cast<nsapi_msghdr_t *>(reinterpret_cast<uint8_t *>(current) + current->len);
69+
current = reinterpret_cast<nsapi_msghdr *>(get_next_aligned_addr());
6170
}
6271

6372
return current;
6473
}
6574

6675
private:
76+
// Get address of the next member aligned to the size of msghdr_t.
77+
void *get_next_aligned_addr()
78+
{
79+
size_t remaining_size = size - (reinterpret_cast<uintptr_t>(current) - reinterpret_cast<uintptr_t>(start));
80+
void *next = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(current) + current->len);
81+
82+
next = std::align(
83+
alignof(nsapi_msghdr_t),
84+
sizeof(nsapi_msghdr_t),
85+
next,
86+
remaining_size
87+
);
88+
89+
return next;
90+
}
91+
6792
nsapi_msghdr_t *start;
6893
nsapi_msghdr_t *current;
6994
nsapi_size_t size;

connectivity/netsocket/include/netsocket/NetworkStack.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,16 @@ class NetworkStack : public DNS {
412412
* @return Number of sent bytes on success, negative error
413413
* code on failure
414414
*/
415-
virtual nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
416-
const void *data, nsapi_size_t size,
417-
nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
415+
virtual nsapi_size_or_error_t socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address,
416+
const void *data, nsapi_size_t size,
417+
nsapi_msghdr_t *control, nsapi_size_t control_size)
418+
{
419+
if (control != NULL) {
420+
return NSAPI_ERROR_UNSUPPORTED;
421+
}
422+
423+
return socket_sendto(handle, address, data, size);
424+
}
418425

419426
/** Receive a packet with ancillary data over a UDP socket
420427
*
@@ -436,9 +443,16 @@ class NetworkStack : public DNS {
436443
* @return Number of received bytes on success, negative error
437444
* code on failure
438445
*/
439-
virtual nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
440-
void *data, nsapi_size_t size,
441-
nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
446+
virtual nsapi_size_or_error_t socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address,
447+
void *data, nsapi_size_t size,
448+
nsapi_msghdr_t *control, nsapi_size_t control_size)
449+
{
450+
if (control != NULL) {
451+
return NSAPI_ERROR_UNSUPPORTED;
452+
}
453+
454+
return socket_recvfrom(handle, address, data, size);
455+
}
442456

443457
/** Register a callback on state change of the socket
444458
*

connectivity/netsocket/include/netsocket/Socket.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Socket {
159159

160160
/** Send a message on a socket.
161161
*
162-
* The sendmsg() function sends a message through a connection-mode or connectionless-mode socket.
162+
* The sendto_control() function sends a message through a connection-mode or connectionless-mode socket.
163163
* If the socket is a connectionless-mode socket, the message is sent to the address specified.
164164
* If the socket is a connected-mode socket, address is ignored.
165165
*
@@ -175,9 +175,9 @@ class Socket {
175175
* @return Number of sent bytes on success, negative subclass-dependent error
176176
* code on failure
177177
*/
178-
virtual nsapi_size_or_error_t sendmsg(const SocketAddress &address,
179-
const void *data, nsapi_size_t size,
180-
nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
178+
virtual nsapi_size_or_error_t sendto_control(const SocketAddress &address,
179+
const void *data, nsapi_size_t size,
180+
nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
181181

182182

183183
/** Receive a data from a socket
@@ -190,7 +190,7 @@ class Socket {
190190
*
191191
* Additional information related to the message can be retrieved with the control data.
192192
*
193-
* @note recvmsg() is allowed write to address and data buffers even if error occurs.
193+
* @note recvfrom_control() is allowed write to address and data buffers even if error occurs.
194194
*
195195
* By default, recvfrom blocks until a datagram is received. If socket is set to
196196
* non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK
@@ -202,9 +202,9 @@ class Socket {
202202
* @return Number of received bytes on success, negative subclass-dependent
203203
* error code on failure
204204
*/
205-
virtual nsapi_size_or_error_t recvmsg(SocketAddress *address,
206-
void *data, nsapi_size_t size,
207-
nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
205+
virtual nsapi_size_or_error_t recvfrom_control(SocketAddress *address,
206+
void *data, nsapi_size_t size,
207+
nsapi_msghdr_t *control, nsapi_size_t control_size) = 0;
208208

209209
/** Bind a specific address to a socket.
210210
*

0 commit comments

Comments
 (0)