18 #ifndef XEMU_COMMON_EMUTOOLS_SOCKETAPI_H_INCLUDED
19 #define XEMU_COMMON_EMUTOOLS_SOCKETAPI_H_INCLUDED
20 #ifdef XEMU_HAS_SOCKET_API
23 # include <winsock2.h>
24 typedef SOCKET xemusock_socket_t;
25 typedef int xemusock_socklen_t;
27 # define XSEAGAIN WSAEWOULDBLOCK
28 # define XSEWOULDBLOCK WSAEWOULDBLOCK
29 # define XSEINPROGRESS WSAEINPROGRESS
30 # define XSEALREADY WSAEALREADY
31 # define XSEINTR WSAEINTR
32 # define XS_INVALID_SOCKET INVALID_SOCKET
33 # define XS_SOCKET_ERROR SOCKET_ERROR
34 # define SHUT_RDWR SD_BOTH
35 extern const char *xemusock_strerror (
int err );
37 # include <arpa/inet.h>
38 # include <sys/socket.h>
39 # include <netinet/in.h>
41 typedef int xemusock_socket_t;
42 typedef socklen_t xemusock_socklen_t;
43 # define XSEAGAIN EAGAIN
44 # define XSEWOULDBLOCK EWOULDBLOCK
45 # define XSEINPROGRESS EINPROGRESS
46 # define XSEALREADY EALREADY
47 # define XSEINTR EINTR
48 # define XS_INVALID_SOCKET -1
49 # define XS_SOCKET_ERROR -1
50 # define xemusock_strerror(_n) strerror(_n)
53 #define XEMUSOCK_SELECT_R 1
54 #define XEMUSOCK_SELECT_W 2
55 #define XEMUSOCK_SELECT_E 4
57 #define XEMUSOCK_UDP 0
58 #define XEMUSOCK_TCP 1
59 #define XEMUSOCK_BLOCKING 0
60 #define XEMUSOCK_NONBLOCKING 1
62 extern int xemusock_close ( xemusock_socket_t sock,
int *xerrno );
63 extern const char *xemusock_init(
void );
64 extern void xemusock_uninit (
void );
65 extern int xemusock_select_1 ( xemusock_socket_t sock,
int usec,
int what,
int *xerrno );
66 extern void xemusock_fill_servaddr_for_inet_ip_netlong (
struct sockaddr_in *servaddr,
unsigned int ip_netlong,
int port );
67 extern void xemusock_fill_servaddr_for_inet_ip_native (
struct sockaddr_in *servaddr,
unsigned int ip_native,
int port );
68 extern int xemusock_set_nonblocking ( xemusock_socket_t sock,
int is_nonblock,
int *xerrno );
69 extern int xemusock_connect ( xemusock_socket_t sock,
struct sockaddr_in *servaddr,
int *xerrno );
70 extern int xemusock_send ( xemusock_socket_t sock,
const void *buffer,
int length,
int *xerrno );
71 extern int xemusock_sendto ( xemusock_socket_t sock,
const void *buffer,
int length,
struct sockaddr_in *servaddr,
int *xerrno );
72 extern int xemusock_recv ( xemusock_socket_t sock,
void *buffer,
int length,
int *xerrno );
73 extern int xemusock_recvfrom ( xemusock_socket_t sock,
void *buffer,
int length,
struct sockaddr_in *servaddr,
int *xerrno );
74 extern int xemusock_shutdown ( xemusock_socket_t sock,
int *xerrno );
75 extern int xemusock_bind ( xemusock_socket_t sock,
struct sockaddr *
addr, xemusock_socklen_t addrlen,
int *xerrno );
76 extern int xemusock_listen ( xemusock_socket_t sock,
int backlog,
int *xerrno );
77 extern int xemusock_setsockopt ( xemusock_socket_t sock,
int level,
int option,
const void *
value,
int len,
int *xerrno );
78 extern int xemusock_setsockopt_reuseaddr ( xemusock_socket_t sock,
int *xerrno );
79 extern xemusock_socket_t xemusock_accept ( xemusock_socket_t sock,
struct sockaddr *
addr, xemusock_socklen_t *addrlen,
int *xerrno );
80 extern xemusock_socket_t xemusock_create_for_inet (
int is_tcp,
int is_nonblock,
int *xerrno );
82 static inline int xemusock_should_repeat_from_error (
int xerr ) {
83 return (xerr == XSEAGAIN || xerr == XSEWOULDBLOCK || xerr == XSEINPROGRESS || xerr == XSEINTR || xerr == XSEALREADY);
85 static inline unsigned int xemusock_ipv4_octetarray_to_netlong (
const unsigned char ip[4] ) {
86 return htonl((
unsigned int)((((
unsigned int)ip[0]) << 24) + (((
unsigned int)ip[1]) << 16) + (((
unsigned int)ip[2]) << 8) + ((
unsigned int)ip[3])));
88 static inline unsigned int xemusock_ipv4_netoctetarray_to_netlong (
const unsigned char ip[4] ) {
89 return ((
unsigned int)((((
unsigned int)ip[0]) << 24) + (((
unsigned int)ip[1]) << 16) + (((
unsigned int)ip[2]) << 8) + ((
unsigned int)ip[3])));
91 static inline unsigned int xemusock_ipv4_octetstring_to_netlong (
const char *ipstr ) {
92 return inet_addr(ipstr);