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) || defined(USE_NSS)
21 # include "mutt_ssl.h"
24 #include "mutt_idna.h"
27 #include <netinet/in.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 /* support for multiple socket connections */
37 static CONNECTION *Connections = NULL;
39 /* forward declarations */
40 static int socket_preconnect (void);
41 static int socket_connect (int fd, struct sockaddr *sa);
42 static CONNECTION *socket_new_conn ();
45 int mutt_socket_open (CONNECTION * conn)
47 if (socket_preconnect ())
50 return conn->conn_open (conn);
53 int mutt_socket_close (CONNECTION * conn)
60 "mutt_socket_close: Attempt to close closed connection.\n"));
62 rc = conn->conn_close (conn);
70 int mutt_socket_read (CONNECTION * conn, char *buf, size_t len)
77 "mutt_socket_read: attempt to read from closed connection\n"));
81 rc = conn->conn_read (conn, buf, len);
84 mutt_error (_("Connection to %s closed"), conn->account.host);
88 mutt_socket_close (conn);
93 int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg)
98 dprint (dbg, (debugfile, "> %s", buf));
103 "mutt_socket_write: attempt to write to closed connection\n"));
107 len = mutt_strlen (buf);
108 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
109 dprint (1, (debugfile,
110 "mutt_socket_write: error writing, closing socket\n"));
111 mutt_socket_close (conn);
117 dprint (1, (debugfile,
118 "mutt_socket_write: ERROR: wrote %d of %d bytes!\n", rc,
125 /* simple read buffering to speed things up. */
126 int mutt_socket_readchar (CONNECTION * conn, char *c)
128 if (conn->bufpos >= conn->available) {
131 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
135 "mutt_socket_readchar: attempt to read from closed connection.\n"));
139 if (conn->available == 0) {
140 mutt_error (_("Connection to %s closed"), conn->account.host);
143 if (conn->available <= 0) {
144 mutt_socket_close (conn);
148 *c = conn->inbuf[conn->bufpos];
153 int mutt_socket_readln_d (char *buf, size_t buflen, CONNECTION * conn,
159 for (i = 0; i < buflen - 1; i++) {
160 if (mutt_socket_readchar (conn, &ch) != 1) {
170 /* strip \r from \r\n termination */
171 if (i && buf[i - 1] == '\r')
176 dprint (dbg, (debugfile, "< %s\n", buf));
178 /* number of bytes read, not mutt_strlen */
182 CONNECTION *mutt_socket_head (void)
187 /* mutt_socket_free: remove connection from connection list and free it */
188 void mutt_socket_free (CONNECTION * conn)
195 /* head is special case, doesn't need prev updated */
197 Connections = iter->next;
203 if (iter->next == conn) {
205 iter->next = tmp->next;
213 /* mutt_conn_find: find a connection off the list of connections whose
214 * account matches account. If start is not null, only search for
215 * connections after the given connection (allows higher level socket code
216 * to make more fine-grained searches than account info - eg in IMAP we may
217 * wish to find a connection which is not in IMAP_SELECTED state) */
218 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
222 char hook[LONG_STRING];
224 /* account isn't actually modified, since url isn't either */
225 mutt_account_tourl ((ACCOUNT *) account, &url);
227 url_ciss_tostring (&url, hook, sizeof (hook), 0);
228 mutt_account_hook (hook);
230 conn = start ? start->next : Connections;
232 if (mutt_account_match (account, &(conn->account)))
237 conn = socket_new_conn ();
238 memcpy (&conn->account, account, sizeof (ACCOUNT));
240 conn->next = Connections;
243 if (Tunnel && *Tunnel)
244 mutt_tunnel_socket_setup (conn);
245 else if (account->flags & M_ACCT_SSL) {
247 ssl_socket_setup (conn);
249 mutt_nss_socket_setup (conn);
251 if (mutt_gnutls_socket_setup (conn) < 0) {
252 mutt_socket_free (conn);
256 mutt_error _("SSL is unavailable.");
259 mutt_socket_free (conn);
265 conn->conn_read = raw_socket_read;
266 conn->conn_write = raw_socket_write;
267 conn->conn_open = raw_socket_open;
268 conn->conn_close = raw_socket_close;
274 static int socket_preconnect (void)
279 if (mutt_strlen (Preconnect)) {
280 dprint (2, (debugfile, "Executing preconnect: %s\n", Preconnect));
281 rc = mutt_system (Preconnect);
282 dprint (2, (debugfile, "Preconnect result: %d\n", rc));
285 mutt_perror (_("Preconnect command failed."));
295 /* socket_connect: set up to connect to a socket fd. */
296 static int socket_connect (int fd, struct sockaddr *sa)
301 if (sa->sa_family == AF_INET)
302 sa_size = sizeof (struct sockaddr_in);
303 #ifdef HAVE_GETADDRINFO
304 else if (sa->sa_family == AF_INET6)
305 sa_size = sizeof (struct sockaddr_in6);
308 dprint (1, (debugfile, "Unknown address family!\n"));
312 if (ConnectTimeout > 0)
313 alarm (ConnectTimeout);
315 mutt_allow_interrupt (1);
319 if (connect (fd, sa, sa_size) < 0) {
321 dprint (2, (debugfile, "Connection failed. errno: %d...\n", errno));
322 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
325 if (ConnectTimeout > 0)
327 mutt_allow_interrupt (0);
332 /* socket_new_conn: allocate and initialise a new connection. */
333 static CONNECTION *socket_new_conn ()
337 conn = (CONNECTION *) safe_calloc (1, sizeof (CONNECTION));
343 int raw_socket_close (CONNECTION * conn)
345 return close (conn->fd);
348 int raw_socket_read (CONNECTION * conn, char *buf, size_t len)
352 if ((rc = read (conn->fd, buf, len)) == -1) {
353 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
361 int raw_socket_write (CONNECTION * conn, const char *buf, size_t count)
365 if ((rc = write (conn->fd, buf, count)) == -1) {
366 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
374 int raw_socket_open (CONNECTION * conn)
379 char *host_idna = NULL;
381 #ifdef HAVE_GETADDRINFO
386 struct addrinfo hints;
387 struct addrinfo *res;
388 struct addrinfo *cur;
390 /* we accept v4 or v6 STREAM sockets */
391 memset (&hints, 0, sizeof (hints));
393 if (option (OPTUSEIPV6))
394 hints.ai_family = AF_UNSPEC;
396 hints.ai_family = AF_INET;
398 hints.ai_socktype = SOCK_STREAM;
400 snprintf (port, sizeof (port), "%d", conn->account.port);
403 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
404 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
408 host_idna = conn->account.host;
411 mutt_message (_("Looking up %s..."), conn->account.host);
414 rc = getaddrinfo (host_idna, port, &hints, &res);
421 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
425 mutt_message (_("Connecting to %s..."), conn->account.host);
428 for (cur = res; cur != NULL; cur = cur->ai_next) {
429 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
431 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
432 fcntl (fd, F_SETFD, FD_CLOEXEC);
444 /* --- IPv4 only --- */
446 struct sockaddr_in sin;
450 memset (&sin, 0, sizeof (sin));
451 sin.sin_port = htons (conn->account.port);
452 sin.sin_family = AF_INET;
455 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
456 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
460 host_idna = conn->account.host;
463 mutt_message (_("Looking up %s..."), conn->account.host);
465 if ((he = gethostbyname (host_idna)) == NULL) {
469 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
478 mutt_message (_("Connecting to %s..."), conn->account.host);
481 for (i = 0; he->h_addr_list[i] != NULL; i++) {
482 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
483 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
486 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
497 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
498 (rc > 0) ? strerror (rc) : _("unknown error"));