winsock.pp 32 KB

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