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"
32 #include "mutt_idna.h"
34 /* support for multiple socket connections */
35 static CONNECTION *Connections = NULL;
37 /* forward declarations */
38 static int socket_preconnect (void);
39 static int socket_connect (int fd, struct sockaddr *sa);
40 static CONNECTION *socket_new_conn (void);
43 int mutt_socket_open (CONNECTION * conn)
45 if (socket_preconnect ())
48 return conn->conn_open (conn);
51 int mutt_socket_close (CONNECTION * conn)
56 rc = conn->conn_close (conn);
64 int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len)
72 rc = conn->conn_read (conn, buf, len);
75 mutt_error (_("Connection to %s closed"), conn->account.host);
79 mutt_socket_close (conn);
84 int mutt_socket_write(CONNECTION * conn, const char *buf)
94 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
95 mutt_socket_close (conn);
103 /* simple read buffering to speed things up. */
104 int mutt_socket_readchar (CONNECTION * conn, char *c)
106 if (conn->bufpos >= conn->available) {
109 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
114 if (conn->available == 0) {
115 mutt_error (_("Connection to %s closed"), conn->account.host);
118 if (conn->available <= 0) {
119 mutt_socket_close (conn);
123 *c = conn->inbuf[conn->bufpos];
128 int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn)
133 for (i = 0; i < buflen - 1; i++) {
134 if (mutt_socket_readchar (conn, &ch) != 1) {
144 /* strip \r from \r\n termination */
145 if (i && buf[i - 1] == '\r')
150 /* number of bytes read, not m_strlen*/
154 CONNECTION *mutt_socket_head (void)
159 /* mutt_socket_free: remove connection from connection list and free it */
160 void mutt_socket_free (CONNECTION * conn)
167 /* head is special case, doesn't need prev updated */
169 Connections = iter->next;
175 if (iter->next == conn) {
177 iter->next = tmp->next;
185 /* mutt_conn_find: find a connection off the list of connections whose
186 * account matches account. If start is not null, only search for
187 * connections after the given connection (allows higher level socket code
188 * to make more fine-grained searches than account info - eg in IMAP we may
189 * wish to find a connection which is not in IMAP_SELECTED state) */
190 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
194 char hook[LONG_STRING];
196 /* account isn't actually modified, since url isn't either */
197 mutt_account_tourl ((ACCOUNT *) account, &url);
199 url_ciss_tostring (&url, hook, sizeof (hook), 0);
200 mutt_account_hook (hook);
202 conn = start ? start->next : Connections;
204 if (mutt_account_match (account, &(conn->account)))
209 conn = socket_new_conn ();
210 memcpy (&conn->account, account, sizeof (ACCOUNT));
212 conn->next = Connections;
215 if (Tunnel && *Tunnel)
216 mutt_tunnel_socket_setup (conn);
217 else if (account->flags & M_ACCT_SSL) {
218 #if defined (USE_SSL) || defined (USE_GNUTLS)
219 if (mutt_ssl_socket_setup (conn) < 0) {
220 mutt_socket_free (conn);
224 mutt_error _("SSL is unavailable.");
227 mutt_socket_free (conn);
233 conn->conn_read = raw_socket_read;
234 conn->conn_write = raw_socket_write;
235 conn->conn_open = raw_socket_open;
236 conn->conn_close = raw_socket_close;
242 static int socket_preconnect (void)
247 if (m_strlen(Preconnect)) {
248 rc = mutt_system (Preconnect);
251 mutt_perror (_("Preconnect command failed."));
261 /* socket_connect: set up to connect to a socket fd. */
262 static int socket_connect (int fd, struct sockaddr *sa)
267 if (sa->sa_family == AF_INET)
268 sa_size = sizeof (struct sockaddr_in);
269 #ifdef HAVE_GETADDRINFO
270 else if (sa->sa_family == AF_INET6)
271 sa_size = sizeof (struct sockaddr_in6);
277 if (ConnectTimeout > 0)
278 alarm (ConnectTimeout);
280 mutt_allow_interrupt (1);
284 if (connect (fd, sa, sa_size) < 0) {
286 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
289 if (ConnectTimeout > 0)
291 mutt_allow_interrupt (0);
296 /* socket_new_conn: allocate and initialise a new connection. */
297 static CONNECTION *socket_new_conn (void)
301 conn = p_new(CONNECTION, 1);
307 int raw_socket_close (CONNECTION * conn)
309 return close (conn->fd);
312 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
316 if ((rc = read (conn->fd, buf, len)) == -1) {
317 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
325 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
329 if ((rc = write (conn->fd, buf, count)) == -1) {
330 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
338 int raw_socket_open (CONNECTION * conn)
343 char *host_idna = NULL;
345 #ifdef HAVE_GETADDRINFO
350 struct addrinfo hints;
351 struct addrinfo *res;
352 struct addrinfo *cur;
354 /* we accept v4 or v6 STREAM sockets */
357 if (option (OPTUSEIPV6))
358 hints.ai_family = AF_UNSPEC;
360 hints.ai_family = AF_INET;
362 hints.ai_socktype = SOCK_STREAM;
364 snprintf (port, sizeof (port), "%d", conn->account.port);
367 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
368 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
372 host_idna = conn->account.host;
375 mutt_message (_("Looking up %s..."), conn->account.host);
378 rc = getaddrinfo (host_idna, port, &hints, &res);
381 p_delete(&host_idna);
385 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
390 mutt_message (_("Connecting to %s..."), conn->account.host);
393 for (cur = res; cur != NULL; cur = cur->ai_next) {
394 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
396 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
397 fcntl (fd, F_SETFD, FD_CLOEXEC);
409 /* --- IPv4 only --- */
411 struct sockaddr_in sin;
416 sin.sin_port = htons (conn->account.port);
417 sin.sin_family = AF_INET;
420 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
421 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
425 host_idna = conn->account.host;
428 mutt_message (_("Looking up %s..."), conn->account.host);
430 if ((he = gethostbyname (host_idna)) == NULL) {
432 p_delete(&host_idna);
434 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
440 p_delete(&host_idna);
443 mutt_message (_("Connecting to %s..."), conn->account.host);
446 for (i = 0; he->h_addr_list[i] != NULL; i++) {
447 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
448 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
451 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
452 fcntl (fd, F_SETFD, FD_CLOEXEC);
463 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
464 (rc > 0) ? strerror (rc) : _("unknown error"));