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"
31 #include "mutt_idna.h"
33 /* support for multiple socket connections */
34 static CONNECTION *Connections = NULL;
36 /* forward declarations */
37 static int socket_preconnect (void);
38 static int socket_connect (int fd, struct sockaddr *sa);
39 static CONNECTION *socket_new_conn (void);
42 int mutt_socket_open (CONNECTION * conn)
44 if (socket_preconnect ())
47 return conn->conn_open (conn);
50 int mutt_socket_close (CONNECTION * conn)
55 rc = conn->conn_close (conn);
63 int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len)
71 rc = conn->conn_read (conn, buf, len);
74 mutt_error (_("Connection to %s closed"), conn->account.host);
78 mutt_socket_close (conn);
83 int mutt_socket_write(CONNECTION * conn, const char *buf)
93 if ((rc = conn->conn_write (conn, buf, len)) < 0) {
94 mutt_socket_close (conn);
102 /* simple read buffering to speed things up. */
103 int mutt_socket_readchar (CONNECTION * conn, char *c)
105 if (conn->bufpos >= conn->available) {
108 conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
113 if (conn->available == 0) {
114 mutt_error (_("Connection to %s closed"), conn->account.host);
117 if (conn->available <= 0) {
118 mutt_socket_close (conn);
122 *c = conn->inbuf[conn->bufpos];
127 int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn)
132 for (i = 0; i < buflen - 1; i++) {
133 if (mutt_socket_readchar (conn, &ch) != 1) {
143 /* strip \r from \r\n termination */
144 if (i && buf[i - 1] == '\r')
149 /* number of bytes read, not m_strlen*/
153 int mutt_socket_readln2(buffer_t *buf, CONNECTION *conn)
157 while (mutt_socket_readchar(conn, &ch) == 1) {
159 if (buf->len && buf->data[buf->len - 1] == '\r') {
160 buf->data[--buf->len] = '\0';
164 buffer_addch(buf, ch);
170 CONNECTION *mutt_socket_head (void)
175 /* mutt_socket_free: remove connection from connection list and free it */
176 void mutt_socket_free (CONNECTION * conn)
183 /* head is special case, doesn't need prev updated */
185 Connections = iter->next;
191 if (iter->next == conn) {
193 iter->next = tmp->next;
201 /* mutt_conn_find: find a connection off the list of connections whose
202 * account matches account. If start is not null, only search for
203 * connections after the given connection (allows higher level socket code
204 * to make more fine-grained searches than account info - eg in IMAP we may
205 * wish to find a connection which is not in IMAP_SELECTED state) */
206 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
210 char hook[LONG_STRING];
212 /* account isn't actually modified, since url isn't either */
213 mutt_account_tourl ((ACCOUNT *) account, &url);
215 url_ciss_tostring (&url, hook, sizeof (hook), 0);
216 mutt_account_hook (hook);
218 conn = start ? start->next : Connections;
220 if (mutt_account_match (account, &(conn->account)))
225 conn = socket_new_conn ();
226 memcpy (&conn->account, account, sizeof (ACCOUNT));
228 conn->next = Connections;
231 if (Tunnel && *Tunnel)
232 mutt_tunnel_socket_setup (conn);
233 else if (account->flags & M_ACCT_SSL) {
234 if (mutt_ssl_socket_setup (conn) < 0) {
235 mutt_socket_free (conn);
239 conn->conn_read = raw_socket_read;
240 conn->conn_write = raw_socket_write;
241 conn->conn_open = raw_socket_open;
242 conn->conn_close = raw_socket_close;
248 static int socket_preconnect (void)
253 if (m_strlen(Preconnect)) {
254 rc = mutt_system (Preconnect);
257 mutt_perror (_("Preconnect command failed."));
267 /* socket_connect: set up to connect to a socket fd. */
268 static int socket_connect (int fd, struct sockaddr *sa)
273 if (sa->sa_family == AF_INET)
274 sa_size = sizeof (struct sockaddr_in);
275 else if (sa->sa_family == AF_INET6)
276 sa_size = sizeof (struct sockaddr_in6);
281 if (ConnectTimeout > 0)
282 alarm (ConnectTimeout);
284 mutt_allow_interrupt (1);
288 if (connect (fd, sa, sa_size) < 0) {
290 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
293 if (ConnectTimeout > 0)
295 mutt_allow_interrupt (0);
300 /* socket_new_conn: allocate and initialise a new connection. */
301 static CONNECTION *socket_new_conn (void)
305 conn = p_new(CONNECTION, 1);
311 int raw_socket_close (CONNECTION * conn)
313 return close (conn->fd);
316 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
320 if ((rc = read (conn->fd, buf, len)) == -1) {
321 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
329 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
333 if ((rc = write (conn->fd, buf, count)) == -1) {
334 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
342 int raw_socket_open (CONNECTION * conn)
347 char *host_idna = NULL;
351 struct addrinfo hints;
352 struct addrinfo *res;
353 struct addrinfo *cur;
355 /* we accept v4 or v6 STREAM sockets */
358 if (option (OPTUSEIPV6))
359 hints.ai_family = AF_UNSPEC;
361 hints.ai_family = AF_INET;
363 hints.ai_socktype = SOCK_STREAM;
365 snprintf (port, sizeof (port), "%d", conn->account.port);
368 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
369 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
373 host_idna = conn->account.host;
376 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 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
410 (rc > 0) ? strerror (rc) : _("unknown error"));