Cleanups.

This commit is contained in:
Jan Vidar Krey 2009-06-22 19:37:56 +02:00
parent c7777e2624
commit ca7544df9b
1 changed files with 77 additions and 62 deletions

View File

@ -78,6 +78,11 @@ int net_destroy()
return -1; return -1;
} }
static void net_error_out(int fd, const char* func)
{
int err = net_error();
hub_log(log_error, "%s, fd=%d: %s (%d)", func, fd, net_error_string(err), err);
}
int net_error() int net_error()
{ {
@ -103,22 +108,42 @@ const char* net_error_string(int code)
static int net_setsockopt(int fd, int level, int opt, const void* optval, socklen_t optlen) static int net_setsockopt(int fd, int level, int opt, const void* optval, socklen_t optlen)
{ {
int ret = -1;
#ifdef WINSOCK #ifdef WINSOCK
return setsockopt(fd, level, opt, (const char*) optval, optlen); ret = setsockopt(fd, level, opt, (const char*) optval, optlen);
#else #else
return setsockopt(fd, level, opt, optval, optlen); ret = setsockopt(fd, level, opt, optval, optlen);
#endif #endif
if (ret == -1)
{
net_error_out(fd, "net_setsockopt");
}
return ret;
}
static int net_getsockopt(int fd, int level, int opt, void* optval, socklen_t* optlen)
{
int ret = -1;
#ifdef WINSOCK
ret = getsockopt(fd, level, opt, (char*) optval, optlen);
#else
ret = getsockopt(fd, level, opt, optval, optlen);
#endif
if (ret == -1)
{
net_error_out(fd, "net_getsockopt");
}
return ret;
} }
int net_set_nonblocking(int fd, int toggle) int net_set_nonblocking(int fd, int toggle)
{ {
int ret; int ret = -1;
#ifdef NETAPI_DUMP
hub_log(log_dump, "net_set_nonblocking(): fd=%d", fd);
#endif
#ifdef WINSOCK #ifdef WINSOCK
u_long on = toggle ? 1 : 0; u_long on = toggle ? 1 : 0;
ret = ioctlsocket(fd, FIONBIO, &on); ret = ioctlsocket(fd, FIONBIO, &on);
@ -127,36 +152,27 @@ int net_set_nonblocking(int fd, int toggle)
#endif #endif
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_set_nonblocking(): ioctl failed (fd=%d): %s", fd, net_error_string(net_error())); net_error_out(fd, "net_set_nonblocking");
return -1;
} }
return 0; return ret;
} }
/* NOTE: Possibly only supported on BSD and OSX? */ /* NOTE: Possibly only supported on BSD and OSX? */
int net_set_nosigpipe(int fd, int toggle) int net_set_nosigpipe(int fd, int toggle)
{ {
int ret = -1;
#ifdef SO_NOSIGPIPE #ifdef SO_NOSIGPIPE
int ret;
#ifdef NETAPI_DUMP
hub_log(log_dump, "net_set_nosigpipe(): fd=%d", fd);
#endif
ret = net_setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &toggle, sizeof(toggle)); ret = net_setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &toggle, sizeof(toggle));
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_set_linger(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error())); net_error_out(fd, "net_set_nosigpipe");
return -1;
} }
#endif #endif
return 0; return ret;
} }
int net_set_close_on_exec(int fd, int toggle) int net_set_close_on_exec(int fd, int toggle)
{ {
#ifdef NETAPI_DUMP
hub_log(log_dump, "net_set_close_on_exec(): fd=%d", fd);
#endif
#ifdef WINSOCK #ifdef WINSOCK
return -1; /* FIXME: How is this done on Windows? */ return -1; /* FIXME: How is this done on Windows? */
#else #else
@ -164,54 +180,63 @@ int net_set_close_on_exec(int fd, int toggle)
#endif #endif
} }
int net_set_linger(int fd, int toggle) int net_set_linger(int fd, int toggle)
{ {
int ret; int ret;
#ifdef NETAPI_DUMP
hub_log(log_dump, "net_set_linger(): fd=%d", fd);
#endif
ret = net_setsockopt(fd, SOL_SOCKET, SO_LINGER, &toggle, sizeof(toggle)); ret = net_setsockopt(fd, SOL_SOCKET, SO_LINGER, &toggle, sizeof(toggle));
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_set_linger(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error())); net_error_out(fd, "net_set_linger");
return -1;
} }
return 0; return ret;
} }
int net_set_keepalive(int fd, int toggle) int net_set_keepalive(int fd, int toggle)
{ {
int ret; int ret;
#ifdef NETAPI_DUMP
hub_log(log_dump, "net_set_keepalive(): fd=%d", fd);
#endif
ret = net_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &toggle, sizeof(toggle)); ret = net_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &toggle, sizeof(toggle));
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_set_keepalive(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error())); net_error_out(fd, "net_set_keepalive");
return -1;
} }
return 0; return ret;
} }
int net_set_reuseaddress(int fd, int toggle) int net_set_reuseaddress(int fd, int toggle)
{ {
int ret; int ret;
#ifdef NETAPI_DUMP
hub_log(log_dump, "net_set_reuseaddress(): fd=%d", fd);
#endif
ret = net_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &toggle, sizeof(toggle)); ret = net_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &toggle, sizeof(toggle));
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_set_reuseaddress(): setsockopt failed (fd=%d): %s", fd, net_error_string(net_error())); net_error_out(fd, "net_set_reuseaddress");
return -1;
} }
return 0; return ret;
} }
int net_set_sendbuf_size(int fd, size_t size)
{
return net_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
}
int net_get_sendbuf_size(int fd, size_t* size)
{
socklen_t sz = sizeof(*size);
return net_getsockopt(fd, SOL_SOCKET, SO_SNDBUF, size, &sz);
}
int net_set_recvbuf_size(int fd, size_t size)
{
return net_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
}
int net_get_recvbuf_size(int fd, size_t* size)
{
socklen_t sz = sizeof(*size);
return net_getsockopt(fd, SOL_SOCKET, SO_RCVBUF, size, &sz);
}
int net_close(int fd) int net_close(int fd)
{ {
@ -282,7 +307,7 @@ int net_accept(int fd, struct ip_addr_encap* ipaddr)
case EWOULDBLOCK: case EWOULDBLOCK:
break; break;
default: default:
hub_log(log_error, "net_accept(): accept failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_accept");
net_stats_add_error(); net_stats_add_error();
return -1; return -1;
} }
@ -317,7 +342,7 @@ int net_connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen)
{ {
if (net_error() != EINPROGRESS) if (net_error() != EINPROGRESS)
{ {
hub_log(log_error, "net_connect(): connect failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_connect");
net_stats_add_error(); net_stats_add_error();
} }
} }
@ -344,8 +369,7 @@ int net_is_ipv6_supported()
return 0; return 0;
} }
hub_log(log_error, "net_is_ipv6_supported(): Unknown error (errno=%d, msg=%s)", net_error(), net_error_string(net_error())); net_error_out(ret, "net_is_ipv6_supported");
} }
else else
{ {
@ -375,7 +399,7 @@ int net_socket_create(int af, int type, int protocol)
int sd = socket(af, type, protocol); int sd = socket(af, type, protocol);
if (sd == -1) if (sd == -1)
{ {
hub_log(log_error, "net_socket_create(): socket failed (errno=%d, msg=%s)", net_error(), net_error_string(net_error())); net_error_out(sd, "net_socket_create");
} }
#ifdef SOCK_DUAL_STACK_OPT #ifdef SOCK_DUAL_STACK_OPT
@ -509,19 +533,18 @@ const char* net_get_peer_address(int fd)
{ {
return &address[7]; return &address[7];
} }
hub_log(log_trace, "net_get_peer_address(): address=%s", address);
return address; return address;
} }
else else
{ {
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN); net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
hub_log(log_trace, "net_get_peer_address(): address=%s", address);
return address; return address;
} }
} }
else else
{ {
hub_log(log_error, "net_get_peer_address(): getsockname failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_get_peer_address");
net_stats_add_error();
} }
return "0.0.0.0"; return "0.0.0.0";
@ -539,7 +562,7 @@ ssize_t net_recv(int fd, void* buf, size_t len, int flags)
{ {
if (net_error() != EWOULDBLOCK) if (net_error() != EWOULDBLOCK)
{ {
hub_log(log_debug, "net_recv(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_recv");
net_stats_add_error(); net_stats_add_error();
} }
} }
@ -558,7 +581,7 @@ ssize_t net_send(int fd, const void* buf, size_t len, int flags)
{ {
if (net_error() != EWOULDBLOCK) if (net_error() != EWOULDBLOCK)
{ {
hub_log(log_debug, "net_send(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_send");
net_stats_add_error(); net_stats_add_error();
} }
} }
@ -571,7 +594,7 @@ int net_bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen)
int ret = bind(fd, my_addr, addrlen); int ret = bind(fd, my_addr, addrlen);
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_bind(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_bind");
net_stats_add_error(); net_stats_add_error();
} }
return ret; return ret;
@ -583,7 +606,7 @@ int net_listen(int fd, int backlog)
int ret = listen(fd, backlog); int ret = listen(fd, backlog);
if (ret == -1) if (ret == -1)
{ {
hub_log(log_error, "net_listen(): failed (fd=%d, errno=%d, msg=%s)", fd, net_error(), net_error_string(net_error())); net_error_out(fd, "net_listen");
net_stats_add_error(); net_stats_add_error();
} }
return ret; return ret;
@ -599,14 +622,12 @@ void net_stats_initialize()
stats.timestamp = time(NULL); stats.timestamp = time(NULL);
} }
void net_stats_get(struct net_statistics** intermediate, struct net_statistics** total) void net_stats_get(struct net_statistics** intermediate, struct net_statistics** total)
{ {
*intermediate = &stats; *intermediate = &stats;
*total = &stats_total; *total = &stats_total;
} }
void net_stats_reset() void net_stats_reset()
{ {
stats_total.tx += stats.tx; stats_total.tx += stats.tx;
@ -619,37 +640,31 @@ void net_stats_reset()
stats.timestamp = time(NULL); stats.timestamp = time(NULL);
} }
int net_stats_timeout() int net_stats_timeout()
{ {
return (difftime(time(NULL), stats.timestamp) > TIMEOUT_STATS) ? 1 : 0; return (difftime(time(NULL), stats.timestamp) > TIMEOUT_STATS) ? 1 : 0;
} }
void net_stats_add_tx(size_t bytes) void net_stats_add_tx(size_t bytes)
{ {
stats.tx += bytes; stats.tx += bytes;
} }
void net_stats_add_rx(size_t bytes) void net_stats_add_rx(size_t bytes)
{ {
stats.rx += bytes; stats.rx += bytes;
} }
void net_stats_add_accept() void net_stats_add_accept()
{ {
stats.accept++; stats.accept++;
} }
void net_stats_add_error() void net_stats_add_error()
{ {
stats.errors++; stats.errors++;
} }
void net_stats_add_close() void net_stats_add_close()
{ {
stats.closed++; stats.closed++;