Skip to content

Commit 188b87f

Browse files
authored
[components][at_socket] 添加errno设置
1 parent 1cc0fb8 commit 188b87f

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

components/net/at/at_socket/at_socket.c

+63-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -463,9 +463,13 @@ int at_socket(int domain, int type, int protocol)
463463
enum at_socket_type socket_type;
464464

465465
/* check socket family protocol */
466-
RT_ASSERT(domain == AF_AT || domain == AF_INET);
466+
if(domain != AF_INET && domain != AF_AT)
467+
{
468+
rt_set_errno(EAFNOSUPPORT);
469+
return -1;
470+
}
467471

468-
//TODO check protocol
472+
/*TODO check protocol*/
469473

470474
switch(type)
471475
{
@@ -479,13 +483,16 @@ int at_socket(int domain, int type, int protocol)
479483

480484
default :
481485
LOG_E("Don't support socket type (%d)!", type);
486+
rt_set_errno(EPROTOTYPE);
482487
return -1;
483488
}
484489

485490
/* allocate and initialize a new AT socket */
486491
sock = alloc_socket(socket_type);
487492
if (sock == RT_NULL)
488493
{
494+
LOG_E("Failed to allocate socket");
495+
rt_set_errno(EIO);
489496
return -1;
490497
}
491498
sock->type = socket_type;
@@ -558,6 +565,7 @@ int at_closesocket(int socket)
558565
sock = at_get_socket(socket);
559566
if (sock == RT_NULL)
560567
{
568+
rt_set_errno(ENXIO);
561569
return -1;
562570
}
563571

@@ -571,6 +579,7 @@ int at_closesocket(int socket)
571579
if (sock->ops->at_closesocket(sock) != 0)
572580
{
573581
free_socket(sock);
582+
rt_set_errno(EIO);
574583
return -1;
575584
}
576585
}
@@ -587,6 +596,7 @@ int at_shutdown(int socket, int how)
587596
sock = at_get_socket(socket);
588597
if (sock == RT_NULL)
589598
{
599+
rt_set_errno(ENXIO);
590600
return -1;
591601
}
592602

@@ -600,6 +610,7 @@ int at_shutdown(int socket, int how)
600610
if (sock->ops->at_closesocket(sock) != 0)
601611
{
602612
free_socket(sock);
613+
rt_set_errno(EIO);
603614
return -1;
604615
}
605616
}
@@ -654,9 +665,16 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
654665
ip_addr_t input_ipaddr, local_ipaddr;
655666
uint16_t port = 0;
656667

668+
if (name == NULL || namelen == 0)
669+
{
670+
rt_set_errno(EINVAL);
671+
return -1;
672+
}
673+
657674
sock = at_get_socket(socket);
658675
if (sock == RT_NULL)
659676
{
677+
rt_set_errno(ENXIO);
660678
return -1;
661679
}
662680

@@ -677,20 +695,23 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
677695
/* close old socket */
678696
if (at_closesocket(socket) < 0)
679697
{
698+
rt_set_errno(EIO);
680699
return -1;
681700
}
682701

683702
extern struct at_device *at_device_get_by_ipaddr(ip_addr_t *ip_addr);
684703
new_device = at_device_get_by_ipaddr(&input_ipaddr);
685704
if (new_device == RT_NULL)
686705
{
706+
rt_set_errno(EHOSTUNREACH);
687707
return -1;
688708
}
689709

690710
/* allocate new socket */
691711
new_sock = alloc_socket_by_device(new_device, type);
692712
if (new_sock == RT_NULL)
693713
{
714+
rt_set_errno(EIO);
694715
return -1;
695716
}
696717
new_sock->type = type;
@@ -836,18 +857,21 @@ int at_listen(int socket, int backlog)
836857
sock = at_get_socket(socket);
837858
if (sock == RT_NULL)
838859
{
860+
rt_set_errno(ENXIO);
839861
return -1;
840862
}
841863

842864
if (sock->state != AT_SOCKET_OPEN)
843865
{
844866
LOG_E("Socket(%d) connect state is %d.", sock->socket, sock->state);
867+
rt_set_errno(ENETUNREACH);
845868
result = -1;
846869
goto __exit;
847870
}
848871

849872
if (sock->ops->at_listen(sock, backlog) < 0)
850873
{
874+
rt_set_errno(EIO);
851875
result = -1;
852876
goto __exit;
853877
}
@@ -856,7 +880,6 @@ int at_listen(int socket, int backlog)
856880
sock->state = AT_SOCKET_LISTEN;
857881

858882
__exit:
859-
860883
if (result < 0)
861884
{
862885
at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
@@ -874,15 +897,23 @@ int at_connect(int socket, const struct sockaddr *name, socklen_t namelen)
874897
char ipstr[16] = { 0 };
875898
int result = 0;
876899

900+
if (name == RT_NULL || namelen == 0)
901+
{
902+
rt_set_errno(EINVAL);
903+
return -1;
904+
}
905+
877906
sock = at_get_socket(socket);
878907
if (sock == RT_NULL)
879908
{
909+
rt_set_errno(ENXIO);
880910
return -1;
881911
}
882912

883913
if (sock->state != AT_SOCKET_OPEN)
884914
{
885915
LOG_E("Socket(%d) connect state is %d.", sock->socket, sock->state);
916+
rt_set_errno(EPERM);
886917
result = -1;
887918
goto __exit;
888919
}
@@ -893,14 +924,14 @@ int at_connect(int socket, const struct sockaddr *name, socklen_t namelen)
893924

894925
if (sock->ops->at_connect(sock, ipstr, remote_port, sock->type, RT_TRUE) < 0)
895926
{
927+
rt_set_errno(EIO);
896928
result = -1;
897929
goto __exit;
898930
}
899931

900932
sock->state = AT_SOCKET_CONNECT;
901933

902934
__exit:
903-
904935
if (result < 0)
905936
{
906937
at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
@@ -927,20 +958,22 @@ int at_accept(int socket, struct sockaddr *name, socklen_t *namelen)
927958
sock = at_get_socket(socket);
928959
if (sock == RT_NULL)
929960
{
961+
rt_set_errno(ENXIO);
930962
return -1;
931963
}
932964

933965
if (sock->state != AT_SOCKET_LISTEN)
934966
{
935967
LOG_E("Socket(%d) connect state is %d.", sock->socket, sock->state);
968+
rt_set_errno(EIO);
936969
result = -1;
937970
goto __exit;
938971
}
939972

940973
/* wait the receive semaphore, waiting for info */
941-
if (rt_sem_take(sock->recv_notice, RT_WAITING_FOREVER) < 0)
974+
if (rt_sem_take(sock->recv_notice, RT_WAITING_FOREVER) != RT_EOK)
942975
{
943-
errno = EAGAIN;
976+
rt_set_errno(EAGAIN);
944977
result = -1;
945978
goto __exit;
946979
}
@@ -978,12 +1011,14 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
9781011
if (mem == RT_NULL || len == 0)
9791012
{
9801013
/* if the requested number of bytes to receive from a stream socket was 0. */
981-
return 0;
1014+
rt_set_errno(EFAULT);
1015+
return -1;
9821016
}
9831017

9841018
sock = at_get_socket(socket);
9851019
if (sock == RT_NULL)
9861020
{
1021+
rt_set_errno(ENXIO);
9871022
return -1;
9881023
}
9891024

@@ -1000,8 +1035,9 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
10001035
if (sock->ops->at_connect(sock, ipstr, remote_port, sock->type, RT_TRUE) < 0)
10011036
{
10021037
at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
1038+
rt_set_errno(EIO);
10031039
/* socket shutdown */
1004-
return 0;
1040+
return -1;
10051041
}
10061042
sock->state = AT_SOCKET_CONNECT;
10071043
}
@@ -1026,14 +1062,13 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
10261062
{
10271063
at_do_event_clean(sock, AT_EVENT_RECV);
10281064
}
1029-
errno = 0;
10301065
result = recv_len;
10311066
break;
10321067
}
10331068

10341069
if (flags & MSG_DONTWAIT)
10351070
{
1036-
errno = EAGAIN;
1071+
rt_set_errno(EAGAIN);
10371072
result = -1;
10381073
break;
10391074
}
@@ -1050,7 +1085,7 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
10501085
if (rt_sem_take(sock->recv_notice, timeout) != RT_EOK)
10511086
{
10521087
LOG_D("AT socket (%d) receive timeout (%d)!", socket, timeout);
1053-
errno = EAGAIN;
1088+
rt_set_errno(EAGAIN);
10541089
result = -1;
10551090
break;
10561091
}
@@ -1077,12 +1112,14 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
10771112
if (data == RT_NULL || size == 0)
10781113
{
10791114
LOG_E("AT sendto input data or size error!");
1115+
rt_set_errno(EFAULT);
10801116
return -1;
10811117
}
10821118

10831119
sock = at_get_socket(socket);
10841120
if (sock == RT_NULL)
10851121
{
1122+
rt_set_errno(ENXIO);
10861123
return -1;
10871124
}
10881125

@@ -1091,18 +1128,21 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
10911128
case AT_SOCKET_TCP:
10921129
if (sock->state == AT_SOCKET_CLOSED)
10931130
{
1131+
/* socket passively closed, transmit function return 0 */
10941132
result = 0;
10951133
goto __exit;
10961134
}
10971135
else if (sock->state != AT_SOCKET_CONNECT && sock->state != AT_SOCKET_OPEN)
10981136
{
10991137
LOG_E("send data error, current socket (%d) state (%d) is error.", socket, sock->state);
1138+
rt_set_errno(ENETUNREACH);
11001139
result = -1;
11011140
goto __exit;
11021141
}
11031142

11041143
if ((len = sock->ops->at_send(sock, (const char *) data, size, sock->type)) < 0)
11051144
{
1145+
rt_set_errno(EIO);
11061146
result = -1;
11071147
goto __exit;
11081148
}
@@ -1120,6 +1160,7 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
11201160

11211161
if (sock->ops->at_connect(sock, ipstr, remote_port, sock->type, RT_TRUE) < 0)
11221162
{
1163+
rt_set_errno(EIO);
11231164
result = -1;
11241165
goto __exit;
11251166
}
@@ -1128,13 +1169,15 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
11281169

11291170
if ((len = sock->ops->at_send(sock, (char *) data, size, sock->type)) < 0)
11301171
{
1172+
rt_set_errno(EIO);
11311173
result = -1;
11321174
goto __exit;
11331175
}
11341176
break;
11351177

11361178
default:
11371179
LOG_E("Socket (%d) type %d is not support.", socket, sock->type);
1180+
rt_set_errno(EPERM);
11381181
result = -1;
11391182
goto __exit;
11401183
}
@@ -1166,12 +1209,14 @@ int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *o
11661209
if (optval == RT_NULL || optlen == RT_NULL)
11671210
{
11681211
LOG_E("AT getsocketopt input option value or option length error!");
1212+
rt_set_errno(EFAULT);
11691213
return -1;
11701214
}
11711215

11721216
sock = at_get_socket(socket);
11731217
if (sock == RT_NULL)
11741218
{
1219+
rt_set_errno(ENXIO);
11751220
return -1;
11761221
}
11771222

@@ -1194,12 +1239,14 @@ int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *o
11941239

11951240
default:
11961241
LOG_E("AT socket (%d) not support option name : %d.", socket, optname);
1242+
rt_set_errno(EPERM);
11971243
return -1;
11981244
}
11991245
break;
12001246

12011247
default:
12021248
LOG_E("AT socket (%d) not support option level : %d.", socket, level);
1249+
rt_set_errno(EPERM);
12031250
return -1;
12041251
}
12051252

@@ -1213,12 +1260,14 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
12131260
if (optval == RT_NULL)
12141261
{
12151262
LOG_E("AT setsockopt input option value error!");
1263+
rt_set_errno(EFAULT);
12161264
return -1;
12171265
}
12181266

12191267
sock = at_get_socket(socket);
12201268
if (sock == RT_NULL)
12211269
{
1270+
rt_set_errno(ENXIO);
12221271
return -1;
12231272
}
12241273

@@ -1239,6 +1288,7 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
12391288

12401289
default:
12411290
LOG_E("AT socket (%d) not support option name : %d.", socket, optname);
1291+
rt_set_errno(EPERM);
12421292
return -1;
12431293
}
12441294
break;
@@ -1251,6 +1301,7 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
12511301
break;
12521302
default:
12531303
LOG_E("AT socket (%d) not support option level : %d.", socket, level);
1304+
rt_set_errno(EPERM);
12541305
return -1;
12551306
}
12561307

0 commit comments

Comments
 (0)