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.
17 #include <netinet/in.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
26 #include <lib-lib/mem.h>
27 #include <lib-lib/str.h>
28 #include <lib-lib/macros.h>
34 #include "mutt_socket.h"
35 #include "mutt_tunnel.h"
36 #include "mutt_signal.h"
39 #include "mutt_idna.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 rc = conn->conn_close (conn);
71 int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len)
79 rc = conn->conn_read (conn, buf, len);
82 mutt_error (_("Connection to %s closed"), conn->account.host);
86 mutt_socket_close (conn);
91 int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg)
101 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
102 mutt_socket_close (conn);
110 /* simple read buffering to speed things up. */
111 int mutt_socket_readchar (CONNECTION * conn, char *c)
113 if (conn->bufpos >= conn->available) {
116 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
121 if (conn->available == 0) {
122 mutt_error (_("Connection to %s closed"), conn->account.host);
125 if (conn->available <= 0) {
126 mutt_socket_close (conn);
130 *c = conn->inbuf[conn->bufpos];
135 int mutt_socket_readln_d (char *buf, ssize_t buflen, CONNECTION * conn,
141 for (i = 0; i < buflen - 1; i++) {
142 if (mutt_socket_readchar (conn, &ch) != 1) {
152 /* strip \r from \r\n termination */
153 if (i && buf[i - 1] == '\r')
158 /* number of bytes read, not m_strlen*/
162 CONNECTION *mutt_socket_head (void)
167 /* mutt_socket_free: remove connection from connection list and free it */
168 void mutt_socket_free (CONNECTION * conn)
175 /* head is special case, doesn't need prev updated */
177 Connections = iter->next;
183 if (iter->next == conn) {
185 iter->next = tmp->next;
193 /* mutt_conn_find: find a connection off the list of connections whose
194 * account matches account. If start is not null, only search for
195 * connections after the given connection (allows higher level socket code
196 * to make more fine-grained searches than account info - eg in IMAP we may
197 * wish to find a connection which is not in IMAP_SELECTED state) */
198 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
202 char hook[LONG_STRING];
204 /* account isn't actually modified, since url isn't either */
205 mutt_account_tourl ((ACCOUNT *) account, &url);
207 url_ciss_tostring (&url, hook, sizeof (hook), 0);
208 mutt_account_hook (hook);
210 conn = start ? start->next : Connections;
212 if (mutt_account_match (account, &(conn->account)))
217 conn = socket_new_conn ();
218 memcpy (&conn->account, account, sizeof (ACCOUNT));
220 conn->next = Connections;
223 if (Tunnel && *Tunnel)
224 mutt_tunnel_socket_setup (conn);
225 else if (account->flags & M_ACCT_SSL) {
226 #if defined (USE_SSL) || defined (USE_GNUTLS)
227 if (mutt_ssl_socket_setup (conn) < 0) {
228 mutt_socket_free (conn);
232 mutt_error _("SSL is unavailable.");
235 mutt_socket_free (conn);
241 conn->conn_read = raw_socket_read;
242 conn->conn_write = raw_socket_write;
243 conn->conn_open = raw_socket_open;
244 conn->conn_close = raw_socket_close;
250 static int socket_preconnect (void)
255 if (m_strlen(Preconnect)) {
256 rc = mutt_system (Preconnect);
259 mutt_perror (_("Preconnect command failed."));
269 /* socket_connect: set up to connect to a socket fd. */
270 static int socket_connect (int fd, struct sockaddr *sa)
275 if (sa->sa_family == AF_INET)
276 sa_size = sizeof (struct sockaddr_in);
277 #ifdef HAVE_GETADDRINFO
278 else if (sa->sa_family == AF_INET6)
279 sa_size = sizeof (struct sockaddr_in6);
285 if (ConnectTimeout > 0)
286 alarm (ConnectTimeout);
288 mutt_allow_interrupt (1);
292 if (connect (fd, sa, sa_size) < 0) {
294 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
297 if (ConnectTimeout > 0)
299 mutt_allow_interrupt (0);
304 /* socket_new_conn: allocate and initialise a new connection. */
305 static CONNECTION *socket_new_conn (void)
309 conn = p_new(CONNECTION, 1);
315 int raw_socket_close (CONNECTION * conn)
317 return close (conn->fd);
320 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
324 if ((rc = read (conn->fd, buf, len)) == -1) {
325 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
333 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
337 if ((rc = write (conn->fd, buf, count)) == -1) {
338 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
346 int raw_socket_open (CONNECTION * conn)
351 char *host_idna = NULL;
353 #ifdef HAVE_GETADDRINFO
358 struct addrinfo hints;
359 struct addrinfo *res;
360 struct addrinfo *cur;
362 /* we accept v4 or v6 STREAM sockets */
365 if (option (OPTUSEIPV6))
366 hints.ai_family = AF_UNSPEC;
368 hints.ai_family = AF_INET;
370 hints.ai_socktype = SOCK_STREAM;
372 snprintf (port, sizeof (port), "%d", conn->account.port);
375 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
376 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
380 host_idna = conn->account.host;
383 mutt_message (_("Looking up %s..."), conn->account.host);
386 rc = getaddrinfo (host_idna, port, &hints, &res);
389 p_delete(&host_idna);
393 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
398 mutt_message (_("Connecting to %s..."), conn->account.host);
401 for (cur = res; cur != NULL; cur = cur->ai_next) {
402 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
404 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
405 fcntl (fd, F_SETFD, FD_CLOEXEC);
417 /* --- IPv4 only --- */
419 struct sockaddr_in sin;
424 sin.sin_port = htons (conn->account.port);
425 sin.sin_family = AF_INET;
428 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
429 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
433 host_idna = conn->account.host;
436 mutt_message (_("Looking up %s..."), conn->account.host);
438 if ((he = gethostbyname (host_idna)) == NULL) {
440 p_delete(&host_idna);
442 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
448 p_delete(&host_idna);
451 mutt_message (_("Connecting to %s..."), conn->account.host);
454 for (i = 0; he->h_addr_list[i] != NULL; i++) {
455 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
456 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
459 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
460 fcntl (fd, F_SETFD, FD_CLOEXEC);
471 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
472 (rc > 0) ? strerror (rc) : _("unknown error"));