winsock.pp 32 KB

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