1 {****************************************************************************
2 
3 
4     This file is part of the Free Pascal run time library.
5     Copyrigth (c) 2003 by Yuri Prokushev (prokushev@freemail.ru)
6 
7     This file corresponds to version 1.1 of the Windows Sockets
8     specification.
9 
10     See the file COPYING.FPC, included in this distribution,
11     for details about the copyright.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17  ****************************************************************************}
18 {$ifndef winsock}
19 unit pmwsock;
20 {$endif}
21 
22 {$PACKRECORDS 1}
23 {$MACRO ON}
24 Interface
25 
26 Uses OS2Def;
27 
28 // The new type to be used in all instances which refer to sockets.
29 type
30   TSocket=Cardinal;
31 
32 type
33   PLongint=^Longint;
34   PCardinal=^Cardinal;
35 
36 // Select uses arrays of TSockets.  These macros manipulate such
37 // arrays.  FD_SETSIZE may be defined by the user before including
38 // this file, but the default here should be >= 64.
39 //
40 // CAVEAT IMPLEMENTOR and USER: THESE MACROS AND TYPES MUST BE
41 // INCLUDED IN WINSOCK.H EXACTLY AS SHOWN HERE.
42 const
43   FD_SETSIZE = 64;
44 
45 type
46   fdset=record
47     fd_count: Word;                             // how many are SET?
48     fd_array: Array[0..FD_SETSIZE-1] of TSocket; // an array of TSockets
49   end;
50   TFDSet = fdset;
51   PFDSet = ^fdset;
52 
53 
__WSAFDIsSetnull54 Function __WSAFDIsSet(a: TSocket;var b: fdset): Longint; cdecl;
55     external 'PMWSock' index 151;
__WSAFDIsSet_null56 Function __WSAFDIsSet_(s:TSocket; var FDSet:TFDSet): Longint; cdecl;
57     external 'PMWSock' index 151;
__WSAFDIsSet2_null58 Function __WSAFDIsSet2_(s:TSocket; var FDSet:TFDSet): boolean; cdecl;
59     external 'PMWSock' index 151;
60 
FD_ISSET2null61 Function FD_ISSET2(a: TSocket;var b: fdset): Longint; cdecl;
62     external 'PMWSock' index 151;
63 
FD_ISSETnull64 Function FD_ISSET(a: TSocket;var b: fdset): boolean; cdecl;
65     external 'PMWSock' index 151;
66 
67 Procedure FD_CLR(ASocket: TSocket; var aset: fdset);
68 Procedure FD_SET(Socket:TSocket; var FDSet:TFDSet);
69 
70 // Structure used in select() call, taken from the BSD file sys/time.h.
71 type
72   timeval=record
73     tv_sec: LongInt;    // seconds
74     tv_usec: LongInt;   // and microseconds
75   end;
76 
77   TTimeVal = timeval;
78   PTimeVal = ^TTimeVal;
79 
80   { found no reference to this type in c header files and here. AlexS }
81   { minutes west of Greenwich  }
82   { type of dst correction  }
83   timezone = record
84     tz_minuteswest : longint;
85     tz_dsttime : longint;
86   end;
87        TTimeZone = timezone;
88        PTimeZone = ^TTimeZone;
89 
90 // Operations on timevals.
91 // timercmp does not work for >= or <=.
92 
timerissetnull93 Function timerisset(tvp: timeval): Boolean;
timercmpnull94 //Function timercmp(tvp, uvp, cmp);
95 Procedure timerclear(var tvp: timeval);
96 
97 // Commands for ioctlsocket(),  taken from the BSD file fcntl.h.
98 //
99 // Ioctl's have the command encoded in the lower word,
100 // and the size of any in or out parameters in the upper
101 // word.  The high 2 bits of the upper word are used
102 // to encode the in/out status of the parameter; for now
103 // we restrict parameters to at most 128 bytes.
104 
105 const
106   IOCPARM_MASK    = $7f;               // parameters must be < 128 bytes
107   IOC_VOID        = $20000000;         // no parameters
108   IOC_OUT         = $40000000;         // copy out parameters
109   IOC_IN          = longint ($80000000);         // copy in parameters
110   IOC_INOUT       = IOC_IN or IOC_OUT; // 0x20000000 distinguishes new &
111                                        // old ioctl's
112 const
113   // get # bytes to read
114   FIONREAD=(IOC_OUT or ((Longint (sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('f') shl 8) or 127);
115   // set/clear non-blocking i/o
116   FIONBIO=(IOC_IN or ((Longint(sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('f') shl 8) or 126);
117   // set/clear async i/o
118   FIOASYNC=(IOC_IN or ((Longint(sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('f') shl 8) or 125);
119 
120 // Socket I/O Controls
121 const
122   // set high watermark
123   SIOCSHIWAT=(IOC_IN or ((Longint(sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('s') shl 8) or 0);
124   // get high watermark
125   SIOCGHIWAT=(IOC_OUT or ((Longint (sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('s') shl 8) or 1);
126   // set low watermark
127   SIOCSLOWAT=(IOC_IN or ((Longint(sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('s') shl 8) or 2);
128   // get low watermark
129   SIOCGLOWAT=(IOC_OUT or ((Longint (sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('s') shl 8) or 3);
130   // at oob mark?
131   SIOCATMARK=(IOC_OUT or ((Longint (sizeof(Cardinal)) and IOCPARM_MASK) shl 16) or (Ord('s') shl 8) or 7);
132 
133 // Structures returned by network data base library, taken from the
134 // BSD file netdb.h.  All addresses are supplied in host order, and
135 // returned in network order (suitable for use in system calls).
136 
137 type
138   hostent=record
139     h_name: PChar;             // official name of host
140     h_aliases: PPChar;         // alias list
141     h_addrtype: LongInt;       // host address type
142     h_length: LongInt;         // length of address
143     case byte of
144        0: (h_addr_list: ppchar); // list of addresses from name server
145        1: (h_addr: ppchar)       // address, for backward compatiblity
146   end;
147   phostent=^hostent;
148   THostEnt = hostent;
149 
150 // It is assumed here that a network number
151 // fits in 32 bits.
152 type
153   netent=record
154     n_name: PChar;                       // official name of net
155     n_aliases: PPChar;                   // alias list
156     n_addrtype: Longint;                 // net address type
157     n_net: Cardinal;                     // network #
158   End;
159   pnetent=^netent;
160   TNetEnt = netent;
161 
162 type
163   servent=record
164     s_name: PChar;                      // official service name
165     s_aliases: PPChar;                  // alias list
166     s_port: LongInt;                    // port #
167     s_proto: PChar;                     // protocol to use
168   end;
169   TServEnt = servent;
170   pservent=^servent;
171 
172   protoent=record
173     p_name: PChar;                      // official protocol name
174     p_aliases: PPChar;                  // alias list
175     p_proto: LongInt;                   // protocol #
176   end;
177   TProtoEnt = protoent;
178   pprotoent=^protoent;
179 
180 // Constants and structures defined by the internet system,
181 // Per RFC 790, September 1981, taken from the BSD file netinet/in.h.
182 
183 // Protocols
184 const
185   IPPROTO_IP              =0;               // dummy for IP
186   IPPROTO_ICMP            =1;               // control message protocol
187   IPPROTO_GGP             =2;               // gateway^2 (deprecated)
188   IPPROTO_TCP             =6;               // tcp
189   IPPROTO_PUP             =12;              // pup
190   IPPROTO_UDP             =17;              // user datagram protocol
191   IPPROTO_IDP             =22;              // xns idp
192   IPPROTO_ND              =77;              // UNOFFICIAL net disk proto
193   IPPROTO_RAW             =255;             // raw IP packet
194   IPPROTO_MAX             =256;
195 
196 // Port/socket numbers: network standard functions
197 
198   IPPORT_ECHO             =7;
199   IPPORT_DISCARD          =9;
200   IPPORT_SYSTAT           =11;
201   IPPORT_DAYTIME          =13;
202   IPPORT_NETSTAT          =15;
203   IPPORT_FTP              =21;
204   IPPORT_TELNET           =23;
205   IPPORT_SMTP             =25;
206   IPPORT_TIMESERVER       =37;
207   IPPORT_NAMESERVER       =42;
208   IPPORT_WHOIS            =43;
209   IPPORT_MTP              =57;
210 
211 // Port/socket numbers: host specific functions
212 
213   IPPORT_TFTP             =69;
214   IPPORT_RJE              =77;
215   IPPORT_FINGER           =79;
216   IPPORT_TTYLINK          =87;
217   IPPORT_SUPDUP           =95;
218 
219 // UNIX TCP sockets
220 
221   IPPORT_EXECSERVER       =512;
222   IPPORT_LOGINSERVER      =513;
223   IPPORT_CMDSERVER        =514;
224   IPPORT_EFSSERVER        =520;
225 
226 // UNIX UDP sockets
227 
228   IPPORT_BIFFUDP          =512;
229   IPPORT_WHOSERVER        =513;
230   IPPORT_ROUTESERVER      =520;
231                                         // 520+1 also used
232 
233 // Ports < IPPORT_RESERVED are reserved for
234 // privileged processes (e.g. root).
235 
236   IPPORT_RESERVED         =1024;
237 
238 // Link numbers
239 
240   IMPLINK_IP              =155;
241   IMPLINK_LOWEXPER        =156;
242   IMPLINK_HIGHEXPER       =158;
243 
244 // Internet address (old style... should be updated)
245 
246 type
247   in_addr=record
248   case Integer of
249     1:(S_un_b:record s_b1,s_b2,s_b3,s_b4: Byte; end;);
250     2:(s_un_w:record s_w1,s_w2: Word; end;);
251     3:(s_addr: Cardinal);
252   end;
253   TInAddr = in_addr;
254   PInAddr = ^TInAddr;
255 
256 {$define s_addr:=in_addr.S_addr} // can be used for most tcp & ip code
257 {$define s_host:=in_addr.S_un_b.s_b2} // host on imp
258 {$define s_net:=in_addr.S_un_b.s_b1} // network
259 {$define s_imp:=in_addr.S_un_w.s_w2} // imp
260 {$define s_impno:=in_addr.S_un_b.s_b4} // imp #
261 {$define s_lh:=in_addr.S_un_b.s_b3} // logical host
262 
263 // Definitions of bits in internet address integers.
264 // On subnets, the decomposition of addresses to host and net parts
265 // is done according to subnet mask, not the masks here.
266 const
267 {$define IN_CLASSA(i):=((Longint(i) and $80000000) = 0)}
268   IN_CLASSA_NET           =$ff000000;
269   IN_CLASSA_NSHIFT        =24;
270   IN_CLASSA_HOST          =$00ffffff;
271   IN_CLASSA_MAX           =128;
272 
273 {$define IN_CLASSB(i):=((Longint(i) and $c0000000) = $80000000)}
274   IN_CLASSB_NET           =$ffff0000;
275   IN_CLASSB_NSHIFT        =16;
276   IN_CLASSB_HOST          =$0000ffff;
277   IN_CLASSB_MAX           =65536;
278 
279 {$define IN_CLASSC(i):=((Longint(i) and $e0000000) = $c0000000)}
280   IN_CLASSC_NET           =$ffffff00;
281   IN_CLASSC_NSHIFT        =8;
282   IN_CLASSC_HOST          =$000000ff;
283 
284   INADDR_ANY              =$00000000;
285   INADDR_LOOPBACK         =$7f000001;
286   INADDR_BROADCAST        =$ffffffff;
287   INADDR_NONE             =$ffffffff;
288 
289 // Socket address, internet style.
290 
291 Type
292   sockaddr_in=Record
293     case integer of
294     0 : ( (* equals to sockaddr_in, size is 16 byte *)
295       sin_family : SmallInt;                (* 2 byte *)
296       sin_port : Word;                   (* 2 byte *)
297       sin_addr : TInAddr;                   (* 4 byte *)
298       sin_zero : array[0..8-1] of char;     (* 8 byte *)
299       );
300     1 : ((* equals to sockaddr, size is 16 byte *)
301       sa_family : Smallint; (* 2 byte *)
302       sa_data : array[0..14-1] of char;    (* 14 byte *)
303       );
304     end;
305     TSockAddrIn = sockaddr_in;
306     PSockAddrIn = ^TSockAddrIn;
307     TSockAddr = sockaddr_in;
308     PSockAddr = ^TSockAddr;
309 
310 const
311     WSADESCRIPTION_LEN      =256;
312     WSASYS_STATUS_LEN       =128;
313 
314 Type
315     WSAData=Record
316                wVersion:Word;
317                wHighVersion:Word;
318                szDescription: array[0..WSADESCRIPTION_LEN] of Char;
319                szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
320                iMaxSockets:Word;
321                iMaxUdpDg:Word;
322                // in OS/2 no such entry
323      //          pad1 : SmallInt;              { 2 byte, ofs 394 } { ensure right packaging }
324                lpVendorInfo:PChar;
325     End;
326     PWSADATA=^WSAData;
327     LPWSADATA=^WSAData;
328     TWSAData = WSADATA;
329 
330 // Options for use with [gs]etsockopt at the IP level.
331 
332 Const
333     IP_OPTIONS      =1;  // set/get IP per-packet options
334 
335     IP_MULTICAST_IF = 2;
336     IP_MULTICAST_TTL = 3;
337     IP_MULTICAST_LOOP = 4;
338     IP_ADD_MEMBERSHIP = 5;
339     IP_DROP_MEMBERSHIP = 6;
340     IP_DEFAULT_MULTICAST_TTL = 1;
341     IP_DEFAULT_MULTICAST_LOOP = 1;
342     IP_MAX_MEMBERSHIPS = 20;
343 
344 type
345   ip_mreq = record
346     imr_multiaddr : in_addr;
347     imr_interface : in_addr;
348   end;
349 
350 // Definitions related to sockets: types, address families, options,
351 // taken from the BSD file sys/socket.h.
352 
353 // This is used instead of -1, since the
354 // TSocket type is unsigned.
355 Const
356     INVALID_SOCKET  = -1;
357     SOCKET_ERROR    = -1;
358 
359 // Types
360 
361 Const
362     SOCK_STREAM     =1;               // stream socket
363     SOCK_DGRAM      =2;               // datagram socket
364     SOCK_RAW        =3;               // raw-protocol interface
365     SOCK_RDM        =4;               // reliably-delivered message
366     SOCK_SEQPACKET  =5;               // sequenced packet stream
367 
368 // Option flags per-socket.
369 
370 Const
371     SO_DEBUG        =$0001;          // turn on debugging info recording
372     SO_ACCEPTCONN   =$0002;          // socket has had listen()
373     SO_REUSEADDR    =$0004;          // allow local address reuse
374     SO_KEEPALIVE    =$0008;          // keep connections alive
375     SO_DONTROUTE    =$0010;          // just use interface addresses
376     SO_BROADCAST    =$0020;          // permit sending of broadcast msgs
377     SO_USELOOPBACK  =$0040;          // bypass hardware when possible
378     SO_LINGER       =$0080;          // linger on close if data present
379     SO_OOBINLINE    =$0100;          // leave received OOB data in line
380     SO_DONTLINGER   =NOT SO_LINGER;  // dont linger
381 
382 // Additional options.
383 
384     SO_SNDBUF       =$1001;          // send buffer size
385     SO_RCVBUF       =$1002;          // receive buffer size
386     SO_SNDLOWAT     =$1003;          // send low-water mark
387     SO_RCVLOWAT     =$1004;          // receive low-water mark
388     SO_SNDTIMEO     =$1005;          // send timeout
389     SO_RCVTIMEO     =$1006;          // receive timeout
390     SO_ERROR        =$1007;          // get error status and clear
391     SO_TYPE         =$1008;          // get socket type
392 
393     {
394      Options for connect and disconnect data and options.  Used only by
395      non-TCP/IP transports such as DECNet, OSI TP4, etc.
396     }
397     SO_CONNDATA = $7000;
398     SO_CONNOPT = $7001;
399     SO_DISCDATA = $7002;
400     SO_DISCOPT = $7003;
401     SO_CONNDATALEN = $7004;
402     SO_CONNOPTLEN = $7005;
403     SO_DISCDATALEN = $7006;
404     SO_DISCOPTLEN = $7007;
405 
406        {
407          Option for opening sockets for synchronous access.
408        }
409     SO_OPENTYPE = $7008;
410     SO_SYNCHRONOUS_ALERT = $10;
411     SO_SYNCHRONOUS_NONALERT = $20;
412 
413        {
414          Other NT-specific options.
415        }
416     SO_MAXDG = $7009;
417     SO_MAXPATHDG = $700A;
418     SO_UPDATE_ACCEPT_CONTEXT = $700B;
419     SO_CONNECT_TIME = $700C;
420 
421 // TCP options.
422 
423 Const
424     TCP_NODELAY    = $0001;
425     TCP_BSDURGENT  = $7000;
426 
427 // Address families.
428 
429 Const
430     AF_UNSPEC       =0;               // unspecified
431     AF_UNIX         =1;               // local to host (pipes, portals)
432     AF_INET         =2;               // internetwork: UDP, TCP, etc.
433     AF_IMPLINK      =3;               // arpanet imp addresses
434     AF_PUP          =4;               // pup protocols: e.g. BSP
435     AF_CHAOS        =5;               // mit CHAOS protocols
436     AF_NS           =6;               // XEROX NS protocols
437     AF_ISO          =7;               // ISO protocols
438     AF_OSI          =AF_ISO;          // OSI is ISO
439     AF_ECMA         =8;               // european computer manufacturers
440     AF_DATAKIT      =9;               // datakit protocols
441     AF_CCITT        =10;              // CCITT protocols, X.25 etc
442     AF_SNA          =11;              // IBM SNA
443     AF_DECnet       =12;              // DECnet
444     AF_DLI          =13;              // Direct data link interface
445     AF_LAT          =14;              // LAT
446     AF_HYLINK       =15;              // NSC Hyperchannel
447     AF_APPLETALK    =16;              // AppleTalk
448     AF_NETBIOS      =17;              // NetBios-style addresses
449 
450 
451     { FireFox }
452     AF_FIREFOX = 19;
453     { Somebody is using this! }
454     AF_UNKNOWN1 = 20;
455     { Banyan }
456     AF_BAN = 21;
457 
458     AF_MAX          =22;
459 
460 // Structure used by kernel to store most
461 // addresses.
462 
463 Type
464     sockaddr=Record
465         sa_family:Word;                 // address family
466         sa_data:Array[0..13] of char;   // up to 14 bytes of direct address
467     End;
468 
469 // Structure used by kernel to pass protocol
470 // information in raw sockets.
471 
472     sockproto=Record
473         sp_family:Word;                 // address family
474         sp_protocol:Word;               // protocol
475     End;
476 
477     TSockProto = sockproto;
478     PSockProto = ^TSockProto;
479 
480 
481 // Protocol families, same as address families for now.
482 
483 Const
484     PF_UNSPEC       =AF_UNSPEC;
485     PF_UNIX         =AF_UNIX;
486     PF_INET         =AF_INET;
487     PF_IMPLINK      =AF_IMPLINK;
488     PF_PUP          =AF_PUP;
489     PF_CHAOS        =AF_CHAOS;
490     PF_NS           =AF_NS;
491     PF_ISO          =AF_ISO;
492     PF_OSI          =AF_OSI;
493     PF_ECMA         =AF_ECMA;
494     PF_DATAKIT      =AF_DATAKIT;
495     PF_CCITT        =AF_CCITT;
496     PF_SNA          =AF_SNA;
497     PF_DECnet       =AF_DECnet;
498     PF_DLI          =AF_DLI;
499     PF_LAT          =AF_LAT;
500     PF_HYLINK       =AF_HYLINK;
501     PF_APPLETALK    =AF_APPLETALK;
502 
503     PF_FIREFOX = AF_FIREFOX;
504     PF_UNKNOWN1 = AF_UNKNOWN1;
505     PF_BAN = AF_BAN;
506     PF_MAX = AF_MAX;
507 
508 
509 // Structure used for manipulating linger option.
510 
511 Type
512   linger=Record
513     l_onoff:LongInt;                // option on/off
514     l_linger:LongInt;               // linger time
515   End;
516   TLinger = linger;
517   PLinger = ^TLinger;
518 
519 // Level number for (get/set)sockopt() to apply to socket itself.
520 
521 Const
522      SOL_SOCKET      =$ffff;          // options for socket level
523 
524 // Maximum queue length specifiable by listen.
525 
526      SOMAXCONN       =5;
527 
528      MSG_OOB         =1;             // process out-of-band data
529      MSG_PEEK        =2;             // peek at incoming message
530      MSG_DONTROUTE   =4;             // send without using routing tables
531      MSG_MAXIOVLEN   =16;
532 
533 // Define constant based on rfc883, used by gethostbyxxxx() calls.
534 
535      MAXGETHOSTSTRUCT =1024;
536      MAXHOSTNAMELEN = MAXGETHOSTSTRUCT;
537 
538 // Define flags to be used with the WSAAsyncSelect() call.
539 
540      FD_READ         =$01;
541      FD_WRITE        =$02;
542      FD_OOB          =$04;
543      FD_ACCEPT       =$08;
544      FD_CONNECT      =$10;
545      FD_CLOSE        =$20;
546 
547 // All Windows Sockets error constants are biased by WSABASEERR from
548 // the "normal"
549 
550      WSABASEERR              =10000;
551 
552 
553 // Windows Sockets definitions of regular Microsoft C error constants
554 
555      WSAEINTR                =(WSABASEERR+4);
556      WSAEBADF                =(WSABASEERR+9);
557      WSAEACCES               =(WSABASEERR+13);
558      WSAEFAULT               =(WSABASEERR+14);
559      WSAEINVAL               =(WSABASEERR+22);
560      WSAEMFILE               =(WSABASEERR+24);
561 
562 // Windows Sockets definitions of regular Berkeley error constants
563 
564      WSAEWOULDBLOCK          =(WSABASEERR+35);
565      WSAEINPROGRESS          =(WSABASEERR+36);
566      WSAEALREADY             =(WSABASEERR+37);
567      WSAENOTSOCK             =(WSABASEERR+38);
568      WSAEDESTADDRREQ         =(WSABASEERR+39);
569      WSAEMSGSIZE             =(WSABASEERR+40);
570      WSAEPROTOTYPE           =(WSABASEERR+41);
571      WSAENOPROTOOPT          =(WSABASEERR+42);
572      WSAEPROTONOSUPPORT      =(WSABASEERR+43);
573      WSAESOCKTNOSUPPORT      =(WSABASEERR+44);
574      WSAEOPNOTSUPP           =(WSABASEERR+45);
575      WSAEPFNOSUPPORT         =(WSABASEERR+46);
576      WSAEAFNOSUPPORT         =(WSABASEERR+47);
577      WSAEADDRINUSE           =(WSABASEERR+48);
578      WSAEADDRNOTAVAIL        =(WSABASEERR+49);
579      WSAENETDOWN             =(WSABASEERR+50);
580      WSAENETUNREACH          =(WSABASEERR+51);
581      WSAENETRESET            =(WSABASEERR+52);
582      WSAECONNABORTED         =(WSABASEERR+53);
583      WSAECONNRESET           =(WSABASEERR+54);
584      WSAENOBUFS              =(WSABASEERR+55);
585      WSAEISCONN              =(WSABASEERR+56);
586      WSAENOTCONN             =(WSABASEERR+57);
587      WSAESHUTDOWN            =(WSABASEERR+58);
588      WSAETOOMANYREFS         =(WSABASEERR+59);
589      WSAETIMEDOUT            =(WSABASEERR+60);
590      WSAECONNREFUSED         =(WSABASEERR+61);
591      WSAELOOP                =(WSABASEERR+62);
592      WSAENAMETOOLONG         =(WSABASEERR+63);
593      WSAEHOSTDOWN            =(WSABASEERR+64);
594      WSAEHOSTUNREACH         =(WSABASEERR+65);
595      WSAENOTEMPTY            =(WSABASEERR+66);
596      WSAEPROCLIM             =(WSABASEERR+67);
597      WSAEUSERS               =(WSABASEERR+68);
598      WSAEDQUOT               =(WSABASEERR+69);
599      WSAESTALE               =(WSABASEERR+70);
600      WSAEREMOTE              =(WSABASEERR+71);
601      WSAEDISCON = WSABASEERR + 101;
602 
603 // Extended Windows Sockets error constant definitions
604 
605      WSASYSNOTREADY          =(WSABASEERR+91);
606      WSAVERNOTSUPPORTED      =(WSABASEERR+92);
607      WSANOTINITIALISED       =(WSABASEERR+93);
608 
609 // Error return codes from gethostbyname() and gethostbyaddr()
610 // (when using the resolver). Note that these errors are
611 // retrieved via WSAGetLastError() and must therefore follow
612 // the rules for avoiding clashes with error numbers from
613 // specific implementations or language run-time systems.
614 // For this reason the codes are based at WSABASEERR+1001.
615 // Note also that [WSA]NO_ADDRESS is defined only for
616 // compatibility purposes.
617 
618 {$define h_errno:=WSAGetLastError()}
619 
620 // Authoritative Answer: Host not found
621 
622      WSAHOST_NOT_FOUND       =(WSABASEERR+1001);
623      HOST_NOT_FOUND          =WSAHOST_NOT_FOUND;
624 
625 // Non-Authoritative: Host not found, or SERVERFAIL
626 
627      WSATRY_AGAIN            =(WSABASEERR+1002);
628      TRY_AGAIN               =WSATRY_AGAIN;
629 
630 // Non recoverable errors, FORMERR, REFUSED, NOTIMP
631 
632      WSANO_RECOVERY          =(WSABASEERR+1003);
633      NO_RECOVERY             =WSANO_RECOVERY;
634 
635 // Valid name, no data record of requested type
636 
637      WSANO_DATA              =(WSABASEERR+1004);
638      NO_DATA                 =WSANO_DATA;
639 
640 // no address, look for MX record
641 
642      WSANO_ADDRESS           =WSANO_DATA;
643      NO_ADDRESS              =WSANO_ADDRESS;
644 
645 // Windows Sockets errors redefined as regular Berkeley error constants
646 
647 Const
648      EWOULDBLOCK             =WSAEWOULDBLOCK;
649      EINPROGRESS             =WSAEINPROGRESS;
650      EALREADY                =WSAEALREADY;
651      ENOTSOCK                =WSAENOTSOCK;
652      EDESTADDRREQ            =WSAEDESTADDRREQ;
653      EMSGSIZE                =WSAEMSGSIZE;
654      EPROTOTYPE              =WSAEPROTOTYPE;
655      ENOPROTOOPT             =WSAENOPROTOOPT;
656      EPROTONOSUPPORT         =WSAEPROTONOSUPPORT;
657      ESOCKTNOSUPPORT         =WSAESOCKTNOSUPPORT;
658      EOPNOTSUPP              =WSAEOPNOTSUPP;
659      EPFNOSUPPORT            =WSAEPFNOSUPPORT;
660      EAFNOSUPPORT            =WSAEAFNOSUPPORT;
661      EADDRINUSE              =WSAEADDRINUSE;
662      EADDRNOTAVAIL           =WSAEADDRNOTAVAIL;
663      ENETDOWN                =WSAENETDOWN;
664      ENETUNREACH             =WSAENETUNREACH;
665      ENETRESET               =WSAENETRESET;
666      ECONNABORTED            =WSAECONNABORTED;
667      ECONNRESET              =WSAECONNRESET;
668      ENOBUFS                 =WSAENOBUFS;
669      EISCONN                 =WSAEISCONN;
670      ENOTCONN                =WSAENOTCONN;
671      ESHUTDOWN               =WSAESHUTDOWN;
672      ETOOMANYREFS            =WSAETOOMANYREFS;
673      ETIMEDOUT               =WSAETIMEDOUT;
674      ECONNREFUSED            =WSAECONNREFUSED;
675      ELOOP                   =WSAELOOP;
676      ENAMETOOLONG            =WSAENAMETOOLONG;
677      EHOSTDOWN               =WSAEHOSTDOWN;
678      EHOSTUNREACH            =WSAEHOSTUNREACH;
679      ENOTEMPTY               =WSAENOTEMPTY;
680      EPROCLIM                =WSAEPROCLIM;
681      EUSERS                  =WSAEUSERS;
682      EDQUOT                  =WSAEDQUOT;
683      ESTALE                  =WSAESTALE;
684      EREMOTE                 =WSAEREMOTE;
685 
686      TF_DISCONNECT = $01;
687      TF_REUSE_SOCKET = $02;
688      TF_WRITE_BEHIND = $04;
689 
690        {
691          Options for use with [gs]etsockopt at the IP level.
692        }
693        IP_TTL = 7;
694        IP_TOS = 8;
695        IP_DONTFRAGMENT = 9;
696 
697     type
698        _TRANSMIT_FILE_BUFFERS = record
699           Head : Pointer;
700           HeadLength : Cardinal;
701           Tail : Pointer;
702           TailLength : Cardinal;
703        end;
704        TRANSMIT_FILE_BUFFERS = _TRANSMIT_FILE_BUFFERS;
705        TTransmitFileBuffers = _TRANSMIT_FILE_BUFFERS;
706        PTransmitFileBuffers = ^TTransmitFileBuffers;
707 
prototypesnull708 // Socket function prototypes
709 
710 Function accept(s: TSocket; Var addr; Var addrlen: LongInt): TSocket; cdecl;
711     external 'PMWSock' index 1;
acceptnull712 Function accept(s:TSocket; addr: PSockAddr; addrlen : PLongint) : TSocket; cdecl;
713     external 'PMWSock' index 1;
acceptnull714 Function accept(s:TSocket; addr: PSockAddr; var addrlen : Longint) : TSocket; cdecl;
715     external 'PMWSock' index 1;
716 
bindnull717 Function bind(s: TSocket; Const addr; namelen: LongInt): LongInt; cdecl;
718     external 'PMWSock' index 2;
bindnull719 Function bind(s:TSocket; addr: PSockaddr;namelen: Longint): Longint; cdecl;
720     external 'PMWSock' index 2;
721 
closesocketnull722 Function closesocket(s: TSocket): LongInt; cdecl;
723     external 'PMWSock' index 3;
724 
connectnull725 Function connect(s: TSocket; Const name: sockaddr; namelen: LongInt): LongInt; cdecl;
726     external 'PMWSock' index 4;
connectnull727 Function connect(s:TSocket; addr:PSockAddr; namelen: Longint): Longint; cdecl;
728     external 'PMWSock' index 4;
729 
ioctlsocketnull730 Function ioctlsocket(s: TSocket; cmd: LongInt; Var argp: Cardinal): LongInt; cdecl;
731     external 'PMWSock' index 12;
ioctlsocketnull732 Function ioctlsocket(s: TSocket; cmd: longint; var arg:longint): Longint; cdecl;
733     external 'PMWSock' index 12;
ioctlsocketnull734 Function ioctlsocket(s: TSocket; cmd: longint; argp: PCardinal): Longint; cdecl;
735     external 'PMWSock' index 12;
736 
getpeernamenull737 Function getpeername(s: TSocket; Var name: sockaddr; Var nameLen: LongInt): LongInt; cdecl;
738     external 'PMWSock' index 5;
739 
getsocknamenull740 Function getsockname(s: TSocket;Var name: sockaddr; Var namelen: LongInt): LongInt; cdecl;
741     external 'PMWSock' index 6;
742 
getsockoptnull743 Function getsockopt(s: TSocket; level, optname: LongInt;Var optval; Var optlen: LongInt): LongInt; cdecl;
744     external 'PMWSock' index 7;
getsockoptnull745 Function getsockopt(s: TSocket; level: Longint; optname: Longint; optval:pchar;var optlen: Longint): Longint; cdecl;
746     external 'PMWSock' index 7;
747 
htonlnull748 Function htonl(hostlong: Cardinal): Cardinal; cdecl;
749     external 'PMWSock' index 8;
750 
htonsnull751 Function htons(hostshort: Word): Word; cdecl;
752     external 'PMWSock' index 9;
753 
inet_addrnull754 Function inet_addr(cp: pchar): Cardinal; cdecl;
755     external 'PMWSock' index 10;
756 
inet_ntoanull757 Function inet_ntoa(Var _in: in_addr): PChar; cdecl;
758     external 'PMWSock' index 11;
inet_ntoanull759 Function inet_ntoa(i: PInAddr): pchar; cdecl;
760     external 'PMWSock' index 11;
761 
listennull762 Function listen(s: TSocket; backlog: LongInt): LongInt; cdecl;
763     external 'PMWSock' index 13;
764 
ntohlnull765 Function ntohl(netlong: Cardinal): Cardinal; cdecl;
766     external 'PMWSock' index 14;
767 
ntohsnull768 Function ntohs(netshort: Word): Word; cdecl;
769     external 'PMWSock' index 15;
770 
recvnull771 Function recv(s: TSocket;Var Buf; len, flags: LongInt): LongInt; cdecl;
772     external 'PMWSock' index 16;
recvnull773 Function recv(s: TSocket; buf:pchar; len: Longint; flags: Longint): Longint; cdecl;
774     external 'PMWSock' index 16;
775 
recvfromnull776 Function recvfrom(s: TSocket; Var Buf: PChar; len, flags:LongInt;
777                          Var from: sockaddr; Var fromLen: LongInt): LongInt; cdecl;
778     external 'PMWSock' index 17;
recvfromnull779 Function recvfrom(s: TSocket; buf:pchar; len: Longint; flags: Longint;
780                          from: PSockAddr; fromlen: Longint): Longint; cdecl;
781     external 'PMWSock' index 17;
recvfromnull782 Function recvfrom(s: TSocket; var buf; len: Longint; flags: Longint;
783                          Const from: TSockAddr; var fromlen: Longint): Longint; cdecl;
784     external 'PMWSock' index 17;
785 
selectnull786 Function select(nfds: LongInt; Var readfds, writefds, exceptfds: fdset;
787                        Const timeout: timeval): LongInt; cdecl;
788     external 'PMWSock' index 18;
selectnull789 Function select(nfds: Longint; readfds, writefds, exceptfds : PFDSet;
790                        timeout: PTimeVal): Longint; cdecl;
791     external 'PMWSock' index 18;
792 
sendnull793 Function send(s: TSocket; Const Buf: PChar; len, flags: LongInt): LongInt; cdecl;
794     external 'PMWSock' index 19;
795 
sendtonull796 Function sendto(s: TSocket; Const Buf: PChar; len, flags: LongInt;
797                     Const _to: sockaddr; tolen: LongInt): LongInt; cdecl;
798     external 'PMWSock' index 20;
sendtonull799 Function sendto(s: TSocket; buf: pchar; len: Longint; flags: Longint;
800                     toaddr: PSockAddr; tolen: Longint): Longint; cdecl;
801     external 'PMWSock' index 20;
802 
setsockoptnull803 Function setsockopt(s: TSocket; level: Longint; optname: Longint;
804                            optval: pchar; optlen: Longint): Longint; cdecl;
805     external 'PMWSock' index 21;
806 
shutdownnull807 Function shutdown(s: TSocket; how: LongInt): LongInt; cdecl;
808     external 'PMWSock' index 22;
809 
socketnull810 Function socket(af, typ, protocol: LongInt): TSocket; cdecl;
811     external 'PMWSock' index 23;
812 
prototypesnull813 // Database function prototypes
814 
815 Function gethostbyaddr(addr: pchar; len: Longint; t: Longint): PHostEnt; cdecl;
816     external 'PMWSock' index 51;
817 
gethostbynamenull818 Function gethostbyname(name: pchar): PHostEnt; cdecl;
819     external 'PMWSock' index 52;
820 
gethostnamenull821 Function gethostname(name: pchar; namelen: Longint): Longint; cdecl;
822     external 'PMWSock' index 57;
823 
getservbyportnull824 Function getservbyport(port: Longint; proto: pchar): PServEnt; cdecl;
825     external 'PMWSock' index 56;
826 
getservbynamenull827 Function getservbyname(name: pchar; proto: pchar): PServEnt; cdecl;
828     external 'PMWSock' index 55;
829 
getprotobynumbernull830 Function getprotobynumber(proto: LongInt): pprotoent; cdecl;
831     external 'PMWSock' index 54;
832 
getprotobynamenull833 Function getprotobyname(name: pchar): PProtoEnt; cdecl;
834     external 'PMWSock' index 53;
835 
prototypesnull836 // Microsoft Windows Extension function prototypes
837 
838 Function WSAStartup(wVersionRequired: Word;Var aWSAData: WSAData): LongInt; cdecl;
839     external 'PMWSock' index 115;
840 
WSACleanupnull841 Function WSACleanup: LongInt; cdecl;
842     external 'PMWSock' index 116;
843 
844 Procedure WSASetLastError(iError: LongInt); cdecl;
845     external 'PMWSock' index 112;
846 
WSAGetLastErrornull847 Function WSAGetLastError: LongInt; cdecl;
848     external 'PMWSock' index 111;
849 
WSAIsBlockingnull850 Function WSAIsBlocking: Longbool; cdecl;
851     external 'PMWSock' index 114;
852 
WSAUnhookBlockingHooknull853 Function WSAUnhookBlockingHook: LongInt; cdecl;
854     external 'PMWSock' index 110;
855 
WSASetBlockingHooknull856 Function WSASetBlockingHook(lpBlockFunc: Pointer): Pointer; cdecl;
857     external 'PMWSock' index 109;
858 
WSACancelBlockingCallnull859 Function WSACancelBlockingCall: LongInt; cdecl;
860     external 'PMWSock' index 113;
861 
WSAAsyncGetServByNamenull862 Function WSAAsyncGetServByName(hWnd: HWND; wMsg: Cardinal;
863                                      name: pchar; proto: pchar;
864                                      buf: pchar;
865                                      buflen: Longint): Cardinal; cdecl;
866     external 'PMWSock' index 107;
867 
WSAAsyncGetServByPortnull868 Function WSAAsyncGetServByPort(hWnd: HWND; wMsg: Cardinal;
869                                       port: Longint;
870                                       proto: pchar; buf: pchar;
871                                       buflen: Longint): Cardinal; cdecl;
872     external 'PMWSock' index 106;
873 
WSAAsyncGetProtoByNamenull874 Function WSAAsyncGetProtoByName(hWnd: HWND; wMsg: Cardinal;
875                                        name: pchar; buf: pchar;
876                                        buflen: Longint): Cardinal; cdecl;
877     external 'PMWSock' index 105;
878 
WSAAsyncGetProtoByNumbernull879 Function WSAAsyncGetProtoByNumber(hWnd: HWND; wMsg: Cardinal;
880                                          number: Longint;
881                                          buf: pchar;
882                                          buflen: Longint): Cardinal; cdecl;
883     external 'PMWSock' index 104;
884 
WSAAsyncGetHostByNamenull885 Function WSAAsyncGetHostByName(hWnd: HWND; wMsg: Cardinal;
886                                       name: pchar; buf: pchar;
887                                       buflen: Longint): Cardinal; cdecl;
888     external 'PMWSock' index 103;
889 
WSAAsyncGetHostByAddrnull890 Function WSAAsyncGetHostByAddr(hWnd: HWND; wMsg: Cardinal;
891                                       addr: pchar; len: Longint; t: Longint;
892                                       buf: pchar; buflen: Longint): Cardinal; cdecl;
893     external 'PMWSock' index 102;
894 
WSACancelAsyncRequestnull895 Function WSACancelAsyncRequest(hAsyncTaskHandle: Cardinal): LongInt; cdecl;
896     external 'PMWSock' index 108;
897 
WSAAsyncSelectnull898 Function WSAAsyncSelect(s: TSocket; ahWnd: HWND; wMsg: Cardinal; lEvent: LongInt): Cardinal; cdecl;
899     external 'PMWSock' index 101;
900 
901 // Windows message parameter composition and decomposition
902 // macros.
903 //
904 // WSAMAKEASYNCREPLY is intended for use by the Windows Sockets implementation
905 // when constructing the response to a WSAAsyncGetXByY() routine.
906 
WSAMakeAsyncReplynull907 Function WSAMakeAsyncReply(Buflen,Error:Word):dword;
908 // Seems to be error in rtl\win32\winsock.pp
WSAMakeSyncReplynull909 Function WSAMakeSyncReply(Buflen,Error:Word):dword;
910 
911 // WSAMAKESELECTREPLY is intended for use by the Windows Sockets implementation
912 // when constructing the response to WSAAsyncSelect().
913 
WSAMakeSelectReplynull914 Function WSAMakeSelectReply(Event,Error:Word):dword;
915 
916 // WSAGETASYNCBUFLEN is intended for use by the Windows Sockets application
917 // to extract the buffer length from the lParam in the response
918 // to a WSAGetXByY().
919 
WSAGetAsyncBuflennull920 Function WSAGetAsyncBuflen(Param:dword):Word;
921 
922 //
923 // WSAGETASYNCERROR is intended for use by the Windows Sockets application
924 // to extract the error code from the lParam in the response
925 // to a WSAGetXByY().
926 
WSAGetAsyncErrornull927 Function WSAGetAsyncError(Param:dword):Word;
928 
929 // WSAGETSELECTEVENT is intended for use by the Windows Sockets application
930 // to extract the event code from the lParam in the response
931 // to a WSAAsyncSelect().
932 
WSAGetSelectEventnull933 Function WSAGetSelectEvent(Param:dword):Word;
934 
935 // WSAGETSELECTERROR is intended for use by the Windows Sockets application
936 // to extract the error code from the lParam in the response
937 // to a WSAAsyncSelect().
938 
WSAGetSelectErrornull939 Function WSAGetSelectError(Param:dword):Word;
940 
941 Procedure FD_ZERO(var aset: fdset);
942 
943 // Following functions not found in PMWSock
944 {
945     function WSARecvEx(s:TSocket;var buf; len:tOS_INT; flags:ptOS_INT):tOS_INT;stdcall;
946       external winsockdll name 'WSARecvEx';
947     function TransmitFile(hSocket:TSocket; hFile:THandle; nNumberOfBytesToWrite:dword;
948                           nNumberOfBytesPerSend:DWORD; lpOverlapped:POverlapped;
949                           lpTransmitBuffers:PTransmitFileBuffers; dwReserved:dword):Bool;stdcall;
950                           external winsockdll name 'TransmitFile';
951 
952     function AcceptEx(sListenSocket,sAcceptSocket:TSocket;
953                       lpOutputBuffer:Pointer; dwReceiveDataLength,dwLocalAddressLength,
954                       dwRemoteAddressLength:dword; var lpdwBytesReceived:dword;
955                       lpOverlapped:POverlapped):Bool;stdcall;
956                       external winsockdll name 'AcceptEx';
957 
958     procedure GetAcceptExSockaddrs(lpOutputBuffer:Pointer;
959                                    dwReceiveDataLength,dwLocalAddressLength,dwRemoteAddressLength:dword;
960                                    var LocalSockaddr:TSockAddr; var LocalSockaddrLength:tOS_INT;
961                                    var RemoteSockaddr:TSockAddr; var RemoteSockaddrLength:tOS_INT);stdcall;
962                                    external winsockdll name 'GetAcceptExSockaddrs';
963 }
964 
965 Implementation
966 
967 Procedure FD_CLR(ASocket: TSocket; var aset: fdset);
968 var
969   I: Cardinal;
970 begin
971   I := 0;
972   while I <= aset.fd_count do
973   begin
974     if (aset.fd_array[i] = ASocket) then
975     begin
976       while (i < (aset.fd_count-1)) do
977       begin
978         aset.fd_array[I]:=aset.fd_array[i+1];
979         Inc(I);
980       end;
981       Dec(aset.fd_count);
982       break;
983     end;
984     Inc (I);
985   end;
986 end;
987 
988 Procedure FD_ZERO(var aset: fdset);
989 Begin
990   aset.fd_count:=0;
991 End;
992 
993 procedure FD_SET(Socket: TSocket; var FDSet: tfdset);
994 begin
995   if FDSet.fd_count < FD_SETSIZE then
996   begin
997     FDSet.fd_array[FDSet.fd_count] := Socket;
998     Inc(FDSet.fd_count);
999   end;
1000 end;
1001 
MAKELONGnull1002 Function MAKELONG(a,b : longint) : LONGINT;
1003 begin
1004   MAKELONG:=LONGINT((WORD(a)) or ((CARDINAL(WORD(b))) shl 16));
1005 end;
1006 
WSAMakeAsyncReplynull1007 Function WSAMakeAsyncReply(Buflen,Error:Word):dword;
1008 begin
1009   WSAMakeAsyncReply:=MakeLong(Buflen, Error);
1010 end;
1011 
WSAMakeSyncReplynull1012 Function WSAMakeSyncReply(Buflen,Error:Word):dword;
1013 begin
1014   WSAMakeSyncReply:=WSAMakeAsyncReply(Buflen,Error);
1015 end;
1016 
WSAMakeSelectReplynull1017 Function WSAMakeSelectReply(Event,Error:Word):dword;
1018 begin
1019   WSAMakeSelectReply:=MakeLong(Event,Error);
1020 end;
1021 
WSAGetAsyncBuflennull1022 Function WSAGetAsyncBuflen(Param:dword):Word;
1023 begin
1024   WSAGetAsyncBuflen:=lo(Param);
1025 end;
1026 
WSAGetAsyncErrornull1027 Function WSAGetAsyncError(Param:dword):Word;
1028 begin
1029   WSAGetAsyncError:=hi(Param);
1030 end;
1031 
WSAGetSelectEventnull1032 Function WSAGetSelectEvent(Param:dword):Word;
1033 begin
1034   WSAGetSelectEvent:=lo(Param);
1035 end;
1036 
WSAGetSelectErrornull1037 Function WSAGetSelectError(Param:dword):Word;
1038 begin
1039   WSAGetSelectError:=hi(Param);
1040 end;
1041 
timerissetnull1042 Function timerisset(tvp: timeval): Boolean;
1043 Begin
1044   TimerIsSet:=Boolean(tvp.tv_sec or tvp.tv_usec);
1045 End;
1046 
1047 (*
1048 Function timercmp(tvp, uvp, cmp): Boolean;
1049 Begin
1050         ((tvp)->tv_sec cmp (uvp)->tv_sec || \
1051          (tvp)->tv_sec == (uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec)
1052 End;
1053 *)
1054 
1055 Procedure timerclear(var tvp: timeval);
1056 begin
1057   tvp.tv_sec:=0;
1058   tvp.tv_usec:=0;
1059 end;
1060 
1061 end.
1062