Reverted previous changes for NNTP authentication support (needs to be
[claws.git] / src / socket.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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
45 #if USE_GIO
46 #error USE_GIO is currently not supported
47 #endif
48
49 #define BUFFSIZE        8192
50
51 #ifndef INET6
52 static gint sock_connect_by_hostname    (gint            sock,
53                                          const gchar    *hostname,
54                                          gushort         port);
55 #else
56 static gint sock_connect_by_getaddrinfo (const gchar    *hostname,
57                                          gushort         port);
58 #endif
59
60
61 gint fd_connect_unix(const gchar *path)
62 {
63         gint sock;
64         struct sockaddr_un addr;
65
66         sock = socket(PF_UNIX, SOCK_STREAM, 0);
67         if (sock < 0) {
68                 perror("sock_connect_unix(): socket");
69                 return -1;
70         }
71
72         memset(&addr, 0, sizeof(addr));
73         addr.sun_family = AF_UNIX;
74         strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
75
76         if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
77                 close(sock);
78                 return -1;
79         }
80
81         return sock;
82 }
83
84 gint fd_open_unix(const gchar *path)
85 {
86         gint sock;
87         struct sockaddr_un addr;
88
89         sock = socket(PF_UNIX, SOCK_STREAM, 0);
90
91         if (sock < 0) {
92                 perror("sock_open_unix(): socket");
93                 return -1;
94         }
95
96         memset(&addr, 0, sizeof(addr));
97         addr.sun_family = AF_UNIX;
98         strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
99
100         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
101                 perror("bind");
102                 close(sock);
103                 return -1;
104         }
105
106         if (listen(sock, 1) < 0) {
107                 perror("listen");
108                 close(sock);
109                 return -1;              
110         }
111
112         return sock;
113 }
114
115 gint fd_accept(gint sock)
116 {
117         struct sockaddr_in caddr;
118         gint caddr_len;
119
120         caddr_len = sizeof(caddr);
121         return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
122 }
123
124
125 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
126 {
127         gint flags;
128
129         flags = fcntl(fd, F_GETFL, 0);
130         if (flags < 0) {
131                 perror("fcntl");
132                 return -1;
133         }
134
135         if (nonblock)
136                 flags |= O_NONBLOCK;
137         else
138                 flags &= ~O_NONBLOCK;
139
140         return fcntl(fd, F_SETFL, flags);
141 }
142
143 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
144 {
145         g_return_val_if_fail(sock != NULL, -1);
146
147         return set_nonblocking_mode(sock->sock, nonblock);
148 }
149
150
151 static gboolean is_nonblocking_mode(gint fd)
152 {
153         gint flags;
154
155         flags = fcntl(fd, F_GETFL, 0);
156         if (flags < 0) {
157                 perror("fcntl");
158                 return FALSE;
159         }
160
161         return ((flags & O_NONBLOCK) != 0);
162 }
163
164 gboolean sock_is_nonblocking_mode(SockInfo *sock)
165 {
166         g_assert(sock);
167
168         return is_nonblocking_mode(sock->sock);
169 }
170
171
172 #ifndef INET6
173 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
174                                      gushort port)
175 {
176         struct hostent *hp;
177         struct sockaddr_in ad;
178 #ifndef HAVE_INET_ATON
179 #if HAVE_INET_ADDR
180         guint32 inaddr;
181 #endif
182 #endif /* HAVE_INET_ATON */
183
184         memset(&ad, 0, sizeof(ad));
185         ad.sin_family = AF_INET;
186         ad.sin_port = htons(port);
187
188 #if HAVE_INET_ATON
189         if (!inet_aton(hostname, &ad.sin_addr)) {
190 #else
191 #if HAVE_INET_ADDR
192         inaddr = inet_addr(hostname);
193         if (inaddr != -1)
194                 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
195         else {
196 #else
197         {
198 #endif
199 #endif /* HAVE_INET_ATON */
200                 if ((hp = gethostbyname(hostname)) == NULL) {
201                         fprintf(stderr, "%s: unknown host.\n", hostname);
202                         errno = 0;
203                         return -1;
204                 }
205
206                 if (hp->h_length != 4 && hp->h_length != 8) {
207                         h_errno = errno = 0;
208                         fprintf(stderr, "illegal address length received for host %s\n", hostname);
209                         return -1;
210                 }
211
212                 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
213         }
214
215         return connect(sock, (struct sockaddr *)&ad, sizeof(ad));
216 }
217
218 #else /* INET6 */
219 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort  port)
220 {
221         gint sock = -1, gai_error;
222         struct addrinfo hints, *res, *ai;
223         gchar port_str[6];
224
225         memset(&hints, 0, sizeof(hints));
226         /* hints.ai_flags = AI_CANONNAME; */
227         hints.ai_family = AF_UNSPEC;
228         hints.ai_socktype = SOCK_STREAM;
229         hints.ai_protocol = IPPROTO_TCP;
230
231         /* convert port from integer to string. */
232         g_snprintf(port_str, sizeof(port_str), "%d", port);
233
234         if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
235                 fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
236                         hostname, port_str, gai_strerror(gai_error));
237                 return -1;
238         }
239
240         for (ai = res; ai != NULL; ai = ai->ai_next) {
241                 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
242                 if (sock < 0)
243                         continue;
244
245                 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
246                         break;
247
248                 close(sock);
249         }
250
251         if (res != NULL)
252                 freeaddrinfo(res);
253
254         if (ai == NULL)
255                 return -1;
256
257         return sock;
258 }
259 #endif /* !INET6 */
260
261 SockInfo *sock_connect_nb(const gchar *hostname, gushort port)
262 {
263         gint sock;
264         gint ret;
265         SockInfo *sockinfo;
266
267 #ifdef INET6
268         if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
269                 return NULL;
270         if (set_nonblocking_mode(sock, TRUE) < 0) return NULL;
271         ret = sock;
272 #else
273         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
274                 perror("socket");
275                 return NULL;
276         }
277
278         if (set_nonblocking_mode(sock, TRUE) < 0) return NULL;
279
280         ret = sock_connect_by_hostname(sock, hostname, port);
281
282         if (ret < 0 && errno != EINPROGRESS) {
283                 if (errno != 0) perror("connect");
284                 close(sock);
285                 return NULL;
286         }
287 #endif /* INET6 */
288
289         sockinfo = g_new0(SockInfo, 1);
290         sockinfo->sock = sock;
291         sockinfo->hostname = g_strdup(hostname);
292         sockinfo->port = port;
293         sockinfo->state = CONN_LOOKUPSUCCESS;
294
295         if (ret < 0 && errno == EINPROGRESS) return sockinfo;
296
297         sockinfo->state = CONN_ESTABLISHED;
298         return sockinfo;
299 }
300
301 SockInfo *sock_connect(const gchar *hostname, gushort port)
302 {
303         gint sock;
304         SockInfo *sockinfo;
305
306 #ifdef INET6
307         if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
308                 return NULL;
309 #else
310         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
311                 perror("socket");
312                 return NULL;
313         }
314
315         if (sock_connect_by_hostname(sock, hostname, port) < 0) {
316                 if (errno != 0) perror("connect");
317                 close(sock);
318                 return NULL;
319         }
320 #endif /* INET6 */
321
322         sockinfo = g_new0(SockInfo, 1);
323         sockinfo->sock = sock;
324         sockinfo->hostname = g_strdup(hostname);
325         sockinfo->port = port;
326         sockinfo->state = CONN_ESTABLISHED;
327
328         usleep(100000);
329
330         return sockinfo;
331 }
332
333 #if USE_THREADS
334 static void sock_connect_thread(SockInfo *sockinfo)
335 {
336 #ifdef INET6
337         if ((sockinfo->sock = sock_connect_by_getaddrinfo
338                 (sockinfo->hostname, sockinfo->port)) < 0)
339                 pthread_exit((void *)1);
340 #else
341         if (sock_connect_by_hostname(sockinfo->sock, sockinfo->hostname,
342                                      sockinfo->port) < 0) {
343                 if (errno != 0) perror("connect");
344                 sockinfo->state = CONN_FAILED;
345                 pthread_exit((void *)1);
346         }
347 #endif /* INET6 */
348         sockinfo->state = CONN_ESTABLISHED;
349
350         pthread_exit(0);
351 }
352
353 SockInfo *sock_connect_with_thread(const gchar *hostname, gushort port)
354 {
355         gint sock = 0;
356         SockInfo *sockinfo;
357
358 #ifndef INET6
359         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
360                 perror("socket");
361                 return NULL;
362         }
363 #endif /* !INET6 */
364
365         sockinfo = g_new0(SockInfo, 1);
366         sockinfo->sock = sock;
367         sockinfo->hostname = g_strdup(hostname);
368         sockinfo->port = port;
369         sockinfo->state = CONN_READY;
370
371         pthread_create(&sockinfo->connect_thr, NULL,
372                        (void *)sock_connect_thread,
373                        sockinfo);
374         pthread_mutex_init(&sockinfo->mutex, NULL);
375         pthread_detach(sockinfo->connect_thr);
376
377         return sockinfo;
378 }
379 #endif
380
381
382 gint sock_printf(SockInfo *sock, const gchar *format, ...)
383 {
384         va_list args;
385         gchar buf[BUFFSIZE];
386
387         va_start(args, format);
388         g_vsnprintf(buf, sizeof(buf), format, args);
389         va_end(args);
390
391         return sock_write(sock, buf, strlen(buf));
392 }
393
394 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
395 {
396         g_return_val_if_fail(sock != NULL, -1);
397
398         return fd_write(sock->sock, buf, len);
399 }
400
401 gint fd_write(gint fd, const gchar *buf, gint len)
402 {
403         gint n, wrlen = 0;
404
405         while (len) {
406                 n = write(fd, buf, len);
407                 if (n <= 0)
408                         return -1;
409                 len -= n;
410                 wrlen += n;
411                 buf += n;
412         }
413
414         return wrlen;
415 }
416
417 gint sock_read(SockInfo *sock, gchar *buf, gint len)
418 {
419         g_return_val_if_fail(sock != NULL, -1);
420
421         return fd_read(sock->sock, buf, len);
422 }
423
424 gint fd_read(gint fd, gchar *buf, gint len)
425 {
426         gchar *newline, *bp = buf;
427         gint n;
428
429         if (--len < 1)
430                 return -1;
431         do {
432                 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
433                         return -1;
434                 if ((newline = memchr(bp, '\n', n)) != NULL)
435                         n = newline - bp + 1;
436                 if ((n = read(fd, bp, n)) < 0)
437                         return -1;
438                 bp += n;
439                 len -= n;
440         } while (!newline && len);
441
442         *bp = '\0';
443         return bp - buf;
444 }
445
446 gint sock_puts(SockInfo *sock, const gchar *buf)
447 {
448         gint ret;
449
450         if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
451                 return ret;
452         return sock_write(sock, "\r\n", 2);
453 }
454
455 /* peek at the next socket character without actually reading it */
456 gint sock_peek(SockInfo *sock)
457 {
458         gint n;
459         gchar ch;
460
461         g_return_val_if_fail(sock != NULL, -1);
462
463         if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
464                 return -1;
465         else
466                 return ch;
467 }
468
469 gint sock_close(SockInfo *sock)
470 {
471         gint rc;
472
473         if (!sock)
474                 return 0;
475
476         rc = fd_close(sock->sock); 
477         g_free(sock->hostname);
478         g_free(sock);
479         return rc;
480 }
481
482 gint fd_close(gint fd)
483 {
484         return close(fd);
485 }
486
487 gint sock_gdk_input_add(SockInfo *sock,
488                          GdkInputCondition condition,
489                          GdkInputFunction function,
490                          gpointer data)
491 {
492         g_return_val_if_fail(sock != NULL, -1);
493
494         /* :WK: We have to change some things here becuse most likey
495            function() does take SockInfo * and not an gint */
496         return gdk_input_add(sock->sock, condition, function, data);
497 }