diff options
Diffstat (limited to 'libsylph')
-rw-r--r-- | libsylph/defs.h | 4 | ||||
-rw-r--r-- | libsylph/socket.c | 91 | ||||
-rw-r--r-- | libsylph/socket.h | 2 |
3 files changed, 96 insertions, 1 deletions
diff --git a/libsylph/defs.h b/libsylph/defs.h index b1814487..2b0abcda 100644 --- a/libsylph/defs.h +++ b/libsylph/defs.h @@ -77,6 +77,10 @@ #define MARK_VERSION 2 #ifdef G_OS_WIN32 +# define REMOTE_CMD_PORT 49215 +#endif + +#ifdef G_OS_WIN32 # define DEFAULT_SIGNATURE "signature.txt" #else # define DEFAULT_SIGNATURE ".signature" diff --git a/libsylph/socket.c b/libsylph/socket.c index c45aa598..433491c3 100644 --- a/libsylph/socket.c +++ b/libsylph/socket.c @@ -183,6 +183,87 @@ gint sock_set_io_timeout(guint sec) return 0; } +gint fd_connect_inet(gushort port) +{ +#ifdef G_OS_WIN32 + SOCKET sock; +#else + gint sock; +#endif + struct sockaddr_in addr; + +#ifdef G_OS_WIN32 + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { + g_warning("fd_connect_inet(): socket() failed: %ld\n", + WSAGetLastError()); +#else + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("fd_connect_inet(): socket"); +#endif + return -1; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + fd_close(sock); + return -1; + } + + return sock; +} + +gint fd_open_inet(gushort port) +{ +#ifdef G_OS_WIN32 + SOCKET sock; +#else + gint sock; +#endif + struct sockaddr_in addr; + gint val; + +#ifdef G_OS_WIN32 + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { + g_warning("fd_open_inet(): socket() failed: %ld\n", + WSAGetLastError()); +#else + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("fd_open_inet(): socket"); +#endif + return -1; + } + + val = 1; + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { + perror("setsockopt"); + fd_close(sock); + return -1; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("bind"); + fd_close(sock); + return -1; + } + + if (listen(sock, 1) < 0) { + perror("listen"); + fd_close(sock); + return -1; + } + + return sock; +} + gint fd_connect_unix(const gchar *path) { #ifdef G_OS_UNIX @@ -191,7 +272,7 @@ gint fd_connect_unix(const gchar *path) sock = socket(PF_UNIX, SOCK_STREAM, 0); if (sock < 0) { - perror("sock_connect_unix(): socket"); + perror("fd_connect_unix(): socket"); return -1; } @@ -215,6 +296,7 @@ gint fd_open_unix(const gchar *path) #ifdef G_OS_UNIX gint sock; struct sockaddr_un addr; + gint val; sock = socket(PF_UNIX, SOCK_STREAM, 0); @@ -223,6 +305,13 @@ gint fd_open_unix(const gchar *path) return -1; } + val = 1; + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { + perror("setsockopt"); + fd_close(sock); + return -1; + } + memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); diff --git a/libsylph/socket.h b/libsylph/socket.h index 1a63793f..45729c06 100644 --- a/libsylph/socket.h +++ b/libsylph/socket.h @@ -100,6 +100,8 @@ gint sock_peek (SockInfo *sock, gchar *buf, gint len); gint sock_close (SockInfo *sock); /* Functions to directly work on FD. They are needed for pipes */ +gint fd_connect_inet (gushort port); +gint fd_open_inet (gushort port); gint fd_connect_unix (const gchar *path); gint fd_open_unix (const gchar *path); gint fd_accept (gint sock); |