Show key icon for encrypted mails in summary view.
[claws.git] / src / socket.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <signal.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #if USE_THREADS
41 #  include <pthread.h>
42 #endif
43
44 #include "socket.h"
45 #include "utils.h"
46 #if USE_SSL
47 #  include "ssl.h"
48 #endif
49
50 #if USE_GIO
51 #error USE_GIO is currently not supported
52 #endif
53
54 #define BUFFSIZE        8192
55
56 #ifndef INET6
57 static gint sock_connect_by_hostname    (gint            sock,
58                                          const gchar    *hostname,
59                                          gushort         port);
60 #else
61 static gint sock_connect_by_getaddrinfo (const gchar    *hostname,
62                                          gushort         port);
63 #endif
64
65 static SockInfo *sockinfo_from_fd(const gchar *hostname,
66                                   gushort port,
67                                   gint sock);
68
69 gint fd_connect_unix(const gchar *path)
70 {
71         gint sock;
72         struct sockaddr_un addr;
73
74         sock = socket(PF_UNIX, SOCK_STREAM, 0);
75         if (sock < 0) {
76                 perror("sock_connect_unix(): socket");
77                 return -1;
78         }
79
80         memset(&addr, 0, sizeof(addr));
81         addr.sun_family = AF_UNIX;
82         strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
83
84         if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
85                 close(sock);
86                 return -1;
87         }
88
89         return sock;
90 }
91
92 gint fd_open_unix(const gchar *path)
93 {
94         gint sock;
95         struct sockaddr_un addr;
96
97         sock = socket(PF_UNIX, SOCK_STREAM, 0);
98
99         if (sock < 0) {
100                 perror("sock_open_unix(): socket");
101                 return -1;
102         }
103
104         memset(&addr, 0, sizeof(addr));
105         addr.sun_family = AF_UNIX;
106         strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
107
108         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
109                 perror("bind");
110                 close(sock);
111                 return -1;
112         }
113
114         if (listen(sock, 1) < 0) {
115                 perror("listen");
116                 close(sock);
117                 return -1;              
118         }
119
120         return sock;
121 }
122
123 gint fd_accept(gint sock)
124 {
125         struct sockaddr_in caddr;
126         gint caddr_len;
127
128         caddr_len = sizeof(caddr);
129         return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
130 }
131
132
133 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
134 {
135         gint flags;
136
137         flags = fcntl(fd, F_GETFL, 0);
138         if (flags < 0) {
139                 perror("fcntl");
140                 return -1;
141         }
142
143         if (nonblock)
144                 flags |= O_NONBLOCK;
145         else
146                 flags &= ~O_NONBLOCK;
147
148         return fcntl(fd, F_SETFL, flags);
149 }
150
151 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
152 {
153         g_return_val_if_fail(sock != NULL, -1);
154
155         return set_nonblocking_mode(sock->sock, nonblock);
156 }
157
158
159 static gboolean is_nonblocking_mode(gint fd)
160 {
161         gint flags;
162
163         flags = fcntl(fd, F_GETFL, 0);
164         if (flags < 0) {
165                 perror("fcntl");
166                 return FALSE;
167         }
168
169         return ((flags & O_NONBLOCK) != 0);
170 }
171
172 gboolean sock_is_nonblocking_mode(SockInfo *sock)
173 {
174         g_return_val_if_fail(sock != NULL, FALSE);
175
176         return is_nonblocking_mode(sock->sock);
177 }
178
179
180 #ifndef INET6
181 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
182                                      gushort port)
183 {
184         struct hostent *hp;
185         struct sockaddr_in ad;
186 #ifndef HAVE_INET_ATON
187 #if HAVE_INET_ADDR
188         guint32 inaddr;
189 #endif
190 #endif /* HAVE_INET_ATON */
191
192         memset(&ad, 0, sizeof(ad));
193         ad.sin_family = AF_INET;
194         ad.sin_port = htons(port);
195
196 #if HAVE_INET_ATON
197         if (!inet_aton(hostname, &ad.sin_addr)) {
198 #else
199 #if HAVE_INET_ADDR
200         inaddr = inet_addr(hostname);
201         if (inaddr != -1)
202                 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
203         else {
204 #else
205         {
206 #endif
207 #endif /* HAVE_INET_ATON */
208                 if ((hp = gethostbyname(hostname)) == NULL) {
209                         fprintf(stderr, "%s: unknown host.\n", hostname);
210                         errno = 0;
211                         return -1;
212                 }
213
214                 if (hp->h_length != 4 && hp->h_length != 8) {
215                         fprintf(stderr, "illegal address length received for host %s\n", hostname);
216                         errno = 0;
217                         return -1;
218                 }
219
220                 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
221         }
222
223         return connect(sock, (struct sockaddr *)&ad, sizeof(ad));
224 }
225
226 #else /* INET6 */
227 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort  port)
228 {
229         gint sock = -1, gai_error;
230         struct addrinfo hints, *res, *ai;
231         gchar port_str[6];
232
233         memset(&hints, 0, sizeof(hints));
234         /* hints.ai_flags = AI_CANONNAME; */
235         hints.ai_family = AF_UNSPEC;
236         hints.ai_socktype = SOCK_STREAM;
237         hints.ai_protocol = IPPROTO_TCP;
238
239         /* convert port from integer to string. */
240         g_snprintf(port_str, sizeof(port_str), "%d", port);
241
242         if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
243                 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
244                         hostname, port_str, gai_strerror(gai_error));
245                 return -1;
246         }
247
248         for (ai = res; ai != NULL; ai = ai->ai_next) {
249                 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
250                 if (sock < 0)
251                         continue;
252
253                 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
254                         break;
255
256                 close(sock);
257         }
258
259         if (res != NULL)
260                 freeaddrinfo(res);
261
262         if (ai == NULL)
263                 return -1;
264
265         return sock;
266 }
267 #endif /* !INET6 */
268
269
270 /* Open a connection using an external program.  May be useful when
271  * you need to tunnel through a SOCKS or other firewall, or to
272  * establish an IMAP-over-SSH connection. */
273 /* TODO: Recreate this for sock_connect_thread() */
274 SockInfo *sock_connect_cmd(const gchar *hostname, const gchar *tunnelcmd)
275 {
276         gint fd[2];
277         int r;
278                      
279         if ((r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) == -1) {
280                 perror("socketpair");
281                 return NULL;
282         }
283         log_message("launching tunnel command \"%s\"\n", tunnelcmd);
284         if (fork() == 0) {
285                 close(fd[0]);
286                 close(0);
287                 close(1);
288                 dup(fd[1]);     /* set onto stdin */
289                 dup(fd[1]);
290                 execlp("/bin/sh", "/bin/sh", "-c", tunnelcmd, NULL);
291         }
292
293         close(fd[1]);
294         return sockinfo_from_fd(hostname, 0, fd[0]);
295 }
296
297
298 SockInfo *sock_connect(const gchar *hostname, gushort port)
299 {
300         gint sock;
301
302 #ifdef INET6
303         if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
304                 return NULL;
305 #else
306         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
307                 perror("socket");
308                 return NULL;
309         }
310
311         if (sock_connect_by_hostname(sock, hostname, port) < 0) {
312                 if (errno != 0) perror("connect");
313                 close(sock);
314                 return NULL;
315         }
316 #endif /* INET6 */
317
318         return sockinfo_from_fd(hostname, port, sock);
319 }
320
321
322 static SockInfo *sockinfo_from_fd(const gchar *hostname,
323                                   gushort port,
324                                   gint sock)
325 {
326         SockInfo *sockinfo;
327         
328         sockinfo = g_new0(SockInfo, 1);
329         sockinfo->sock = sock;
330         sockinfo->hostname = g_strdup(hostname);
331         sockinfo->port = port;
332         sockinfo->state = CONN_ESTABLISHED;
333
334         usleep(100000);
335
336         return sockinfo;
337 }
338
339 #if USE_THREADS
340 static void sock_connect_thread(SockInfo *sockinfo)
341 {
342 #ifdef INET6
343         if ((sockinfo->sock = sock_connect_by_getaddrinfo
344                 (sockinfo->hostname, sockinfo->port)) < 0)
345                 pthread_exit((void *)1);
346 #else
347         if (sock_connect_by_hostname(sockinfo->sock, sockinfo->hostname,
348                                      sockinfo->port) < 0) {
349                 if (errno != 0) perror("connect");
350                 sockinfo->state = CONN_FAILED;
351                 pthread_exit((void *)1);
352         }
353 #endif /* INET6 */
354         sockinfo->state = CONN_ESTABLISHED;
355
356         pthread_exit(0);
357 }
358
359 SockInfo *sock_connect_with_thread(const gchar *hostname, gushort port)
360 {
361         gint sock = 0;
362         SockInfo *sockinfo;
363
364 #ifndef INET6
365         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
366                 perror("socket");
367                 return NULL;
368         }
369 #endif /* !INET6 */
370
371         sockinfo = g_new0(SockInfo, 1);
372         sockinfo->sock = sock;
373         sockinfo->hostname = g_strdup(hostname);
374         sockinfo->port = port;
375         sockinfo->state = CONN_READY;
376
377         pthread_create(&sockinfo->connect_thr, NULL,
378                        (void *)sock_connect_thread,
379                        sockinfo);
380         pthread_mutex_init(&sockinfo->mutex, NULL);
381         pthread_detach(sockinfo->connect_thr);
382
383         return sockinfo;
384 }
385 #endif
386
387
388 gint sock_printf(SockInfo *sock, const gchar *format, ...)
389 {
390         va_list args;
391         gchar buf[BUFFSIZE];
392
393         va_start(args, format);
394         g_vsnprintf(buf, sizeof(buf), format, args);
395         va_end(args);
396
397         return sock_write(sock, buf, strlen(buf));
398 }
399
400 gint sock_read(SockInfo *sock, gchar *buf, gint len)
401 {
402         g_return_val_if_fail(sock != NULL, -1);
403
404 #if USE_SSL
405         if (sock->ssl)
406                 return ssl_read(sock->ssl, buf, len);
407 #endif
408         return fd_read(sock->sock, buf, len);
409 }
410
411 gint fd_read(gint fd, gchar *buf, gint len)
412 {
413         return read(fd, buf, len);
414 }
415
416 #if USE_SSL
417 gint ssl_read(SSL *ssl, gchar *buf, gint len)
418 {
419         return SSL_read(ssl, buf, len);
420 }
421 #endif
422
423 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
424 {
425         g_return_val_if_fail(sock != NULL, -1);
426
427 #if USE_SSL
428         if (sock->ssl)
429                 return ssl_write(sock->ssl, buf, len);
430 #endif
431         return fd_write(sock->sock, buf, len);
432 }
433
434 gint fd_write(gint fd, const gchar *buf, gint len)
435 {
436         gint n, wrlen = 0;
437
438         while (len) {
439                 signal(SIGPIPE, SIG_IGN);
440                 n = write(fd, buf, len);
441                 if (n <= 0) {
442                         log_error("write on fd%d: %s\n", fd, strerror(errno));
443                         return -1;
444                 }
445                 len -= n;
446                 wrlen += n;
447                 buf += n;
448         }
449
450         return wrlen;
451 }
452
453 #if USE_SSL
454 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
455 {
456         gint n, wrlen = 0;
457
458         while (len) {
459                 n = SSL_write(ssl, buf, len);
460                 if (n <= 0)
461                         return -1;
462                 len -= n;
463                 wrlen += n;
464                 buf += n;
465         }
466
467         return wrlen;
468 }
469 #endif
470
471 gint fd_gets(gint fd, gchar *buf, gint len)
472 {
473         gchar *newline, *bp = buf;
474         gint n;
475
476         if (--len < 1)
477                 return -1;
478         do {
479                 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
480                         return -1;
481                 if ((newline = memchr(bp, '\n', n)) != NULL)
482                         n = newline - bp + 1;
483                 if ((n = read(fd, bp, n)) < 0)
484                         return -1;
485                 bp += n;
486                 len -= n;
487         } while (!newline && len);
488
489         *bp = '\0';
490         return bp - buf;
491 }
492
493 #if USE_SSL
494 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
495 {
496         gchar *newline, *bp = buf;
497         gint n;
498
499         if (--len < 1)
500                 return -1;
501         do {
502                 if ((n = SSL_peek(ssl, bp, len)) <= 0)
503                         return -1;
504                 if ((newline = memchr(bp, '\n', n)) != NULL)
505                         n = newline - bp + 1;
506                 if ((n = SSL_read(ssl, bp, n)) < 0)
507                         return -1;
508                 bp += n;
509                 len -= n;
510         } while (!newline && len);
511
512         *bp = '\0';
513         return bp - buf;
514 }
515 #endif
516
517 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
518 {
519         g_return_val_if_fail(sock != NULL, -1);
520
521 #if USE_SSL
522         if (sock->ssl)
523                 return ssl_gets(sock->ssl, buf, len);
524 #endif
525         return fd_gets(sock->sock, buf, len);
526 }
527
528 gchar *fd_getline(gint fd)
529 {
530         gchar buf[BUFFSIZE];
531         gchar *str = NULL;
532         gint len;
533         gulong size = 1;
534
535         while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
536                 size += len;
537                 if (!str)
538                         str = g_strdup(buf);
539                 else {
540                         str = g_realloc(str, size);
541                         strcat(str, buf);
542                 }
543                 if (buf[len - 1] == '\n')
544                         break;
545         }
546         if (len == -1) {
547                 log_error("Read from socket fd%d failed: %s\n",
548                           fd, strerror(errno));
549                 if (str)
550                         g_free(str);
551                 return NULL;
552         }
553
554         return str;
555 }
556
557 #if USE_SSL
558 gchar *ssl_getline(SSL *ssl)
559 {
560         gchar buf[BUFFSIZE];
561         gchar *str = NULL;
562         gint len;
563         gulong size = 1;
564
565         while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
566                 size += len;
567                 if (!str)
568                         str = g_strdup(buf);
569                 else {
570                         str = g_realloc(str, size);
571                         strcat(str, buf);
572                 }
573                 if (buf[len - 1] == '\n')
574                         break;
575         }
576
577         return str;
578 }
579 #endif
580
581 gchar *sock_getline(SockInfo *sock)
582 {
583         g_return_val_if_fail(sock != NULL, NULL);
584
585 #if USE_SSL
586         if (sock->ssl)
587                 return ssl_getline(sock->ssl);
588 #endif
589         return fd_getline(sock->sock);
590 }
591
592 gint sock_puts(SockInfo *sock, const gchar *buf)
593 {
594         gint ret;
595
596         if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
597                 return ret;
598         return sock_write(sock, "\r\n", 2);
599 }
600
601 /* peek at the next socket character without actually reading it */
602 gint sock_peek(SockInfo *sock)
603 {
604         gint n;
605         gchar ch;
606
607         g_return_val_if_fail(sock != NULL, -1);
608
609         if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
610                 return -1;
611         else
612                 return ch;
613 }
614
615 gint sock_close(SockInfo *sock)
616 {
617         gint ret;
618
619         if (!sock)
620                 return 0;
621
622 #if USE_SSL
623         if (sock->ssl)
624                 ssl_done_socket(sock);
625 #endif
626         ret = fd_close(sock->sock); 
627         g_free(sock->hostname);
628         g_free(sock);
629
630         return ret;
631 }
632
633 gint fd_close(gint fd)
634 {
635         return close(fd);
636 }
637
638 gint sock_gdk_input_add(SockInfo *sock,
639                         GdkInputCondition condition,
640                         GdkInputFunction function,
641                         gpointer data)
642 {
643         g_return_val_if_fail(sock != NULL, -1);
644
645         /* :WK: We have to change some things here becuse most likey
646            function() does take SockInfo * and not an gint */
647         return gdk_input_add(sock->sock, condition, function, data);
648 }