Skip to content

Commit

Permalink
socket-util: Move sock_errno() to socket-util.
Browse files Browse the repository at this point in the history
And add more users.

Signed-off-by: Gurucharan Shetty <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
shettyg committed Feb 21, 2014
1 parent 53fd5c7 commit 0f0b540
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
49 changes: 27 additions & 22 deletions lib/socket-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ set_dscp(int fd, uint8_t dscp)

val = dscp << 2;
if (setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val)) {
return errno;
return sock_errno();
}

return 0;
Expand Down Expand Up @@ -239,7 +239,7 @@ lookup_hostname(const char *host_name, struct in_addr *addr)
#endif

case EAI_SYSTEM:
return errno;
return sock_errno();

default:
return EPROTO;
Expand Down Expand Up @@ -762,8 +762,8 @@ inet_open_active(int style, const char *target, uint16_t default_port,
/* Create non-blocking socket. */
fd = socket(ss.ss_family, style, 0);
if (fd < 0) {
VLOG_ERR("%s: socket: %s", target, ovs_strerror(errno));
error = errno;
error = sock_errno();
VLOG_ERR("%s: socket: %s", target, sock_strerror(error));
goto exit;
}
error = set_nonblocking(fd);
Expand All @@ -776,15 +776,19 @@ inet_open_active(int style, const char *target, uint16_t default_port,
* connect(), the handshake SYN frames will be sent with a TOS of 0. */
error = set_dscp(fd, dscp);
if (error) {
VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
VLOG_ERR("%s: socket: %s", target, sock_strerror(error));
goto exit;
}

/* Connect. */
error = connect(fd, (struct sockaddr *) &ss, ss_length(&ss)) == 0
? 0
: errno;
if (error == EINPROGRESS) {
: sock_errno();
if (error == EINPROGRESS
#ifdef _WIN32
|| error == WSAEALREADY || error == WSAEWOULDBLOCK
#endif
) {
error = EAGAIN;
}

Expand Down Expand Up @@ -881,8 +885,8 @@ inet_open_passive(int style, const char *target, int default_port,
/* Create non-blocking socket, set SO_REUSEADDR. */
fd = socket(ss.ss_family, style, 0);
if (fd < 0) {
error = errno;
VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
error = sock_errno();
VLOG_ERR("%s: socket: %s", target, sock_strerror(error));
return -error;
}
error = set_nonblocking(fd);
Expand All @@ -891,16 +895,16 @@ inet_open_passive(int style, const char *target, int default_port,
}
if (style == SOCK_STREAM
&& setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
error = errno;
error = sock_errno();
VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s",
target, ovs_strerror(error));
target, sock_strerror(error));
goto error;
}

/* Bind. */
if (bind(fd, (struct sockaddr *) &ss, ss_length(&ss)) < 0) {
error = errno;
VLOG_ERR("%s: bind: %s", target, ovs_strerror(error));
error = sock_errno();
VLOG_ERR("%s: bind: %s", target, sock_strerror(error));
goto error;
}

Expand All @@ -909,22 +913,22 @@ inet_open_passive(int style, const char *target, int default_port,
* connect(), the handshake SYN frames will be sent with a TOS of 0. */
error = set_dscp(fd, dscp);
if (error) {
VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
VLOG_ERR("%s: socket: %s", target, sock_strerror(error));
goto error;
}

/* Listen. */
if (style == SOCK_STREAM && listen(fd, 10) < 0) {
error = errno;
VLOG_ERR("%s: listen: %s", target, ovs_strerror(error));
error = sock_errno();
VLOG_ERR("%s: listen: %s", target, sock_strerror(error));
goto error;
}

if (ssp || kernel_chooses_port) {
socklen_t ss_len = sizeof ss;
if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) < 0) {
error = errno;
VLOG_ERR("%s: getsockname: %s", target, ovs_strerror(error));
error = sock_errno();
VLOG_ERR("%s: getsockname: %s", target, sock_strerror(error));
goto error;
}
if (kernel_chooses_port) {
Expand Down Expand Up @@ -1095,8 +1099,8 @@ getsockopt_int(int fd, int level, int option, const char *optname, int *valuep)

len = sizeof value;
if (getsockopt(fd, level, option, &value, &len)) {
error = errno;
VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, ovs_strerror(error));
error = sock_errno();
VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, sock_strerror(error));
} else if (len != sizeof value) {
error = EINVAL;
VLOG_ERR_RL(&rl, "getsockopt(%s): value is %u bytes (expected %"PRIuSIZE")",
Expand Down Expand Up @@ -1256,8 +1260,9 @@ af_inet_ioctl(unsigned long int command, const void *arg)
if (ovsthread_once_start(&once)) {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
sock = -errno;
VLOG_ERR("failed to create inet socket: %s", ovs_strerror(errno));
int error = sock_errno();
VLOG_ERR("failed to create inet socket: %s", sock_strerror(error));
sock = -error;
}
ovsthread_once_done(&once);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/socket-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef SOCKET_UTIL_H
#define SOCKET_UTIL_H 1

#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
Expand Down Expand Up @@ -99,4 +100,15 @@ static inline int rpl_setsockopt(int sock, int level, int optname,
}
#endif

/* In Windows platform, errno is not set for socket calls.
* The last error has to be gotten from WSAGetLastError(). */
static inline int sock_errno(void)
{
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}

#endif /* socket-util.h */
12 changes: 0 additions & 12 deletions lib/stream-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1416,18 +1416,6 @@ ssl_protocol_cb(int write_p, int version OVS_UNUSED, int content_type,
ds_destroy(&details);
}

/* In Windows platform, errno is not set for socket calls.
* The last error has to be gotten from WSAGetLastError(). */
static int
sock_errno(void)
{
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}

static void
clear_handle(int fd OVS_UNUSED, HANDLE wevent OVS_UNUSED)
{
Expand Down

0 comments on commit 0f0b540

Please sign in to comment.