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.
12 #include <lib-lib/lib-lib.h>
14 #include <netinet/in.h>
16 #include <sys/socket.h>
18 #include <lib-ui/curses.h>
24 #include "mutt_socket.h"
25 #include "mutt_tunnel.h"
26 #include "mutt_signal.h"
29 #include "mutt_idna.h"
31 /* support for multiple socket connections */
32 static CONNECTION *Connections = NULL;
34 /* forward declarations */
35 static int socket_preconnect (void);
36 static int socket_connect (int fd, struct sockaddr *sa);
37 static CONNECTION *socket_new_conn (void);
40 int mutt_socket_open (CONNECTION * conn)
42 if (socket_preconnect ())
45 return conn->conn_open (conn);
48 int mutt_socket_close (CONNECTION * conn)
53 rc = conn->conn_close (conn);
61 int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len)
69 rc = conn->conn_read (conn, buf, len);
72 mutt_error (_("Connection to %s closed"), conn->account.host);
76 mutt_socket_close (conn);
81 int mutt_socket_write(CONNECTION * conn, const char *buf)
91 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
92 mutt_socket_close (conn);
100 /* simple read buffering to speed things up. */
101 int mutt_socket_readchar (CONNECTION * conn, char *c)
103 if (conn->bufpos >= conn->available) {
106 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
111 if (conn->available == 0) {
112 mutt_error (_("Connection to %s closed"), conn->account.host);
115 if (conn->available <= 0) {
116 mutt_socket_close (conn);
120 *c = conn->inbuf[conn->bufpos];
125 int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn)
130 for (i = 0; i < buflen - 1; i++) {
131 if (mutt_socket_readchar (conn, &ch) != 1) {
141 /* strip \r from \r\n termination */
142 if (i && buf[i - 1] == '\r')
147 /* number of bytes read, not m_strlen*/
151 CONNECTION *mutt_socket_head (void)
156 /* mutt_socket_free: remove connection from connection list and free it */
157 void mutt_socket_free (CONNECTION * conn)
164 /* head is special case, doesn't need prev updated */
166 Connections = iter->next;
172 if (iter->next == conn) {
174 iter->next = tmp->next;
182 /* mutt_conn_find: find a connection off the list of connections whose
183 * account matches account. If start is not null, only search for
184 * connections after the given connection (allows higher level socket code
185 * to make more fine-grained searches than account info - eg in IMAP we may
186 * wish to find a connection which is not in IMAP_SELECTED state) */
187 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
191 char hook[LONG_STRING];
193 /* account isn't actually modified, since url isn't either */
194 mutt_account_tourl ((ACCOUNT *) account, &url);
196 url_ciss_tostring (&url, hook, sizeof (hook), 0);
197 mutt_account_hook (hook);
199 conn = start ? start->next : Connections;
201 if (mutt_account_match (account, &(conn->account)))
206 conn = socket_new_conn ();
207 memcpy (&conn->account, account, sizeof (ACCOUNT));
209 conn->next = Connections;
212 if (Tunnel && *Tunnel)
213 mutt_tunnel_socket_setup (conn);
214 else if (account->flags & M_ACCT_SSL) {
215 #if defined (USE_SSL) || defined (USE_GNUTLS)
216 if (mutt_ssl_socket_setup (conn) < 0) {
217 mutt_socket_free (conn);
221 mutt_error _("SSL is unavailable.");
224 mutt_socket_free (conn);
230 conn->conn_read = raw_socket_read;
231 conn->conn_write = raw_socket_write;
232 conn->conn_open = raw_socket_open;
233 conn->conn_close = raw_socket_close;
239 static int socket_preconnect (void)
244 if (m_strlen(Preconnect)) {
245 rc = mutt_system (Preconnect);
248 mutt_perror (_("Preconnect command failed."));
258 /* socket_connect: set up to connect to a socket fd. */
259 static int socket_connect (int fd, struct sockaddr *sa)
264 if (sa->sa_family == AF_INET)
265 sa_size = sizeof (struct sockaddr_in);
266 #ifdef HAVE_GETADDRINFO
267 else if (sa->sa_family == AF_INET6)
268 sa_size = sizeof (struct sockaddr_in6);
274 if (ConnectTimeout > 0)
275 alarm (ConnectTimeout);
277 mutt_allow_interrupt (1);
281 if (connect (fd, sa, sa_size) < 0) {
283 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
286 if (ConnectTimeout > 0)
288 mutt_allow_interrupt (0);
293 /* socket_new_conn: allocate and initialise a new connection. */
294 static CONNECTION *socket_new_conn (void)
298 conn = p_new(CONNECTION, 1);
304 int raw_socket_close (CONNECTION * conn)
306 return close (conn->fd);
309 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
313 if ((rc = read (conn->fd, buf, len)) == -1) {
314 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
322 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
326 if ((rc = write (conn->fd, buf, count)) == -1) {
327 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
335 int raw_socket_open (CONNECTION * conn)
340 char *host_idna = NULL;
342 #ifdef HAVE_GETADDRINFO
347 struct addrinfo hints;
348 struct addrinfo *res;
349 struct addrinfo *cur;
351 /* we accept v4 or v6 STREAM sockets */
354 if (option (OPTUSEIPV6))
355 hints.ai_family = AF_UNSPEC;
357 hints.ai_family = AF_INET;
359 hints.ai_socktype = SOCK_STREAM;
361 snprintf (port, sizeof (port), "%d", conn->account.port);
364 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
365 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
369 host_idna = conn->account.host;
372 mutt_message (_("Looking up %s..."), conn->account.host);
375 rc = getaddrinfo (host_idna, port, &hints, &res);
378 p_delete(&host_idna);
382 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
387 mutt_message (_("Connecting to %s..."), conn->account.host);
390 for (cur = res; cur != NULL; cur = cur->ai_next) {
391 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
393 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
394 fcntl (fd, F_SETFD, FD_CLOEXEC);
406 /* --- IPv4 only --- */
408 struct sockaddr_in sin;
413 sin.sin_port = htons (conn->account.port);
414 sin.sin_family = AF_INET;
417 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
418 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
422 host_idna = conn->account.host;
425 mutt_message (_("Looking up %s..."), conn->account.host);
427 if ((he = gethostbyname (host_idna)) == NULL) {
429 p_delete(&host_idna);
431 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
437 p_delete(&host_idna);
440 mutt_message (_("Connecting to %s..."), conn->account.host);
443 for (i = 0; he->h_addr_list[i] != NULL; i++) {
444 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
445 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
448 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
449 fcntl (fd, F_SETFD, FD_CLOEXEC);
460 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
461 (rc > 0) ? strerror (rc) : _("unknown error"));