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 ;
12 ! Some differences between Win32 and Win64
13 cpu x86.64? "windows.winsock.64" "windows.winsock.32" ? use-vocab
18 : <wsadata> ( -- byte-array )
21 CONSTANT: SOCK_STREAM 1
22 CONSTANT: SOCK_DGRAM 2
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
52 CONSTANT: AF_IMPLINK 3
59 CONSTANT: AF_DATAKIT 9
62 CONSTANT: AF_DECnet 12
65 CONSTANT: AF_HYLINK 15
66 CONSTANT: AF_APPLETALK 16
67 CONSTANT: AF_NETBIOS 17
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
117 CONSTANT: SOL_SOCKET 0xffff
139 { addr-list void* } ;
152 { canonname c-string }
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 ;
170 { fd_array SOCKET[64] } ;
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,
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
213 TYPEDEF: void* LPCONDITIONPROC
214 TYPEDEF: HANDLE WSAEVENT
215 TYPEDEF: LPHANDLE LPWSAEVENT
216 TYPEDEF: sockaddr* LPSOCKADDR
220 { TokenBucketSize uint }
221 { PeakBandwidth uint }
223 { DelayVariation uint }
224 { ServiceType SERVICETYPE }
226 { MinimumPolicedSize uint } ;
227 TYPEDEF: FLOWSPEC* PFLOWSPEC
228 TYPEDEF: FLOWSPEC* LPFLOWSPEC
233 TYPEDEF: WSABUF* LPWSABUF
236 { SendingFlowspec FLOWSPEC }
237 { ReceivingFlowspec FLOWSPEC }
238 { ProviderSpecific WSABUF } ;
241 CONSTANT: MAX_PROTOCOL_CHAIN 7
243 STRUCT: WSAPROTOCOLCHAIN
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 }
258 { dwCatalogEntryId DWORD }
259 { ProtocolChain WSAPROTOCOLCHAIN }
261 { iAddressFamily 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 }
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
302 ! { InternalHigh DWORD }
304 ! { OffsetHigh DWORD }
305 ! { hEvent WSAEVENT }
306 ! { bytesTransferred DWORD } ;
307 ! TYPEDEF: WSAOVERLAPPED* LPWSAOVERLAPPED
309 FUNCTION: SOCKET WSAAccept ( SOCKET s,
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,
323 LPWSABUF lpCallerData,
324 LPWSABUF lpCalleeData,
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,
344 TYPEDEF: void* LPWSAOVERLAPPED_COMPLETION_ROUTINE
345 FUNCTION: int WSAIoctl ( SOCKET s,
346 DWORD dwIoControlCode,
351 LPDWORD lpcbBytesReturned,
352 LPWSAOVERLAPPED lpOverlapped,
353 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
355 FUNCTION: int WSARecv ( SOCKET s,
358 LPDWORD lpNumberOfBytesRecvd,
360 LPWSAOVERLAPPED lpOverlapped,
361 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
363 FUNCTION: int WSARecvFrom ( SOCKET s,
366 LPDWORD lpNumberOfBytesRecvd,
370 LPWSAOVERLAPPED lpOverlapped,
371 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
373 FUNCTION: BOOL WSAResetEvent ( WSAEVENT hEvent )
374 FUNCTION: int WSASend ( SOCKET s,
377 LPDWORD lpNumberOfBytesSent,
379 LPWSAOVERLAPPED lpOverlapped,
380 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
382 FUNCTION: int WSASendTo ( SOCKET s,
385 LPDWORD lpNumberOfBytesSent,
389 LPWSAOVERLAPPED lpOverlapped,
390 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
392 FUNCTION: int WSAStartup ( WORD version, LPWSADATA out-data )
394 FUNCTION: SOCKET WSASocketW ( int af,
397 LPWSAPROTOCOL_INFOW lpProtocolInfo,
400 ALIAS: WSASocket WSASocketW
402 FUNCTION: DWORD WSAWaitForMultipleEvents ( DWORD cEvents,
411 FUNCTION: int AcceptEx ( SOCKET listen,
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? [
468 [ ] [ n>win32-error-string ] bi winsock-exception
471 : socket-error* ( n -- )
474 dup WSA_IO_PENDING = [
477 (maybe-winsock-exception) throw
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