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 #ifndef 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 (const char *cptr, char *host, size_t hostlen, const char *default_host, char *port, size_t port_len, const char *default_port, const char *validate_addr);
87 # ifdef UNUSED
88 int sim_parse_addr_ex (const char *cptr, char *host, size_t hostlen, const char *default_host, char *port, size_t port_len, char *localport, size_t local_port_len, const char *default_port);
89 # endif
90 # define SIM_SOCK_OPT_REUSEADDR 0x0001
91 # define SIM_SOCK_OPT_DATAGRAM 0x0002
92 # define SIM_SOCK_OPT_NODELAY 0x0004
93 # define SIM_SOCK_OPT_BLOCKING 0x0008
94 SOCKET sim_master_sock_ex (const char *hostport, int *parse_status, int opt_flags);
95 # define sim_master_sock(hostport, parse_status) sim_master_sock_ex(hostport, parse_status, ((sim_switches & SWMASK ('U')) ? SIM_SOCK_OPT_REUSEADDR : 0))
96 SOCKET sim_connect_sock_ex (const char *sourcehostport, const char *hostport, const char *default_host, const char *default_port, int opt_flags);
97 # define sim_connect_sock(hostport, default_host, default_port) sim_connect_sock_ex(NULL, hostport, default_host, default_port, SIM_SOCK_OPT_NONBLOCK)
98 SOCKET sim_accept_conn_ex (SOCKET master, char **connectaddr, int opt_flags);
99 # define sim_accept_conn(master, connectaddr) sim_accept_conn_ex(master, connectaddr, 0)
100 int sim_check_conn (SOCKET sock, int rd);
101 int sim_read_sock (SOCKET sock, char *buf, int nbytes);
102 int sim_write_sock (SOCKET sock, const char *msg, int nbytes);
103 void sim_close_sock (SOCKET sock);
104 const char *sim_get_err_sock (const char *emsg);
105 SOCKET sim_err_sock (SOCKET sock, const char *emsg);
106 int sim_getnames_sock (SOCKET sock, char **socknamebuf, char **peernamebuf);
107 void sim_init_sock (void);
108 void sim_cleanup_sock (void);
109
110 #endif