New way to connect a job_t.
authorPierre Habouzit <madcoder@debian.org>
Sun, 13 Jan 2008 15:48:54 +0000 (16:48 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sun, 13 Jan 2008 15:48:54 +0000 (16:48 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
lib-sys/evtloop.c
lib-sys/evtloop.h

index 8e1d361..f9ae21e 100644 (file)
@@ -18,6 +18,7 @@
  *  Copyright © 2007 Pierre Habouzit
  */
 
  *  Copyright © 2007 Pierre Habouzit
  */
 
+#include <netdb.h>
 #include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
@@ -32,6 +33,9 @@
 #include "evtloop.h"
 #include "mutt.h"
 #include "mutt_ssl.li"
 #include "evtloop.h"
 #include "mutt.h"
 #include "mutt_ssl.li"
+#ifdef HAVE_LIBIDN
+#include <idna.h>
+#endif
 
 DO_ARRAY_TYPE(job_t, job);
 
 
 DO_ARRAY_TYPE(job_t, job);
 
@@ -228,6 +232,8 @@ int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
         goto error;
     if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0)
         goto error;
         goto error;
     if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0)
         goto error;
+    if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
+        goto error;
     if (connect(sock, addr, len) < 0)
         goto error;
 
     if (connect(sock, addr, len) < 0)
         goto error;
 
@@ -243,6 +249,47 @@ int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
     return el_job_release(w, EL_ERROR);
 }
 
     return el_job_release(w, EL_ERROR);
 }
 
+int el_job_connect2(job_t *w, const ACCOUNT *act)
+{
+    int rc;
+    char *host = NULL;
+    struct addrinfo *res;
+    struct addrinfo hints = {
+        .ai_family = AF_UNSPEC,
+        .ai_socktype = SOCK_STREAM,
+    };
+
+# ifdef HAVE_LIBIDN
+    if (idna_to_ascii_lz(act->host, &host, 1) != IDNA_SUCCESS) {
+        mutt_error(_("Bad IDN \"%s\"."), act->host);
+        return -1;
+    }
+# else
+    host = act->host;
+# endif
+    mutt_message(_("Looking up %s..."), act->host);
+    rc = getaddrinfo(host, NULL, &hints, &res);
+# ifdef HAVE_LIBIDN
+    p_delete(&host);
+# endif
+
+    if (rc) {
+        mutt_error(_("Could not find the host \"%s\""), act->host);
+        mutt_sleep(2);
+        return -1;
+    }
+    mutt_message(_("Connecting to %s..."), act->host);
+    rc = el_job_connect(w, res->ai_addr, res->ai_addrlen,
+                        res->ai_family, res->ai_socktype, res->ai_protocol);
+    freeaddrinfo (res);
+    if (rc) {
+        mutt_error(_("Could not connect to %s (%m)."), act->host);
+        mutt_sleep(2);
+        return -1;
+    }
+    return 0;
+}
+
 int el_job_starttls(job_t *w)
 {
     if (tls_negotiate(w) < 0)
 int el_job_starttls(job_t *w)
 {
     if (tls_negotiate(w) < 0)
@@ -363,7 +410,7 @@ int el_dispatch(int timeout)
     return 0;
 }
 
     return 0;
 }
 
-void el_wait(job_t *w)
+void el_wait(volatile job_t *w)
 {
     w->cond = true;
     pthread_cond_wait(&el_cond, &el_mx);
 {
     w->cond = true;
     pthread_cond_wait(&el_cond, &el_mx);
index bb7c50e..e9eff3a 100644 (file)
@@ -24,6 +24,7 @@
 #include <lib-lib/lib-lib.h>
 #include <gnutls/gnutls.h>
 #include <sys/socket.h>
 #include <lib-lib/lib-lib.h>
 #include <gnutls/gnutls.h>
 #include <sys/socket.h>
+#include "account.h"
 
 typedef enum el_state {
     EL_LLP_INIT,
 
 typedef enum el_state {
     EL_LLP_INIT,
@@ -92,13 +93,14 @@ __must_check__ int el_job_release(job_t *j, el_status);
 
 __must_check__ int el_job_connect(job_t *w, struct sockaddr *, socklen_t len,
                                   int type, int proto, int ssl);
 
 __must_check__ int el_job_connect(job_t *w, struct sockaddr *, socklen_t len,
                                   int type, int proto, int ssl);
+__must_check__ int el_job_connect2(job_t *w, const ACCOUNT *);
 __must_check__ int el_job_starttls(job_t *w);
 __must_check__ ssize_t el_job_read(job_t *w, buffer_t *buf);
 __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf);
 
 void el_lock(void);
 void el_unlock(void);
 __must_check__ int el_job_starttls(job_t *w);
 __must_check__ ssize_t el_job_read(job_t *w, buffer_t *buf);
 __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf);
 
 void el_lock(void);
 void el_unlock(void);
-void el_wait(job_t *w);
+void el_wait(volatile job_t *w);
 
 int el_dispatch(int timeout);
 
 
 int el_dispatch(int timeout);