2 * Copyright (C) 1998 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
4 * Copyright (C) 1999-2000 Tommi Komulainen <Tommi.Komulainen@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 #include "mutt_socket.h"
24 #include "mutt_tunnel.h"
25 #if defined(USE_SSL) || defined(USE_GNUTLS) || defined(USE_NSS)
26 # include "mutt_ssl.h"
29 #include "mutt_idna.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 ();
50 int mutt_socket_open (CONNECTION* conn)
52 if (socket_preconnect ())
55 return conn->open (conn);
58 int mutt_socket_close (CONNECTION* conn)
63 dprint (1, (debugfile, "mutt_socket_close: Attempt to close closed connection.\n"));
65 rc = conn->close (conn);
73 int mutt_socket_read (CONNECTION* conn, char* buf, size_t len)
79 dprint (1, (debugfile, "mutt_socket_read: attempt to read from closed connection\n"));
83 rc = conn->read (conn, buf, len);
87 mutt_error (_("Connection to %s closed"), conn->account.host);
91 mutt_socket_close (conn);
96 int mutt_socket_write_d (CONNECTION *conn, const char *buf, int dbg)
101 dprint (dbg, (debugfile,"> %s", buf));
105 dprint (1, (debugfile, "mutt_socket_write: attempt to write to closed connection\n"));
109 len = mutt_strlen (buf);
110 if ((rc = conn->write (conn, buf, len)) < 0)
112 dprint (1, (debugfile,
113 "mutt_socket_write: error writing, closing socket\n"));
114 mutt_socket_close (conn);
121 dprint (1, (debugfile,
122 "mutt_socket_write: ERROR: wrote %d of %d bytes!\n", rc, len));
128 /* simple read buffering to speed things up. */
129 int mutt_socket_readchar (CONNECTION *conn, char *c)
131 if (conn->bufpos >= conn->available)
134 conn->available = conn->read (conn, conn->inbuf, sizeof (conn->inbuf));
137 dprint (1, (debugfile, "mutt_socket_readchar: attempt to read from closed connection.\n"));
141 if (conn->available == 0)
143 mutt_error (_("Connection to %s closed"), conn->account.host);
146 if (conn->available <= 0)
148 mutt_socket_close (conn);
152 *c = conn->inbuf[conn->bufpos];
157 int mutt_socket_readln_d (char* buf, size_t buflen, CONNECTION* conn, int dbg)
162 for (i = 0; i < buflen-1; i++)
164 if (mutt_socket_readchar (conn, &ch) != 1)
175 /* strip \r from \r\n termination */
176 if (i && buf[i-1] == '\r')
181 dprint (dbg, (debugfile, "< %s\n", buf));
183 /* number of bytes read, not strlen */
187 CONNECTION* mutt_socket_head (void)
192 /* mutt_socket_free: remove connection from connection list and free it */
193 void mutt_socket_free (CONNECTION* conn)
200 /* head is special case, doesn't need prev updated */
203 Connections = iter->next;
210 if (iter->next == conn)
213 iter->next = tmp->next;
221 /* mutt_conn_find: find a connection off the list of connections whose
222 * account matches account. If start is not null, only search for
223 * connections after the given connection (allows higher level socket code
224 * to make more fine-grained searches than account info - eg in IMAP we may
225 * wish to find a connection which is not in IMAP_SELECTED state) */
226 CONNECTION* mutt_conn_find (const CONNECTION* start, const ACCOUNT* account)
230 char hook[LONG_STRING];
232 /* account isn't actually modified, since url isn't either */
233 mutt_account_tourl ((ACCOUNT*) account, &url);
235 url_ciss_tostring (&url, hook, sizeof (hook), 0);
236 mutt_account_hook (hook);
238 conn = start ? start->next : Connections;
241 if (mutt_account_match (account, &(conn->account)))
246 conn = socket_new_conn ();
247 memcpy (&conn->account, account, sizeof (ACCOUNT));
249 conn->next = Connections;
252 if (Tunnel && *Tunnel)
253 mutt_tunnel_socket_setup (conn);
254 else if (account->flags & M_ACCT_SSL)
257 ssl_socket_setup (conn);
259 mutt_nss_socket_setup (conn);
261 if (mutt_gnutls_socket_setup (conn) < 0)
263 mutt_socket_free (conn);
267 mutt_error _("SSL is unavailable.");
269 mutt_socket_free (conn);
276 conn->read = raw_socket_read;
277 conn->write = raw_socket_write;
278 conn->open = raw_socket_open;
279 conn->close = raw_socket_close;
285 static int socket_preconnect (void)
290 if (mutt_strlen (Preconnect))
292 dprint (2, (debugfile, "Executing preconnect: %s\n", Preconnect));
293 rc = mutt_system (Preconnect);
294 dprint (2, (debugfile, "Preconnect result: %d\n", rc));
298 mutt_perror (_("Preconnect command failed."));
308 /* socket_connect: set up to connect to a socket fd. */
309 static int socket_connect (int fd, struct sockaddr* sa)
314 if (sa->sa_family == AF_INET)
315 sa_size = sizeof (struct sockaddr_in);
316 #ifdef HAVE_GETADDRINFO
317 else if (sa->sa_family == AF_INET6)
318 sa_size = sizeof (struct sockaddr_in6);
322 dprint (1, (debugfile, "Unknown address family!\n"));
326 if (ConnectTimeout > 0)
327 alarm (ConnectTimeout);
329 mutt_allow_interrupt (1);
333 if (connect (fd, sa, sa_size) < 0)
336 dprint (2, (debugfile, "Connection failed. errno: %d...\n", errno));
337 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
340 if (ConnectTimeout > 0)
342 mutt_allow_interrupt (0);
347 /* socket_new_conn: allocate and initialise a new connection. */
348 static CONNECTION* socket_new_conn ()
352 conn = (CONNECTION *) safe_calloc (1, sizeof (CONNECTION));
358 int raw_socket_close (CONNECTION *conn)
360 return close (conn->fd);
363 int raw_socket_read (CONNECTION* conn, char* buf, size_t len)
367 if ((rc = read (conn->fd, buf, len)) == -1)
369 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
377 int raw_socket_write (CONNECTION* conn, const char* buf, size_t count)
381 if ((rc = write (conn->fd, buf, count)) == -1)
383 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
391 int raw_socket_open (CONNECTION* conn)
396 char *host_idna = NULL;
398 #ifdef HAVE_GETADDRINFO
403 struct addrinfo hints;
404 struct addrinfo* res;
405 struct addrinfo* cur;
407 /* we accept v4 or v6 STREAM sockets */
408 memset (&hints, 0, sizeof (hints));
410 if (option (OPTUSEIPV6))
411 hints.ai_family = AF_UNSPEC;
413 hints.ai_family = AF_INET;
415 hints.ai_socktype = SOCK_STREAM;
417 snprintf (port, sizeof (port), "%d", conn->account.port);
420 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS)
422 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
426 host_idna = conn->account.host;
429 mutt_message (_("Looking up %s..."), conn->account.host);
432 rc = getaddrinfo (host_idna, port, &hints, &res);
440 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
444 mutt_message (_("Connecting to %s..."), conn->account.host);
447 for (cur = res; cur != NULL; cur = cur->ai_next)
449 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
452 if ((rc = socket_connect (fd, cur->ai_addr)) == 0)
454 fcntl (fd, F_SETFD, FD_CLOEXEC);
466 /* --- IPv4 only --- */
468 struct sockaddr_in sin;
472 memset (&sin, 0, sizeof (sin));
473 sin.sin_port = htons (conn->account.port);
474 sin.sin_family = AF_INET;
477 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS)
479 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
483 host_idna = conn->account.host;
486 mutt_message (_("Looking up %s..."), conn->account.host);
488 if ((he = gethostbyname (host_idna)) == NULL)
493 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
502 mutt_message (_("Connecting to %s..."), conn->account.host);
505 for (i = 0; he->h_addr_list[i] != NULL; i++)
507 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
508 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
512 if ((rc = socket_connect (fd, (struct sockaddr*) &sin)) == 0)
525 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
526 (rc > 0) ? strerror (rc) : _("unknown error"));