Skip to content

Commit a1ce448

Browse files
committed
[nrf fromtree] net: sockets: Remove SET_ERRNO() macro
Macros with flow control are discouraged and generate compliance error, hence remove it and replace the corresponding code with simple errno assignments. Signed-off-by: Robert Lubos <[email protected]> (cherry picked from commit 0c1550d)
1 parent 790894d commit a1ce448

File tree

1 file changed

+130
-54
lines changed

1 file changed

+130
-54
lines changed

subsys/net/lib/sockets/sockets_inet.c

Lines changed: 130 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ LOG_MODULE_DECLARE(net_sock, CONFIG_NET_SOCKETS_LOG_LEVEL);
3434
#include "../../ip/tcp_internal.h"
3535
#include "../../ip/net_private.h"
3636

37-
#define SET_ERRNO(x) \
38-
{ int _err = x; if (_err < 0) { errno = -_err; return -1; } }
39-
4037
const struct socket_op_vtable sock_fd_op_vtable;
4138

4239
static void zsock_received_cb(struct net_context *ctx,
@@ -141,6 +138,8 @@ static int zsock_socket_internal(int family, int type, int proto)
141138

142139
int zsock_close_ctx(struct net_context *ctx)
143140
{
141+
int ret;
142+
144143
/* Reset callbacks to avoid any race conditions while
145144
* flushing queues. No need to check return values here,
146145
* as these are fail-free operations and we're closing
@@ -157,7 +156,11 @@ int zsock_close_ctx(struct net_context *ctx)
157156

158157
zsock_flush_queue(ctx);
159158

160-
SET_ERRNO(net_context_put(ctx));
159+
ret = net_context_put(ctx);
160+
if (ret < 0) {
161+
errno = -ret;
162+
return -1;
163+
}
161164

162165
return 0;
163166
}
@@ -250,36 +253,61 @@ static void zsock_received_cb(struct net_context *ctx,
250253

251254
int zsock_shutdown_ctx(struct net_context *ctx, int how)
252255
{
256+
int ret;
257+
253258
if (how == ZSOCK_SHUT_RD) {
254259
if (net_context_get_state(ctx) == NET_CONTEXT_LISTENING) {
255-
SET_ERRNO(net_context_accept(ctx, NULL, K_NO_WAIT, NULL));
260+
ret = net_context_accept(ctx, NULL, K_NO_WAIT, NULL);
261+
if (ret < 0) {
262+
errno = -ret;
263+
return -1;
264+
}
256265
} else {
257-
SET_ERRNO(net_context_recv(ctx, NULL, K_NO_WAIT, NULL));
266+
ret = net_context_recv(ctx, NULL, K_NO_WAIT, NULL);
267+
if (ret < 0) {
268+
errno = -ret;
269+
return -1;
270+
}
258271
}
259272

260273
sock_set_eof(ctx);
261274

262275
zsock_flush_queue(ctx);
263-
} else if (how == ZSOCK_SHUT_WR || how == ZSOCK_SHUT_RDWR) {
264-
SET_ERRNO(-ENOTSUP);
265-
} else {
266-
SET_ERRNO(-EINVAL);
276+
277+
return 0;
267278
}
268279

269-
return 0;
280+
if (how == ZSOCK_SHUT_WR || how == ZSOCK_SHUT_RDWR) {
281+
errno = ENOTSUP;
282+
return -1;
283+
}
284+
285+
errno = EINVAL;
286+
return -1;
270287
}
271288

272289
int zsock_bind_ctx(struct net_context *ctx, const struct sockaddr *addr,
273290
socklen_t addrlen)
274291
{
275-
SET_ERRNO(net_context_bind(ctx, addr, addrlen));
292+
int ret;
293+
294+
ret = net_context_bind(ctx, addr, addrlen);
295+
if (ret < 0) {
296+
errno = -ret;
297+
return -1;
298+
}
299+
276300
/* For DGRAM socket, we expect to receive packets after call to
277301
* bind(), but for STREAM socket, next expected operation is
278302
* listen(), which doesn't work if recv callback is set.
279303
*/
280304
if (net_context_get_type(ctx) == SOCK_DGRAM) {
281-
SET_ERRNO(net_context_recv(ctx, zsock_received_cb, K_NO_WAIT,
282-
ctx->user_data));
305+
ret = net_context_recv(ctx, zsock_received_cb, K_NO_WAIT,
306+
ctx->user_data);
307+
if (ret < 0) {
308+
errno = -ret;
309+
return -1;
310+
}
283311
}
284312

285313
return 0;
@@ -296,47 +324,75 @@ static void zsock_connected_cb(struct net_context *ctx, int status, void *user_d
296324
int zsock_connect_ctx(struct net_context *ctx, const struct sockaddr *addr,
297325
socklen_t addrlen)
298326
{
327+
k_timeout_t timeout = K_MSEC(CONFIG_NET_SOCKETS_CONNECT_TIMEOUT);
328+
net_context_connect_cb_t cb = NULL;
329+
int ret;
299330

300331
#if defined(CONFIG_SOCKS)
301332
if (net_context_is_proxy_enabled(ctx)) {
302-
SET_ERRNO(net_socks5_connect(ctx, addr, addrlen));
303-
SET_ERRNO(net_context_recv(ctx, zsock_received_cb,
304-
K_NO_WAIT, ctx->user_data));
333+
ret = net_socks5_connect(ctx, addr, addrlen);
334+
if (ret < 0) {
335+
errno = -ret;
336+
return -1;
337+
}
338+
ret = net_context_recv(ctx, zsock_received_cb,
339+
K_NO_WAIT, ctx->user_data);
340+
if (ret < 0) {
341+
errno = -ret;
342+
return -1;
343+
}
305344
return 0;
306345
}
307346
#endif
308347
if (net_context_get_state(ctx) == NET_CONTEXT_CONNECTED) {
309348
return 0;
310-
} else if (net_context_get_state(ctx) == NET_CONTEXT_CONNECTING) {
349+
}
350+
351+
if (net_context_get_state(ctx) == NET_CONTEXT_CONNECTING) {
311352
if (sock_is_error(ctx)) {
312-
SET_ERRNO(-POINTER_TO_INT(ctx->user_data));
313-
} else {
314-
SET_ERRNO(-EALREADY);
353+
errno = POINTER_TO_INT(ctx->user_data);
354+
return -1;
315355
}
316-
} else {
317-
k_timeout_t timeout = K_MSEC(CONFIG_NET_SOCKETS_CONNECT_TIMEOUT);
318-
net_context_connect_cb_t cb = NULL;
319356

320-
if (sock_is_nonblock(ctx)) {
321-
timeout = K_NO_WAIT;
322-
cb = zsock_connected_cb;
323-
}
357+
errno = EALREADY;
358+
return -1;
359+
}
324360

325-
if (net_context_get_type(ctx) == SOCK_STREAM) {
326-
/* For STREAM sockets net_context_recv() only installs
327-
* recv callback w/o side effects, and it has to be done
328-
* first to avoid race condition, when TCP stream data
329-
* arrives right after connect.
330-
*/
331-
SET_ERRNO(net_context_recv(ctx, zsock_received_cb,
332-
K_NO_WAIT, ctx->user_data));
333-
SET_ERRNO(net_context_connect(ctx, addr, addrlen, cb,
334-
timeout, ctx->user_data));
335-
} else {
336-
SET_ERRNO(net_context_connect(ctx, addr, addrlen, cb,
337-
timeout, ctx->user_data));
338-
SET_ERRNO(net_context_recv(ctx, zsock_received_cb,
339-
K_NO_WAIT, ctx->user_data));
361+
if (sock_is_nonblock(ctx)) {
362+
timeout = K_NO_WAIT;
363+
cb = zsock_connected_cb;
364+
}
365+
366+
if (net_context_get_type(ctx) == SOCK_STREAM) {
367+
/* For STREAM sockets net_context_recv() only installs
368+
* recv callback w/o side effects, and it has to be done
369+
* first to avoid race condition, when TCP stream data
370+
* arrives right after connect.
371+
*/
372+
ret = net_context_recv(ctx, zsock_received_cb,
373+
K_NO_WAIT, ctx->user_data);
374+
if (ret < 0) {
375+
errno = -ret;
376+
return -1;
377+
}
378+
ret = net_context_connect(ctx, addr, addrlen, cb,
379+
timeout, ctx->user_data);
380+
if (ret < 0) {
381+
errno = -ret;
382+
return -1;
383+
}
384+
} else {
385+
ret = net_context_connect(ctx, addr, addrlen, cb,
386+
timeout, ctx->user_data);
387+
if (ret < 0) {
388+
errno = -ret;
389+
return -1;
390+
}
391+
ret = net_context_recv(ctx, zsock_received_cb,
392+
K_NO_WAIT, ctx->user_data);
393+
if (ret < 0) {
394+
errno = -ret;
395+
return -1;
340396
}
341397
}
342398

@@ -345,8 +401,19 @@ int zsock_connect_ctx(struct net_context *ctx, const struct sockaddr *addr,
345401

346402
int zsock_listen_ctx(struct net_context *ctx, int backlog)
347403
{
348-
SET_ERRNO(net_context_listen(ctx, backlog));
349-
SET_ERRNO(net_context_accept(ctx, zsock_accepted_cb, K_NO_WAIT, ctx));
404+
int ret;
405+
406+
ret = net_context_listen(ctx, backlog);
407+
if (ret < 0) {
408+
errno = -ret;
409+
return -1;
410+
}
411+
412+
ret = net_context_accept(ctx, zsock_accepted_cb, K_NO_WAIT, ctx);
413+
if (ret < 0) {
414+
errno = -ret;
415+
return -1;
416+
}
350417

351418
return 0;
352419
}
@@ -2460,16 +2527,19 @@ int zsock_getpeername_ctx(struct net_context *ctx, struct sockaddr *addr,
24602527
socklen_t newlen = 0;
24612528

24622529
if (addr == NULL || addrlen == NULL) {
2463-
SET_ERRNO(-EINVAL);
2530+
errno = EINVAL;
2531+
return -1;
24642532
}
24652533

24662534
if (!(ctx->flags & NET_CONTEXT_REMOTE_ADDR_SET)) {
2467-
SET_ERRNO(-ENOTCONN);
2535+
errno = ENOTCONN;
2536+
return -1;
24682537
}
24692538

24702539
if (net_context_get_type(ctx) == SOCK_STREAM &&
24712540
net_context_get_state(ctx) != NET_CONTEXT_CONNECTED) {
2472-
SET_ERRNO(-ENOTCONN);
2541+
errno = ENOTCONN;
2542+
return -1;
24732543
}
24742544

24752545
if (IS_ENABLED(CONFIG_NET_IPV4) && ctx->remote.sa_family == AF_INET) {
@@ -2494,7 +2564,8 @@ int zsock_getpeername_ctx(struct net_context *ctx, struct sockaddr *addr,
24942564

24952565
memcpy(addr, &addr6, MIN(*addrlen, newlen));
24962566
} else {
2497-
SET_ERRNO(-EINVAL);
2567+
errno = EINVAL;
2568+
return -1;
24982569
}
24992570

25002571
*addrlen = newlen;
@@ -2512,7 +2583,8 @@ int zsock_getsockname_ctx(struct net_context *ctx, struct sockaddr *addr,
25122583
struct sockaddr_in addr4 = { 0 };
25132584

25142585
if (net_sin_ptr(&ctx->local)->sin_addr == NULL) {
2515-
SET_ERRNO(-EINVAL);
2586+
errno = EINVAL;
2587+
return -1;
25162588
}
25172589

25182590
newlen = sizeof(struct sockaddr_in);
@@ -2521,7 +2593,8 @@ int zsock_getsockname_ctx(struct net_context *ctx, struct sockaddr *addr,
25212593
(struct sockaddr *)&addr4,
25222594
&newlen);
25232595
if (ret < 0) {
2524-
SET_ERRNO(-ret);
2596+
errno = -ret;
2597+
return -1;
25252598
}
25262599

25272600
memcpy(addr, &addr4, MIN(*addrlen, newlen));
@@ -2530,7 +2603,8 @@ int zsock_getsockname_ctx(struct net_context *ctx, struct sockaddr *addr,
25302603
struct sockaddr_in6 addr6 = { 0 };
25312604

25322605
if (net_sin6_ptr(&ctx->local)->sin6_addr == NULL) {
2533-
SET_ERRNO(-EINVAL);
2606+
errno = EINVAL;
2607+
return -1;
25342608
}
25352609

25362610
newlen = sizeof(struct sockaddr_in6);
@@ -2539,12 +2613,14 @@ int zsock_getsockname_ctx(struct net_context *ctx, struct sockaddr *addr,
25392613
(struct sockaddr *)&addr6,
25402614
&newlen);
25412615
if (ret < 0) {
2542-
SET_ERRNO(-ret);
2616+
errno = -ret;
2617+
return -1;
25432618
}
25442619

25452620
memcpy(addr, &addr6, MIN(*addrlen, newlen));
25462621
} else {
2547-
SET_ERRNO(-EINVAL);
2622+
errno = EINVAL;
2623+
return -1;
25482624
}
25492625

25502626
*addrlen = newlen;

0 commit comments

Comments
 (0)