Revert "lists: Add list literal doc example."
[factor.git] / basis / windows / winsock / winsock.factor
blob234d4b1c5b88e96c8e58c72b04e4c435bba32e10
1 ! Copyright (C) 2006 Mackenzie Straight, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.strings
4 alien.syntax arrays byte-arrays classes.struct grouping init
5 io.encodings.utf16n kernel literals math math.bitwise
6 math.parser sequences system vocabs.parser windows.com.syntax
7 windows.errors windows.kernel32 windows.types ;
8 FROM: alien.c-types => short ;
9 IN: windows.winsock
12 ! Some differences between Win32 and Win64
13 cpu x86.64? "windows.winsock.64" "windows.winsock.32" ? use-vocab
16 TYPEDEF: int* SOCKET
18 : <wsadata> ( -- byte-array )
19     0x190 <byte-array> ;
21 CONSTANT: SOCK_STREAM    1
22 CONSTANT: SOCK_DGRAM     2
23 CONSTANT: SOCK_RAW       3
24 CONSTANT: SOCK_RDM       4
25 CONSTANT: SOCK_SEQPACKET 5
27 CONSTANT: SO_DEBUG       0x1
28 CONSTANT: SO_ACCEPTCONN  0x2
29 CONSTANT: SO_REUSEADDR   0x4
30 CONSTANT: SO_KEEPALIVE   0x8
31 CONSTANT: SO_DONTROUTE   0x10
32 CONSTANT: SO_BROADCAST   0x20
33 CONSTANT: SO_USELOOPBACK 0x40
34 CONSTANT: SO_LINGER      0x80
35 CONSTANT: SO_OOBINLINE   0x100
36 : SO_DONTLINGER ( -- n ) SO_LINGER bitnot ; inline
38 CONSTANT: SO_SNDBUF     0x1001
39 CONSTANT: SO_RCVBUF     0x1002
40 CONSTANT: SO_SNDLOWAT   0x1003
41 CONSTANT: SO_RCVLOWAT   0x1004
42 CONSTANT: SO_SNDTIMEO   0x1005
43 CONSTANT: SO_RCVTIMEO   0x1006
44 CONSTANT: SO_ERROR      0x1007
45 CONSTANT: SO_TYPE       0x1008
47 CONSTANT: TCP_NODELAY   0x1
49 CONSTANT: AF_UNSPEC      0
50 CONSTANT: AF_UNIX        1
51 CONSTANT: AF_INET        2
52 CONSTANT: AF_IMPLINK     3
53 CONSTANT: AF_PUP         4
54 CONSTANT: AF_CHAOS       5
55 CONSTANT: AF_NS          6
56 CONSTANT: AF_ISO         7
57 ALIAS: AF_OSI    AF_ISO
58 CONSTANT: AF_ECMA        8
59 CONSTANT: AF_DATAKIT     9
60 CONSTANT: AF_CCITT      10
61 CONSTANT: AF_SNA        11
62 CONSTANT: AF_DECnet     12
63 CONSTANT: AF_DLI        13
64 CONSTANT: AF_LAT        14
65 CONSTANT: AF_HYLINK     15
66 CONSTANT: AF_APPLETALK  16
67 CONSTANT: AF_NETBIOS    17
68 CONSTANT: AF_MAX        18
69 CONSTANT: AF_INET6      23
70 CONSTANT: AF_IRDA       26
71 CONSTANT: AF_BTM        32
73 CONSTANT: PF_UNSPEC      0
74 CONSTANT: PF_LOCAL       1
75 CONSTANT: PF_INET        2
76 CONSTANT: PF_INET6      23
78 CONSTANT: AI_PASSIVE        0x0001
79 CONSTANT: AI_CANONNAME      0x0002
80 CONSTANT: AI_NUMERICHOST    0x0004
81 CONSTANT: AI_ALL            0x0100
82 CONSTANT: AI_ADDRCONFIG     0x0400
84 CONSTANT: AI_MASK flags{ AI_PASSIVE AI_CANONNAME AI_NUMERICHOST }
86 CONSTANT: NI_NUMERICHOST 1
87 CONSTANT: NI_NUMERICSERV 2
89 CONSTANT: IPPROTO_TCP    6
90 CONSTANT: IPPROTO_UDP   17
91 CONSTANT: IPPROTO_RM   113
93 CONSTANT: FIOASYNC      0x8004667d
94 CONSTANT: FIONBIO       0x8004667e
95 CONSTANT: FIONREAD      0x4004667f
97 CONSTANT: WSA_FLAG_OVERLAPPED 1
98 ALIAS: WSA_WAIT_EVENT_0 WAIT_OBJECT_0
99 ALIAS: WSA_MAXIMUM_WAIT_EVENTS MAXIMUM_WAIT_OBJECTS
100 CONSTANT: WSA_INVALID_EVENT f
101 CONSTANT: WSA_WAIT_FAILED -1
102 ALIAS: WSA_WAIT_IO_COMPLETION WAIT_IO_COMPLETION
103 ALIAS: WSA_WAIT_TIMEOUT WAIT_TIMEOUT
104 ALIAS: WSA_INFINITE INFINITE
105 ALIAS: WSA_IO_PENDING ERROR_IO_PENDING
107 CONSTANT: INADDR_ANY 0
109 : INVALID_SOCKET ( -- n ) -1 <alien> ; inline
111 : SOCKET_ERROR ( -- n ) -1 ; inline
113 CONSTANT: SD_RECV 0
114 CONSTANT: SD_SEND 1
115 CONSTANT: SD_BOTH 2
117 CONSTANT: SOL_SOCKET 0xffff
119 C-TYPE: sockaddr
121 STRUCT: sockaddr-in
122     { family short }
123     { port ushort }
124     { addr uint }
125     { pad char[8] } ;
127 STRUCT: sockaddr-in6
128     { family uchar }
129     { port ushort }
130     { flowinfo uint }
131     { addr uchar[16] }
132     { scopeid uint } ;
134 STRUCT: hostent
135     { name c-string }
136     { aliases void* }
137     { addrtype short }
138     { length short }
139     { addr-list void* } ;
141 STRUCT: protoent
142     { name c-string }
143     { aliases void* }
144     { proto short } ;
146 STRUCT: addrinfo
147     { flags int }
148     { family int }
149     { socktype int }
150     { protocol int }
151     { addrlen size_t }
152     { canonname c-string }
153     { addr sockaddr* }
154     { next addrinfo* } ;
156 STRUCT: timeval
157     { sec long }
158     { usec long } ;
160 GENERIC: sockaddr>ip ( sockaddr -- string )
162 M: sockaddr-in sockaddr>ip ( sockaddr -- string )
163     addr>> uint <ref> [ number>string ] { } map-as "." join ;
165 M: sockaddr-in6 sockaddr>ip ( uchar-array -- string )
166     addr>> [ >hex ] { } map-as 2 group [ concat ] map ":" join ;
168 STRUCT: fd_set
169     { fd_count uint }
170     { fd_array SOCKET[64] } ;
172 LIBRARY: winsock
174 FUNCTION: int setsockopt ( SOCKET s, int level, int optname, c-string optval, int optlen )
175 FUNCTION: int ioctlsocket ( SOCKET s, long cmd, ulong* *argp )
177 FUNCTION: ushort htons ( ushort n )
178 FUNCTION: ushort ntohs ( ushort n )
179 FUNCTION: int bind ( SOCKET socket, sockaddr-in* sockaddr, int len )
180 FUNCTION: int listen ( SOCKET socket, int backlog )
181 FUNCTION: c-string inet_ntoa ( int in-addr )
182 FUNCTION: int getaddrinfo ( c-string nodename,
183                             c-string servername,
184                             addrinfo* hints,
185                             addrinfo** res )
187 FUNCTION: void freeaddrinfo ( addrinfo* ai )
190 FUNCTION: hostent* gethostbyname ( c-string name )
191 FUNCTION: int gethostname ( c-string name, int len )
192 FUNCTION: SOCKET socket ( int domain, int type, int protocol )
193 FUNCTION: int connect ( SOCKET socket, sockaddr-in* sockaddr, int addrlen )
194 FUNCTION: int select ( int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timeval* timeout )
195 FUNCTION: int closesocket ( SOCKET s )
196 FUNCTION: int shutdown ( SOCKET s, int how )
197 FUNCTION: int send ( SOCKET s, c-string buf, int len, int flags )
198 FUNCTION: int recv ( SOCKET s, c-string buf, int len, int flags )
200 FUNCTION: int getsockname ( SOCKET s, sockaddr-in* address, int* addrlen )
201 FUNCTION: int getpeername ( SOCKET s, sockaddr-in* address, int* addrlen )
203 FUNCTION: protoent* getprotobyname ( c-string name )
205 FUNCTION: servent* getservbyname ( c-string name, c-string prot )
206 FUNCTION: servent* getservbyport ( int port, c-string prot )
208 TYPEDEF: uint SERVICETYPE
209 TYPEDEF: void* LPWSADATA
210 TYPEDEF: OVERLAPPED WSAOVERLAPPED
211 TYPEDEF: WSAOVERLAPPED* LPWSAOVERLAPPED
212 TYPEDEF: uint GROUP
213 TYPEDEF: void* LPCONDITIONPROC
214 TYPEDEF: HANDLE WSAEVENT
215 TYPEDEF: LPHANDLE LPWSAEVENT
216 TYPEDEF: sockaddr* LPSOCKADDR
218 STRUCT: FLOWSPEC
219     { TokenRate          uint        }
220     { TokenBucketSize    uint        }
221     { PeakBandwidth      uint        }
222     { Latency            uint        }
223     { DelayVariation     uint        }
224     { ServiceType        SERVICETYPE }
225     { MaxSduSize         uint        }
226     { MinimumPolicedSize uint        } ;
227 TYPEDEF: FLOWSPEC* PFLOWSPEC
228 TYPEDEF: FLOWSPEC* LPFLOWSPEC
230 STRUCT: WSABUF
231     { len ulong }
232     { buf void* } ;
233 TYPEDEF: WSABUF* LPWSABUF
235 STRUCT: QOS
236     { SendingFlowspec FLOWSPEC }
237     { ReceivingFlowspec FLOWSPEC }
238     { ProviderSpecific WSABUF } ;
239 TYPEDEF: QOS* LPQOS
241 CONSTANT: MAX_PROTOCOL_CHAIN 7
243 STRUCT: WSAPROTOCOLCHAIN
244     { ChainLen int }
245     { ChainEntries { DWORD 7 } } ;
246     ! { ChainEntries { DWORD MAX_PROTOCOL_CHAIN } } ;
247 TYPEDEF: WSAPROTOCOLCHAIN* LPWSAPROTOCOLCHAIN
249 CONSTANT: WSAPROTOCOL_LEN 255
251 STRUCT: WSAPROTOCOL_INFOW
252     { dwServiceFlags1 DWORD }
253     { dwServiceFlags2 DWORD }
254     { dwServiceFlags3 DWORD }
255     { dwServiceFlags4 DWORD }
256     { dwProviderFlags DWORD }
257     { ProviderId GUID }
258     { dwCatalogEntryId DWORD }
259     { ProtocolChain WSAPROTOCOLCHAIN }
260     { iVersion int }
261     { iAddressFamily int }
262     { iMaxSockAddr int }
263     { iMinSockAddr int }
264     { iSocketType int }
265     { iProtocol int }
266     { iProtocolMaxOffset int }
267     { iNetworkByteOrder int }
268     { iSecurityScheme int }
269     { dwMessageSize DWORD }
270     { dwProviderReserved DWORD }
271     { szProtocol { WCHAR 256 } } ;
272     ! { szProtocol[WSAPROTOCOL_LEN+1] { WCHAR 256 } } ;
273 TYPEDEF: WSAPROTOCOL_INFOW* PWSAPROTOCOL_INFOW
274 TYPEDEF: WSAPROTOCOL_INFOW* LPWSAPROTOCOL_INFOW
275 TYPEDEF: WSAPROTOCOL_INFOW WSAPROTOCOL_INFO
276 TYPEDEF: WSAPROTOCOL_INFOW* PWSAPROTOCOL_INFO
277 TYPEDEF: WSAPROTOCOL_INFOW* LPWSAPROTOCOL_INFO
280 STRUCT: WSANAMESPACE_INFOW
281     { NSProviderId   GUID    }
282     { dwNameSpace    DWORD   }
283     { fActive        BOOL    }
284     { dwVersion      DWORD   }
285     { lpszIdentifier LPWSTR  } ;
286 TYPEDEF: WSANAMESPACE_INFOW* PWSANAMESPACE_INFOW
287 TYPEDEF: WSANAMESPACE_INFOW* LPWSANAMESPACE_INFOW
288 TYPEDEF: WSANAMESPACE_INFOW WSANAMESPACE_INFO
289 TYPEDEF: WSANAMESPACE_INFO* PWSANAMESPACE_INFO
290 TYPEDEF: WSANAMESPACE_INFO* LPWSANAMESPACE_INFO
292 CONSTANT: FD_MAX_EVENTS 10
294 STRUCT: WSANETWORKEVENTS
295     { lNetworkEvents long }
296     { iErrorCode { int FD_MAX_EVENTS } } ;
297 TYPEDEF: WSANETWORKEVENTS* PWSANETWORKEVENTS
298 TYPEDEF: WSANETWORKEVENTS* LPWSANETWORKEVENTS
300 ! STRUCT: WSAOVERLAPPED
301     ! { Internal DWORD }
302     ! { InternalHigh DWORD }
303     ! { Offset DWORD }
304     ! { OffsetHigh DWORD }
305     ! { hEvent WSAEVENT }
306     ! { bytesTransferred DWORD } ;
307 ! TYPEDEF: WSAOVERLAPPED* LPWSAOVERLAPPED
309 FUNCTION: SOCKET WSAAccept ( SOCKET s,
310                              sockaddr* addr,
311                              LPINT addrlen,
312                              LPCONDITIONPROC lpfnCondition,
313                              DWORD dwCallbackData )
315 ! FUNCTION: INT WSAAddressToString ( LPSOCKADDR lpsaAddress, DWORD dwAddressLength, LPWSAPROTOCOL_INFO lpProtocolInfo, LPTSTR lpszAddressString, LPDWORD lpdwAddressStringLength ) ;
317 FUNCTION: int WSACleanup ( )
318 FUNCTION: BOOL WSACloseEvent ( WSAEVENT hEvent )
320 FUNCTION: int WSAConnect ( SOCKET s,
321                            sockaddr* name,
322                            int namelen,
323                            LPWSABUF lpCallerData,
324                            LPWSABUF lpCalleeData,
325                            LPQOS lpSQOS,
326                            LPQOS lpGQOS )
327 FUNCTION: WSAEVENT WSACreateEvent ( )
328 ! FUNCTION: INT WSAEnumNameSpaceProviders ( LPDWORD lpdwBufferLength, LPWSANAMESPACE_INFO lpnspBuffer ) ;
329 FUNCTION: int WSAEnumNetworkEvents ( SOCKET s,
330                                      WSAEVENT hEventObject,
331                                      LPWSANETWORKEVENTS lpNetworkEvents )
332 ! FUNCTION: int WSAEnumProtocols ( LPINT lpiProtocols, LPWSAPROTOCOL_INFO lpProtocolBuffer, LPDWORD lpwdBufferLength ) ;
334 FUNCTION: int WSAEventSelect ( SOCKET s,
335                                WSAEVENT hEventObject,
336                                long lNetworkEvents )
337 FUNCTION: int WSAGetLastError ( )
338 FUNCTION: BOOL WSAGetOverlappedResult ( SOCKET s,
339                                         LPWSAOVERLAPPED lpOverlapped,
340                                         LPDWORD lpcbTransfer,
341                                         BOOL fWait,
342                                         LPDWORD lpdwFlags )
344 TYPEDEF: void* LPWSAOVERLAPPED_COMPLETION_ROUTINE
345 FUNCTION: int WSAIoctl ( SOCKET s,
346                          DWORD dwIoControlCode,
347                          LPVOID lpvInBuffer,
348                          DWORD cbInBuffer,
349                          LPVOID lpvOutBuffer,
350                          DWORD cbOutBuffer,
351                          LPDWORD lpcbBytesReturned,
352                          LPWSAOVERLAPPED lpOverlapped,
353                          LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
355 FUNCTION: int WSARecv ( SOCKET s,
356                         LPWSABUF lpBuffers,
357                         DWORD dwBufferCount,
358                         LPDWORD lpNumberOfBytesRecvd,
359                         LPDWORD lpFlags,
360                         LPWSAOVERLAPPED lpOverlapped,
361                         LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
363 FUNCTION: int WSARecvFrom ( SOCKET s,
364                             LPWSABUF lpBuffers,
365                             DWORD dwBufferCount,
366                             LPDWORD lpNumberOfBytesRecvd,
367                             LPDWORD lpFlags,
368                             sockaddr* lpFrom,
369                             LPINT lpFromlen,
370                             LPWSAOVERLAPPED lpOverlapped,
371                             LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
373 FUNCTION: BOOL WSAResetEvent ( WSAEVENT hEvent )
374 FUNCTION: int WSASend ( SOCKET s,
375                         LPWSABUF lpBuffers,
376                         DWORD dwBufferCount,
377                         LPDWORD lpNumberOfBytesSent,
378                         LPDWORD lpFlags,
379                         LPWSAOVERLAPPED lpOverlapped,
380                  LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
382 FUNCTION: int WSASendTo ( SOCKET s,
383                           LPWSABUF lpBuffers,
384                           DWORD dwBufferCount,
385                           LPDWORD lpNumberOfBytesSent,
386                           DWORD dwFlags,
387                           sockaddr* lpTo,
388                           int iToLen,
389                           LPWSAOVERLAPPED lpOverlapped,
390   LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
392 FUNCTION: int WSAStartup ( WORD version,  LPWSADATA out-data )
394 FUNCTION: SOCKET WSASocketW ( int af,
395                              int type,
396                              int protocol,
397                              LPWSAPROTOCOL_INFOW lpProtocolInfo,
398                              GROUP g,
399                              DWORD flags )
400 ALIAS: WSASocket WSASocketW
402 FUNCTION: DWORD WSAWaitForMultipleEvents ( DWORD cEvents,
403                                            WSAEVENT* lphEvents,
404                                            BOOL fWaitAll,
405                                            DWORD dwTimeout,
406                                            BOOL fAlertable )
409 LIBRARY: mswsock
411 FUNCTION: int AcceptEx ( SOCKET listen,
412                          SOCKET accept,
413                          PVOID out-buf,
414                          DWORD recv-len,
415                          DWORD addr-len,
416                          DWORD remote-len,
417                          LPDWORD out-len,
418                          LPOVERLAPPED overlapped )
420 FUNCTION: void GetAcceptExSockaddrs (
421   PVOID lpOutputBuffer,
422   DWORD dwReceiveDataLength,
423   DWORD dwLocalAddressLength,
424   DWORD dwRemoteAddressLength,
425   LPSOCKADDR* LocalSockaddr,
426   LPINT LocalSockaddrLength,
427   LPSOCKADDR* RemoteSockaddr,
428   LPINT RemoteSockaddrLength
431 CONSTANT: SIO_GET_EXTENSION_FUNCTION_POINTER -939524090
433 CONSTANT: WSAID_CONNECTEX GUID: {25a207b9-ddf3-4660-8ee9-76e58c74063e}
435 ERROR: winsock-exception n string ;
437 : winsock-expected-error? ( n -- ? )
438     ${ ERROR_IO_PENDING ERROR_SUCCESS WSA_IO_PENDING } member? ;
440 : (maybe-winsock-exception) ( n -- winsock-exception/f )
441     ! ! WSAStartup returns the error code 'n' directly
442     dup winsock-expected-error?
443     [ drop f ] [ [ ] [ n>win32-error-string ] bi \ winsock-exception boa ] if ;
445 : maybe-winsock-exception ( -- winsock-exception/f )
446     WSAGetLastError (maybe-winsock-exception) ;
448 : winsock-error ( -- )
449     maybe-winsock-exception [ throw ] when* ;
451 : (winsock-error) ( n -- * )
452     [ ] [ n>win32-error-string ] bi winsock-exception ;
454 : throw-winsock-error ( -- * )
455     WSAGetLastError (winsock-error) ;
457 : winsock-error=0/f ( n/f -- )
458     { 0 f } member? [ winsock-error ] when ;
460 : winsock-error!=0/f ( n/f -- )
461     { 0 f } member? [ winsock-error ] unless ;
463 ! WSAStartup and WSACleanup return the error code directly
464 : winsock-return-check ( n/f -- )
465     dup { 0 f } member? [
466         drop
467     ] [
468         [ ] [ n>win32-error-string ] bi winsock-exception
469     ] if ;
471 : socket-error* ( n -- )
472     SOCKET_ERROR = [
473         WSAGetLastError
474         dup WSA_IO_PENDING = [
475             drop
476         ] [
477             (maybe-winsock-exception) throw
478         ] if
479     ] when ;
481 : socket-error ( n -- )
482     SOCKET_ERROR = [ winsock-error ] when ;
484 : init-winsock ( -- )
485     0x0202 <wsadata> WSAStartup winsock-return-check ;
487 : shutdown-winsock ( -- ) WSACleanup winsock-return-check ;
489 [ init-winsock ] "windows.winsock" add-startup-hook
490 [ shutdown-winsock ] "windows.winsock" add-shutdown-hook