47
47
#define INT_IP6 "fd00::2"
48
48
#define INT_PORT 8008
49
49
50
- #define IO_TIMEOUT_SEC 3
51
-
52
50
enum server {
53
51
SERVER_A = 0 ,
54
52
SERVER_B = 1 ,
@@ -108,46 +106,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog)
108
106
return 0 ;
109
107
}
110
108
111
- static socklen_t inetaddr_len (const struct sockaddr_storage * addr )
112
- {
113
- return (addr -> ss_family == AF_INET ? sizeof (struct sockaddr_in ) :
114
- addr -> ss_family == AF_INET6 ? sizeof (struct sockaddr_in6 ) : 0 );
115
- }
116
-
117
- static int make_socket (int sotype , const char * ip , int port ,
118
- struct sockaddr_storage * addr )
119
- {
120
- struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC };
121
- int err , family , fd ;
122
-
123
- family = is_ipv6 (ip ) ? AF_INET6 : AF_INET ;
124
- err = make_sockaddr (family , ip , port , addr , NULL );
125
- if (CHECK (err , "make_address" , "failed\n" ))
126
- return -1 ;
127
-
128
- fd = socket (addr -> ss_family , sotype , 0 );
129
- if (CHECK (fd < 0 , "socket" , "failed\n" )) {
130
- log_err ("failed to make socket" );
131
- return -1 ;
132
- }
133
-
134
- err = setsockopt (fd , SOL_SOCKET , SO_SNDTIMEO , & timeo , sizeof (timeo ));
135
- if (CHECK (err , "setsockopt(SO_SNDTIMEO)" , "failed\n" )) {
136
- log_err ("failed to set SNDTIMEO" );
137
- close (fd );
138
- return -1 ;
139
- }
140
-
141
- err = setsockopt (fd , SOL_SOCKET , SO_RCVTIMEO , & timeo , sizeof (timeo ));
142
- if (CHECK (err , "setsockopt(SO_RCVTIMEO)" , "failed\n" )) {
143
- log_err ("failed to set RCVTIMEO" );
144
- close (fd );
145
- return -1 ;
146
- }
147
-
148
- return fd ;
149
- }
150
-
151
109
static int setsockopts (int fd , void * opts )
152
110
{
153
111
struct cb_opts * co = (struct cb_opts * )opts ;
@@ -229,27 +187,6 @@ static int make_server(int sotype, const char *ip, int port,
229
187
return -1 ;
230
188
}
231
189
232
- static int make_client (int sotype , const char * ip , int port )
233
- {
234
- struct sockaddr_storage addr = {0 };
235
- int err , fd ;
236
-
237
- fd = make_socket (sotype , ip , port , & addr );
238
- if (fd < 0 )
239
- return -1 ;
240
-
241
- err = connect (fd , (void * )& addr , inetaddr_len (& addr ));
242
- if (CHECK (err , "make_client" , "connect" )) {
243
- log_err ("failed to connect client socket" );
244
- goto fail ;
245
- }
246
-
247
- return fd ;
248
- fail :
249
- close (fd );
250
- return -1 ;
251
- }
252
-
253
190
static __u64 socket_cookie (int fd )
254
191
{
255
192
__u64 cookie ;
@@ -646,8 +583,9 @@ static void run_lookup_prog(const struct test *t)
646
583
goto close ;
647
584
}
648
585
649
- client_fd = make_client (t -> sotype , t -> connect_to .ip , t -> connect_to .port );
650
- if (client_fd < 0 )
586
+ client_fd = connect_to_addr_str (is_ipv6 (t -> connect_to .ip ) ? AF_INET6 : AF_INET ,
587
+ t -> sotype , t -> connect_to .ip , t -> connect_to .port , NULL );
588
+ if (!ASSERT_OK_FD (client_fd , "connect_to_addr_str" ))
651
589
goto close ;
652
590
653
591
if (t -> sotype == SOCK_STREAM )
@@ -862,9 +800,11 @@ static void test_redirect_lookup(struct test_sk_lookup *skel)
862
800
863
801
static void drop_on_lookup (const struct test * t )
864
802
{
803
+ int family = is_ipv6 (t -> connect_to .ip ) ? AF_INET6 : AF_INET ;
865
804
struct sockaddr_storage dst = {};
866
805
int client_fd , server_fd , err ;
867
806
struct bpf_link * lookup_link ;
807
+ socklen_t len ;
868
808
ssize_t n ;
869
809
870
810
lookup_link = attach_lookup_prog (t -> lookup_prog );
@@ -876,12 +816,14 @@ static void drop_on_lookup(const struct test *t)
876
816
if (server_fd < 0 )
877
817
goto detach ;
878
818
879
- client_fd = make_socket (t -> sotype , t -> connect_to .ip ,
880
- t -> connect_to .port , & dst );
881
- if (client_fd < 0 )
819
+ client_fd = client_socket (family , t -> sotype , NULL );
820
+ if (!ASSERT_OK_FD (client_fd , "client_socket" ))
882
821
goto close_srv ;
883
822
884
- err = connect (client_fd , (void * )& dst , inetaddr_len (& dst ));
823
+ err = make_sockaddr (family , t -> connect_to .ip , t -> connect_to .port , & dst , & len );
824
+ if (!ASSERT_OK (err , "make_sockaddr" ))
825
+ goto close_all ;
826
+ err = connect (client_fd , (void * )& dst , len );
885
827
if (t -> sotype == SOCK_DGRAM ) {
886
828
err = send_byte (client_fd );
887
829
if (err )
@@ -976,9 +918,11 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel)
976
918
977
919
static void drop_on_reuseport (const struct test * t )
978
920
{
921
+ int family = is_ipv6 (t -> connect_to .ip ) ? AF_INET6 : AF_INET ;
979
922
struct sockaddr_storage dst = { 0 };
980
923
int client , server1 , server2 , err ;
981
924
struct bpf_link * lookup_link ;
925
+ socklen_t len ;
982
926
ssize_t n ;
983
927
984
928
lookup_link = attach_lookup_prog (t -> lookup_prog );
@@ -1000,12 +944,14 @@ static void drop_on_reuseport(const struct test *t)
1000
944
if (server2 < 0 )
1001
945
goto close_srv1 ;
1002
946
1003
- client = make_socket (t -> sotype , t -> connect_to .ip ,
1004
- t -> connect_to .port , & dst );
1005
- if (client < 0 )
947
+ client = client_socket (family , t -> sotype , NULL );
948
+ if (!ASSERT_OK_FD (client , "client_socket" ))
1006
949
goto close_srv2 ;
1007
950
1008
- err = connect (client , (void * )& dst , inetaddr_len (& dst ));
951
+ err = make_sockaddr (family , t -> connect_to .ip , t -> connect_to .port , & dst , & len );
952
+ if (!ASSERT_OK (err , "make_sockaddr" ))
953
+ goto close_all ;
954
+ err = connect (client , (void * )& dst , len );
1009
955
if (t -> sotype == SOCK_DGRAM ) {
1010
956
err = send_byte (client );
1011
957
if (err )
@@ -1152,8 +1098,8 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel,
1152
1098
if (server_fd < 0 )
1153
1099
return ;
1154
1100
1155
- connected_fd = make_client ( sotype , EXT_IP4 , EXT_PORT );
1156
- if (connected_fd < 0 )
1101
+ connected_fd = connect_to_addr_str ( AF_INET , sotype , EXT_IP4 , EXT_PORT , NULL );
1102
+ if (! ASSERT_OK_FD ( connected_fd , "connect_to_addr_str" ) )
1157
1103
goto out_close_server ;
1158
1104
1159
1105
/* Put a connected socket in redirect map */
@@ -1166,8 +1112,8 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel,
1166
1112
goto out_close_connected ;
1167
1113
1168
1114
/* Try to redirect TCP SYN / UDP packet to a connected socket */
1169
- client_fd = make_client ( sotype , EXT_IP4 , EXT_PORT );
1170
- if (client_fd < 0 )
1115
+ client_fd = connect_to_addr_str ( AF_INET , sotype , EXT_IP4 , EXT_PORT , NULL );
1116
+ if (! ASSERT_OK_FD ( client_fd , "connect_to_addr_str" ) )
1171
1117
goto out_unlink_prog ;
1172
1118
if (sotype == SOCK_DGRAM ) {
1173
1119
send_byte (client_fd );
@@ -1219,6 +1165,7 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t)
1219
1165
int map_fd , server_fd , client_fd ;
1220
1166
struct bpf_link * link1 , * link2 ;
1221
1167
int prog_idx , done , err ;
1168
+ socklen_t len ;
1222
1169
1223
1170
map_fd = bpf_map__fd (t -> run_map );
1224
1171
@@ -1248,11 +1195,14 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t)
1248
1195
if (err )
1249
1196
goto out_close_server ;
1250
1197
1251
- client_fd = make_socket ( SOCK_STREAM , EXT_IP4 , EXT_PORT , & dst );
1252
- if (client_fd < 0 )
1198
+ client_fd = client_socket ( AF_INET , SOCK_STREAM , NULL );
1199
+ if (! ASSERT_OK_FD ( client_fd , "client_socket" ) )
1253
1200
goto out_close_server ;
1254
1201
1255
- err = connect (client_fd , (void * )& dst , inetaddr_len (& dst ));
1202
+ err = make_sockaddr (AF_INET , EXT_IP4 , EXT_PORT , & dst , & len );
1203
+ if (!ASSERT_OK (err , "make_sockaddr" ))
1204
+ goto out_close_client ;
1205
+ err = connect (client_fd , (void * )& dst , len );
1256
1206
if (CHECK (err && !t -> expect_errno , "connect" ,
1257
1207
"unexpected error %d\n" , errno ))
1258
1208
goto out_close_client ;
0 commit comments