ii-ipv6.diff - sites - public wiki contents of suckless.org
(HTM) git clone git://git.suckless.org/sites
(DIR) Log
(DIR) Files
(DIR) Refs
---
ii-ipv6.diff (1649B)
---
1 --- ii.c.orig 2007-07-22 11:54:29.061710701 +0100
2 +++ ii.c 2007-07-22 14:08:42.833057387 +0100
3 @@ -153,25 +153,45 @@
4
5 static int tcpopen(unsigned short port) {
6 int fd;
7 - struct sockaddr_in sin;
8 - struct hostent *hp = gethostbyname(host);
9 -
10 - memset(&sin, 0, sizeof(struct sockaddr_in));
11 - if(!hp) {
12 - perror("ii: cannot retrieve host information");
13 + struct addrinfo req, *res, *orig_res;
14 + char service[6];
15 + char errmsg[512];
16 +
17 + snprintf(service, 6, "%u", port);
18 + memset(&req, 0, sizeof(req));
19 + req.ai_flags = AI_NUMERICSERV;
20 + req.ai_socktype = SOCK_STREAM;
21 + int e = getaddrinfo(host, service, &req, &res);
22 + if(e) {
23 + fprintf(stderr, "ii: getaddrinfo() failed : %s\n", gai_strerror(e));
24 exit(EXIT_FAILURE);
25 }
26 - sin.sin_family = AF_INET;
27 - memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
28 - sin.sin_port = htons(port);
29 - if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
30 - perror("ii: cannot create socket");
31 - exit(EXIT_FAILURE);
32 +
33 + orig_res = res;
34 + for (; res; res = res->ai_next ) {
35 + fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
36 +
37 + if(fd < 0) {
38 + snprintf(errmsg, sizeof(errmsg), "ii: socket() failed : %s", strerror(fd));
39 + continue;
40 + }
41 +
42 + if(connect(fd, res->ai_addr, res->ai_addrlen) != 0) {
43 + snprintf(errmsg, sizeof(errmsg), "ii: connect() failed : %s", strerror(errno));
44 + fd = -1;
45 + continue;
46 + }
47 +
48 + /* sucessful connection */
49 + break;
50 }
51 - if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) {
52 - perror("ii: cannot connect to host");
53 + freeaddrinfo(orig_res);
54 +
55 + if(fd < 0) {
56 + fprintf(stderr, "%s\n", errmsg);
57 exit(EXIT_FAILURE);
58 }
59 +
60 return fd;
61 }
62