Add files and tweak others to get 'native' Mingw32 gcc port as
[p5sagit/p5-mst-13.2.git] / win32 / include / sys / socket.h
1 // sys/socket.h
2
3 // djl
4 // Provide UNIX compatibility
5
6 #ifndef  _INC_SYS_SOCKET
7 #define  _INC_SYS_SOCKET
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 #ifndef  _WINDOWS_
14 #ifdef   __GNUC__
15 #define WIN32_LEAN_AND_MEAN
16 #ifdef __GNUC__
17 #define Win32_Winsock
18 #endif
19 #include <windows.h>
20 #else
21 #define  _WINDOWS_
22
23 #define  FAR
24 #define  PASCAL     __stdcall
25 #define  WINAPI     __stdcall
26
27 #undef WORD
28 typedef  int        BOOL;
29 typedef  unsigned short WORD;
30 typedef  void*      HANDLE;
31 typedef  void*      HWND;
32 typedef  int (FAR WINAPI *FARPROC)();
33
34 typedef unsigned long       DWORD;
35 typedef void *PVOID;
36
37 #define IN
38 #define OUT
39
40 typedef struct _OVERLAPPED {
41     DWORD   Internal;
42     DWORD   InternalHigh;
43     DWORD   Offset;
44     DWORD   OffsetHigh;
45     HANDLE  hEvent;
46 } OVERLAPPED, *LPOVERLAPPED;
47
48 #endif
49 #endif //_WINDOWS_
50 #ifndef __GNUC__
51 #include <winsock.h>
52 #endif
53
54 #define  ENOTSOCK       WSAENOTSOCK
55 #undef   HOST_NOT_FOUND
56
57 #ifdef USE_SOCKETS_AS_HANDLES
58
59 #ifndef PERL_FD_SETSIZE
60 #define PERL_FD_SETSIZE         64
61 #endif
62
63 #define PERL_BITS_PER_BYTE      8
64 #define PERL_NFDBITS            (sizeof(Perl_fd_mask)*PERL_BITS_PER_BYTE)
65
66 typedef int                     Perl_fd_mask;
67
68 typedef struct  Perl_fd_set {
69     Perl_fd_mask bits[(PERL_FD_SETSIZE+PERL_NFDBITS-1)/PERL_NFDBITS];
70 }                               Perl_fd_set;
71
72 #define PERL_FD_CLR(n,p) \
73     ((p)->bits[(n)/PERL_NFDBITS] &= ~((unsigned)1 << ((n)%PERL_NFDBITS)))
74
75 #define PERL_FD_SET(n,p) \
76     ((p)->bits[(n)/PERL_NFDBITS] |=  ((unsigned)1 << ((n)%PERL_NFDBITS)))
77
78 #define PERL_FD_ZERO(p) memset((char *)(p),0,sizeof(*(p)))
79
80 #define PERL_FD_ISSET(n,p) \
81     ((p)->bits[(n)/PERL_NFDBITS] &   ((unsigned)1 << ((n)%PERL_NFDBITS)))
82
83 #else   /* USE_SOCKETS_AS_HANDLES */
84
85 #define Perl_fd_set     fd_set
86 #define PERL_FD_SET(n,p)        FD_SET(n,p)
87 #define PERL_FD_CLR(n,p)        FD_CLR(n,p)
88 #define PERL_FD_ISSET(n,p)      FD_ISSET(n,p)
89 #define PERL_FD_ZERO(p)         FD_ZERO(p)
90
91 #endif  /* USE_SOCKETS_AS_HANDLES */
92
93 SOCKET win32_accept (SOCKET s, struct sockaddr *addr, int *addrlen);
94 int win32_bind (SOCKET s, const struct sockaddr *addr, int namelen);
95 int win32_closesocket (SOCKET s);
96 int win32_connect (SOCKET s, const struct sockaddr *name, int namelen);
97 int win32_ioctlsocket (SOCKET s, long cmd, u_long *argp);
98 int win32_getpeername (SOCKET s, struct sockaddr *name, int * namelen);
99 int win32_getsockname (SOCKET s, struct sockaddr *name, int * namelen);
100 int win32_getsockopt (SOCKET s, int level, int optname, char * optval, int *optlen);
101 u_long win32_htonl (u_long hostlong);
102 u_short win32_htons (u_short hostshort);
103 unsigned long win32_inet_addr (const char * cp);
104 char * win32_inet_ntoa (struct in_addr in);
105 int win32_listen (SOCKET s, int backlog);
106 u_long win32_ntohl (u_long netlong);
107 u_short win32_ntohs (u_short netshort);
108 int win32_recv (SOCKET s, char * buf, int len, int flags);
109 int win32_recvfrom (SOCKET s, char * buf, int len, int flags,
110                          struct sockaddr *from, int * fromlen);
111 int win32_select (int nfds, Perl_fd_set *rfds, Perl_fd_set *wfds, Perl_fd_set *xfds,
112                   const struct timeval *timeout);
113 int win32_send (SOCKET s, const char * buf, int len, int flags);
114 int win32_sendto (SOCKET s, const char * buf, int len, int flags,
115                        const struct sockaddr *to, int tolen);
116 int win32_setsockopt (SOCKET s, int level, int optname,
117                            const char * optval, int optlen);
118 SOCKET win32_socket (int af, int type, int protocol);
119 int win32_shutdown (SOCKET s, int how);
120
121 /* Database function prototypes */
122
123 struct hostent * win32_gethostbyaddr(const char * addr, int len, int type);
124 struct hostent * win32_gethostbyname(const char * name);
125 int win32_gethostname (char * name, int namelen);
126 struct servent * win32_getservbyport(int port, const char * proto);
127 struct servent * win32_getservbyname(const char * name, const char * proto);
128 struct protoent * win32_getprotobynumber(int proto);
129 struct protoent * win32_getprotobyname(const char * name);
130 struct protoent *win32_getprotoent(void);
131 struct servent *win32_getservent(void);
132 void win32_sethostent(int stayopen);
133 void win32_setnetent(int stayopen);
134 struct netent * win32_getnetent(void);
135 struct netent * win32_getnetbyname(char *name);
136 struct netent * win32_getnetbyaddr(long net, int type);
137 void win32_setprotoent(int stayopen);
138 void win32_setservent(int stayopen);
139 void win32_endhostent(void);
140 void win32_endnetent(void);
141 void win32_endprotoent(void);
142 void win32_endservent(void);
143
144 #ifndef WIN32SCK_IS_STDSCK
145 //
146 // direct to our version
147 //
148 #define htonl           win32_htonl
149 #define htons           win32_htons
150 #define ntohl           win32_ntohl
151 #define ntohs           win32_ntohs
152 #define inet_addr       win32_inet_addr
153 #define inet_ntoa       win32_inet_ntoa
154
155 #define socket          win32_socket
156 #define bind            win32_bind
157 #define listen          win32_listen
158 #define accept          win32_accept
159 #define connect         win32_connect
160 #define send            win32_send
161 #define sendto          win32_sendto
162 #define recv            win32_recv
163 #define recvfrom        win32_recvfrom
164 #define shutdown        win32_shutdown
165 #define closesocket     win32_closesocket
166 #define ioctlsocket     win32_ioctlsocket
167 #define setsockopt      win32_setsockopt
168 #define getsockopt      win32_getsockopt
169 #define getpeername     win32_getpeername
170 #define getsockname     win32_getsockname
171 #define gethostname     win32_gethostname
172 #define gethostbyname   win32_gethostbyname
173 #define gethostbyaddr   win32_gethostbyaddr
174 #define getprotobyname  win32_getprotobyname
175 #define getprotobynumber win32_getprotobynumber
176 #define getservbyname   win32_getservbyname
177 #define getservbyport   win32_getservbyport
178 #define select          win32_select
179 #define endhostent      win32_endhostent
180 #define endnetent       win32_endnetent
181 #define endprotoent     win32_endprotoent
182 #define endservent      win32_endservent
183 #define getnetent       win32_getnetent
184 #define getnetbyname    win32_getnetbyname
185 #define getnetbyaddr    win32_getnetbyaddr
186 #define getprotoent     win32_getprotoent
187 #define getservent      win32_getservent
188 #define sethostent      win32_sethostent
189 #define setnetent       win32_setnetent
190 #define setprotoent     win32_setprotoent
191 #define setservent      win32_setservent
192
193 #ifdef USE_SOCKETS_AS_HANDLES
194 #undef fd_set
195 #undef FD_SET
196 #undef FD_CLR
197 #undef FD_ISSET
198 #undef FD_ZERO
199 #define fd_set          Perl_fd_set
200 #define FD_SET(n,p)     PERL_FD_SET(n,p)
201 #define FD_CLR(n,p)     PERL_FD_CLR(n,p)
202 #define FD_ISSET(n,p)   PERL_FD_ISSET(n,p)
203 #define FD_ZERO(p)      PERL_FD_ZERO(p)
204 #endif  /* USE_SOCKETS_AS_HANDLES */
205
206 #endif  /* WIN32SCK_IS_STDSCK */
207
208 #ifdef __cplusplus
209 }
210 #endif
211
212 #endif  // _INC_SYS_SOCKET