pmwsock.pas 35 KB

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