ServerSocket, SocketUtil: use the SocketError library

Use WinSock errors on Windows.
This commit is contained in:
Max Kellermann 2013-01-15 22:27:24 +01:00
parent e90d606f0a
commit 8917264110
2 changed files with 18 additions and 23 deletions

View File

@ -25,6 +25,7 @@
#include "ServerSocket.hxx"
#include "SocketUtil.hxx"
#include "SocketError.hxx"
#include "resolver.h"
#include "fd_util.h"
#include "glib_socket.h"
@ -35,7 +36,6 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
@ -181,14 +181,17 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
int fd = accept_cloexec_nonblock(s->fd, (struct sockaddr*)&address,
&address_length);
if (fd >= 0) {
if (socket_keepalive(fd))
if (socket_keepalive(fd)) {
const SocketErrorMessage msg;
g_warning("Could not set TCP keepalive option: %s",
g_strerror(errno));
(const char *)msg);
}
s->parent.callback(fd, (const struct sockaddr*)&address,
address_length, get_remote_uid(fd),
s->parent.callback_ctx);
} else {
g_warning("accept() failed: %s", g_strerror(errno));
const SocketErrorMessage msg;
g_warning("accept() failed: %s", (const char *)msg);
}
return true;
@ -342,9 +345,8 @@ server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
socklen_t address_length;
if (getsockname(fd, (struct sockaddr *)&address,
&address_length) < 0) {
g_set_error(error_r, server_socket_quark(), errno,
"Failed to get socket address: %s",
g_strerror(errno));
SetSocketError(error_r);
g_prefix_error(error_r, "Failed to get socket address");
return false;
}

View File

@ -19,11 +19,11 @@
#include "config.h"
#include "SocketUtil.hxx"
#include "SocketError.hxx"
#include "fd_util.h"
#include <glib.h>
#include <errno.h>
#include <unistd.h>
#ifndef G_OS_WIN32
@ -37,49 +37,42 @@
#include <string.h>
#endif
static GQuark
listen_quark(void)
{
return g_quark_from_static_string("listen");
}
int
socket_bind_listen(int domain, int type, int protocol,
const struct sockaddr *address, size_t address_length,
int backlog,
GError **error)
GError **error_r)
{
int fd, ret;
const int reuse = 1;
fd = socket_cloexec_nonblock(domain, type, protocol);
if (fd < 0) {
g_set_error(error, listen_quark(), errno,
"Failed to create socket: %s", g_strerror(errno));
SetSocketError(error_r);
g_prefix_error(error_r, "Failed to create socket");
return -1;
}
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &reuse, sizeof(reuse));
if (ret < 0) {
g_set_error(error, listen_quark(), errno,
"setsockopt() failed: %s", g_strerror(errno));
SetSocketError(error_r);
g_prefix_error(error_r, "setsockopt() failed");
close_socket(fd);
return -1;
}
ret = bind(fd, address, address_length);
if (ret < 0) {
g_set_error(error, listen_quark(), errno,
"%s", g_strerror(errno));
SetSocketError(error_r);
close_socket(fd);
return -1;
}
ret = listen(fd, backlog);
if (ret < 0) {
g_set_error(error, listen_quark(), errno,
"listen() failed: %s", g_strerror(errno));
SetSocketError(error_r);
g_prefix_error(error_r, "listen() failed");
close_socket(fd);
return -1;
}