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(CONNECTION * conn, const char *buf)
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(char *buf, ssize_t buflen, CONNECTION * conn)
140 for (i = 0; i < buflen - 1; i++) {
141 if (mutt_socket_readchar (conn, &ch) != 1) {
151 /* strip \r from \r\n termination */
152 if (i && buf[i - 1] == '\r')
157 /* number of bytes read, not m_strlen*/
161 CONNECTION *mutt_socket_head (void)
166 /* mutt_socket_free: remove connection from connection list and free it */
167 void mutt_socket_free (CONNECTION * conn)
174 /* head is special case, doesn't need prev updated */
176 Connections = iter->next;
182 if (iter->next == conn) {
184 iter->next = tmp->next;
192 /* mutt_conn_find: find a connection off the list of connections whose
193 * account matches account. If start is not null, only search for
194 * connections after the given connection (allows higher level socket code
195 * to make more fine-grained searches than account info - eg in IMAP we may
196 * wish to find a connection which is not in IMAP_SELECTED state) */
197 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
201 char hook[LONG_STRING];
203 /* account isn't actually modified, since url isn't either */
204 mutt_account_tourl ((ACCOUNT *) account, &url);
206 url_ciss_tostring (&url, hook, sizeof (hook), 0);
207 mutt_account_hook (hook);
209 conn = start ? start->next : Connections;
211 if (mutt_account_match (account, &(conn->account)))
216 conn = socket_new_conn ();
217 memcpy (&conn->account, account, sizeof (ACCOUNT));
219 conn->next = Connections;
222 if (Tunnel && *Tunnel)
223 mutt_tunnel_socket_setup (conn);
224 else if (account->flags & M_ACCT_SSL) {
225 #if defined (USE_SSL) || defined (USE_GNUTLS)
226 if (mutt_ssl_socket_setup (conn) < 0) {
227 mutt_socket_free (conn);
231 mutt_error _("SSL is unavailable.");
234 mutt_socket_free (conn);
240 conn->conn_read = raw_socket_read;
241 conn->conn_write = raw_socket_write;
242 conn->conn_open = raw_socket_open;
243 conn->conn_close = raw_socket_close;
249 static int socket_preconnect (void)
254 if (m_strlen(Preconnect)) {
255 rc = mutt_system (Preconnect);
258 mutt_perror (_("Preconnect command failed."));
268 /* socket_connect: set up to connect to a socket fd. */
269 static int socket_connect (int fd, struct sockaddr *sa)
274 if (sa->sa_family == AF_INET)
275 sa_size = sizeof (struct sockaddr_in);
276 #ifdef HAVE_GETADDRINFO
277 else if (sa->sa_family == AF_INET6)
278 sa_size = sizeof (struct sockaddr_in6);
284 if (ConnectTimeout > 0)
285 alarm (ConnectTimeout);
287 mutt_allow_interrupt (1);
291 if (connect (fd, sa, sa_size) < 0) {
293 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
296 if (ConnectTimeout > 0)
298 mutt_allow_interrupt (0);
303 /* socket_new_conn: allocate and initialise a new connection. */
304 static CONNECTION *socket_new_conn (void)
308 conn = p_new(CONNECTION, 1);
314 int raw_socket_close (CONNECTION * conn)
316 return close (conn->fd);
319 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
323 if ((rc = read (conn->fd, buf, len)) == -1) {
324 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
332 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
336 if ((rc = write (conn->fd, buf, count)) == -1) {
337 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
345 int raw_socket_open (CONNECTION * conn)
350 char *host_idna = NULL;
352 #ifdef HAVE_GETADDRINFO
357 struct addrinfo hints;
358 struct addrinfo *res;
359 struct addrinfo *cur;
361 /* we accept v4 or v6 STREAM sockets */
364 if (option (OPTUSEIPV6))
365 hints.ai_family = AF_UNSPEC;
367 hints.ai_family = AF_INET;
369 hints.ai_socktype = SOCK_STREAM;
371 snprintf (port, sizeof (port), "%d", conn->account.port);
374 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
375 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
379 host_idna = conn->account.host;
382 mutt_message (_("Looking up %s..."), conn->account.host);
385 rc = getaddrinfo (host_idna, port, &hints, &res);
388 p_delete(&host_idna);
392 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
397 mutt_message (_("Connecting to %s..."), conn->account.host);
400 for (cur = res; cur != NULL; cur = cur->ai_next) {
401 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
403 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
404 fcntl (fd, F_SETFD, FD_CLOEXEC);
416 /* --- IPv4 only --- */
418 struct sockaddr_in sin;
423 sin.sin_port = htons (conn->account.port);
424 sin.sin_family = AF_INET;
427 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
428 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
432 host_idna = conn->account.host;
435 mutt_message (_("Looking up %s..."), conn->account.host);
437 if ((he = gethostbyname (host_idna)) == NULL) {
439 p_delete(&host_idna);
441 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
447 p_delete(&host_idna);
450 mutt_message (_("Connecting to %s..."), conn->account.host);
453 for (i = 0; he->h_addr_list[i] != NULL; i++) {
454 memcpy (&sin.sin_addr, he->h_addr_list[i], he->h_length);
455 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_IP);
458 if ((rc = socket_connect (fd, (struct sockaddr *) &sin)) == 0) {
459 fcntl (fd, F_SETFD, FD_CLOEXEC);
470 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
471 (rc > 0) ? strerror (rc) : _("unknown error"));