2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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.
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.
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.
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
49 #error USE_GIO is currently not supported
55 static gint sock_connect_by_hostname (gint sock,
56 const gchar *hostname,
59 static gint sock_connect_by_getaddrinfo (const gchar *hostname,
64 gint fd_connect_unix(const gchar *path)
67 struct sockaddr_un addr;
69 sock = socket(PF_UNIX, SOCK_STREAM, 0);
71 perror("sock_connect_unix(): socket");
75 memset(&addr, 0, sizeof(addr));
76 addr.sun_family = AF_UNIX;
77 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
79 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
87 gint fd_open_unix(const gchar *path)
90 struct sockaddr_un addr;
92 sock = socket(PF_UNIX, SOCK_STREAM, 0);
95 perror("sock_open_unix(): socket");
99 memset(&addr, 0, sizeof(addr));
100 addr.sun_family = AF_UNIX;
101 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
103 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
109 if (listen(sock, 1) < 0) {
118 gint fd_accept(gint sock)
120 struct sockaddr_in caddr;
123 caddr_len = sizeof(caddr);
124 return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
128 static gint set_nonblocking_mode(gint fd, gboolean nonblock)
132 flags = fcntl(fd, F_GETFL, 0);
141 flags &= ~O_NONBLOCK;
143 return fcntl(fd, F_SETFL, flags);
146 gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
148 g_return_val_if_fail(sock != NULL, -1);
150 return set_nonblocking_mode(sock->sock, nonblock);
154 static gboolean is_nonblocking_mode(gint fd)
158 flags = fcntl(fd, F_GETFL, 0);
164 return ((flags & O_NONBLOCK) != 0);
167 gboolean sock_is_nonblocking_mode(SockInfo *sock)
169 g_return_val_if_fail(sock != NULL, FALSE);
171 return is_nonblocking_mode(sock->sock);
176 static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
180 struct sockaddr_in ad;
181 #ifndef HAVE_INET_ATON
185 #endif /* HAVE_INET_ATON */
187 memset(&ad, 0, sizeof(ad));
188 ad.sin_family = AF_INET;
189 ad.sin_port = htons(port);
192 if (!inet_aton(hostname, &ad.sin_addr)) {
195 inaddr = inet_addr(hostname);
197 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
202 #endif /* HAVE_INET_ATON */
203 if ((hp = gethostbyname(hostname)) == NULL) {
204 fprintf(stderr, "%s: unknown host.\n", hostname);
209 if (hp->h_length != 4 && hp->h_length != 8) {
211 fprintf(stderr, "illegal address length received for host %s\n", hostname);
215 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
218 return connect(sock, (struct sockaddr *)&ad, sizeof(ad));
222 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
224 gint sock = -1, gai_error;
225 struct addrinfo hints, *res, *ai;
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;
234 /* convert port from integer to string. */
235 g_snprintf(port_str, sizeof(port_str), "%d", port);
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));
243 for (ai = res; ai != NULL; ai = ai->ai_next) {
244 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
248 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
264 SockInfo *sock_connect(const gchar *hostname, gushort port)
270 if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
273 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
278 if (sock_connect_by_hostname(sock, hostname, port) < 0) {
279 if (errno != 0) perror("connect");
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;
297 static void sock_connect_thread(SockInfo *sockinfo)
300 if ((sockinfo->sock = sock_connect_by_getaddrinfo
301 (sockinfo->hostname, sockinfo->port)) < 0)
302 pthread_exit((void *)1);
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);
311 sockinfo->state = CONN_ESTABLISHED;
316 SockInfo *sock_connect_with_thread(const gchar *hostname, gushort port)
322 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
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;
334 pthread_create(&sockinfo->connect_thr, NULL,
335 (void *)sock_connect_thread,
337 pthread_mutex_init(&sockinfo->mutex, NULL);
338 pthread_detach(sockinfo->connect_thr);
345 gint sock_printf(SockInfo *sock, const gchar *format, ...)
350 va_start(args, format);
351 g_vsnprintf(buf, sizeof(buf), format, args);
354 return sock_write(sock, buf, strlen(buf));
357 gint sock_read(SockInfo *sock, gchar *buf, gint len)
359 g_return_val_if_fail(sock != NULL, -1);
363 return ssl_read(sock->ssl, buf, len);
365 return fd_read(sock->sock, buf, len);
368 gint fd_read(gint fd, gchar *buf, gint len)
370 return read(fd, buf, len);
374 gint ssl_read(SSL *ssl, gchar *buf, gint len)
376 return SSL_read(ssl, buf, len);
380 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
382 g_return_val_if_fail(sock != NULL, -1);
386 return ssl_write(sock->ssl, buf, len);
388 return fd_write(sock->sock, buf, len);
391 gint fd_write(gint fd, const gchar *buf, gint len)
396 n = write(fd, buf, len);
408 gint ssl_write(SSL *ssl, const gchar *buf, gint len)
413 n = SSL_write(ssl, buf, len);
425 gint fd_gets(gint fd, gchar *buf, gint len)
427 gchar *newline, *bp = buf;
433 if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
435 if ((newline = memchr(bp, '\n', n)) != NULL)
436 n = newline - bp + 1;
437 if ((n = read(fd, bp, n)) < 0)
441 } while (!newline && len);
448 gint ssl_gets(SSL *ssl, gchar *buf, gint len)
451 gboolean newline = FALSE;
456 while (len > 0 && !newline) {
458 if ((n = SSL_read(ssl, bp, 1)) <= 0)
470 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
472 g_return_val_if_fail(sock != NULL, -1);
476 return ssl_gets(sock->ssl, buf, len);
478 return fd_gets(sock->sock, buf, len);
481 gchar *fd_getline(gint fd)
488 while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
493 str = g_realloc(str, size);
496 if (buf[len - 1] == '\n')
504 gchar *ssl_getline(SSL *ssl)
511 while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
516 str = g_realloc(str, size);
519 if (buf[len - 1] == '\n')
527 gchar *sock_getline(SockInfo *sock)
529 g_return_val_if_fail(sock != NULL, NULL);
533 return ssl_getline(sock->ssl);
535 return fd_getline(sock->sock);
538 gint sock_puts(SockInfo *sock, const gchar *buf)
542 if ((ret = sock_write(sock, buf, strlen(buf))) < 0)
544 return sock_write(sock, "\r\n", 2);
547 /* peek at the next socket character without actually reading it */
548 gint sock_peek(SockInfo *sock)
553 g_return_val_if_fail(sock != NULL, -1);
555 if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
561 gint sock_close(SockInfo *sock)
570 ssl_done_socket(sock);
572 ret = fd_close(sock->sock);
573 g_free(sock->hostname);
579 gint fd_close(gint fd)
584 gint sock_gdk_input_add(SockInfo *sock,
585 GdkInputCondition condition,
586 GdkInputFunction function,
589 g_return_val_if_fail(sock != NULL, -1);
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);