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

View File

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