winsock.pp 32 KB

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