2
0

pmwsock.pas 35 KB

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