and don't forget to bump up version...
[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 <unistd.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <fcntl.h>
37 #include <errno.h>
38
39 #if USE_THREADS
40 #  include <pthread.h>
41 #endif
42
43 #include "socket.h"
44 #if USE_SSL
45 #  include "ssl.h"
46 #endif
47
48 #if USE_GIO
49 #error USE_GIO is currently not supported
50 #endif
51
52 #define BUFFSIZE        8192
53
54 #ifndef INET6
55 static gint sock_connect_by_hostname    (gint            sock,
56                                          const gchar    *hostname,
57                                          gushort         port);
58 #else
59 static gint sock_connect_by_getaddrinfo (const gchar    *hostname,
60                                          gushort         port);
61 #endif
62
63
64 gint fd_connect_unix(const gchar *path)
65 {
66         gint sock;
67         struct sockaddr_un addr;
68
69         sock = socket(PF_UNIX, SOCK_STREAM, 0);
70         if (sock < 0) {
71                 perror("sock_connect_unix(): socket");
72                 return -1;
73         }
74
75         memset(&addr, 0, sizeof(addr));
76         addr.sun_family = AF_UNIX;
77         strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
78
79         if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
80                 close(sock);
81                 return -1;
82         }
83
84         return sock;
85 }
86
87 gint fd_open_unix(const gchar *path)
88 {
89         gint sock;
90         struct sockaddr_un addr;
91
92         sock = socket(PF_UNIX, SOCK_STREAM, 0);
93
94         if (sock < 0) {
95                 perror("sock_open_unix(): socket");
96                 return -1;
97         }
98
99         memset(&addr, 0, sizeof(addr));
100         addr.sun_family = AF_UNIX;
101         strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
102
103         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
104                 perror("bind");
105                 close(sock);
106                 return -1;
107         }
108
109         if (listen(sock, 1) < 0) {
110                 perror("listen");
111                 close(sock);
112                 return -1;              
113         }
114
115         return sock;
116 }
117
118 gint fd_accept(gint sock)
119 {
120         struct sockaddr_in caddr;
121         gint caddr_len;
122
123         caddr_len = sizeof(caddr);
124         return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
125 }
126
127
128 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
129 {
130         gint flags;
131
132         flags = fcntl(fd, F_GETFL, 0);
133         if (flags < 0) {
134                 perror("fcntl");
135                 return -1;
136         }
137
138         if (nonblock)
139                 flags |= O_NONBLOCK;
140         else
141                 flags &= ~O_NONBLOCK;
142
143         return fcntl(fd, F_SETFL, flags);
144 }
145
146 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
147 {
148         g_return_val_if_fail(sock != NULL, -1);
149
150         return set_nonblocking_mode(sock->sock, nonblock);
151 }
152
153
154 static gboolean is_nonblocking_mode(gint fd)
155 {
156         gint flags;
157
158         flags = fcntl(fd, F_GETFL, 0);
159         if (flags < 0) {
160                 perror("fcntl");
161                 return FALSE;
162         }
163
164         return ((flags & O_NONBLOCK) != 0);
165 }
166
167 gboolean sock_is_nonblocking_mode(SockInfo *sock)
168 {
169         g_return_val_if_fail(sock != NULL, FALSE);
170
171         return is_nonblocking_mode(sock->sock);
172 }
173
174
175 #ifndef INET6
176 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
177                                      gushort port)
178 {
179         struct hostent *hp;
180         struct sockaddr_in ad;
181 #ifndef HAVE_INET_ATON
182 #if HAVE_INET_ADDR
183         guint32 inaddr;
184 #endif
185 #endif /* HAVE_INET_ATON */
186
187         memset(&ad, 0, sizeof(ad));
188         ad.sin_family = AF_INET;
189         ad.sin_port = htons(port);
190
191 #if HAVE_INET_ATON
192         if (!inet_aton(hostname, &ad.sin_addr)) {
193 #else
194 #if HAVE_INET_ADDR
195         inaddr = inet_addr(hostname);
196         if (inaddr != -1)
197                 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
198         else {
199 #else
200         {
201 #endif
202 #endif /* HAVE_INET_ATON */
203                 if ((hp = gethostbyname(hostname)) == NULL) {
204                         fprintf(stderr, "%s: unknown host.\n", hostname);
205                         errno = 0;
206                         return -1;
207                 }
208
209                 if (hp->h_length != 4 && hp->h_length != 8) {
210                         h_errno = errno = 0;
211                         fprintf(stderr, "illegal address length received for host %s\n", hostname);
212                         return -1;
213                 }
214
215                 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
216         }
217
218         return connect(sock, (struct sockaddr *)&ad, sizeof(ad));
219 }
220
221 #else /* INET6 */
222 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort  port)
223 {
224         gint sock = -1, gai_error;
225         struct addrinfo hints, *res, *ai;
226         gchar port_str[6];
227
228         memset(&hints, 0, sizeof(hints));
229         /* hints.ai_flags = AI_CANONNAME; */
230         hints.ai_family = AF_UNSPEC;
231         hints.ai_socktype = SOCK_STREAM;
232         hints.ai_protocol = IPPROTO_TCP;
233
234         /* convert port from integer to string. */
235         g_snprintf(port_str, sizeof(port_str), "%d", port);
236
237         if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
238                 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
239                         hostname, port_str, gai_strerror(gai_error));
240                 return -1;
241         }
242
243         for (ai = res; ai != NULL; ai = ai->ai_next) {
244                 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
245                 if (sock < 0)
246                         continue;
247
248                 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
249                         break;
250
251                 close(sock);
252         }
253
254         if (res != NULL)
255                 freeaddrinfo(res);
256
257         if (ai == NULL)
258                 return -1;
259
260         return sock;
261 }
262 #endif /* !INET6 */
263
264 SockInfo *sock_connect(const gchar *hostname, gushort port)
265 {
266         gint sock;
267         SockInfo *sockinfo;
268
269 #ifdef INET6
270         if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
271                 return NULL;
272 #else
273         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
274                 perror("socket");
275                 return NULL;
276         }
277
278         if (sock_connect_by_hostname(sock, hostname, port) < 0) {
279                 if (errno != 0) perror("connect");
280                 close(sock);
281                 return NULL;
282         }
283 #endif /* INET6 */
284
285         sockinfo = g_new0(SockInfo, 1);
286         sockinfo->sock = sock;
287         sockinfo->hostname = g_strdup(hostname);
288         sockinfo->port = port;
289         sockinfo->state = CONN_ESTABLISHED;
290
291         usleep(100000);
292
293         return sockinfo;
294 }
295
296 #if USE_THREADS
297 static void sock_connect_thread(SockInfo *sockinfo)
298 {
299 #ifdef INET6
300         if ((sockinfo->sock = sock_connect_by_getaddrinfo
301                 (sockinfo->hostname, sockinfo->port)) < 0)
302                 pthread_exit((void *)1);
303 #else
304         if (sock_connect_by_hostname(sockinfo->sock, sockinfo->hostname,
305                                      sockinfo->port) < 0) {
306                 if (errno != 0) perror("connect");
307                 sockinfo->state = CONN_FAILED;
308                 pthread_exit((void *)1);
309         }
310 #endif /* INET6 */
311         sockinfo->state = CONN_ESTABLISHED;
312
313         pthread_exit(0);
314 }
315
316 SockInfo *sock_connect_with_thread(const gchar *hostname, gushort port)
317 {
318         gint sock = 0;
319         SockInfo *sockinfo;
320
321 #ifndef INET6
322         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
323                 perror("socket");
324                 return NULL;
325         }
326 #endif /* !INET6 */
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_READY;
333
334         pthread_create(&sockinfo->connect_thr, NULL,
335                        (void *)sock_connect_thread,
336                        sockinfo);
337         pthread_mutex_init(&sockinfo->mutex, NULL);
338         pthread_detach(sockinfo->connect_thr);
339
340         return sockinfo;
341 }
342 #endif
343
344
345 gint sock_printf(SockInfo *sock, const gchar *format, ...)
346 {
347         va_list args;
348         gchar buf[BUFFSIZE];
349
350         va_start(args, format);
351         g_vsnprintf(buf, sizeof(buf), format, args);
352         va_end(args);
353
354         return sock_write(sock, buf, strlen(buf));
355 }
356
357 gint sock_read(SockInfo *sock, gchar *buf, gint len)
358 {
359         g_return_val_if_fail(sock != NULL, -1);
360
361 #if USE_SSL
362         if (sock->ssl)
363                 return ssl_read(sock->ssl, buf, len);
364 #endif
365         return fd_read(sock->sock, buf, len);
366 }
367
368 gint fd_read(gint fd, gchar *buf, gint len)
369 {
370         return read(fd, buf, len);
371 }
372
373 #if USE_SSL
374 gint ssl_read(SSL *ssl, gchar *buf, gint len)
375 {
376         return SSL_read(ssl, buf, len);
377 }
378 #endif
379
380 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
381 {
382         g_return_val_if_fail(sock != NULL, -1);
383
384 #if USE_SSL
385         if (sock->ssl)
386                 return ssl_write(sock->ssl, buf, len);
387 #endif
388         return fd_write(sock->sock, buf, len);
389 }
390
391 gint fd_write(gint fd, const gchar *buf, gint len)
392 {
393         gint n, wrlen = 0;
394
395         while (len) {
396                 n = write(fd, buf, len);
397                 if (n <= 0)
398                         return -1;
399                 len -= n;
400                 wrlen += n;
401                 buf += n;
402         }
403
404         return wrlen;
405 }
406
407 #if USE_SSL
408 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
409 {
410         gint n, wrlen = 0;
411
412         while (len) {
413                 n = SSL_write(ssl, buf, len);
414                 if (n <= 0)
415                         return -1;
416                 len -= n;
417                 wrlen += n;
418                 buf += n;
419         }
420
421         return wrlen;
422 }
423 #endif
424
425 gint fd_gets(gint fd, gchar *buf, gint len)
426 {
427         gchar *newline, *bp = buf;
428         gint n;
429
430         if (--len < 1)
431                 return -1;
432         do {
433                 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
434                         return -1;
435                 if ((newline = memchr(bp, '\n', n)) != NULL)
436                         n = newline - bp + 1;
437                 if ((n = read(fd, bp, n)) < 0)
438                         return -1;
439                 bp += n;
440                 len -= n;
441         } while (!newline && len);
442
443         *bp = '\0';
444         return bp - buf;
445 }
446
447 #if USE_SSL
448 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
449 {
450         gchar *bp = buf;
451         gboolean newline = FALSE;
452         gint n;
453
454         if (--len < 1)
455                 return -1;
456         while (len > 0 && !newline) {
457                 *bp = '\0';
458                 if ((n = SSL_read(ssl, bp, 1)) < 0)
459                         return -1;
460                 if (*bp == '\n')
461                         newline = TRUE;
462                 bp += n;
463         }
464
465         *bp = '\0';
466         return bp - buf;
467 }
468 #endif
469
470 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
471 {
472         g_return_val_if_fail(sock != NULL, -1);
473
474 #if USE_SSL
475         if (sock->ssl)
476                 return ssl_gets(sock->ssl, buf, len);
477 #endif
478         return fd_gets(sock->sock, buf, len);
479 }
480
481 gchar *fd_getline(gint fd)
482 {
483         gchar buf[BUFFSIZE];
484         gchar *str = NULL;
485         gint len;
486         gulong size = 1;
487
488         while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
489                 size += len;
490                 if (!str)
491                         str = g_strdup(buf);
492                 else {
493                         str = g_realloc(str, size);
494                         strcat(str, buf);
495                 }
496                 if (buf[len - 1] == '\n')
497                         break;
498         }
499
500         return str;
501 }
502
503 #if USE_SSL
504 gchar *ssl_getline(SSL *ssl)
505 {
506         gchar buf[BUFFSIZE];
507         gchar *str = NULL;
508         gint len;
509         gulong size = 1;
510
511         while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
512                 size += len;
513                 if (!str)
514                         str = g_strdup(buf);
515                 else {
516                         str = g_realloc(str, size);
517                         strcat(str, buf);
518                 }
519                 if (buf[len - 1] == '\n')
520                         break;
521         }
522
523         return str;
524 }
525 #endif
526
527 gchar *sock_getline(SockInfo *sock)
528 {
529         g_return_val_if_fail(sock != NULL, NULL);
530
531 #if USE_SSL
532         if (sock->ssl)
533                 return ssl_getline(sock->ssl);
534 #endif
535         return fd_getline(sock->sock);
536 }
537
538 gint sock_puts(SockInfo *sock, const gchar *buf)
539 {
540         gint ret;
541
542         if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
543                 return ret;
544         return sock_write(sock, "\r\n", 2);
545 }
546
547 /* peek at the next socket character without actually reading it */
548 gint sock_peek(SockInfo *sock)
549 {
550         gint n;
551         gchar ch;
552
553         g_return_val_if_fail(sock != NULL, -1);
554
555         if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
556                 return -1;
557         else
558                 return ch;
559 }
560
561 gint sock_close(SockInfo *sock)
562 {
563         gint ret;
564
565         if (!sock)
566                 return 0;
567
568 #if USE_SSL
569         if (sock->ssl)
570                 ssl_done_socket(sock);
571 #endif
572         ret = fd_close(sock->sock); 
573         g_free(sock->hostname);
574         g_free(sock);
575
576         return ret;
577 }
578
579 gint fd_close(gint fd)
580 {
581         return close(fd);
582 }
583
584 gint sock_gdk_input_add(SockInfo *sock,
585                         GdkInputCondition condition,
586                         GdkInputFunction function,
587                         gpointer data)
588 {
589         g_return_val_if_fail(sock != NULL, -1);
590
591         /* :WK: We have to change some things here becuse most likey
592            function() does take SockInfo * and not an gint */
593         return gdk_input_add(sock->sock, condition, function, data);
594 }