aboutsummaryrefslogtreecommitdiffstats
path: root/src/socket_util.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2009-03-14 18:29:38 +0100
committerMax Kellermann <max@duempel.org>2009-03-14 18:29:38 +0100
commitc8c392050069f9a6b922ccd8ea8a4c71dda4a2c8 (patch)
tree37aedc11cd4fc5a7a77fe7e0cb93835a33e2520b /src/socket_util.c
parentdccb973cfef9664644e4945fbddedb3eee8c215b (diff)
downloadmpd-c8c392050069f9a6b922ccd8ea8a4c71dda4a2c8.tar.gz
mpd-c8c392050069f9a6b922ccd8ea8a4c71dda4a2c8.tar.xz
mpd-c8c392050069f9a6b922ccd8ea8a4c71dda4a2c8.zip
socket_util: added socket_bind_listen()
Moved code from listen_add_address() (listen.c) to socket_util.c.
Diffstat (limited to 'src/socket_util.c')
-rw-r--r--src/socket_util.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/socket_util.c b/src/socket_util.c
index 34111e90c..d8a58f9ad 100644
--- a/src/socket_util.c
+++ b/src/socket_util.c
@@ -20,17 +20,27 @@
#include "socket_util.h"
#include "config.h"
+#include <errno.h>
+#include <unistd.h>
+
#ifndef G_OS_WIN32
#include <sys/socket.h>
#include <netdb.h>
#else /* G_OS_WIN32 */
#include <ws2tcpip.h>
+#include <winsock.h>
#endif /* G_OS_WIN32 */
#ifdef HAVE_IPV6
#include <string.h>
#endif
+static GQuark
+listen_quark(void)
+{
+ return g_quark_from_static_string("listen");
+}
+
char *
sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
{
@@ -79,3 +89,54 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
return g_strconcat(host, ":", serv, NULL);
}
+
+int
+socket_bind_listen(int domain, int type, int protocol,
+ const struct sockaddr *address, size_t address_length,
+ int backlog,
+ GError **error)
+{
+ int fd, ret;
+ const int reuse = 1;
+#ifdef HAVE_STRUCT_UCRED
+ int passcred = 1;
+#endif
+
+ fd = socket(domain, type, protocol);
+ if (fd < 0) {
+ g_set_error(error, listen_quark(), errno,
+ "Failed to create socket: %s", g_strerror(errno));
+ return -1;
+ }
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
+ &reuse, sizeof(reuse));
+ if (ret < 0) {
+ g_set_error(error, listen_quark(), errno,
+ "setsockopt() failed: %s", g_strerror(errno));
+ close(fd);
+ return -1;
+ }
+
+ ret = bind(fd, address, address_length);
+ if (ret < 0) {
+ g_set_error(error, listen_quark(), errno,
+ "%s", strerror(errno));
+ close(fd);
+ return -1;
+ }
+
+ ret = listen(fd, backlog);
+ if (ret < 0) {
+ g_set_error(error, listen_quark(), errno,
+ "listen() failed: %s", g_strerror(errno));
+ close(fd);
+ return -1;
+ }
+
+#ifdef HAVE_STRUCT_UCRED
+ setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
+#endif
+
+ return fd;
+}