2 * Copyright notice from original mutt:
3 * Copyright (C) 1998 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>
5 * Copyright (C) 1999-2000 Tommi Komulainen <Tommi.Komulainen@iki.fi>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
18 #include "mutt_socket.h"
19 #include "mutt_tunnel.h"
20 #if defined(USE_SSL) || defined(USE_GNUTLS)
21 # include "mutt_ssl.h"
24 #include "mutt_idna.h"
29 #include "lib/debug.h"
32 #include <netinet/in.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
41 /* support for multiple socket connections */
42 static CONNECTION *Connections = NULL;
44 /* forward declarations */
45 static int socket_preconnect (void);
46 static int socket_connect (int fd, struct sockaddr *sa);
47 static CONNECTION *socket_new_conn (void);
50 int mutt_socket_open (CONNECTION * conn)
52 if (socket_preconnect ())
55 return conn->conn_open (conn);
58 int mutt_socket_close (CONNECTION * conn)
63 debug_print (1, ("Attempt to close closed connection.\n"));
65 rc = conn->conn_close (conn);
73 int mutt_socket_read (CONNECTION * conn, char *buf, size_t len)
78 debug_print (1, ("attempt to read from closed connection\n"));
82 rc = conn->conn_read (conn, buf, len);
85 mutt_error (_("Connection to %s closed"), conn->account.host);
89 mutt_socket_close (conn);
94 int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg)
99 debug_print (dbg, ("> %s", buf));
102 debug_print (1, ("attempt to write to closed connection\n"));
107 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
108 debug_print (1, ("error writing, closing socket\n"));
109 mutt_socket_close (conn);
115 debug_print (1, ("ERROR: wrote %d of %d bytes!\n", rc, len));
121 /* simple read buffering to speed things up. */
122 int mutt_socket_readchar (CONNECTION * conn, char *c)
124 if (conn->bufpos >= conn->available) {
127 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
129 debug_print (1, ("attempt to read from closed connection.\n"));
133 if (conn->available == 0) {
134 mutt_error (_("Connection to %s closed"), conn->account.host);
137 if (conn->available <= 0) {
138 mutt_socket_close (conn);
142 *c = conn->inbuf[conn->bufpos];
147 int mutt_socket_readln_d (char *buf, size_t buflen, CONNECTION * conn,
153 for (i = 0; i < buflen - 1; i++) {
154 if (mutt_socket_readchar (conn, &ch) != 1) {
164 /* strip \r from \r\n termination */
165 if (i && buf[i - 1] == '\r')
170 debug_print (dbg, ("< %s\n", buf));
172 /* number of bytes read, not str_len */
176 CONNECTION *mutt_socket_head (void)
181 /* mutt_socket_free: remove connection from connection list and free it */
182 void mutt_socket_free (CONNECTION * conn)
189 /* head is special case, doesn't need prev updated */
191 Connections = iter->next;
197 if (iter->next == conn) {
199 iter->next = tmp->next;
207 /* mutt_conn_find: find a connection off the list of connections whose
208 * account matches account. If start is not null, only search for
209 * connections after the given connection (allows higher level socket code
210 * to make more fine-grained searches than account info - eg in IMAP we may
211 * wish to find a connection which is not in IMAP_SELECTED state) */
212 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
216 char hook[LONG_STRING];
218 /* account isn't actually modified, since url isn't either */
219 mutt_account_tourl ((ACCOUNT *) account, &url);
221 url_ciss_tostring (&url, hook, sizeof (hook), 0);
222 mutt_account_hook (hook);
224 conn = start ? start->next : Connections;
226 if (mutt_account_match (account, &(conn->account)))
231 conn = socket_new_conn ();
232 memcpy (&conn->account, account, sizeof (ACCOUNT));
234 conn->next = Connections;
237 if (Tunnel && *Tunnel)
238 mutt_tunnel_socket_setup (conn);
239 else if (account->flags & M_ACCT_SSL) {
240 #if defined (USE_SSL) || defined (USE_GNUTLS)
241 if (mutt_ssl_socket_setup (conn) < 0) {
242 mutt_socket_free (conn);
246 mutt_error _("SSL is unavailable.");
249 mutt_socket_free (conn);
255 conn->conn_read = raw_socket_read;
256 conn->conn_write = raw_socket_write;
257 conn->conn_open = raw_socket_open;
258 conn->conn_close = raw_socket_close;
264 static int socket_preconnect (void)
269 if (str_len (Preconnect)) {
270 debug_print (2, ("Executing preconnect: %s\n", Preconnect));
271 rc = mutt_system (Preconnect);
272 debug_print (2, ("Preconnect result: %d\n", rc));
275 mutt_perror (_("Preconnect command failed."));
285 /* socket_connect: set up to connect to a socket fd. */
286 static int socket_connect (int fd, struct sockaddr *sa)
291 if (sa->sa_family == AF_INET)
292 sa_size = sizeof (struct sockaddr_in);
293 #ifdef HAVE_GETADDRINFO
294 else if (sa->sa_family == AF_INET6)
295 sa_size = sizeof (struct sockaddr_in6);
298 debug_print (1, ("Unknown address family!\n"));
302 if (ConnectTimeout > 0)
303 alarm (ConnectTimeout);
305 mutt_allow_interrupt (1);
309 if (connect (fd, sa, sa_size) < 0) {
311 debug_print (2, ("Connection failed. errno: %d...\n", errno));
312 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
315 if (ConnectTimeout > 0)
317 mutt_allow_interrupt (0);
322 /* socket_new_conn: allocate and initialise a new connection. */
323 static CONNECTION *socket_new_conn (void)
327 conn = (CONNECTION *) mem_calloc (1, sizeof (CONNECTION));
333 int raw_socket_close (CONNECTION * conn)
335 return close (conn->fd);
338 int raw_socket_read (CONNECTION * conn, char *buf, size_t len)
342 if ((rc = read (conn->fd, buf, len)) == -1) {
343 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
351 int raw_socket_write (CONNECTION * conn, const char *buf, size_t count)
355 if ((rc = write (conn->fd, buf, count)) == -1) {
356 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
364 int raw_socket_open (CONNECTION * conn)
369 char *host_idna = NULL;
371 #ifdef HAVE_GETADDRINFO
376 struct addrinfo hints;
377 struct addrinfo *res;
378 struct addrinfo *cur;
380 /* we accept v4 or v6 STREAM sockets */
381 memset (&hints, 0, sizeof (hints));
383 if (option (OPTUSEIPV6))
384 hints.ai_family = AF_UNSPEC;
386 hints.ai_family = AF_INET;
388 hints.ai_socktype = SOCK_STREAM;
390 snprintf (port, sizeof (port), "%d", conn->account.port);
393 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
394 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
398 host_idna = conn->account.host;
401 mutt_message (_("Looking up %s..."), conn->account.host);
404 rc = getaddrinfo (host_idna, port, &hints, &res);
407 mem_free (&host_idna);
411 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
416 mutt_message (_("Connecting to %s..."), conn->account.host);
419 for (cur = res; cur != NULL; cur = cur->ai_next) {
420 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
422 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
423 fcntl (fd, F_SETFD, FD_CLOEXEC);
435 /* --- IPv4 only --- */
437 struct sockaddr_in sin;
441 memset (&sin, 0, sizeof (sin));
442 sin.sin_port = htons (conn->account.port);
443 sin.sin_family = AF_INET;
446 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
447 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
451 host_idna = conn->account.host;
454 mutt_message (_("Looking up %s..."), conn->account.host);
456 if ((he = gethostbyname (host_idna)) == NULL) {
458 mem_free (&host_idna);
460 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
466 mem_free (&host_idna);
469 mutt_message (_("Connecting to %s..."), conn->account.host);
472 for (i = 0; he->h_addr_list[i] != NULL; i++) {
473 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
474 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
477 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
478 fcntl (fd, F_SETFD, FD_CLOEXEC);
489 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
490 (rc > 0) ? strerror (rc) : _("unknown error"));