1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #if !defined(SIM_SOCK_H_)
40 # define SIM_SOCK_H_ 0
41
42 # if defined (_WIN32)
43 # include <winsock2.h>
44
45 # elif !defined (_WIN32)
46 # include <sys/types.h>
47 # include <sys/socket.h>
48 # include <string.h>
49 # include <errno.h>
50 # include <fcntl.h>
51 # include <unistd.h>
52 # include <netinet/in.h>
53 # include <netinet/tcp.h>
54 # include <arpa/inet.h>
55 # include <netdb.h>
56 # include <sys/time.h>
57
58 # define WSAGetLastError() errno
59 # define WSASetLastError(err) errno = err
60 # define closesocket close
61 # define SOCKET int
62 # define WSAEWOULDBLOCK EWOULDBLOCK
63 # define WSAENAMETOOLONG ENAMETOOLONG
64 # define WSAEINPROGRESS EINPROGRESS
65 # define WSAETIMEDOUT ETIMEDOUT
66 # define WSAEISCONN EISCONN
67 # define WSAECONNRESET ECONNRESET
68 # define WSAECONNREFUSED ECONNREFUSED
69 # define WSAECONNABORTED ECONNABORTED
70 # define WSAEHOSTUNREACH EHOSTUNREACH
71 # define WSAEADDRINUSE EADDRINUSE
72 # if defined(EAFNOSUPPORT)
73 # define WSAEAFNOSUPPORT EAFNOSUPPORT
74 # endif
75 # define WSAEACCES EACCES
76 # define WSAEINTR EINTR
77 # define INVALID_SOCKET ((SOCKET)-1)
78 # define SOCKET_ERROR -1
79 # endif
80
81 # if !defined(CBUFSIZE)
82 # define CBUFSIZE 1024
83 # define sim_printf printf
84 # endif
85
86 int sim_parse_addr \
87 (const char *cptr, char *host, size_t hostlen, const char *default_host,
88 char *port, size_t port_len, const char *default_port, const char *validate_addr);
89 # if defined(UNUSED)
90 int sim_parse_addr_ex \
91 (const char *cptr, char *host, size_t hostlen, const char *default_host,
92 char *port, size_t port_len, char *localport, size_t local_port_len, const char *default_port);
93 # endif
94 # define SIM_SOCK_OPT_REUSEADDR 0x0001
95 # define SIM_SOCK_OPT_DATAGRAM 0x0002
96 # define SIM_SOCK_OPT_NODELAY 0x0004
97 # define SIM_SOCK_OPT_BLOCKING 0x0008
98 SOCKET sim_master_sock_ex \
99 (const char *hostport, int *parse_status, int opt_flags);
100 # define sim_master_sock(hostport, parse_status) \
101 sim_master_sock_ex(hostport, parse_status, ((sim_switches & SWMASK ('U')) ? SIM_SOCK_OPT_REUSEADDR : 0))
102 SOCKET sim_connect_sock_ex \
103 (const char *sourcehostport, const char *hostport, const char *default_host, const char *default_port, int opt_flags);
104 # define sim_connect_sock(hostport, default_host, default_port) \
105 sim_connect_sock_ex(NULL, hostport, default_host, default_port, SIM_SOCK_OPT_NONBLOCK)
106 SOCKET sim_accept_conn_ex \
107 (SOCKET master, char **connectaddr, int opt_flags);
108 # define sim_accept_conn(master, connectaddr) \
109 sim_accept_conn_ex(master, connectaddr, 0)
110 int sim_check_conn (SOCKET sock, int rd);
111 int sim_read_sock (SOCKET sock, char *buf, int nbytes);
112 int sim_write_sock (SOCKET sock, const char *msg, int nbytes);
113 void sim_close_sock (SOCKET sock);
114 const char *sim_get_err_sock (const char *emsg);
115 SOCKET sim_err_sock (SOCKET sock, const char *emsg);
116 int sim_getnames_sock (SOCKET sock, char **socknamebuf, char **peernamebuf);
117 void sim_init_sock (void);
118 void sim_cleanup_sock (void);
119 extern const char *xstrerror_l(int errnum);
120
121 #endif