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