winsock.pp 34 KB

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