@matheusbsbs / matheus-bagliano / commits / 94ad243702
Fork 0

imap-send: add support for IPv6

Add IPv6 support by implementing name resolution with the protocol agnostic getaddrinfo(3) API. The old gethostbyname(3) code is still available when git is compiled with NO_IPV6. Signed-off-by: Benjamin Kramer <benny.kra@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Benjamin Kramer committed May 25, 2009 at 21:13 UTC 94ad2437020038b022ce19b8b8b35f1cc86b16f6
1 file changed +51 -3
imap-send.c
+51 -3
@@ -982,9 +982,7 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
982 struct imap_store *ctx;
983 struct imap *imap;
984 char *arg, *rsp;
985 - struct hostent *he;
986 - struct sockaddr_in addr;
987 - int s, a[2], preauth;
985 + int s = -1, a[2], preauth;
986 pid_t pid;
987
988 ctx = xcalloc(sizeof(*ctx), 1);
@@ -1021,6 +1019,51 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
1019
1020 imap_info("ok\n");
1021 } else {
1022 +#ifndef NO_IPV6
1023 + struct addrinfo hints, *ai0, *ai;
1024 + int gai;
1025 + char portstr[6];
1026 +
1027 + snprintf(portstr, sizeof(portstr), "%hu", srvc->port);
1028 +
1029 + memset(&hints, 0, sizeof(hints));
1030 + hints.ai_socktype = SOCK_STREAM;
1031 + hints.ai_protocol = IPPROTO_TCP;
1032 +
1033 + imap_info("Resolving %s... ", srvc->host);
1034 + gai = getaddrinfo(srvc->host, portstr, &hints, &ai);
1035 + if (gai) {
1036 + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai));
1037 + goto bail;
1038 + }
1039 + imap_info("ok\n");
1040 +
1041 + for (ai0 = ai; ai; ai = ai->ai_next) {
1042 + char addr[NI_MAXHOST];
1043 +
1044 + s = socket(ai->ai_family, ai->ai_socktype,
1045 + ai->ai_protocol);
1046 + if (s < 0)
1047 + continue;
1048 +
1049 + getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1050 + sizeof(addr), NULL, 0, NI_NUMERICHOST);
1051 + imap_info("Connecting to [%s]:%s... ", addr, portstr);
1052 +
1053 + if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1054 + close(s);
1055 + s = -1;
1056 + perror("connect");
1057 + continue;
1058 + }
1059 +
1060 + break;
1061 + }
1062 + freeaddrinfo(ai0);
1063 +#else /* NO_IPV6 */
1064 + struct hostent *he;
1065 + struct sockaddr_in addr;
1066 +
1067 memset(&addr, 0, sizeof(addr));
1068 addr.sin_port = htons(srvc->port);
1069 addr.sin_family = AF_INET;
@@ -1040,7 +1083,12 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
1083 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1084 if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1085 close(s);
1086 + s = -1;
1087 perror("connect");
1088 + }
1089 +#endif
1090 + if (s < 0) {
1091 + fputs("Error: unable to connect to server.\n", stderr);
1092 goto bail;
1093 }
1094