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 conn = start ? start->next : Connections;
212 if (mutt_account_match (account, &(conn->account)))
217 conn = socket_new_conn ();
218 conn->account = *account;
220 conn->next = Connections;
223 if (Tunnel && *Tunnel)
224 mutt_tunnel_socket_setup (conn);
225 else if (account->has_ssl) {
226 if (mutt_ssl_socket_setup (conn) < 0) {
227 mutt_socket_free (conn);
231 conn->conn_read = raw_socket_read;
232 conn->conn_write = raw_socket_write;
233 conn->conn_open = raw_socket_open;
234 conn->conn_close = raw_socket_close;
240 static int socket_preconnect (void)
245 if (m_strlen(Preconnect)) {
246 rc = mutt_system (Preconnect);
249 mutt_perror (_("Preconnect command failed."));
259 /* socket_connect: set up to connect to a socket fd. */
260 static int socket_connect (int fd, struct sockaddr *sa)
265 if (sa->sa_family == AF_INET)
266 sa_size = sizeof (struct sockaddr_in);
267 else if (sa->sa_family == AF_INET6)
268 sa_size = sizeof (struct sockaddr_in6);
273 if (ConnectTimeout > 0)
274 alarm (ConnectTimeout);
276 mutt_allow_interrupt (1);
280 if (connect (fd, sa, sa_size) < 0) {
282 SigInt = 0; /* reset in case we caught SIGINTR while in connect() */
285 if (ConnectTimeout > 0)
287 mutt_allow_interrupt (0);
292 /* socket_new_conn: allocate and initialise a new connection. */
293 static CONNECTION *socket_new_conn (void)
297 conn = p_new(CONNECTION, 1);
303 int raw_socket_close (CONNECTION * conn)
305 return close (conn->fd);
308 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
312 if ((rc = read (conn->fd, buf, len)) == -1) {
313 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
321 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
325 if ((rc = write (conn->fd, buf, count)) == -1) {
326 mutt_error (_("Error talking to %s (%s)"), conn->account.host,
334 int raw_socket_open (CONNECTION * conn)
339 char *host_idna = NULL;
343 struct addrinfo hints;
344 struct addrinfo *res;
345 struct addrinfo *cur;
347 /* we accept v4 or v6 STREAM sockets */
350 if (option (OPTUSEIPV6))
351 hints.ai_family = AF_UNSPEC;
353 hints.ai_family = AF_INET;
355 hints.ai_socktype = SOCK_STREAM;
357 snprintf (port, sizeof (port), "%d", conn->account.port);
360 if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
361 mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
365 host_idna = conn->account.host;
368 mutt_message (_("Looking up %s..."), conn->account.host);
370 rc = getaddrinfo (host_idna, port, &hints, &res);
373 p_delete(&host_idna);
377 mutt_error (_("Could not find the host \"%s\""), conn->account.host);
382 mutt_message (_("Connecting to %s..."), conn->account.host);
385 for (cur = res; cur != NULL; cur = cur->ai_next) {
386 fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
388 if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
389 fcntl (fd, F_SETFD, FD_CLOEXEC);
401 mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
402 (rc > 0) ? strerror (rc) : _("unknown error"));