mirror of
https://github.com/openvswitch/ovs
synced 2025-09-01 06:45:17 +00:00
socket-util: error number to string for sockets.
For winsock2 functions, error number has to be converted to string using FormatMessage(). Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
@@ -35,6 +35,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "dynamic-string.h"
|
#include "dynamic-string.h"
|
||||||
#include "fatal-signal.h"
|
#include "fatal-signal.h"
|
||||||
|
#include "ovs-thread.h"
|
||||||
#include "packets.h"
|
#include "packets.h"
|
||||||
#include "poll-loop.h"
|
#include "poll-loop.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
@@ -64,6 +65,13 @@ VLOG_DEFINE_THIS_MODULE(socket_util);
|
|||||||
* space for a null terminator. */
|
* space for a null terminator. */
|
||||||
#define MAX_UN_LEN (sizeof(((struct sockaddr_un *) 0)->sun_path) - 1)
|
#define MAX_UN_LEN (sizeof(((struct sockaddr_un *) 0)->sun_path) - 1)
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
/* Buffer used by sock_strerror(). */
|
||||||
|
DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; },
|
||||||
|
sockerror_buffer,
|
||||||
|
{ "" });
|
||||||
|
#endif
|
||||||
|
|
||||||
static int getsockopt_int(int fd, int level, int option, const char *optname,
|
static int getsockopt_int(int fd, int level, int option, const char *optname,
|
||||||
int *valuep);
|
int *valuep);
|
||||||
|
|
||||||
@@ -1344,3 +1352,25 @@ ss_length(const struct sockaddr_storage *ss)
|
|||||||
OVS_NOT_REACHED();
|
OVS_NOT_REACHED();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* For Windows socket calls, 'errno' is not set. One has to call
|
||||||
|
* WSAGetLastError() to get the error number and then pass it to
|
||||||
|
* FormatMessage() (through this function) to get the correct error string.
|
||||||
|
|
||||||
|
* ovs_strerror() calls strerror_r() and would not get the correct error
|
||||||
|
* string for Windows sockets, but is good for POSIX. */
|
||||||
|
const char *
|
||||||
|
sock_strerror(int error)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
enum { BUFSIZE = sizeof sockerror_buffer_get()->s };
|
||||||
|
char *buffer = sockerror_buffer_get()->s;
|
||||||
|
|
||||||
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
|
||||||
|
| FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0,
|
||||||
|
buffer, BUFSIZE, NULL);
|
||||||
|
return buffer;
|
||||||
|
#else
|
||||||
|
return ovs_strerror(error);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
@@ -86,5 +86,6 @@ uint16_t ss_get_port(const struct sockaddr_storage *);
|
|||||||
char *ss_format_address(const struct sockaddr_storage *,
|
char *ss_format_address(const struct sockaddr_storage *,
|
||||||
char *buf, size_t bufsize);
|
char *buf, size_t bufsize);
|
||||||
size_t ss_length(const struct sockaddr_storage *);
|
size_t ss_length(const struct sockaddr_storage *);
|
||||||
|
const char *sock_strerror(int error);
|
||||||
|
|
||||||
#endif /* socket-util.h */
|
#endif /* socket-util.h */
|
||||||
|
@@ -323,6 +323,10 @@ ovs_retval_to_string(int retval)
|
|||||||
: ovs_strerror(retval));
|
: ovs_strerror(retval));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* This function returns the string describing the error number in 'error'
|
||||||
|
* for POSIX platforms. For Windows, this function can be used for C library
|
||||||
|
* calls. For socket calls that are also used in Windows, use sock_strerror()
|
||||||
|
* instead. For WINAPI calls, look at ovs_lasterror_to_string(). */
|
||||||
const char *
|
const char *
|
||||||
ovs_strerror(int error)
|
ovs_strerror(int error)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user