pmwsock.pas 35 KB

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