winsock.pp 31 KB

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