5
5
#include <string.h>
6
6
#include <stdint.h>
7
7
8
+
8
9
#include <unistd.h>
9
10
#include <poll.h>
10
11
#include <errno.h>
13
14
#include <sys/types.h>
14
15
#include <sys/socket.h>
15
16
#include <netinet/in.h>
17
+ #include <netdb.h>
16
18
17
19
#include <tlspool/starttls.h>
18
20
@@ -42,12 +44,19 @@ void sigcont_handler (int signum) {
42
44
}
43
45
44
46
int main (int argc , char * * argv ) {
45
- int sox , cnx ;
47
+ int sox , cnx , rc ;
46
48
int plainfd ;
47
- struct sockaddr_in6 sin6 ;
49
+ const char * host = argc < 2 ? "::" : argv [1 ];
50
+ const char * service = argc < 3 ? "12345" : argv [2 ];
51
+ struct addrinfo * res ;
48
52
sigset_t sigcontset ;
49
53
uint8_t rndbuf [16 ];
50
54
55
+ rc = getaddrinfo (host , service , NULL , & res );
56
+ if (rc != 0 ) {
57
+ fprintf (stderr , "Error in getaddrinfo: %s\n" , gai_strerror (rc ));
58
+ exit (1 );
59
+ }
51
60
if (sigemptyset (& sigcontset ) ||
52
61
sigaddset (& sigcontset , SIGCONT ) ||
53
62
pthread_sigmask (SIG_BLOCK , & sigcontset , NULL )) {
@@ -56,16 +65,12 @@ int main (int argc, char **argv) {
56
65
}
57
66
58
67
reconnect :
59
- sox = socket (AF_INET6 , SOCK_STREAM , 0 );
68
+ sox = socket (res -> ai_family , SOCK_STREAM , 0 );
60
69
if (sox == -1 ) {
61
70
perror ("Failed to create socket on testsrv" );
62
71
exit (1 );
63
72
}
64
- memset (& sin6 , 0 , sizeof (sin6 ));
65
- sin6 .sin6_family = AF_INET6 ;
66
- sin6 .sin6_port = htons (12345 );
67
- memcpy (& sin6 .sin6_addr , & in6addr_loopback , 16 );
68
- if (bind (sox , (struct sockaddr * ) & sin6 , sizeof (sin6 )) == -1 ) {
73
+ if (bind (sox , res -> ai_addr , res -> ai_addrlen ) == -1 ) {
69
74
perror ("Socket failed to bind on testsrv" );
70
75
if (errno == EADDRINUSE ) {
71
76
close (sox );
@@ -133,6 +138,7 @@ int main (int argc, char **argv) {
133
138
exit (1 );
134
139
}
135
140
}
141
+ freeaddrinfo (res );
136
142
return 0 ;
137
143
}
138
144
0 commit comments