small fixes
[apps/madmutt.git] / lib-sys / mutt_socket.c
1 /*
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>
6  *
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.
10  */
11
12 #include <lib-lib/lib-lib.h>
13
14 #include <netinet/in.h>
15 #include <netdb.h>
16 #include <sys/socket.h>
17
18 #include <lib-ui/curses.h>
19
20 #include "mutt.h"
21 #include "globals.h"
22
23 #include "unix.h"
24 #include "mutt_socket.h"
25 #include "mutt_tunnel.h"
26 #include "mutt_signal.h"
27
28 #ifdef HAVE_LIBIDN
29 #include <idna.h>
30 #endif
31 #include "mutt_idna.h"
32
33 /* support for multiple socket connections */
34 static CONNECTION *Connections = NULL;
35
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);
40
41 /* Wrappers */
42 int mutt_socket_open (CONNECTION * conn)
43 {
44   if (socket_preconnect ())
45     return -1;
46
47   return conn->conn_open (conn);
48 }
49
50 int mutt_socket_close (CONNECTION * conn)
51 {
52   int rc = -1;
53
54   if (conn->fd >= 0)
55     rc = conn->conn_close (conn);
56
57   conn->fd = -1;
58   conn->ssf = 0;
59
60   return rc;
61 }
62
63 int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len)
64 {
65   int rc;
66
67   if (conn->fd < 0) {
68     return -1;
69   }
70
71   rc = conn->conn_read (conn, buf, len);
72   /* EOF */
73   if (rc == 0) {
74     mutt_error (_("Connection to %s closed"), conn->account.host);
75     mutt_sleep (2);
76   }
77   if (rc <= 0)
78     mutt_socket_close (conn);
79
80   return rc;
81 }
82
83 int mutt_socket_write(CONNECTION * conn, const char *buf)
84 {
85   int rc;
86   int len;
87
88   if (conn->fd < 0) {
89     return -1;
90   }
91
92   len = m_strlen(buf);
93   if ((rc = conn->conn_write (conn, buf, len)) < 0) {
94     mutt_socket_close (conn);
95
96     return -1;
97   }
98
99   return rc;
100 }
101
102 /* simple read buffering to speed things up. */
103 int mutt_socket_readchar (CONNECTION * conn, char *c)
104 {
105   if (conn->bufpos >= conn->available) {
106     if (conn->fd >= 0)
107       conn->available =
108         conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
109     else {
110       return -1;
111     }
112     conn->bufpos = 0;
113     if (conn->available == 0) {
114       mutt_error (_("Connection to %s closed"), conn->account.host);
115       mutt_sleep (2);
116     }
117     if (conn->available <= 0) {
118       mutt_socket_close (conn);
119       return -1;
120     }
121   }
122   *c = conn->inbuf[conn->bufpos];
123   conn->bufpos++;
124   return 1;
125 }
126
127 int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn)
128 {
129   char ch;
130   ssize_t i;
131
132   for (i = 0; i < buflen - 1; i++) {
133     if (mutt_socket_readchar (conn, &ch) != 1) {
134       buf[i] = '\0';
135       return -1;
136     }
137
138     if (ch == '\n')
139       break;
140     buf[i] = ch;
141   }
142
143   /* strip \r from \r\n termination */
144   if (i && buf[i - 1] == '\r')
145     buf[--i] = '\0';
146   else
147     buf[i] = '\0';
148
149   /* number of bytes read, not m_strlen*/
150   return i + 1;
151 }
152
153 CONNECTION *mutt_socket_head (void)
154 {
155   return Connections;
156 }
157
158 /* mutt_socket_free: remove connection from connection list and free it */
159 void mutt_socket_free (CONNECTION * conn)
160 {
161   CONNECTION *iter;
162   CONNECTION *tmp;
163
164   iter = Connections;
165
166   /* head is special case, doesn't need prev updated */
167   if (iter == conn) {
168     Connections = iter->next;
169     p_delete(&iter);
170     return;
171   }
172
173   while (iter->next) {
174     if (iter->next == conn) {
175       tmp = iter->next;
176       iter->next = tmp->next;
177       p_delete(&tmp);
178       return;
179     }
180     iter = iter->next;
181   }
182 }
183
184 /* mutt_conn_find: find a connection off the list of connections whose
185  *   account matches account. If start is not null, only search for
186  *   connections after the given connection (allows higher level socket code
187  *   to make more fine-grained searches than account info - eg in IMAP we may
188  *   wish to find a connection which is not in IMAP_SELECTED state) */
189 CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
190 {
191   CONNECTION *conn;
192   ciss_url_t url;
193   char hook[LONG_STRING];
194
195   /* account isn't actually modified, since url isn't either */
196   mutt_account_tourl ((ACCOUNT *) account, &url);
197   url.path = NULL;
198   url_ciss_tostring (&url, hook, sizeof (hook), 0);
199   mutt_account_hook (hook);
200
201   conn = start ? start->next : Connections;
202   while (conn) {
203     if (mutt_account_match (account, &(conn->account)))
204       return conn;
205     conn = conn->next;
206   }
207
208   conn = socket_new_conn ();
209   memcpy (&conn->account, account, sizeof (ACCOUNT));
210
211   conn->next = Connections;
212   Connections = conn;
213
214   if (Tunnel && *Tunnel)
215     mutt_tunnel_socket_setup (conn);
216   else if (account->flags & M_ACCT_SSL) {
217     if (mutt_ssl_socket_setup (conn) < 0) {
218       mutt_socket_free (conn);
219       return NULL;
220     }
221   } else {
222     conn->conn_read = raw_socket_read;
223     conn->conn_write = raw_socket_write;
224     conn->conn_open = raw_socket_open;
225     conn->conn_close = raw_socket_close;
226   }
227
228   return conn;
229 }
230
231 static int socket_preconnect (void)
232 {
233   int rc;
234   int save_errno;
235
236   if (m_strlen(Preconnect)) {
237     rc = mutt_system (Preconnect);
238     if (rc) {
239       save_errno = errno;
240       mutt_perror (_("Preconnect command failed."));
241       mutt_sleep (1);
242
243       return save_errno;
244     }
245   }
246
247   return 0;
248 }
249
250 /* socket_connect: set up to connect to a socket fd. */
251 static int socket_connect (int fd, struct sockaddr *sa)
252 {
253   int sa_size;
254   int save_errno;
255
256   if (sa->sa_family == AF_INET)
257     sa_size = sizeof (struct sockaddr_in);
258   else if (sa->sa_family == AF_INET6)
259     sa_size = sizeof (struct sockaddr_in6);
260   else {
261     return -1;
262   }
263
264   if (ConnectTimeout > 0)
265     alarm (ConnectTimeout);
266
267   mutt_allow_interrupt (1);
268
269   save_errno = 0;
270
271   if (connect (fd, sa, sa_size) < 0) {
272     save_errno = errno;
273     SigInt = 0;                 /* reset in case we caught SIGINTR while in connect() */
274   }
275
276   if (ConnectTimeout > 0)
277     alarm (0);
278   mutt_allow_interrupt (0);
279
280   return save_errno;
281 }
282
283 /* socket_new_conn: allocate and initialise a new connection. */
284 static CONNECTION *socket_new_conn (void)
285 {
286   CONNECTION *conn;
287
288   conn = p_new(CONNECTION, 1);
289   conn->fd = -1;
290
291   return conn;
292 }
293
294 int raw_socket_close (CONNECTION * conn)
295 {
296   return close (conn->fd);
297 }
298
299 int raw_socket_read (CONNECTION * conn, char *buf, ssize_t len)
300 {
301   int rc;
302
303   if ((rc = read (conn->fd, buf, len)) == -1) {
304     mutt_error (_("Error talking to %s (%s)"), conn->account.host,
305                 strerror (errno));
306     mutt_sleep (2);
307   }
308
309   return rc;
310 }
311
312 int raw_socket_write (CONNECTION * conn, const char *buf, ssize_t count)
313 {
314   int rc;
315
316   if ((rc = write (conn->fd, buf, count)) == -1) {
317     mutt_error (_("Error talking to %s (%s)"), conn->account.host,
318                 strerror (errno));
319     mutt_sleep (2);
320   }
321
322   return rc;
323 }
324
325 int raw_socket_open (CONNECTION * conn)
326 {
327   int rc;
328   int fd;
329
330   char *host_idna = NULL;
331
332   /* "65536\0" */
333   char port[6];
334   struct addrinfo hints;
335   struct addrinfo *res;
336   struct addrinfo *cur;
337
338   /* we accept v4 or v6 STREAM sockets */
339   p_clear(&hints, 1);
340
341   if (option (OPTUSEIPV6))
342     hints.ai_family = AF_UNSPEC;
343   else
344     hints.ai_family = AF_INET;
345
346   hints.ai_socktype = SOCK_STREAM;
347
348   snprintf (port, sizeof (port), "%d", conn->account.port);
349
350 # ifdef HAVE_LIBIDN
351   if (idna_to_ascii_lz (conn->account.host, &host_idna, 1) != IDNA_SUCCESS) {
352     mutt_error (_("Bad IDN \"%s\"."), conn->account.host);
353     return -1;
354   }
355 # else
356   host_idna = conn->account.host;
357 # endif
358
359   mutt_message (_("Looking up %s..."), conn->account.host);
360
361   rc = getaddrinfo (host_idna, port, &hints, &res);
362
363 # ifdef HAVE_LIBIDN
364   p_delete(&host_idna);
365 # endif
366
367   if (rc) {
368     mutt_error (_("Could not find the host \"%s\""), conn->account.host);
369     mutt_sleep (2);
370     return -1;
371   }
372
373   mutt_message (_("Connecting to %s..."), conn->account.host);
374
375   rc = -1;
376   for (cur = res; cur != NULL; cur = cur->ai_next) {
377     fd = socket (cur->ai_family, cur->ai_socktype, cur->ai_protocol);
378     if (fd >= 0) {
379       if ((rc = socket_connect (fd, cur->ai_addr)) == 0) {
380         fcntl (fd, F_SETFD, FD_CLOEXEC);
381         conn->fd = fd;
382         break;
383       }
384       else
385         close (fd);
386     }
387   }
388
389   freeaddrinfo (res);
390
391   if (rc) {
392     mutt_error (_("Could not connect to %s (%s)."), conn->account.host,
393                 (rc > 0) ? strerror (rc) : _("unknown error"));
394     mutt_sleep (2);
395     return -1;
396   }
397
398   return 0;
399 }