winsock.pp 35 KB

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