1
1
/*
2
- * Copyright (c) 2006-2021, RT-Thread Development Team
2
+ * Copyright (c) 2006-2024 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
@@ -463,9 +463,13 @@ int at_socket(int domain, int type, int protocol)
463
463
enum at_socket_type socket_type ;
464
464
465
465
/* 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
+ }
467
471
468
- // TODO check protocol
472
+ /* TODO check protocol*/
469
473
470
474
switch (type )
471
475
{
@@ -479,13 +483,16 @@ int at_socket(int domain, int type, int protocol)
479
483
480
484
default :
481
485
LOG_E ("Don't support socket type (%d)!" , type );
486
+ rt_set_errno (EPROTOTYPE );
482
487
return -1 ;
483
488
}
484
489
485
490
/* allocate and initialize a new AT socket */
486
491
sock = alloc_socket (socket_type );
487
492
if (sock == RT_NULL )
488
493
{
494
+ LOG_E ("Failed to allocate socket" );
495
+ rt_set_errno (EIO );
489
496
return -1 ;
490
497
}
491
498
sock -> type = socket_type ;
@@ -558,6 +565,7 @@ int at_closesocket(int socket)
558
565
sock = at_get_socket (socket );
559
566
if (sock == RT_NULL )
560
567
{
568
+ rt_set_errno (ENXIO );
561
569
return -1 ;
562
570
}
563
571
@@ -571,6 +579,7 @@ int at_closesocket(int socket)
571
579
if (sock -> ops -> at_closesocket (sock ) != 0 )
572
580
{
573
581
free_socket (sock );
582
+ rt_set_errno (EIO );
574
583
return -1 ;
575
584
}
576
585
}
@@ -587,6 +596,7 @@ int at_shutdown(int socket, int how)
587
596
sock = at_get_socket (socket );
588
597
if (sock == RT_NULL )
589
598
{
599
+ rt_set_errno (ENXIO );
590
600
return -1 ;
591
601
}
592
602
@@ -600,6 +610,7 @@ int at_shutdown(int socket, int how)
600
610
if (sock -> ops -> at_closesocket (sock ) != 0 )
601
611
{
602
612
free_socket (sock );
613
+ rt_set_errno (EIO );
603
614
return -1 ;
604
615
}
605
616
}
@@ -654,9 +665,16 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
654
665
ip_addr_t input_ipaddr , local_ipaddr ;
655
666
uint16_t port = 0 ;
656
667
668
+ if (name == NULL || namelen == 0 )
669
+ {
670
+ rt_set_errno (EINVAL );
671
+ return -1 ;
672
+ }
673
+
657
674
sock = at_get_socket (socket );
658
675
if (sock == RT_NULL )
659
676
{
677
+ rt_set_errno (ENXIO );
660
678
return -1 ;
661
679
}
662
680
@@ -677,20 +695,23 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
677
695
/* close old socket */
678
696
if (at_closesocket (socket ) < 0 )
679
697
{
698
+ rt_set_errno (EIO );
680
699
return -1 ;
681
700
}
682
701
683
702
extern struct at_device * at_device_get_by_ipaddr (ip_addr_t * ip_addr );
684
703
new_device = at_device_get_by_ipaddr (& input_ipaddr );
685
704
if (new_device == RT_NULL )
686
705
{
706
+ rt_set_errno (EHOSTUNREACH );
687
707
return -1 ;
688
708
}
689
709
690
710
/* allocate new socket */
691
711
new_sock = alloc_socket_by_device (new_device , type );
692
712
if (new_sock == RT_NULL )
693
713
{
714
+ rt_set_errno (EIO );
694
715
return -1 ;
695
716
}
696
717
new_sock -> type = type ;
@@ -836,18 +857,21 @@ int at_listen(int socket, int backlog)
836
857
sock = at_get_socket (socket );
837
858
if (sock == RT_NULL )
838
859
{
860
+ rt_set_errno (ENXIO );
839
861
return -1 ;
840
862
}
841
863
842
864
if (sock -> state != AT_SOCKET_OPEN )
843
865
{
844
866
LOG_E ("Socket(%d) connect state is %d." , sock -> socket , sock -> state );
867
+ rt_set_errno (ENETUNREACH );
845
868
result = -1 ;
846
869
goto __exit ;
847
870
}
848
871
849
872
if (sock -> ops -> at_listen (sock , backlog ) < 0 )
850
873
{
874
+ rt_set_errno (EIO );
851
875
result = -1 ;
852
876
goto __exit ;
853
877
}
@@ -856,7 +880,6 @@ int at_listen(int socket, int backlog)
856
880
sock -> state = AT_SOCKET_LISTEN ;
857
881
858
882
__exit :
859
-
860
883
if (result < 0 )
861
884
{
862
885
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)
874
897
char ipstr [16 ] = { 0 };
875
898
int result = 0 ;
876
899
900
+ if (name == RT_NULL || namelen == 0 )
901
+ {
902
+ rt_set_errno (EINVAL );
903
+ return -1 ;
904
+ }
905
+
877
906
sock = at_get_socket (socket );
878
907
if (sock == RT_NULL )
879
908
{
909
+ rt_set_errno (ENXIO );
880
910
return -1 ;
881
911
}
882
912
883
913
if (sock -> state != AT_SOCKET_OPEN )
884
914
{
885
915
LOG_E ("Socket(%d) connect state is %d." , sock -> socket , sock -> state );
916
+ rt_set_errno (EPERM );
886
917
result = -1 ;
887
918
goto __exit ;
888
919
}
@@ -893,14 +924,14 @@ int at_connect(int socket, const struct sockaddr *name, socklen_t namelen)
893
924
894
925
if (sock -> ops -> at_connect (sock , ipstr , remote_port , sock -> type , RT_TRUE ) < 0 )
895
926
{
927
+ rt_set_errno (EIO );
896
928
result = -1 ;
897
929
goto __exit ;
898
930
}
899
931
900
932
sock -> state = AT_SOCKET_CONNECT ;
901
933
902
934
__exit :
903
-
904
935
if (result < 0 )
905
936
{
906
937
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)
927
958
sock = at_get_socket (socket );
928
959
if (sock == RT_NULL )
929
960
{
961
+ rt_set_errno (ENXIO );
930
962
return -1 ;
931
963
}
932
964
933
965
if (sock -> state != AT_SOCKET_LISTEN )
934
966
{
935
967
LOG_E ("Socket(%d) connect state is %d." , sock -> socket , sock -> state );
968
+ rt_set_errno (EIO );
936
969
result = -1 ;
937
970
goto __exit ;
938
971
}
939
972
940
973
/* 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 )
942
975
{
943
- errno = EAGAIN ;
976
+ rt_set_errno ( EAGAIN ) ;
944
977
result = -1 ;
945
978
goto __exit ;
946
979
}
@@ -978,12 +1011,14 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
978
1011
if (mem == RT_NULL || len == 0 )
979
1012
{
980
1013
/* 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 ;
982
1016
}
983
1017
984
1018
sock = at_get_socket (socket );
985
1019
if (sock == RT_NULL )
986
1020
{
1021
+ rt_set_errno (ENXIO );
987
1022
return -1 ;
988
1023
}
989
1024
@@ -1000,8 +1035,9 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
1000
1035
if (sock -> ops -> at_connect (sock , ipstr , remote_port , sock -> type , RT_TRUE ) < 0 )
1001
1036
{
1002
1037
at_do_event_changes (sock , AT_EVENT_ERROR , RT_TRUE );
1038
+ rt_set_errno (EIO );
1003
1039
/* socket shutdown */
1004
- return 0 ;
1040
+ return -1 ;
1005
1041
}
1006
1042
sock -> state = AT_SOCKET_CONNECT ;
1007
1043
}
@@ -1026,14 +1062,13 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
1026
1062
{
1027
1063
at_do_event_clean (sock , AT_EVENT_RECV );
1028
1064
}
1029
- errno = 0 ;
1030
1065
result = recv_len ;
1031
1066
break ;
1032
1067
}
1033
1068
1034
1069
if (flags & MSG_DONTWAIT )
1035
1070
{
1036
- errno = EAGAIN ;
1071
+ rt_set_errno ( EAGAIN ) ;
1037
1072
result = -1 ;
1038
1073
break ;
1039
1074
}
@@ -1050,7 +1085,7 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
1050
1085
if (rt_sem_take (sock -> recv_notice , timeout ) != RT_EOK )
1051
1086
{
1052
1087
LOG_D ("AT socket (%d) receive timeout (%d)!" , socket , timeout );
1053
- errno = EAGAIN ;
1088
+ rt_set_errno ( EAGAIN ) ;
1054
1089
result = -1 ;
1055
1090
break ;
1056
1091
}
@@ -1077,12 +1112,14 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
1077
1112
if (data == RT_NULL || size == 0 )
1078
1113
{
1079
1114
LOG_E ("AT sendto input data or size error!" );
1115
+ rt_set_errno (EFAULT );
1080
1116
return -1 ;
1081
1117
}
1082
1118
1083
1119
sock = at_get_socket (socket );
1084
1120
if (sock == RT_NULL )
1085
1121
{
1122
+ rt_set_errno (ENXIO );
1086
1123
return -1 ;
1087
1124
}
1088
1125
@@ -1091,18 +1128,21 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
1091
1128
case AT_SOCKET_TCP :
1092
1129
if (sock -> state == AT_SOCKET_CLOSED )
1093
1130
{
1131
+ /* socket passively closed, transmit function return 0 */
1094
1132
result = 0 ;
1095
1133
goto __exit ;
1096
1134
}
1097
1135
else if (sock -> state != AT_SOCKET_CONNECT && sock -> state != AT_SOCKET_OPEN )
1098
1136
{
1099
1137
LOG_E ("send data error, current socket (%d) state (%d) is error." , socket , sock -> state );
1138
+ rt_set_errno (ENETUNREACH );
1100
1139
result = -1 ;
1101
1140
goto __exit ;
1102
1141
}
1103
1142
1104
1143
if ((len = sock -> ops -> at_send (sock , (const char * ) data , size , sock -> type )) < 0 )
1105
1144
{
1145
+ rt_set_errno (EIO );
1106
1146
result = -1 ;
1107
1147
goto __exit ;
1108
1148
}
@@ -1120,6 +1160,7 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
1120
1160
1121
1161
if (sock -> ops -> at_connect (sock , ipstr , remote_port , sock -> type , RT_TRUE ) < 0 )
1122
1162
{
1163
+ rt_set_errno (EIO );
1123
1164
result = -1 ;
1124
1165
goto __exit ;
1125
1166
}
@@ -1128,13 +1169,15 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
1128
1169
1129
1170
if ((len = sock -> ops -> at_send (sock , (char * ) data , size , sock -> type )) < 0 )
1130
1171
{
1172
+ rt_set_errno (EIO );
1131
1173
result = -1 ;
1132
1174
goto __exit ;
1133
1175
}
1134
1176
break ;
1135
1177
1136
1178
default :
1137
1179
LOG_E ("Socket (%d) type %d is not support." , socket , sock -> type );
1180
+ rt_set_errno (EPERM );
1138
1181
result = -1 ;
1139
1182
goto __exit ;
1140
1183
}
@@ -1166,12 +1209,14 @@ int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *o
1166
1209
if (optval == RT_NULL || optlen == RT_NULL )
1167
1210
{
1168
1211
LOG_E ("AT getsocketopt input option value or option length error!" );
1212
+ rt_set_errno (EFAULT );
1169
1213
return -1 ;
1170
1214
}
1171
1215
1172
1216
sock = at_get_socket (socket );
1173
1217
if (sock == RT_NULL )
1174
1218
{
1219
+ rt_set_errno (ENXIO );
1175
1220
return -1 ;
1176
1221
}
1177
1222
@@ -1194,12 +1239,14 @@ int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *o
1194
1239
1195
1240
default :
1196
1241
LOG_E ("AT socket (%d) not support option name : %d." , socket , optname );
1242
+ rt_set_errno (EPERM );
1197
1243
return -1 ;
1198
1244
}
1199
1245
break ;
1200
1246
1201
1247
default :
1202
1248
LOG_E ("AT socket (%d) not support option level : %d." , socket , level );
1249
+ rt_set_errno (EPERM );
1203
1250
return -1 ;
1204
1251
}
1205
1252
@@ -1213,12 +1260,14 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
1213
1260
if (optval == RT_NULL )
1214
1261
{
1215
1262
LOG_E ("AT setsockopt input option value error!" );
1263
+ rt_set_errno (EFAULT );
1216
1264
return -1 ;
1217
1265
}
1218
1266
1219
1267
sock = at_get_socket (socket );
1220
1268
if (sock == RT_NULL )
1221
1269
{
1270
+ rt_set_errno (ENXIO );
1222
1271
return -1 ;
1223
1272
}
1224
1273
@@ -1239,6 +1288,7 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
1239
1288
1240
1289
default :
1241
1290
LOG_E ("AT socket (%d) not support option name : %d." , socket , optname );
1291
+ rt_set_errno (EPERM );
1242
1292
return -1 ;
1243
1293
}
1244
1294
break ;
@@ -1251,6 +1301,7 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
1251
1301
break ;
1252
1302
default :
1253
1303
LOG_E ("AT socket (%d) not support option level : %d." , socket , level );
1304
+ rt_set_errno (EPERM );
1254
1305
return -1 ;
1255
1306
}
1256
1307
0 commit comments