#include <glib.h>
#include <sys/time.h>
#include <sys/types.h>
+#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#endif
#define BUFFSIZE 8192
-#define IO_TIMEOUT 60
+
+typedef gint (*SockAddrFunc) (GList *addr_list,
+ gpointer data);
+
+typedef struct _SockConnectData SockConnectData;
+typedef struct _SockLookupData SockLookupData;
+typedef struct _SockAddrData SockAddrData;
+typedef struct _SockSource SockSource;
+
+struct _SockConnectData {
+ gint id;
+ gchar *hostname;
+ gushort port;
+ GList *addr_list;
+ GList *cur_addr;
+ SockLookupData *lookup_data;
+ GIOChannel *channel;
+ guint io_tag;
+ SockConnectFunc func;
+ gpointer data;
+};
+
+struct _SockLookupData {
+ gchar *hostname;
+ pid_t child_pid;
+ GIOChannel *channel;
+ guint io_tag;
+ SockAddrFunc func;
+ gpointer data;
+};
+
+struct _SockAddrData {
+ gint family;
+ gint socktype;
+ gint protocol;
+ gint addr_len;
+ struct sockaddr *addr;
+};
+
+struct _SockSource {
+ GSource parent;
+ SockInfo *sock;
+};
+
+static guint io_timeout = 60;
+
+static GList *sock_connect_data_list = NULL;
+
+static gboolean sock_prepare (GSource *source,
+ gint *timeout);
+static gboolean sock_check (GSource *source);
+static gboolean sock_dispatch (GSource *source,
+ GSourceFunc callback,
+ gpointer user_data);
+
+GSourceFuncs sock_watch_funcs = {
+ sock_prepare,
+ sock_check,
+ sock_dispatch,
+ NULL
+};
static gint sock_connect_with_timeout (gint sock,
const struct sockaddr *serv_addr,
static SockInfo *sockinfo_from_fd(const gchar *hostname,
gushort port,
gint sock);
+static void sock_address_list_free (GList *addr_list);
+
+static gboolean sock_connect_async_cb (GIOChannel *source,
+ GIOCondition condition,
+ gpointer data);
+static gint sock_connect_async_get_address_info_cb
+ (GList *addr_list,
+ gpointer data);
+
+static gint sock_connect_address_list_async (SockConnectData *conn_data);
+
+static gboolean sock_get_address_info_async_cb (GIOChannel *source,
+ GIOCondition condition,
+ gpointer data);
+static SockLookupData *sock_get_address_info_async
+ (const gchar *hostname,
+ gushort port,
+ SockAddrFunc func,
+ gpointer data);
+static gint sock_get_address_info_async_cancel (SockLookupData *lookup_data);
+
+
+gint sock_set_io_timeout(guint sec)
+{
+ io_timeout = sec;
+ return 0;
+}
gint fd_connect_unix(const gchar *path)
{
return set_nonblocking_mode(sock->sock, nonblock);
}
-
static gboolean is_nonblocking_mode(gint fd)
{
gint flags;
return is_nonblocking_mode(sock->sock);
}
+
+static gboolean sock_prepare(GSource *source, gint *timeout)
+{
+ *timeout = 1;
+ return FALSE;
+}
+
+static gboolean sock_check(GSource *source)
+{
+ SockInfo *sock = ((SockSource *)source)->sock;
+ struct timeval timeout = {0, 0};
+ fd_set fds;
+ GIOCondition condition = sock->condition;
+
+#if USE_OPENSSL
+ if (sock->ssl) {
+ if (condition & G_IO_IN) {
+ if (SSL_pending(sock->ssl) > 0)
+ return TRUE;
+ if (SSL_want_write(sock->ssl))
+ condition |= G_IO_OUT;
+ }
+
+ if (condition & G_IO_OUT) {
+ if (SSL_want_read(sock->ssl))
+ condition |= G_IO_IN;
+ }
+ }
+#endif
+
+ FD_ZERO(&fds);
+ FD_SET(sock->sock, &fds);
+
+ select(sock->sock + 1,
+ (condition & G_IO_IN) ? &fds : NULL,
+ (condition & G_IO_OUT) ? &fds : NULL,
+ NULL, &timeout);
+
+ return FD_ISSET(sock->sock, &fds) != 0;
+}
+
+static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
+ gpointer user_data)
+{
+ SockInfo *sock = ((SockSource *)source)->sock;
+
+ return sock->callback(sock, sock->condition, user_data);
+}
+
+static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
+ gpointer data)
+{
+ SockInfo *sock = (SockInfo *)data;
+
+ return sock->callback(sock, condition, sock->data);
+}
+
+guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
+ gpointer data)
+{
+ sock->callback = func;
+ sock->condition = condition;
+ sock->data = data;
+
+#if USE_OPENSSL
+ if (sock->ssl)
+ {
+ GSource *source = g_source_new(&sock_watch_funcs,
+ sizeof(SockSource));
+ ((SockSource *) source)->sock = sock;
+ g_source_set_priority(source, G_PRIORITY_DEFAULT);
+ g_source_set_can_recurse(source, FALSE);
+ g_source_attach(source, NULL);
+ }
+#endif
+
+ return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
+}
+
static gint fd_check_io(gint fd, GIOCondition cond)
{
struct timeval timeout;
fd_set fds;
- timeout.tv_sec = IO_TIMEOUT;
+ if (is_nonblocking_mode(fd))
+ return 0;
+
+ timeout.tv_sec = io_timeout;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (cond == G_IO_IN) {
- select(fd + 1, &fds, NULL, NULL, &timeout);
+ select(fd + 1, &fds, NULL, NULL,
+ io_timeout > 0 ? &timeout : NULL);
} else {
- select(fd + 1, NULL, &fds, NULL, &timeout);
+ select(fd + 1, NULL, &fds, NULL,
+ io_timeout > 0 ? &timeout : NULL);
}
if (FD_ISSET(fd, &fds)) {
{
struct hostent *hp;
void (*prev_handler)(gint);
- guint timeout_secs = IO_TIMEOUT;
alarm(0);
prev_handler = signal(SIGALRM, timeout_handler);
errno = 0;
return NULL;
}
- alarm(timeout_secs);
+ alarm(io_timeout);
if ((hp = gethostbyname(hostname)) == NULL) {
alarm(0);
{
struct hostent *hp;
struct sockaddr_in ad;
- guint timeout_secs = IO_TIMEOUT;
memset(&ad, 0, sizeof(ad));
ad.sin_family = AF_INET;
}
return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
- sizeof(ad), timeout_secs);
+ sizeof(ad), io_timeout);
}
#else /* INET6 */
{
gint sock = -1, gai_error;
struct addrinfo hints, *res, *ai;
- guint timeout_secs = IO_TIMEOUT;
gchar port_str[6];
memset(&hints, 0, sizeof(hints));
continue;
if (sock_connect_with_timeout
- (sock, ai->ai_addr, ai->ai_addrlen, timeout_secs) == 0)
+ (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
break;
close(sock);
return sockinfo_from_fd(hostname, port, sock);
}
+static void sock_address_list_free(GList *addr_list)
+{
+ GList *cur;
+
+ for (cur = addr_list; cur != NULL; cur = cur->next) {
+ SockAddrData *addr_data = (SockAddrData *)cur->data;
+ g_free(addr_data->addr);
+ g_free(addr_data);
+ }
+
+ g_list_free(addr_list);
+}
+
+/* asynchronous TCP connection */
+
+static gboolean sock_connect_async_cb(GIOChannel *source,
+ GIOCondition condition, gpointer data)
+{
+ SockConnectData *conn_data = (SockConnectData *)data;
+ gint fd;
+ gint val;
+ gint len;
+ SockInfo *sockinfo;
+
+ fd = g_io_channel_unix_get_fd(source);
+
+ conn_data->io_tag = 0;
+ conn_data->channel = NULL;
+ g_io_channel_unref(source);
+
+ len = sizeof(val);
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
+ perror("getsockopt");
+ close(fd);
+ sock_connect_address_list_async(conn_data);
+ return FALSE;
+ }
+
+ if (val != 0) {
+ close(fd);
+ sock_connect_address_list_async(conn_data);
+ return FALSE;
+ }
+
+ sockinfo = g_new0(SockInfo, 1);
+ sockinfo->sock = fd;
+ sockinfo->sock_ch = g_io_channel_unix_new(fd);
+ sockinfo->hostname = g_strdup(conn_data->hostname);
+ sockinfo->port = conn_data->port;
+ sockinfo->state = CONN_ESTABLISHED;
+
+ conn_data->func(sockinfo, conn_data->data);
+
+ sock_connect_async_cancel(conn_data->id);
+
+ return FALSE;
+}
+
+static gint sock_connect_async_get_address_info_cb(GList *addr_list,
+ gpointer data)
+{
+ SockConnectData *conn_data = (SockConnectData *)data;
+
+ conn_data->addr_list = addr_list;
+ conn_data->cur_addr = addr_list;
+ conn_data->lookup_data = NULL;
+
+ return sock_connect_address_list_async(conn_data);
+}
+
+gint sock_connect_async(const gchar *hostname, gushort port,
+ SockConnectFunc func, gpointer data)
+{
+ static gint id = 1;
+ SockConnectData *conn_data;
+
+ conn_data = g_new0(SockConnectData, 1);
+ conn_data->id = id++;
+ conn_data->hostname = g_strdup(hostname);
+ conn_data->port = port;
+ conn_data->addr_list = NULL;
+ conn_data->cur_addr = NULL;
+ conn_data->io_tag = 0;
+ conn_data->func = func;
+ conn_data->data = data;
+
+ conn_data->lookup_data = sock_get_address_info_async
+ (hostname, port, sock_connect_async_get_address_info_cb,
+ conn_data);
+
+ if (conn_data->lookup_data == NULL) {
+ g_free(conn_data->hostname);
+ g_free(conn_data);
+ return -1;
+ }
+
+ sock_connect_data_list = g_list_append(sock_connect_data_list,
+ conn_data);
+
+ return conn_data->id;
+}
+
+gint sock_connect_async_cancel(gint id)
+{
+ SockConnectData *conn_data = NULL;
+ GList *cur;
+
+ for (cur = sock_connect_data_list; cur != NULL; cur = cur->next) {
+ if (((SockConnectData *)cur->data)->id == id) {
+ conn_data = (SockConnectData *)cur->data;
+ break;
+ }
+ }
+
+ if (conn_data) {
+ sock_connect_data_list = g_list_remove(sock_connect_data_list,
+ conn_data);
+
+ if (conn_data->lookup_data)
+ sock_get_address_info_async_cancel
+ (conn_data->lookup_data);
+
+ if (conn_data->io_tag > 0)
+ g_source_remove(conn_data->io_tag);
+ if (conn_data->channel) {
+ g_io_channel_close(conn_data->channel);
+ g_io_channel_unref(conn_data->channel);
+ }
+
+ sock_address_list_free(conn_data->addr_list);
+ g_free(conn_data->hostname);
+ g_free(conn_data);
+ } else {
+ g_warning("sock_connect_async_cancel: id %d not found.\n", id);
+ return -1;
+ }
+
+ return 0;
+}
+
+static gint sock_connect_address_list_async(SockConnectData *conn_data)
+{
+ SockAddrData *addr_data;
+ gint sock = -1;
+
+ for (; conn_data->cur_addr != NULL;
+ conn_data->cur_addr = conn_data->cur_addr->next) {
+ addr_data = (SockAddrData *)conn_data->cur_addr->data;
+
+ if ((sock = socket(addr_data->family, addr_data->socktype,
+ addr_data->protocol)) < 0) {
+ perror("socket");
+ continue;
+ }
+
+ set_nonblocking_mode(sock, TRUE);
+
+ if (connect(sock, addr_data->addr, addr_data->addr_len) < 0) {
+ if (EINPROGRESS == errno) {
+ break;
+ } else {
+ perror("connect");
+ close(sock);
+ }
+ } else
+ break;
+ }
+
+ if (conn_data->cur_addr == NULL) {
+ g_warning("sock_connect_address_list_async: "
+ "connection to %s:%d failed\n",
+ conn_data->hostname, conn_data->port);
+ conn_data->func(NULL, conn_data->data);
+ sock_connect_async_cancel(conn_data->id);
+ return -1;
+ }
+
+ conn_data->cur_addr = conn_data->cur_addr->next;
+
+ conn_data->channel = g_io_channel_unix_new(sock);
+ conn_data->io_tag = g_io_add_watch(conn_data->channel, G_IO_IN|G_IO_OUT,
+ sock_connect_async_cb, conn_data);
+
+ return 0;
+}
+
+/* asynchronous DNS lookup */
+
+static gboolean sock_get_address_info_async_cb(GIOChannel *source,
+ GIOCondition condition,
+ gpointer data)
+{
+ SockLookupData *lookup_data = (SockLookupData *)data;
+ GList *addr_list = NULL;
+ SockAddrData *addr_data;
+ guint bytes_read;
+ gint ai_member[4];
+ struct sockaddr *addr;
+
+ for (;;) {
+ if (g_io_channel_read(source, (gchar *)ai_member,
+ sizeof(ai_member), &bytes_read)
+ != G_IO_ERROR_NONE) {
+ g_warning("sock_get_address_info_async_cb: "
+ "address length read error\n");
+ break;
+ }
+
+ if (bytes_read == 0 || bytes_read != sizeof(ai_member))
+ break;
+
+ if (ai_member[0] == AF_UNSPEC) {
+ g_warning("DNS lookup failed\n");
+ break;
+ }
+
+ addr = g_malloc(ai_member[3]);
+ if (g_io_channel_read(source, (gchar *)addr, ai_member[3],
+ &bytes_read)
+ != G_IO_ERROR_NONE) {
+ g_warning("sock_get_address_info_async_cb: "
+ "address data read error\n");
+ g_free(addr);
+ break;
+ }
+
+ if (bytes_read != ai_member[3]) {
+ g_warning("sock_get_address_info_async_cb: "
+ "incomplete address data\n");
+ g_free(addr);
+ break;
+ }
+
+ addr_data = g_new0(SockAddrData, 1);
+ addr_data->family = ai_member[0];
+ addr_data->socktype = ai_member[1];
+ addr_data->protocol = ai_member[2];
+ addr_data->addr_len = ai_member[3];
+ addr_data->addr = addr;
+
+ addr_list = g_list_append(addr_list, addr_data);
+ }
+
+ g_io_channel_close(source);
+ g_io_channel_unref(source);
+
+ kill(lookup_data->child_pid, SIGKILL);
+ waitpid(lookup_data->child_pid, NULL, 0);
+
+ lookup_data->func(addr_list, lookup_data->data);
+
+ g_free(lookup_data->hostname);
+ g_free(lookup_data);
+
+ return FALSE;
+}
+
+static SockLookupData *sock_get_address_info_async(const gchar *hostname,
+ gushort port,
+ SockAddrFunc func,
+ gpointer data)
+{
+ SockLookupData *lookup_data = NULL;
+ gint pipe_fds[2];
+ pid_t pid;
+
+ if (pipe(pipe_fds) < 0) {
+ perror("pipe");
+ func(NULL, data);
+ return NULL;
+ }
+
+ if ((pid = fork()) < 0) {
+ perror("fork");
+ func(NULL, data);
+ return NULL;
+ }
+
+ /* child process */
+ if (pid == 0) {
+#ifdef INET6
+ gint gai_err;
+ struct addrinfo hints, *res, *ai;
+ gchar port_str[6];
+#else /* !INET6 */
+ struct hostent *hp;
+ gchar **addr_list_p;
+ struct sockaddr_in ad;
+#endif /* INET6 */
+ gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
+
+ close(pipe_fds[0]);
+
+#ifdef INET6
+ memset(&hints, 0, sizeof(hints));
+ /* hints.ai_flags = AI_CANONNAME; */
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ g_snprintf(port_str, sizeof(port_str), "%d", port);
+
+ gai_err = getaddrinfo(hostname, port_str, &hints, &res);
+ if (gai_err != 0) {
+ g_warning("getaddrinfo for %s:%s failed: %s\n",
+ hostname, port_str, gai_strerror(gai_err));
+ fd_write_all(pipe_fds[1], (gchar *)ai_member,
+ sizeof(ai_member));
+ close(pipe_fds[1]);
+ _exit(1);
+ }
+
+ for (ai = res; ai != NULL; ai = ai->ai_next) {
+ ai_member[0] = ai->ai_family;
+ ai_member[1] = ai->ai_socktype;
+ ai_member[2] = ai->ai_protocol;
+ ai_member[3] = ai->ai_addrlen;
+
+ fd_write_all(pipe_fds[1], (gchar *)ai_member,
+ sizeof(ai_member));
+ fd_write_all(pipe_fds[1], (gchar *)ai->ai_addr,
+ ai->ai_addrlen);
+ }
+
+ if (res != NULL)
+ freeaddrinfo(res);
+#else /* !INET6 */
+ hp = my_gethostbyname(hostname);
+ if (hp == NULL || hp->h_addrtype != AF_INET) {
+ fd_write_all(pipe_fds[1], (gchar *)ai_member,
+ sizeof(ai_member));
+ close(pipe_fds[1]);
+ _exit(1);
+ }
+
+ ai_member[0] = AF_INET;
+ ai_member[1] = SOCK_STREAM;
+ ai_member[2] = IPPROTO_TCP;
+ ai_member[3] = sizeof(ad);
+
+ memset(&ad, 0, sizeof(ad));
+ ad.sin_family = AF_INET;
+ ad.sin_port = htons(port);
+
+ for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
+ addr_list_p++) {
+ memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
+ fd_write_all(pipe_fds[1], (gchar *)ai_member,
+ sizeof(ai_member));
+ fd_write_all(pipe_fds[1], (gchar *)&ad, sizeof(ad));
+ }
+#endif /* INET6 */
+
+ close(pipe_fds[1]);
+
+ _exit(0);
+ } else {
+ close(pipe_fds[1]);
+
+ lookup_data = g_new0(SockLookupData, 1);
+ lookup_data->hostname = g_strdup(hostname);
+ lookup_data->child_pid = pid;
+ lookup_data->func = func;
+ lookup_data->data = data;
+
+ lookup_data->channel = g_io_channel_unix_new(pipe_fds[0]);
+ lookup_data->io_tag = g_io_add_watch
+ (lookup_data->channel, G_IO_IN,
+ sock_get_address_info_async_cb, lookup_data);
+ }
+
+ return lookup_data;
+}
+
+static gint sock_get_address_info_async_cancel(SockLookupData *lookup_data)
+{
+ if (lookup_data->io_tag > 0)
+ g_source_remove(lookup_data->io_tag);
+ if (lookup_data->channel) {
+ g_io_channel_close(lookup_data->channel);
+ g_io_channel_unref(lookup_data->channel);
+ }
+
+ if (lookup_data->child_pid > 0) {
+ kill(lookup_data->child_pid, SIGKILL);
+ waitpid(lookup_data->child_pid, NULL, 0);
+ }
+
+ g_free(lookup_data->hostname);
+ g_free(lookup_data);
+
+ return 0;
+}
+
static SockInfo *sockinfo_from_fd(const gchar *hostname,
gushort port,
gint sock)
{
SockInfo *sockinfo;
-
+
sockinfo = g_new0(SockInfo, 1);
sockinfo->sock = sock;
+ sockinfo->sock_ch = g_io_channel_unix_new(sock);
sockinfo->hostname = g_strdup(hostname);
sockinfo->port = port;
sockinfo->state = CONN_ESTABLISHED;
return sock_write_all(sock, buf, strlen(buf));
}
-gint sock_read(SockInfo *sock, gchar *buf, gint len)
-{
- g_return_val_if_fail(sock != NULL, -1);
-
-#if USE_OPENSSL
- if (sock->ssl)
- return ssl_read(sock->ssl, buf, len);
-#endif
- return fd_read(sock->sock, buf, len);
-}
-
gint fd_read(gint fd, gchar *buf, gint len)
{
if (fd_check_io(fd, G_IO_IN) < 0)
#if USE_OPENSSL
gint ssl_read(SSL *ssl, gchar *buf, gint len)
{
- return SSL_read(ssl, buf, len);
+ gint ret;
+
+ ret = SSL_read(ssl, buf, len);
+
+ switch (SSL_get_error(ssl, ret)) {
+ case SSL_ERROR_NONE:
+ return ret;
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ errno = EAGAIN;
+ return -1;
+ default:
+ return -1;
+ }
}
#endif
-gint sock_write(SockInfo *sock, const gchar *buf, gint len)
+gint sock_read(SockInfo *sock, gchar *buf, gint len)
{
+ gint ret;
+
g_return_val_if_fail(sock != NULL, -1);
#if USE_OPENSSL
if (sock->ssl)
- return ssl_write(sock->ssl, buf, len);
+ ret = ssl_read(sock->ssl, buf, len);
+ else
#endif
- return fd_write(sock->sock, buf, len);
+ ret = fd_read(sock->sock, buf, len);
+
+ if (ret < 0)
+ sock->state = CONN_DISCONNECTED;
+ return ret;
}
gint fd_write(gint fd, const gchar *buf, gint len)
#if USE_OPENSSL
gint ssl_write(SSL *ssl, const gchar *buf, gint len)
{
- return SSL_write(ssl, buf, len);
+ gint ret;
+
+ ret = SSL_write(ssl, buf, len);
+
+ switch (SSL_get_error(ssl, ret)) {
+ case SSL_ERROR_NONE:
+ return ret;
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ errno = EAGAIN;
+ return -1;
+ default:
+ return -1;
+ }
}
#endif
-gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
+gint sock_write(SockInfo *sock, const gchar *buf, gint len)
{
+ gint ret;
+
g_return_val_if_fail(sock != NULL, -1);
#if USE_OPENSSL
if (sock->ssl)
- return ssl_write_all(sock->ssl, buf, len);
+ ret = ssl_write(sock->ssl, buf, len);
+ else
#endif
- return fd_write_all(sock->sock, buf, len);
+ ret = fd_write(sock->sock, buf, len);
+
+ if (ret < 0)
+ sock->state = CONN_DISCONNECTED;
+ return ret;
}
gint fd_write_all(gint fd, const gchar *buf, gint len)
gint n, wrlen = 0;
while (len) {
- n = SSL_write(ssl, buf, len);
+ n = ssl_write(ssl, buf, len);
if (n <= 0)
return -1;
len -= n;
}
#endif
+gint sock_write_all(SockInfo *sock, const gchar *buf, gint len)
+{
+ gint ret;
+
+ g_return_val_if_fail(sock != NULL, -1);
+
+#if USE_OPENSSL
+ if (sock->ssl)
+ ret = ssl_write_all(sock->ssl, buf, len);
+ else
+#endif
+ ret = fd_write_all(sock->sock, buf, len);
+
+ if (ret < 0)
+ sock->state = CONN_DISCONNECTED;
+ return ret;
+}
+
gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
{
if (fd_check_io(fd, G_IO_IN) < 0)
if (--len < 1)
return -1;
do {
- if ((n = SSL_peek(ssl, bp, len)) <= 0)
+ if ((n = ssl_peek(ssl, bp, len)) <= 0)
return -1;
if ((newline = memchr(bp, '\n', n)) != NULL)
n = newline - bp + 1;
- if ((n = SSL_read(ssl, bp, n)) < 0)
+ if ((n = ssl_read(ssl, bp, n)) < 0)
return -1;
bp += n;
len -= n;
gint sock_gets(SockInfo *sock, gchar *buf, gint len)
{
+ gint ret;
+
g_return_val_if_fail(sock != NULL, -1);
#if USE_OPENSSL
if (sock->ssl)
return ssl_gets(sock->ssl, buf, len);
+ else
#endif
- return fd_gets(sock->sock, buf, len);
+ return fd_gets(sock->sock, buf, len);
+
+ if (ret < 0)
+ sock->state = CONN_DISCONNECTED;
+ return ret;
}
-gchar *fd_getline(gint fd)
+gint fd_getline(gint fd, gchar **str)
{
gchar buf[BUFFSIZE];
- gchar *str = NULL;
gint len;
gulong size = 1;
while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
size += len;
- if (!str)
- str = g_strdup(buf);
+ if (!*str)
+ *str = g_strdup(buf);
else {
- str = g_realloc(str, size);
- strcat(str, buf);
+ *str = g_realloc(*str, size);
+ strcat(*str, buf);
}
if (buf[len - 1] == '\n')
break;
}
- if (len == -1) {
- log_error("Read from socket fd%d failed: %s\n",
- fd, strerror(errno));
- if (str)
- g_free(str);
- return NULL;
- }
+ if (len == -1 && *str)
+ g_free(*str);
- return str;
+ return len;
}
#if USE_OPENSSL
-gchar *ssl_getline(SSL *ssl)
+gint ssl_getline(SSL *ssl, gchar **str)
{
gchar buf[BUFFSIZE];
- gchar *str = NULL;
gint len;
gulong size = 1;
while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
size += len;
- if (!str)
- str = g_strdup(buf);
+ if (!*str)
+ *str = g_strdup(buf);
else {
- str = g_realloc(str, size);
- strcat(str, buf);
+ *str = g_realloc(*str, size);
+ strcat(*str, buf);
}
if (buf[len - 1] == '\n')
break;
}
+ if (len == -1 && *str)
+ g_free(*str);
- return str;
+ return len;
}
#endif
gchar *sock_getline(SockInfo *sock)
{
+ gint ret;
+ gchar *str = NULL;
+
g_return_val_if_fail(sock != NULL, NULL);
#if USE_OPENSSL
if (sock->ssl)
- return ssl_getline(sock->ssl);
+ ret = ssl_getline(sock->ssl, &str);
+ else
#endif
- return fd_getline(sock->sock);
+ ret = fd_getline(sock->sock, &str);
+
+ if (ret < 0)
+ sock->state = CONN_DISCONNECTED;
+ return str;
}
gint sock_puts(SockInfo *sock, const gchar *buf)
return sock_write_all(sock, "\r\n", 2);
}
-/* peek at the next socket character without actually reading it */
-gint sock_peek(SockInfo *sock)
+/* peek at the socket data without actually reading it */
+#if USE_OPENSSL
+gint ssl_peek(SSL *ssl, gchar *buf, gint len)
{
- gint n;
- gchar ch;
+ gint ret;
+
+ ret = SSL_peek(ssl, buf, len);
+ switch (SSL_get_error(ssl, ret)) {
+ case SSL_ERROR_NONE:
+ return ret;
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ errno = EAGAIN;
+ return -1;
+ default:
+ return -1;
+ }
+}
+#endif
+
+gint sock_peek(SockInfo *sock, gchar *buf, gint len)
+{
g_return_val_if_fail(sock != NULL, -1);
#if USE_OPENSSL
- if (sock->ssl) {
- if ((n = SSL_peek(sock->ssl, &ch, 1)) < 0)
- return -1;
- else
- return ch;
- }
+ if (sock->ssl)
+ return ssl_peek(sock->ssl, buf, len);
#endif
- if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
- return -1;
- else
- return ch;
+ return fd_recv(sock->sock, buf, len, MSG_PEEK);
}
gint sock_close(SockInfo *sock)
if (!sock)
return 0;
+ if (sock->sock_ch)
+ g_io_channel_unref(sock->sock_ch);
+
#if USE_OPENSSL
if (sock->ssl)
ssl_done_socket(sock);
{
return close(fd);
}
-
-gint sock_input_add(SockInfo *sock,
- GIOCondition condition,
- GIOFunc function,
- gpointer data)
-{
- GIOChannel *channel;
- guint result;
-
- g_return_val_if_fail(sock != NULL, -1);
-
- channel = g_io_channel_unix_new(sock->sock);
- /* :WK: We have to change some things here becuse most likey
- function() does take SockInfo * and not an gint */
- result = g_io_add_watch(channel, condition, function, data);
- g_io_channel_unref(channel);
-
- return result;
-}