Use getaddrinfo instead of gethostbyname
[monky] / src / mail.c
index 7fef098..2d91c62 100644 (file)
@@ -497,7 +497,8 @@ struct mail_s *parse_mail_args(char type, const char *arg)
        } else {
                mail->command[0] = '\0';
        }
-       printf("'%s' '%s'\n", mail->folder, mail->command);
+       DBGP("mail args parsed: folder: '%s' command: '%s' user: '%s' host: '%s'\n",
+                       mail->folder, mail->command, mail->user, mail->host);
        mail->p_timed_thread = NULL;
        return mail;
 }
@@ -658,14 +659,13 @@ static void *imap_thread(void *arg)
        unsigned long old_unseen = ULONG_MAX;
        unsigned long old_messages = ULONG_MAX;
        struct stat stat_buf;
-       struct hostent he, *he_res = 0;
-       int he_errno;
-       char hostbuff[2048];
-       struct sockaddr_in their_addr;  // connector's address information
        struct mail_s *mail = (struct mail_s *)arg;
        int has_idle = 0;
        int threadfd = timed_thread_readfd(mail->p_timed_thread);
        char resolved_host = 0;
+       struct addrinfo hints;
+       struct addrinfo *ai, *rp;
+       char portbuf[8];
 
        while (fail < mail->retries) {
                struct timeval fetchtimeout;
@@ -673,19 +673,19 @@ static void *imap_thread(void *arg)
                fd_set fdset;
 
                if (!resolved_host) {
-#ifdef HAVE_GETHOSTBYNAME_R
-                       if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
-                               NORM_ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
+                       memset(&hints, 0, sizeof(struct addrinfo));
+                       hints.ai_family = AF_UNSPEC;
+                       hints.ai_socktype = SOCK_STREAM;
+                       hints.ai_flags = 0;
+                       hints.ai_protocol = 0;
+                       snprintf(portbuf, 8, "%lu", mail->port);
+
+                       res = getaddrinfo(mail->host, portbuf, &hints, &ai);
+                       if (res != 0) {
+                               NORM_ERR("IMAP getaddrinfo: %s", gai_strerror(res));
                                fail++;
                                break;
                        }
-#else /* HAVE_GETHOSTBYNAME_R */
-                       if ((he_res = gethostbyname(mail->host)) == NULL) {     // get the host info
-                               herror("gethostbyname");
-                               fail++;
-                               break;
-                       }
-#endif /* HAVE_GETHOSTBYNAME_R */
                        resolved_host = 1;
                }
                if (fail > 0) {
@@ -693,22 +693,18 @@ static void *imap_thread(void *arg)
                                        mail->user, mail->host, fail + 1, mail->retries);
                }
                do {
-                       if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
-                               perror("socket");
-                               fail++;
-                               break;
+                       for (rp = ai; rp != NULL; rp = rp->ai_next) {
+                               sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+                               if (sockfd == -1) {
+                                       continue;
+                               }
+                               if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
+                                       break;
+                               }
+                               close(sockfd);
                        }
-
-                       // host byte order
-                       their_addr.sin_family = AF_INET;
-                       // short, network byte order
-                       their_addr.sin_port = htons(mail->port);
-                       their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
-                       // zero the rest of the struct
-                       memset(&(their_addr.sin_zero), '\0', 8);
-
-                       if (connect(sockfd, (struct sockaddr *) &their_addr,
-                                               sizeof(struct sockaddr)) == -1) {
+                       freeaddrinfo(ai);
+                       if (rp == NULL) {
                                perror("connect");
                                fail++;
                                break;
@@ -797,11 +793,13 @@ static void *imap_thread(void *arg)
                                         */
                                        fetchtimeout.tv_sec = 600;
                                        fetchtimeout.tv_usec = 0;
-                                       DBGP2("idling...");
+                                       DBGP("idling...");
                                        FD_ZERO(&fdset);
                                        FD_SET(sockfd, &fdset);
                                        FD_SET(threadfd, &fdset);
-                                       res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, &fetchtimeout);
+                                       res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL,
+                                                       NULL, &fetchtimeout);
+                                       DBGP("done idling");
                                        if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
                                                if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
                                                        /* if a valid socket, close it */
@@ -814,9 +812,6 @@ static void *imap_thread(void *arg)
                                                        fail++;
                                                        break;
                                                }
-                                       } else if (fetchtimeout.tv_sec > 0) {
-                                               fail++;
-                                               break;
                                        }
                                        recvbuf[numbytes] = '\0';
                                        DBGP2("imap_thread() received: %s", recvbuf);
@@ -1012,31 +1007,30 @@ static void *pop3_thread(void *arg)
        unsigned int fail = 0;
        unsigned long old_unseen = ULONG_MAX;
        struct stat stat_buf;
-       struct hostent he, *he_res = 0;
-       int he_errno;
-       char hostbuff[2048];
-       struct sockaddr_in their_addr;  // connector's address information
        struct mail_s *mail = (struct mail_s *)arg;
        char resolved_host = 0;
+       struct addrinfo hints;
+       struct addrinfo *ai, *rp;
+       char portbuf[8];
 
        while (fail < mail->retries) {
                struct timeval fetchtimeout;
                int res;
                fd_set fdset;
                if (!resolved_host) {
-#ifdef HAVE_GETHOSTBYNAME_R
-                       if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
-                               NORM_ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
+                       memset(&hints, 0, sizeof(struct addrinfo));
+                       hints.ai_family = AF_UNSPEC;
+                       hints.ai_socktype = SOCK_STREAM;
+                       hints.ai_flags = 0;
+                       hints.ai_protocol = 0;
+                       snprintf(portbuf, 8, "%lu", mail->port);
+
+                       res = getaddrinfo(mail->host, portbuf, &hints, &ai);
+                       if (res != 0) {
+                               NORM_ERR("POP3 getaddrinfo: %s", gai_strerror(res));
                                fail++;
                                break;
                        }
-#else /* HAVE_GETHOSTBYNAME_R */
-                       if ((he_res = gethostbyname(mail->host)) == NULL) {     // get the host info
-                               herror("gethostbyname");
-               fail++;
-               break;
-       }
-#endif /* HAVE_GETHOSTBYNAME_R */
        resolved_host = 1;
 }
                if (fail > 0) {
@@ -1044,22 +1038,18 @@ static void *pop3_thread(void *arg)
                                        mail->user, mail->host, fail + 1, mail->retries);
                }
                do {
-                       if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
-                               perror("socket");
-                               fail++;
-                               break;
+                       for (rp = ai; rp != NULL; rp = rp->ai_next) {
+                               sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+                               if (sockfd == -1) {
+                                       continue;
+                               }
+                               if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
+                                       break;
+                               }
+                               close(sockfd);
                        }
-
-                       // host byte order
-                       their_addr.sin_family = AF_INET;
-                       // short, network byte order
-                       their_addr.sin_port = htons(mail->port);
-                       their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
-                       // zero the rest of the struct
-                       memset(&(their_addr.sin_zero), '\0', 8);
-
-                       if (connect(sockfd, (struct sockaddr *) &their_addr,
-                                               sizeof(struct sockaddr)) == -1) {
+                       freeaddrinfo(ai);
+                       if (rp == NULL) {
                                perror("connect");
                                fail++;
                                break;