winsock.pp 34 KB

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