pmwsock.pas 36 KB

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