5 #include <sys/socket.h>
6 #include <netinet/in.h>
11 #define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12 #define TESTE(c) (errno=0, TEST(c, "errno = %s\n", strerror(errno)))
16 struct sockaddr_in sa
= { .sin_family
= AF_INET
};
20 TESTE((s
=socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
))>=0);
21 TESTE(bind(s
, (void *)&sa
, sizeof sa
)==0);
22 TESTE(getsockname(s
, (void *)&sa
, (socklen_t
[]){sizeof sa
})==0);
24 TESTE(setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
,
25 &(struct timeval
){.tv_usec
=1}, sizeof(struct timeval
))==0);
27 TESTE((c
=socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
))>=0);
28 sa
.sin_addr
.s_addr
= htonl(0x7f000001);
29 TESTE(sendto(c
, "x", 1, 0, (void *)&sa
, sizeof sa
)==1);
30 TESTE(recvfrom(s
, buf
, sizeof buf
, 0, (void *)&sa
, (socklen_t
[]){sizeof sa
})==1);
31 TEST(buf
[0]=='x', "'%c'\n", buf
[0]);
36 memset(&sa
, 0, sizeof sa
);
37 sa
.sin_family
= AF_INET
;
38 TESTE((s
=socket(PF_INET
, SOCK_STREAM
|SOCK_CLOEXEC
, IPPROTO_TCP
))>=0);
39 TEST(fcntl(s
, F_GETFD
)&FD_CLOEXEC
, "SOCK_CLOEXEC did not work\n");
40 TESTE(bind(s
, (void *)&sa
, sizeof sa
)==0);
41 TESTE(getsockname(s
, (void *)&sa
, (socklen_t
[]){sizeof sa
})==0);
42 sa
.sin_addr
.s_addr
= htonl(0x7f000001);
44 TESTE(listen(s
, 1)==0);
46 TESTE((c
=socket(PF_INET
, SOCK_STREAM
|SOCK_NONBLOCK
, IPPROTO_TCP
))>=0);
47 TEST(fcntl(c
, F_GETFL
)&O_NONBLOCK
, "SOCK_NONBLOCK did not work\n");
49 TESTE(connect(c
, (void *)&sa
, sizeof sa
)==0 || errno
==EINPROGRESS
);
51 TESTE((t
=accept(s
, (void *)&sa
, &(socklen_t
){sizeof sa
}))>=0);