#include "socket.h"
+#if USE_GIO
+#error USE_GIO is currently not supported
+#endif
+
#define BUFFSIZE 8192
#ifndef INET6
gushort port);
#endif
-gint sock_connect_unix(const gchar *path)
+
+gint fd_connect_unix(const gchar *path)
{
gint sock;
struct sockaddr_un addr;
return sock;
}
-gint sock_open_unix(const gchar *path)
+gint fd_open_unix(const gchar *path)
{
gint sock;
struct sockaddr_un addr;
return sock;
}
-gint sock_accept(gint sock)
+gint fd_accept(gint sock)
{
struct sockaddr_in caddr;
gint caddr_len;
return accept(sock, (struct sockaddr *)&caddr, &caddr_len);
}
-gint sock_set_nonblocking_mode(gint sock, gboolean nonblock)
+
+static gint set_nonblocking_mode(gint fd, gboolean nonblock)
{
gint flags;
- flags = fcntl(sock, F_GETFL, 0);
+ flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
perror("fcntl");
return -1;
else
flags &= ~O_NONBLOCK;
- return fcntl(sock, F_SETFL, flags);
+ return fcntl(fd, F_SETFL, flags);
}
-gboolean sock_is_nonblocking_mode(gint sock)
+gint sock_set_nonblocking_mode(SockInfo *sock, gboolean nonblock)
+{
+ g_return_val_if_fail(sock != NULL, -1);
+
+ return set_nonblocking_mode(sock->sock, nonblock);
+}
+
+
+static gboolean is_nonblocking_mode(gint fd)
{
gint flags;
- flags = fcntl(sock, F_GETFL, 0);
+ flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
perror("fcntl");
return FALSE;
return ((flags & O_NONBLOCK) != 0);
}
+gboolean sock_is_nonblocking_mode(SockInfo *sock)
+{
+ g_assert(sock);
+
+ return is_nonblocking_mode(sock->sock);
+}
+
+
#ifndef INET6
static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
gushort port)
#ifdef INET6
if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
return NULL;
- if (sock_set_nonblocking_mode(sock, TRUE) < 0) return NULL;
+ if (set_nonblocking_mode(sock, TRUE) < 0) return NULL;
ret = sock;
#else
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return NULL;
}
- if (sock_set_nonblocking_mode(sock, TRUE) < 0) return NULL;
+ if (set_nonblocking_mode(sock, TRUE) < 0) return NULL;
ret = sock_connect_by_hostname(sock, hostname, port);
if (ret < 0 && errno != EINPROGRESS) {
if (errno != 0) perror("connect");
- sock_close(sock);
+ close(sock);
return NULL;
}
#endif /* INET6 */
if (sock_connect_by_hostname(sock, hostname, port) < 0) {
if (errno != 0) perror("connect");
- sock_close(sock);
+ close(sock);
return NULL;
}
#endif /* INET6 */
}
#endif
-void sock_sockinfo_free(SockInfo *sockinfo)
-{
- g_free(sockinfo->hostname);
- g_free(sockinfo);
-}
-gint sock_printf(gint sock, const gchar *format, ...)
+gint sock_printf(SockInfo *sock, const gchar *format, ...)
{
va_list args;
gchar buf[BUFFSIZE];
return sock_write(sock, buf, strlen(buf));
}
-gint sock_write(gint sock, const gchar *buf, gint len)
+gint sock_write(SockInfo *sock, const gchar *buf, gint len)
+{
+ g_return_val_if_fail(sock != NULL, -1);
+
+ return fd_write(sock->sock, buf, len);
+}
+
+gint fd_write(gint fd, const gchar *buf, gint len)
{
gint n, wrlen = 0;
while (len) {
- n = write(sock, buf, len);
+ n = write(fd, buf, len);
if (n <= 0)
return -1;
len -= n;
return wrlen;
}
-gint sock_read(gint sock, gchar *buf, gint len)
+gint sock_read(SockInfo *sock, gchar *buf, gint len)
+{
+ g_return_val_if_fail(sock != NULL, -1);
+
+ return fd_read(sock->sock, buf, len);
+}
+
+gint fd_read(gint fd, gchar *buf, gint len)
{
gchar *newline, *bp = buf;
gint n;
if (--len < 1)
return -1;
do {
- if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0)
+ if ((n = recv(fd, bp, len, MSG_PEEK)) <= 0)
return -1;
if ((newline = memchr(bp, '\n', n)) != NULL)
n = newline - bp + 1;
- if ((n = read(sock, bp, n)) < 0)
+ if ((n = read(fd, bp, n)) < 0)
return -1;
bp += n;
len -= n;
return bp - buf;
}
-gint sock_puts(gint sock, const gchar *buf)
+gint sock_puts(SockInfo *sock, const gchar *buf)
{
gint ret;
}
/* peek at the next socket character without actually reading it */
-gint sock_peek(gint sock)
+gint sock_peek(SockInfo *sock)
{
gint n;
gchar ch;
- if ((n = recv(sock, &ch, 1, MSG_PEEK)) < 0)
+ g_return_val_if_fail(sock != NULL, -1);
+
+ if ((n = recv(sock->sock, &ch, 1, MSG_PEEK)) < 0)
return -1;
else
return ch;
}
-gint sock_close(gint sock)
+gint sock_close(SockInfo *sock)
{
- return close(sock);
+ gint rc;
+
+ if (!sock)
+ return 0;
+
+ rc = fd_close(sock->sock);
+ g_free(sock->hostname);
+ g_free(sock);
+ return rc;
+}
+
+gint fd_close(gint fd)
+{
+ return close(fd);
+}
+
+gint sock_gdk_input_add(SockInfo *sock,
+ GdkInputCondition condition,
+ GdkInputFunction function,
+ gpointer data)
+{
+ g_return_val_if_fail(sock != NULL, -1);
+
+ /* :WK: We have to change some things here becuse most likey
+ function() does take SockInfo * and not an gint */
+ return gdk_input_add(sock->sock, condition, function, data);
}