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.
16 #include <lib-lib/mem.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/macros.h>
19 #include <lib-lib/debug.h>
23 #include "mutt_socket.h"
24 #include "mutt_tunnel.h"
25 #if defined(USE_SSL) || defined(USE_GNUTLS)
26 # include "mutt_ssl.h"
29 #include "mutt_idna.h"
33 #include <netinet/in.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
42 /* support for multiple socket connections */
43 static CONNECTION *Connections = NULL;
45 /* forward declarations */
46 static int socket_preconnect (void);
47 static int socket_connect (int fd, struct sockaddr *sa);
48 static CONNECTION *socket_new_conn (void);
51 int mutt_socket_open (CONNECTION * conn)
53 if (socket_preconnect ())
56 return conn->conn_open (conn);
59 int mutt_socket_close (CONNECTION * conn)
64 debug_print (1, ("Attempt to close closed connection.\n"));
66 rc = conn->conn_close (conn);
74 int mutt_socket_read (CONNECTION * conn, char *buf, size_t len)
79 debug_print (1, ("attempt to read from closed connection\n"));
83 rc = conn->conn_read (conn, buf, len);
86 mutt_error (_("Connection to %s closed"), conn->account.host);
90 mutt_socket_close (conn);
95 int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg)
100 debug_print (dbg, ("> %s", buf));
103 debug_print (1, ("attempt to write to closed connection\n"));
108 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
109 debug_print (1, ("error writing, closing socket\n"));
110 mutt_socket_close (conn);
116 debug_print (1, ("ERROR: wrote %d of %d bytes!\n", rc, len));
122 /* simple read buffering to speed things up. */
123 int mutt_socket_readchar (CONNECTION * conn, char *c)
125 if (conn->bufpos >= conn->available) {
128 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
130 debug_print (1, ("attempt to read from closed connection.\n"));
134 if (conn->available == 0) {
135 mutt_error (_("Connection to %s closed"), conn->account.host);
138 if (conn->available <= 0) {
139 mutt_socket_close (conn);
143 *c = conn->inbuf[conn->bufpos];
148 int mutt_socket_readln_d (char *buf, ssize_t buflen, CONNECTION * conn,
154 for (i = 0; i < buflen - 1; i++) {
155 if (mutt_socket_readchar (conn, &ch) != 1) {
165 /* strip \r from \r\n termination */
166 if (i && buf[i - 1] == '\r')
171 debug_print (dbg, ("< %s\n", buf));
173 /* number of bytes read, not m_strlen*/
177 CONNECTION *mutt_socket_head (void)
182 /* mutt_socket_free: remove connection from connection list and free it */
183 void mutt_socket_free (CONNECTION * conn)
190 /* head is special case, doesn't need prev updated */
192 Connections = iter->next;
198 if (iter->next == conn) {
200 iter->next = tmp->next;
208 /* mutt_conn_find: find a connection off the list of connections whose
209 * account matches account. If start is not null, only search for
210 * connections after the given connection (allows higher level socket code
211 * to make more fine-grained searches than account info - eg in IMAP we may
212 * wish to find a connection which is not in IMAP_SELECTED state) */
213 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
217 char hook[LONG_STRING];
219 /* account isn't actually modified, since url isn't either */
220 mutt_account_tourl ((ACCOUNT *) account, &url);
222 url_ciss_tostring (&url, hook, sizeof (hook), 0);
223 mutt_account_hook (hook);
225 conn = start ? start->next : Connections;
227 if (mutt_account_match (account, &(conn->account)))
232 conn = socket_new_conn ();
233 memcpy (&conn->account, account, sizeof (ACCOUNT));
235 conn->next = Connections;
238 if (Tunnel && *Tunnel)
239 mutt_tunnel_socket_setup (conn);
240 else if (account->flags & M_ACCT_SSL) {
241 #if defined (USE_SSL) || defined (USE_GNUTLS)
242 if (mutt_ssl_socket_setup (conn) < 0) {
243 mutt_socket_free (conn);
247 mutt_error _("SSL is unavailable.");
250 mutt_socket_free (conn);
256 conn->conn_read = raw_socket_read;
257 conn->conn_write = raw_socket_write;
258 conn->conn_open = raw_socket_open;
259 conn->conn_close = raw_socket_close;
265 static int socket_preconnect (void)
270 if (m_strlen(Preconnect)) {
271 debug_print (2, ("Executing preconnect: %s\n", Preconnect));
272 rc = mutt_system (Preconnect);
273 debug_print (2, ("Preconnect result: %d\n", rc));
276 mutt_perror (_("Preconnect command failed."));
286 /* socket_connect: set up to connect to a socket fd. */
287 static int socket_connect (int fd, struct sockaddr *sa)
292 if (sa->sa_family == AF_INET)
293 sa_size = sizeof (struct sockaddr_in);
294 #ifdef HAVE_GETADDRINFO
295 else if (sa->sa_family == AF_INET6)
296 sa_size = sizeof (struct sockaddr_in6);
299 debug_print (1, ("Unknown address family!\n"));
303 if (ConnectTimeout > 0)
304 alarm (ConnectTimeout);
306 mutt_allow_interrupt (1);
310 if (connect (fd, sa, sa_size) < 0) {
312 debug_print (2, ("Connection failed. errno: %d...\n", errno));
313 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
316 if (ConnectTimeout > 0)
318 mutt_allow_interrupt (0);
323 /* socket_new_conn: allocate and initialise a new connection. */
324 static CONNECTION *socket_new_conn (void)
328 conn = p_new(CONNECTION, 1);
334 int raw_socket_close (CONNECTION * conn)
336 return close (conn->fd);
339 int raw_socket_read (CONNECTION * conn, char *buf, size_t len)
343 if ((rc = read (conn->fd, buf, len)) == -1) {
344 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
352 int raw_socket_write (CONNECTION * conn, const char *buf, size_t count)
356 if ((rc = write (conn->fd, buf, count)) == -1) {
357 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
365 int raw_socket_open (CONNECTION * conn)
370 char *host_idna = NULL;
372 #ifdef HAVE_GETADDRINFO
377 struct addrinfo hints;
378 struct addrinfo *res;
379 struct addrinfo *cur;
381 /* we accept v4 or v6 STREAM sockets */
384 if (option (OPTUSEIPV6))
385 hints.ai_family = AF_UNSPEC;
387 hints.ai_family = AF_INET;
389 hints.ai_socktype = SOCK_STREAM;
391 snprintf (port, sizeof (port), "%d", conn->account.port);
394 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
395 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
399 host_idna = conn->account.host;
402 mutt_message (_("Looking up %s..."), conn->account.host);
405 rc = getaddrinfo (host_idna, port, &hints, &res);
408 p_delete(&host_idna);
412 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
417 mutt_message (_("Connecting to %s..."), conn->account.host);
420 for (cur = res; cur != NULL; cur = cur->ai_next) {
421 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
423 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
424 fcntl (fd, F_SETFD, FD_CLOEXEC);
436 /* --- IPv4 only --- */
438 struct sockaddr_in sin;
443 sin.sin_port = htons (conn->account.port);
444 sin.sin_family = AF_INET;
447 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
448 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
452 host_idna = conn->account.host;
455 mutt_message (_("Looking up %s..."), conn->account.host);
457 if ((he = gethostbyname (host_idna)) == NULL) {
459 p_delete(&host_idna);
461 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
467 p_delete(&host_idna);
470 mutt_message (_("Connecting to %s..."), conn->account.host);
473 for (i = 0; he->h_addr_list[i] != NULL; i++) {
474 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
475 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
478 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
479 fcntl (fd, F_SETFD, FD_CLOEXEC);
490 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
491 (rc > 0) ? strerror (rc) : _("unknown error"));