so32dll.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. {
  2. /c/ 2000, 2001 by madded2 ([email protected]).
  3. (C) 2002, 2004 Yuri Prokushev ([email protected]
  4. Inet & Sockets Unit v1.04.
  5. /c/ 2000, 2001 by madded2 ([email protected]).
  6. based on units from SIBYL & infos from Toolkit 4.0.
  7. for help use tcppr.inf and C samples from toolkit.
  8. without res_* and dh_* funcs, and have very
  9. bad suppot for select() and ioctl() funcs
  10. new in ver 1.04 : little ioctl() & iptrace support + errors SOCE* constants
  11. new in ver 1.03 : used inet_lib.lib file for fixing VP linker bug
  12. new in ver 1.02 : $saves sections, need for correct registers operations
  13. new in ver 1.01 : ip header struct
  14. }
  15. {
  16. @abstract(a unit to handle sockets)
  17. @author(Yuri Prokushev ([email protected]))
  18. @author(madded2 ([email protected]))
  19. @created(3 Sep 2002)
  20. @lastmod(23 Sep 2002)
  21. @todo(sys/ioctl.h, sys/ioctlos2.h, sys/itypes.h)
  22. This is functions from SO32DLL.DLL. These functions allows to use
  23. protocol-independed sockets. Equal to SYS\SOCKET.H, NERRNO.H, SYS\SYSCTL.H.
  24. }
  25. unit SO32Dll;
  26. interface
  27. {$MODE ObjFPC}
  28. {$ASMMODE Intel}
  29. (***************************************************************************)
  30. (* *)
  31. (* Types *)
  32. (* *)
  33. (***************************************************************************)
  34. const
  35. // stream socket
  36. SOCK_STREAM = 1;
  37. // datagram socket
  38. SOCK_DGRAM = 2;
  39. // raw-protocol interface
  40. SOCK_RAW = 3;
  41. // reliably-delivered message
  42. SOCK_RDM = 4;
  43. // sequenced packet stream
  44. SOCK_SEQPACKET = 5;
  45. (***************************************************************************)
  46. (* *)
  47. (* Option flags per-socket *)
  48. (* *)
  49. (***************************************************************************)
  50. const
  51. // turn on debugging info recording
  52. SO_DEBUG = $0001;
  53. // socket has had listen()
  54. SO_ACCEPTCONN = $0002;
  55. // allow local address reuse
  56. SO_REUSEADDR = $0004;
  57. // keep connections alive
  58. SO_KEEPALIVE = $0008;
  59. // just use interface addresses
  60. SO_DONTROUTE = $0010;
  61. // permit sending of broadcast msgs
  62. SO_BROADCAST = $0020;
  63. // bypass hardware when possible
  64. SO_USELOOPBACK = $0040;
  65. // linger on close if data present
  66. SO_LINGER = $0080;
  67. // leave received OOB data in line
  68. SO_OOBINLINE = $0100;
  69. // limited broadcast sent on all IFs
  70. SO_L_BROADCAST = $0200;
  71. // set if shut down called for rcv
  72. SO_RCV_SHUTDOWN = $0400;
  73. // set if shutdown called for send
  74. SO_SND_SHUTDOWN = $0800;
  75. // allow local address & port reuse
  76. SO_REUSEPORT = $1000;
  77. // allow t/tcp on socket
  78. SO_TTCP = $2000;
  79. (***************************************************************************)
  80. (* *)
  81. (* Additional options, not kept in so_options *)
  82. (* *)
  83. (***************************************************************************)
  84. const
  85. // send buffer size
  86. SO_SNDBUF = $1001;
  87. // receive buffer size
  88. SO_RCVBUF = $1002;
  89. // send low-water mark
  90. SO_SNDLOWAT = $1003;
  91. // receive low-water mark
  92. SO_RCVLOWAT = $1004;
  93. // send timeout
  94. SO_SNDTIMEO = $1005;
  95. // receive timeout
  96. SO_RCVTIMEO = $1006;
  97. // get error status and clear
  98. SO_ERROR = $1007;
  99. // get socket type
  100. SO_TYPE = $1008;
  101. // get socket options
  102. SO_OPTIONS = $1010;
  103. (***************************************************************************)
  104. (* *)
  105. (* Structure used for manipulating linger option *)
  106. (* *)
  107. (***************************************************************************)
  108. type
  109. //Structure used for manipulating linger option
  110. linger = record
  111. l_onoff : Longint; // option on/off
  112. l_linger : Longint; // linger time
  113. end;
  114. (***************************************************************************)
  115. (* *)
  116. (* Level number for (get/set)sockopt() to apply to socket itself *)
  117. (* *)
  118. (***************************************************************************)
  119. const
  120. // options for socket level
  121. SOL_SOCKET = $ffff;
  122. (***************************************************************************)
  123. (* *)
  124. (* Address families *)
  125. (* *)
  126. (***************************************************************************)
  127. const
  128. // unspecified
  129. AF_UNSPEC = 0;
  130. // local to host (pipes, portals)
  131. AF_LOCAL = 1;
  132. // backward compatibility
  133. AF_UNIX = AF_LOCAL;
  134. AF_OS2 = AF_UNIX;
  135. // internetwork: UDP, TCP, etc.
  136. AF_INET = 2;
  137. // arpanet imp addresses
  138. AF_IMPLINK = 3;
  139. // pup protocols: e.g. BSP
  140. AF_PUP = 4;
  141. // mit CHAOS protocols
  142. AF_CHAOS = 5;
  143. // XEROX NS protocols
  144. AF_NS = 6;
  145. // ISO protocols
  146. AF_ISO = 7;
  147. // ISO protocols
  148. AF_OSI = AF_ISO;
  149. // european computer manufacturers
  150. AF_ECMA = 8;
  151. // datakit protocols
  152. AF_DATAKIT = 9;
  153. // CCITT protocols, X.25 etc
  154. AF_CCITT = 10;
  155. // IBM SNA
  156. AF_SNA = 11;
  157. // DECnet
  158. AF_DECnet = 12;
  159. // DEC Direct data link interface
  160. AF_DLI = 13;
  161. // LAT
  162. AF_LAT = 14;
  163. // NSC Hyperchannel
  164. AF_HYLINK = 15;
  165. // Apple Talk
  166. AF_APPLETALK = 16;
  167. // Netbios
  168. AF_NB = 17;
  169. // Netbios
  170. AF_NETBIOS = AF_NB;
  171. // Link layer interface
  172. AF_LINK = 18;
  173. // eXpress Transfer Protocol (no AF)
  174. pseudo_AF_XTP = 19;
  175. // connection-oriented IP, aka ST II
  176. AF_COIP = 20;
  177. // Computer Network Technology
  178. AF_CNT = 21;
  179. // Help Identify RTIP packets
  180. pseudo_AF_RTIP = 22;
  181. // Novell Internet Protocol
  182. AF_IPX = 23;
  183. // Simple Internet Protocol
  184. AF_SIP = 24;
  185. AF_INET6 = 24;
  186. // Help Identify PIP packets
  187. pseudo_AF_PIP = 25;
  188. // Internal Routing Protocol
  189. AF_ROUTE = 39;
  190. // firewall support
  191. AF_FWIP = 40;
  192. // IPSEC and encryption techniques
  193. AF_IPSEC = 41;
  194. // DES
  195. AF_DES = 42;
  196. AF_MD5 = 43;
  197. AF_CDMF = 44;
  198. AF_MAX = 45;
  199. (***************************************************************************)
  200. (* *)
  201. (* Structure used by kernel to store most addresses *)
  202. (* *)
  203. (***************************************************************************)
  204. type
  205. // Structure used by kernel to store most addresses
  206. sockaddr = record
  207. sa_len: Byte; // total length
  208. sa_family: Byte; // address family
  209. sa_data: array [0..13] of Byte; // up to 14 bytes of direct address
  210. end;
  211. psockaddr = ^sockaddr;
  212. (***************************************************************************)
  213. (* *)
  214. (* Structure used by kernel to pass protocol information in raw sockets *)
  215. (* *)
  216. (***************************************************************************)
  217. type
  218. // Structure used by kernel to pass protocol information in raw sockets
  219. sockproto = record
  220. sp_family: Word; // address family
  221. sp_protocol: Word; // protocol
  222. end;
  223. (***************************************************************************)
  224. (* *)
  225. (* Protocol families, same as address families for now *)
  226. (* *)
  227. (***************************************************************************)
  228. const
  229. PF_UNSPEC = AF_UNSPEC;
  230. PF_LOCAL = AF_LOCAL;
  231. PF_UNIX = AF_UNIX;
  232. PF_OS2 = AF_OS2;
  233. PF_INET = AF_INET;
  234. PF_IMPLINK = AF_IMPLINK;
  235. PF_PUP = AF_PUP;
  236. PF_CHAOS = AF_CHAOS;
  237. PF_NS = AF_NS;
  238. PF_ISO = AF_ISO;
  239. PF_OSI = AF_OSI;
  240. PF_ECMA = AF_ECMA;
  241. PF_DATAKIT = AF_DATAKIT;
  242. PF_CCITT = AF_CCITT;
  243. PF_SNA = AF_SNA;
  244. PF_DECnet = AF_DECnet;
  245. PF_DLI = AF_DLI;
  246. PF_LAT = AF_LAT;
  247. PF_HYLINK = AF_HYLINK;
  248. PF_APPLETALK = AF_APPLETALK;
  249. PF_NETBIOS = AF_NB;
  250. PF_NB = AF_NB;
  251. PF_ROUTE = AF_ROUTE;
  252. PF_LINK = AF_LINK;
  253. // really just proto family, no AF
  254. PF_XTP = pseudo_AF_XTP;
  255. PF_COIP = AF_COIP;
  256. PF_CNT = AF_CNT;
  257. PF_SIP = AF_SIP;
  258. PF_INET6 = AF_INET6;
  259. // same format as AF_NS
  260. PF_IPX = AF_IPX;
  261. // same format as AF_INET
  262. PF_RTIP = pseudo_AF_RTIP;
  263. PF_PIP = pseudo_AF_PIP;
  264. PF_MAX = AF_MAX;
  265. (***************************************************************************)
  266. (* *)
  267. (* Definitions for sysctl call. The sysctl call uses a hierarchical name *)
  268. (* for objects that can be examined or modified. The name is expressed as *)
  269. (* a sequence of integers. Like a file path name, the meaning of each *)
  270. (* component depends on its place in the hierarchy. The top-level and kern *)
  271. (* identifiers are defined here, and other identifiers are defined in the *)
  272. (* respective subsystem header files. *)
  273. (* *)
  274. (***************************************************************************)
  275. const
  276. // largest number of components supported
  277. CTL_MAXNAME = 12;
  278. (***************************************************************************)
  279. (* *)
  280. (* Each subsystem defined by sysctl defines a list of variables *)
  281. (* for that subsystem. Each name is either a node with further *)
  282. (* levels defined below it, or it is a leaf of some particular *)
  283. (* type given below. Each sysctl level defines a set of name/type *)
  284. (* pairs to be used by sysctl(1) in manipulating the subsystem. *)
  285. (* *)
  286. (***************************************************************************)
  287. type
  288. ctlname=record
  289. ctl_name: PChar; // subsystem name
  290. ctl_type: Longint; // type of name
  291. end;
  292. const
  293. // name is a node
  294. CTLTYPE_NODE =1;
  295. // name describes an integer
  296. CTLTYPE_INT =2;
  297. // name describes a string
  298. CTLTYPE_STRING =3;
  299. // name describes a 64-bit number
  300. CTLTYPE_QUAD =4;
  301. // name describes a structure
  302. CTLTYPE_STRUCT =5;
  303. // inetcfg sysctl code
  304. CTLTYPE_INETCFG =6;
  305. // inetver sysctl code
  306. CTLTYPE_INEVER =7;
  307. (*
  308. * Top-level identifiers
  309. *)
  310. const
  311. // "high kernel": proc, limits
  312. CTL_KERN = 1;
  313. // network, see socket.h
  314. CTL_NET = 4;
  315. // OS/2 specific codes
  316. CTL_OS2 = 9;
  317. (*
  318. #define CTL_NAMES { \
  319. { 0, 0 }, \
  320. { "kern", CTLTYPE_NODE }, \
  321. { "net", CTLTYPE_NODE }, \
  322. { "os2", CTLTYPE_NODE }, \
  323. }
  324. /*
  325. * CTL_KERN identifiers
  326. */
  327. #define KERN_MAXFILES 7 /* int: max open files */
  328. #define KERN_HOSTNAME 10 /* string: hostname */
  329. #define KERN_HOSTID 11 /* int: host identifier */
  330. #define CTL_KERN_NAMES { \
  331. { 0, 0 }, \
  332. { "ostype", CTLTYPE_STRING }, \
  333. { "osrelease", CTLTYPE_STRING }, \
  334. { "osrevision", CTLTYPE_INT }, \
  335. { "version", CTLTYPE_STRING }, \
  336. { "maxvnodes", CTLTYPE_INT }, \
  337. { "maxproc", CTLTYPE_INT }, \
  338. { "maxfiles", CTLTYPE_INT }, \
  339. { "argmax", CTLTYPE_INT }, \
  340. { "securelevel", CTLTYPE_INT }, \
  341. { "hostname", CTLTYPE_STRING }, \
  342. { "hostid", CTLTYPE_INT }, \
  343. { "clockrate", CTLTYPE_STRUCT }, \
  344. { "vnode", CTLTYPE_STRUCT }, \
  345. { "proc", CTLTYPE_STRUCT }, \
  346. { "file", CTLTYPE_STRUCT }, \
  347. { "profiling", CTLTYPE_NODE }, \
  348. { "posix1version", CTLTYPE_INT }, \
  349. { "ngroups", CTLTYPE_INT }, \
  350. { "job_control", CTLTYPE_INT }, \
  351. { "saved_ids", CTLTYPE_INT }, \
  352. { "boottime", CTLTYPE_STRUCT }, \
  353. }
  354. /*
  355. * KERN_SYSCTL objects
  356. */
  357. #define KERNCTL_INETVER 70 /* Sysctl code for sockets Inetversion */
  358. #define OS2_MEMMAPIO 1 /* memory map io */
  359. #define OS2_QUERY_MEMMAPIO 2 /* Query if mapped memory usable */
  360. /* Generic Structure for Inetcfg calls */
  361. struct inetcfg_ctl{
  362. unsigned long var_name;
  363. unsigned long var_cur_val;
  364. unsigned long var_max_val;
  365. unsigned long var_def_val;
  366. unsigned long var_min_val;
  367. };
  368. /* Inetversion */
  369. struct inetvers_ctl {
  370. float version;
  371. char versionstr[10]; /* Less than 10 chars in version string */
  372. };
  373. #include <sys/cdefs.h>
  374. #ifndef KERNEL
  375. __BEGIN_DECLS
  376. int _System sysctl __TCPPROTO((int *, u_int, void *, size_t *, void *, size_t));
  377. __END_DECLS
  378. #endif
  379. *)
  380. (* !!TODO!! Not finished yet!!
  381. /*
  382. * Definitions for network related sysctl, CTL_NET.
  383. *
  384. * Second level is protocol family.
  385. * Third level is protocol number.
  386. *
  387. * Further levels are defined by the individual families below.
  388. */
  389. const
  390. NET_MAXID = AF_MAX;
  391. #define CTL_NET_NAMES { \
  392. { 0, 0 }, \
  393. { "local", CTLTYPE_NODE }, \
  394. { "inet", CTLTYPE_NODE }, \
  395. { "implink", CTLTYPE_NODE }, \
  396. { "pup", CTLTYPE_NODE }, \
  397. { "chaos", CTLTYPE_NODE }, \
  398. { "xerox_ns", CTLTYPE_NODE }, \
  399. { "iso", CTLTYPE_NODE }, \
  400. { "emca", CTLTYPE_NODE }, \
  401. { "datakit", CTLTYPE_NODE }, \
  402. { "ccitt", CTLTYPE_NODE }, \
  403. { "ibm_sna", CTLTYPE_NODE }, \
  404. { "decnet", CTLTYPE_NODE }, \
  405. { "dec_dli", CTLTYPE_NODE }, \
  406. { "lat", CTLTYPE_NODE }, \
  407. { "hylink", CTLTYPE_NODE }, \
  408. { "appletalk", CTLTYPE_NODE }, \
  409. { "netbios", CTLTYPE_NODE }, \
  410. { "route", CTLTYPE_NODE }, \
  411. { "link_layer", CTLTYPE_NODE }, \
  412. { "xtp", CTLTYPE_NODE }, \
  413. { "coip", CTLTYPE_NODE }, \
  414. { "cnt", CTLTYPE_NODE }, \
  415. { "rtip", CTLTYPE_NODE }, \
  416. { "ipx", CTLTYPE_NODE }, \
  417. { "sip", CTLTYPE_NODE }, \
  418. { "pip", CTLTYPE_NODE }, \
  419. }
  420. /*
  421. * PF_ROUTE - Routing table
  422. *
  423. * Three additional levels are defined:
  424. * Fourth: address family, 0 is wildcard
  425. * Fifth: type of info, defined below
  426. * Sixth: flag(s) to mask with for NET_RT_FLAGS
  427. */
  428. const
  429. // dump; may limit to a.f.
  430. NET_RT_DUMP = 1;
  431. // by flags, e.g. RESOLVING
  432. NET_RT_FLAGS = 2;
  433. // survey interface list
  434. NET_RT_IFLIST = 3;
  435. NET_RT_MAXID = 4;
  436. #define CTL_NET_RT_NAMES { \
  437. { 0, 0 }, \
  438. { "dump", CTLTYPE_STRUCT }, \
  439. { "flags", CTLTYPE_STRUCT }, \
  440. { "iflist", CTLTYPE_STRUCT }, \
  441. }
  442. *)
  443. (***************************************************************************)
  444. (* *)
  445. (* Maximum queue length specifiable by listen *)
  446. (* *)
  447. (***************************************************************************)
  448. const
  449. // Maximum queue length specifiable by listen
  450. SOMAXCONN = 1024;
  451. (***************************************************************************)
  452. (* *)
  453. (* Message header for recvmsg and sendmsg calls *)
  454. (* Used value-result for recvmsg, value only for sendmsg *)
  455. (* *)
  456. (***************************************************************************)
  457. type
  458. iovec = record
  459. iov_base : Pointer;
  460. iov_len : Longint;
  461. end;
  462. // Message header for recvmsg and sendmsg calls
  463. msghdr = record
  464. msg_name: pChar; // optional address
  465. msg_namelen: Longint; // size of address
  466. msg_iov: ^iovec; // scatter/gather array
  467. msg_iovlen: Longint; // # elements in msg_iov (max 1024)
  468. msg_control: pChar; // ancillary data, see below
  469. msg_controllen: Longint; // ancillary data buffer len
  470. msg_flags: Longint; // flags on received message
  471. end;
  472. const
  473. // process out-of-band data
  474. MSG_OOB = $1;
  475. // peek at incoming message
  476. MSG_PEEK = $2;
  477. // send without using routing tables
  478. MSG_DONTROUTE = $4;
  479. // send without using routing tables
  480. MSG_FULLREAD = $8;
  481. // data completes record
  482. MSG_EOR = $10;
  483. // data discarded before delivery
  484. MSG_TRUNC = $20;
  485. // control data lost before delivery
  486. MSG_CTRUNC = $40;
  487. // wait for full request or error
  488. MSG_WAITALL = $80;
  489. // this message should be nonblocking
  490. MSG_DONTWAIT = $100;
  491. MSG_EOF = $200;
  492. // mem mapped io
  493. MSG_MAPIO = $400;
  494. (***************************************************************************)
  495. (* *)
  496. (* Header for ancillary data objects in msg_control buffer *)
  497. (* Used for additional information with/about a datagram *)
  498. (* not expressible by flags. The format is a sequence *)
  499. (* of message elements headed by cmsghdr structures *)
  500. (* *)
  501. (***************************************************************************)
  502. type
  503. // Header for ancillary data objects in msg_control buffer
  504. cmsghdr = record
  505. cmsg_len: Longint; // data byte count, including hdr
  506. cmsg_level: Longint; // originating protocol
  507. cmsg_type: Longint; // protocol-specific type
  508. end;
  509. cmsg = record
  510. cmsg_hdr: cmsghdr;
  511. cmsg_data: array [0..0] of Byte;
  512. end;
  513. (***************************************************************************)
  514. (* *)
  515. (* "Socket"-level control message types *)
  516. (* *)
  517. (***************************************************************************)
  518. const
  519. // access rights (array of int)
  520. SCM_RIGHTS = $01;
  521. (***************************************************************************)
  522. (* *)
  523. (* 4.3 compat sockaddr, move to compat file later *)
  524. (* *)
  525. (***************************************************************************)
  526. type
  527. // 4.3 compat sockaddr
  528. osockaddr = record
  529. sa_family: Word; // address family
  530. sa_data: array [0..13] of Byte; // up to 14 bytes of direct address
  531. end;
  532. (***************************************************************************)
  533. (* *)
  534. (* 4.3-compat message header (move to compat file later) *)
  535. (* *)
  536. (***************************************************************************)
  537. type
  538. // 4.3-compat message header
  539. omsghdr = record
  540. msg_name: pChar; // optional address
  541. msg_namelen: Longint; // size of address
  542. msg_iov: ^iovec; // scatter/gather array
  543. msg_iovlen: Longint; // # elements in msg_iov
  544. msg_accrights: pChar; // access rights sent/received
  545. msg_accrightslen: Longint;
  546. end;
  547. (* !!TODO
  548. /*
  549. * send_file parameter structure
  550. */
  551. struct sf_parms {
  552. void *header_data; /* ptr to header data */
  553. size_t header_length; /* size of header data */
  554. int file_handle; /* file handle to send from */
  555. size_t file_size; /* size of file */
  556. int file_offset; /* byte offset in file to send from */
  557. size_t file_bytes; /* bytes of file to be sent */
  558. void *trailer_data; /* ptr to trailer data */
  559. size_t trailer_length; /* size of trailer data */
  560. size_t bytes_sent; /* bytes sent in this send_file call */
  561. };
  562. *)
  563. { !!TODO Check is all this functions defined
  564. __BEGIN_DECLS
  565. int _System accept_and_recv __TCPPROTO((long, long*, struct sockaddr *, long*, struct sockaddr*, long*, caddr_t, size_t));
  566. ssize_t _System recvfrom __TCPPROTO((int, void *, size_t, int, struct sockaddr *, int *));
  567. ssize_t _System recvmsg __TCPPROTO((int, struct msghdr *, int));
  568. ssize_t _System send __TCPPROTO((int, const void *, size_t, int));
  569. ssize_t _System sendto __TCPPROTO((int, const void *, size_t, int, const struct sockaddr *, int));
  570. ssize_t _System sendmsg __TCPPROTO((int, const struct msghdr *, int));
  571. ssize_t _System send_file __TCPPROTO((int *, struct sf_parms *, int ));
  572. int _System setsockopt __TCPPROTO((int, int, int, const void *, int));
  573. int _System shutdown __TCPPROTO((int, int));
  574. int _System socket __TCPPROTO((int, int, int));
  575. int _System socketpair __TCPPROTO((int, int, int, int *));
  576. /* OS/2 additions */
  577. int _System sock_init __TCPPROTO((void));
  578. int _System sock_errno __TCPPROTO((void));
  579. void _System psock_errno __TCPPROTO((const char *));
  580. char * _System sock_strerror __TCPPROTO((int));
  581. int _System soabort __TCPPROTO((int));
  582. int _System so_cancel __TCPPROTO((int));
  583. int _System getinetversion __TCPPROTO((char *));
  584. void _System addsockettolist __TCPPROTO((int));
  585. int _System removesocketfromlist __TCPPROTO((int));
  586. /*int _System removesocketfromlist __TCPPROTO((long *));*/ /*changed on 09-30-98 for corresponding change in sockets.c file*/
  587. /* SOCKS additions */
  588. int _System Raccept __TCPPROTO((int, struct sockaddr *, int *));
  589. int _System Rbind __TCPPROTO((int, struct sockaddr *, int, struct sockaddr *));
  590. int _System Rconnect __TCPPROTO((int, const struct sockaddr *, int));
  591. int _System Rgetsockname __TCPPROTO((int, struct sockaddr *, int *));
  592. int _System Rlisten __TCPPROTO((int, int));
  593. __END_DECLS
  594. /* more OS/2 stuff */
  595. const
  596. // should be on free list
  597. MT_FREE = 0;
  598. // dynamic (data) allocation
  599. MT_DATA = 1;
  600. // packet header
  601. MT_HEADER = 2;
  602. // socket structure
  603. MT_SOCKET = 3;
  604. // protocol control block
  605. MT_PCB = 4;
  606. // routing tables
  607. MT_RTABLE = 5;
  608. // IMP host tables
  609. MT_HTABLE = 6;
  610. // address resolution tables
  611. MT_ATABLE = 7;
  612. // socket name
  613. MT_SONAME = 8;
  614. // zombie proc status
  615. MT_ZOMBIE = 9;
  616. // socket options
  617. MT_SOOPTS = 10;
  618. // fragment reassembly header
  619. MT_FTABLE = 11;
  620. // access rights
  621. MT_RIGHTS = 12;
  622. // interface address
  623. MT_IFADDR = 13;
  624. Type
  625. sostats=record
  626. count: integer;
  627. socketdata: array[0..13*MAXSOCKETS-1] of integer;
  628. end;
  629. }
  630. (***************************************************************************)
  631. (* *)
  632. (* SOCE* constants - socket errors from NERRNO.H *)
  633. (* All OS/2 SOCKET API error constants are biased by SOCBASEERR from the *)
  634. (* "normal" *)
  635. (* *)
  636. (***************************************************************************)
  637. const
  638. SOCBASEERR = 10000;
  639. // Not owner
  640. SOCEPERM = (SOCBASEERR+1);
  641. // No such file or directory
  642. SOCENOENT = (SOCBASEERR+2);
  643. // No such process
  644. SOCESRCH = (SOCBASEERR+3);
  645. // Interrupted system call
  646. SOCEINTR = (SOCBASEERR+4);
  647. // Input/output error
  648. SOCEIO = (SOCBASEERR+5);
  649. SOCENXIO = (SOCBASEERR+6); // No such device or address
  650. SOCE2BIG = (SOCBASEERR+7); // Argument list too long
  651. SOCENOEXEC = (SOCBASEERR+8); // Exec format error
  652. SOCEBADF = (SOCBASEERR+9); // Bad file number
  653. SOCECHILD = (SOCBASEERR+10); // No child processes
  654. SOCEDEADLK = (SOCBASEERR+11); // Resource deadlock avoided
  655. SOCENOMEM = (SOCBASEERR+12); // Cannot allocate memory
  656. SOCEACCES = (SOCBASEERR+13); // Permission denied
  657. SOCEFAULT = (SOCBASEERR+14); // Bad address
  658. SOCENOTBLK = (SOCBASEERR+15); // Block device required
  659. SOCEBUSY = (SOCBASEERR+16); // Device busy
  660. SOCEEXIST = (SOCBASEERR+17); // File exists
  661. SOCEXDEV = (SOCBASEERR+18); // Cross-device link
  662. SOCENODEV = (SOCBASEERR+19); // Operation not supported by device
  663. SOCENOTDIR = (SOCBASEERR+20); // Not a directory
  664. SOCEISDIR = (SOCBASEERR+21); // Is a directory
  665. SOCEINVAL = (SOCBASEERR+22); // Invalid argument
  666. SOCENFILE = (SOCBASEERR+23); // Too many open files in system
  667. SOCEMFILE = (SOCBASEERR+24); // Too many open files
  668. SOCENOTTY = (SOCBASEERR+25); // Inappropriate ioctl for device
  669. SOCETXTBSY = (SOCBASEERR+26); // Text file busy
  670. SOCEFBIG = (SOCBASEERR+27); // File too large
  671. SOCENOSPC = (SOCBASEERR+28); // No space left on device
  672. SOCESPIPE = (SOCBASEERR+29); // Illegal seek
  673. SOCEROFS = (SOCBASEERR+30); // Read-only file system
  674. SOCEMLINK = (SOCBASEERR+31); // Too many links
  675. SOCEPIPE = (SOCBASEERR+32); // Broken pipe
  676. // math software
  677. SOCEDOM = (SOCBASEERR+33); // Numerical argument out of domain
  678. SOCERANGE = (SOCBASEERR+34); // Result too large
  679. // non-blocking and interrupt i/o
  680. SOCEAGAIN = (SOCBASEERR+35); // Resource temporarily unavailable
  681. SOCEWOULDBLOCK = SOCEAGAIN; // Operation would block
  682. SOCEINPROGRESS = (SOCBASEERR+36); // Operation now in progress
  683. SOCEALREADY = (SOCBASEERR+37); // Operation already in progress
  684. // ipc/network software -- argument errors
  685. SOCENOTSOCK = (SOCBASEERR+38); // Socket operation on non-socket
  686. SOCEDESTADDRREQ = (SOCBASEERR+39); // Destination address required
  687. SOCEMSGSIZE = (SOCBASEERR+40); // Message too long
  688. SOCEPROTOTYPE = (SOCBASEERR+41); // Protocol wrong type for socket
  689. SOCENOPROTOOPT = (SOCBASEERR+42); // Protocol not available
  690. SOCEPROTONOSUPPORT = (SOCBASEERR+43); // Protocol not supported
  691. SOCESOCKTNOSUPPORT = (SOCBASEERR+44); // Socket type not supported
  692. SOCEOPNOTSUPP = (SOCBASEERR+45); // Operation not supported
  693. SOCEPFNOSUPPORT = (SOCBASEERR+46); // Protocol family not supported
  694. SOCEAFNOSUPPORT = (SOCBASEERR+47); // Address family not supported by protocol family
  695. SOCEADDRINUSE = (SOCBASEERR+48); // Address already in use
  696. SOCEADDRNOTAVAIL = (SOCBASEERR+49); // Can't assign requested address
  697. // ipc/network software -- operational errors
  698. SOCENETDOWN = (SOCBASEERR+50); // Network is down
  699. SOCENETUNREACH = (SOCBASEERR+51); // Network is unreachable
  700. SOCENETRESET = (SOCBASEERR+52); // Network dropped connection on reset
  701. SOCECONNABORTED = (SOCBASEERR+53); // Software caused connection abort
  702. SOCECONNRESET = (SOCBASEERR+54); // Connection reset by peer
  703. SOCENOBUFS = (SOCBASEERR+55); // No buffer space available
  704. SOCEISCONN = (SOCBASEERR+56); // Socket is already connected
  705. SOCENOTCONN = (SOCBASEERR+57); // Socket is not connected
  706. SOCESHUTDOWN = (SOCBASEERR+58); // Can't send after socket shutdown
  707. SOCETOOMANYREFS = (SOCBASEERR+59); // Too many references: can't splice
  708. SOCETIMEDOUT = (SOCBASEERR+60); // Operation timed out
  709. SOCECONNREFUSED = (SOCBASEERR+61); // Connection refused
  710. SOCELOOP = (SOCBASEERR+62); // Too many levels of symbolic links
  711. SOCENAMETOOLONG = (SOCBASEERR+63); // File name too long
  712. // should be rearranged
  713. SOCEHOSTDOWN = (SOCBASEERR+64); // Host is down
  714. SOCEHOSTUNREACH = (SOCBASEERR+65); // No route to host
  715. SOCENOTEMPTY = (SOCBASEERR+66); // Directory not empty
  716. // quotas & mush
  717. SOCEPROCLIM = (SOCBASEERR+67); // Too many processes
  718. SOCEUSERS = (SOCBASEERR+68); // Too many users
  719. SOCEDQUOT = (SOCBASEERR+69); // Disc quota exceeded
  720. // Network File System
  721. SOCESTALE = (SOCBASEERR+70); // Stale NFS file handle
  722. SOCEREMOTE = (SOCBASEERR+71); // Too many levels of remote in path
  723. SOCEBADRPC = (SOCBASEERR+72); // RPC struct is bad
  724. SOCERPCMISMATCH = (SOCBASEERR+73); // RPC version wrong
  725. SOCEPROGUNAVAIL = (SOCBASEERR+74); // RPC prog. not avail
  726. SOCEPROGMISMATCH = (SOCBASEERR+75); // Program version wrong
  727. SOCEPROCUNAVAIL = (SOCBASEERR+76); // Bad procedure for program
  728. SOCENOLCK = (SOCBASEERR+77); // No locks available
  729. SOCENOSYS = (SOCBASEERR+78); // Function not implemented
  730. SOCEFTYPE = (SOCBASEERR+79); // Inappropriate file type or format
  731. SOCEAUTH = (SOCBASEERR+80); // Authentication error
  732. SOCENEEDAUTH = (SOCBASEERR+81); // Need authenticator
  733. SOCEOS2ERR = (SOCBASEERR+100); // OS/2 Error
  734. SOCELAST = (SOCBASEERR+100); // Must be equal largest errno
  735. (* !!TODO Add this consts
  736. /*
  737. * OS/2 SOCKET API errors redefined as regular BSD error constants
  738. */
  739. #ifndef ENOENT
  740. #define ENOENT SOCENOENT
  741. #endif
  742. #ifndef EFAULT
  743. #define EFAULT SOCEFAULT
  744. #endif
  745. #ifndef EBUSY
  746. #define EBUSY SOCEBUSY
  747. #endif
  748. #ifndef ENXIO
  749. #define ENXIO SOCENXIO
  750. #endif
  751. #ifndef EACCES
  752. #define EACCES SOCEACCES
  753. #endif
  754. #ifndef ENOMEM
  755. #define ENOMEM SOCENOMEM
  756. #endif
  757. #ifndef ENOTDIR
  758. #define ENOTDIR SOCENOTDIR
  759. #endif
  760. #ifndef EPERM
  761. #define EPERM SOCEPERM
  762. #endif
  763. #ifndef ESRCH
  764. #define ESRCH SOCESRCH
  765. #endif
  766. #ifndef EDQUOT
  767. #define EDQUOT SOCEDQUOT
  768. #endif
  769. #ifndef EEXIST
  770. #define EEXIST SOCEEXIST
  771. #endif
  772. #ifndef EBUSY
  773. #define EBUSY SOCEBUSY
  774. #endif
  775. #ifndef EWOULDBLOCK
  776. #define EWOULDBLOCK SOCEWOULDBLOCK
  777. #endif
  778. #ifndef EINPROGRESS
  779. #define EINPROGRESS SOCEINPROGRESS
  780. #endif
  781. #ifndef EALREADY
  782. #define EALREADY SOCEALREADY
  783. #endif
  784. #ifndef ENOTSOCK
  785. #define ENOTSOCK SOCENOTSOCK
  786. #endif
  787. #ifndef EDESTADDRREQ
  788. #define EDESTADDRREQ SOCEDESTADDRREQ
  789. #endif
  790. #ifndef EMSGSIZE
  791. #define EMSGSIZE SOCEMSGSIZE
  792. #endif
  793. #ifndef EPROTOTYPE
  794. #define EPROTOTYPE SOCEPROTOTYPE
  795. #endif
  796. #ifndef ENOPROTOOPT
  797. #define ENOPROTOOPT SOCENOPROTOOPT
  798. #endif
  799. #ifndef EPROTONOSUPPORT
  800. #define EPROTONOSUPPORT SOCEPROTONOSUPPORT
  801. #endif
  802. #ifndef ESOCKTNOSUPPORT
  803. #define ESOCKTNOSUPPORT SOCESOCKTNOSUPPORT
  804. #endif
  805. #ifndef EOPNOTSUPP
  806. #define EOPNOTSUPP SOCEOPNOTSUPP
  807. #endif
  808. #ifndef EPFNOSUPPORT
  809. #define EPFNOSUPPORT SOCEPFNOSUPPORT
  810. #endif
  811. #ifndef EAFNOSUPPORT
  812. #define EAFNOSUPPORT SOCEAFNOSUPPORT
  813. #endif
  814. #ifndef EADDRINUSE
  815. #define EADDRINUSE SOCEADDRINUSE
  816. #endif
  817. #ifndef EADDRNOTAVAIL
  818. #define EADDRNOTAVAIL SOCEADDRNOTAVAIL
  819. #endif
  820. #ifndef ENETDOWN
  821. #define ENETDOWN SOCENETDOWN
  822. #endif
  823. #ifndef ENETUNREACH
  824. #define ENETUNREACH SOCENETUNREACH
  825. #endif
  826. #ifndef ENETRESET
  827. #define ENETRESET SOCENETRESET
  828. #endif
  829. #ifndef ECONNABORTED
  830. #define ECONNABORTED SOCECONNABORTED
  831. #endif
  832. #ifndef ECONNRESET
  833. #define ECONNRESET SOCECONNRESET
  834. #endif
  835. #ifndef ENOBUFS
  836. #define ENOBUFS SOCENOBUFS
  837. #endif
  838. #ifndef EISCONN
  839. #define EISCONN SOCEISCONN
  840. #endif
  841. #ifndef ENOTCONN
  842. #define ENOTCONN SOCENOTCONN
  843. #endif
  844. #ifndef ESHUTDOWN
  845. #define ESHUTDOWN SOCESHUTDOWN
  846. #endif
  847. #ifndef ETOOMANYREFS
  848. #define ETOOMANYREFS SOCETOOMANYREFS
  849. #endif
  850. #ifndef ETIMEDOUT
  851. #define ETIMEDOUT SOCETIMEDOUT
  852. #endif
  853. #ifndef ECONNREFUSED
  854. #define ECONNREFUSED SOCECONNREFUSED
  855. #endif
  856. #ifndef ELOOP
  857. #define ELOOP SOCELOOP
  858. #endif
  859. #ifndef ENAMETOOLONG /* Borland and Watcom define this */
  860. #define ENAMETOOLONG SOCENAMETOOLONG
  861. #endif
  862. #ifndef EHOSTDOWN
  863. #define EHOSTDOWN SOCEHOSTDOWN
  864. #endif
  865. #ifndef EHOSTUNREACH
  866. #define EHOSTUNREACH SOCEHOSTUNREACH
  867. #endif
  868. #ifndef ENOTEMPTY /* Watcom defines this */
  869. #define ENOTEMPTY SOCENOTEMPTY
  870. #endif
  871. #ifndef EINVAL
  872. #define EINVAL SOCEINVAL
  873. #endif
  874. #ifndef EINTR
  875. #define EINTR SOCEINTR
  876. #endif
  877. #ifndef EMFILE
  878. #define EMFILE SOCEMFILE
  879. #endif
  880. #ifndef EPIPE
  881. #define EPIPE SOCEPIPE
  882. #endif
  883. *)
  884. // * bsd select definitions
  885. const
  886. {
  887. * Select uses bit masks of file descriptors in longs. These macros
  888. * manipulate such bit fields (the filesystem macros use chars).
  889. * FD_SETSIZE may be defined by the user, but the default here should
  890. * be enough for most uses.
  891. }
  892. FD_SETSIZE = 64;
  893. type
  894. fd_set = record
  895. fd_count : Word; // how many are SET?
  896. fd_array : array[0..FD_SETSIZE-1] of Longint; // an array of SOCKETs
  897. end;
  898. timeval = record
  899. tv_sec : Longint; // Number of seconds
  900. tv_usec : Longint; // Number of microseconds
  901. end;
  902. (* !!TODO Check all macros from sys/itypes.h
  903. function LSwap(a:Longint):Longint;
  904. function WSwap(a:Word):Word;
  905. { host -> network for long (4 bytes) }
  906. function htonl(a:Longint):Longint;
  907. { network -> host for long (4 bytes) }
  908. function ntohl(a:Longint):Longint;
  909. { host -> network for small (2 bytes) }
  910. function htons(a:Word):Word;
  911. { network -> host for small (2 bytes) }
  912. function ntohs(a:Word):Word;
  913. *)
  914. { * init / misc funcs }
  915. { init sockets system }
  916. function sock_init:Longint; cdecl;
  917. { get inet version. version - buffer of ?? size for returned string. }
  918. function getinetversion(var version):Longint; cdecl;
  919. { * sockets errors reporting funcs }
  920. { last err code for this thread }
  921. function sock_errno:Longint; cdecl;
  922. { print last err string + str if not NIL }
  923. procedure psock_errno(const str:PChar); cdecl;
  924. { * sockets creation / close funcs }
  925. { create new socket }
  926. function socket(domain,stype,protocol:Longint):Longint; cdecl;
  927. { close socket }
  928. function soclose(sock:Longint):Longint; cdecl;
  929. { cancel socket }
  930. function so_cancel(sock:Longint):Longint; cdecl;
  931. { shutdown socket. howto: 0/1/2 }
  932. function shutdown(sock,howto:Longint):Longint; cdecl;
  933. { abort socket. no docs found about it :( }
  934. function soabort(sock:Longint):Longint; cdecl;
  935. (***************************************************************************)
  936. (* *)
  937. (* sockets connection funcs *)
  938. (* *)
  939. (***************************************************************************)
  940. { accept a connection from remote host. returns s_addr & s_addr_len if not nil }
  941. function accept(sock:Longint; var s_addr:sockaddr; s_addr_len:Longint):Longint; cdecl;
  942. { bind a local name to the socket }
  943. function bind(sock:Longint; const s_addr: sockaddr; s_addr_len:Longint):Longint; cdecl;
  944. { connect socket to remote host }
  945. function connect(sock:Longint; const s_addr:sockaddr; s_addr_len:Longint):Longint; cdecl;
  946. { listen on socket. max_conn - queue size of listen. }
  947. function listen(sock,max_conn:Longint):Longint; cdecl;
  948. (***************************************************************************)
  949. (* *)
  950. (* sockets read/write funcs *)
  951. (* *)
  952. (***************************************************************************)
  953. { read data from socket. ! return N of readed bytes, or 0 (closed) or -1 }
  954. function recv(sock:Longint; var buf; buf_len,flags:Longint):Longint; cdecl;
  955. { send data to socket. ! return N of sent bytes. -1 - err }
  956. function send(sock:Longint; const buf; buf_len,flags:Longint):Longint; cdecl;
  957. { read data from socket. ! return N of readed bytes, or 0 (closed) or -1 }
  958. function recvfrom(sock:Longint; var buf; buf_len,flags:Longint; var s_addr:sockaddr; var s_addr_len:Longint):Longint; cdecl;
  959. { send data to socket. ! return N of sent bytes. -1 - err }
  960. function sendto(sock:Longint; const buf; buf_len,flags:Longint; var s_addr:sockaddr; s_addr_len:Longint):Longint; cdecl;
  961. { read data into iov_count number of buffers iov.
  962. ! return N of readed bytes, or 0 (closed) or -1 }
  963. function readv(sock:Longint; var iov:iovec; iov_count:Longint):LONGINT; cdecl;
  964. { write data from iov_count number of buffers iov.
  965. ! return N of writed bytes, or -1 }
  966. function writev(sock:Longint; var iov:iovec; iov_count:Longint):LONGINT; cdecl;
  967. { read data + control info from socket
  968. ! return N of readed bytes, or 0 (closed) or -1 }
  969. function recvmsg(sock:Longint; var msgbuf:msghdr; flags:Longint):Longint; cdecl;
  970. { send data + control info to socket
  971. ! return N of sended bytes, or -1 }
  972. function sendmsg(sock:Longint; var msgbuf:msghdr; flags:Longint):Longint; cdecl;
  973. (***************************************************************************)
  974. (* *)
  975. (* select funcs *)
  976. (* *)
  977. (***************************************************************************)
  978. { OS/2 select. 0 - timeout. -1 - err. XX - N of sockets worked. }
  979. function os2_select(var sockets; N_reads, N_writes, N_exepts, timeout:Longint):Longint; cdecl;
  980. { bsd select here. heavy voodoo.. }
  981. function select(nfds:Longint; const readfds,writefds,exceptfds:fd_set; const timeout:timeval):Longint; cdecl;
  982. (***************************************************************************)
  983. (* *)
  984. (* misc info *)
  985. (* *)
  986. (***************************************************************************)
  987. { get host ip addr - addr of primary interface }
  988. function gethostid:Longint; cdecl;
  989. { get connected to socket hostname }
  990. function getpeername(sock:Longint; var s_addr:sockaddr; var s_addr_len:Longint):Longint; cdecl;
  991. { get local socket name }
  992. function getsockname(sock:Longint; var s_addr:sockaddr; var s_addr_len:Longint):Longint; cdecl;
  993. (***************************************************************************)
  994. (* *)
  995. (* options & ioctls *)
  996. (* *)
  997. (***************************************************************************)
  998. { get socket options }
  999. function getsockopt(sock,level,optname:Longint; var buf; var buf_len:Longint):Longint; cdecl;
  1000. { set socket options }
  1001. function setsockopt(sock,level,optname:Longint; const buf; buf_len:Longint):Longint; cdecl;
  1002. { f@$king ioctl. use sys/ioctl.h }
  1003. function os2_ioctl(sock,cmd:Longint; var data; data_len:Longint):Longint; cdecl;
  1004. (***************************************************************************)
  1005. (* *)
  1006. (* functions only for 4.1+ ip stacks (but also found in 4.02w ;)) *)
  1007. (* *)
  1008. (***************************************************************************)
  1009. function addsockettolist(sock:Longint):Longint; cdecl;
  1010. function removesocketfromlist(sock:Longint):Longint; cdecl;
  1011. implementation
  1012. function LSwap(a:Longint):Longint; assembler;
  1013. asm
  1014. mov eax,a
  1015. xchg ah,al
  1016. ror eax,16
  1017. xchg ah,al
  1018. end;
  1019. function WSwap(a:Word):Word; assembler;
  1020. asm
  1021. mov ax,a
  1022. xchg ah,al
  1023. end;
  1024. function accept(sock:Longint; var s_addr: sockaddr; s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 1;
  1025. function bind(sock:Longint; const s_addr: sockaddr; s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 2;
  1026. function connect(sock:Longint; const s_addr:sockaddr; s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 3;
  1027. function gethostid: Longint; cdecl; external 'SO32DLL' index 4;
  1028. function getpeername(sock:Longint; var s_addr:sockaddr; var s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 5;
  1029. function getsockname(sock:Longint; var s_addr:sockaddr; var s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 6;
  1030. function getsockopt(sock,level,optname:Longint; var buf; var buf_len:Longint):Longint; cdecl; external 'SO32DLL' index 7;
  1031. function os2_ioctl(sock,cmd:Longint; var data; data_len:Longint):Longint; cdecl; external 'SO32DLL' index 8;
  1032. function listen(sock,max_conn:Longint):Longint; cdecl; external 'SO32DLL' index 9;
  1033. function recv(sock:Longint; var buf; buf_len,flags:Longint):Longint; cdecl; external 'SO32DLL' index 10;
  1034. function recvfrom(sock:Longint; var buf; buf_len,flags:Longint;var s_addr:sockaddr; var s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 11;
  1035. function os2_select(var sockets; N_reads, N_writes, N_exepts, timeout:Longint):Longint; cdecl; external 'SO32DLL' index 12;
  1036. function send(sock:Longint; const buf; buf_len,flags:Longint):Longint; cdecl; external 'SO32DLL' index 13;
  1037. function sendto(sock:Longint; const buf; buf_len,flags:Longint;var s_addr:sockaddr; s_addr_len:Longint):Longint; cdecl; external 'SO32DLL' index 14;
  1038. function setsockopt(sock,level,optname:Longint; const buf; buf_len:Longint):Longint; cdecl; external 'SO32DLL' index 15;
  1039. function socket(domain,stype,protocol:Longint):Longint; cdecl; external 'SO32DLL' index 16;
  1040. function soclose(sock:Longint):Longint; cdecl; external 'SO32DLL' index 17;
  1041. function so_cancel(sock:Longint):Longint; cdecl; external 'SO32DLL' index 18;
  1042. function soabort(sock:Longint):Longint; cdecl; external 'SO32DLL' index 19;
  1043. function sock_errno:Longint; cdecl; external 'SO32DLL' index 20;
  1044. function recvmsg(sock:Longint; var msgbuf:msghdr; flags:Longint):Longint; cdecl; external 'SO32DLL' index 21;
  1045. function sendmsg(sock:Longint; var msgbuf:msghdr; flags:Longint):Longint; cdecl; external 'SO32DLL' index 22;
  1046. function readv(sock:Longint; var iov:iovec; iov_count:Longint):LONGINT; cdecl; external 'SO32DLL' index 23;
  1047. function writev(sock:Longint; var iov:iovec; iov_count:Longint):LONGINT; cdecl; external 'SO32DLL' index 24;
  1048. function shutdown(sock,howto:Longint):Longint; cdecl; external 'SO32DLL' index 25;
  1049. function sock_init:Longint; cdecl; external 'SO32DLL' index 26;
  1050. function addsockettolist(sock:Longint):Longint; cdecl; external 'SO32DLL' index 27;
  1051. function removesocketfromlist(sock:Longint):Longint; cdecl; external 'SO32DLL' index 28;
  1052. { entry 29 not used }
  1053. procedure psock_errno(const str:PChar); cdecl; external 'SO32DLL' index 30;
  1054. function getinetversion(var version):Longint; cdecl; external 'SO32DLL' index 31;
  1055. function select(nfds:Longint;
  1056. const readfds,writefds,exceptfds:fd_set;
  1057. const timeout:timeval):Longint; cdecl; external 'SO32DLL' index 32;
  1058. function htonl(a:Longint):Longint;
  1059. begin Result:=LSwap(a); end;
  1060. { host -> network for long (4 bytes) }
  1061. function ntohl(a:Longint):Longint;
  1062. begin Result:=LSwap(a); end;
  1063. { network -> host for long (4 bytes) }
  1064. function htons(a:Word):Word;
  1065. begin Result:=WSwap(a); end;
  1066. { host -> network for small (2 bytes) }
  1067. function ntohs(a:Word):Word;
  1068. begin Result:=WSwap(a); end;
  1069. { network -> host for small (2 bytes) }
  1070. end.
  1071. (* !!TODO Following code not revised as yet
  1072. {*
  1073. * User-settable options (used with setsockopt).
  1074. *}
  1075. TCP_NODELAY = $01; // don't delay send to coalesce packets
  1076. TCP_MAXSEG = $02; // set maximum segment size
  1077. TCP_MSL = $03; // MSL HACK
  1078. TCP_TIMESTMP = $04; // RFC 1323 (RTTM TimeStamp)
  1079. TCP_WINSCALE = $05; // RFC 1323 (Window Scale)
  1080. TCP_CC = $06; // RFC 1644 (Connection Count)
  1081. {
  1082. * Structures returned by network data base library. All addresses are
  1083. * supplied in host order, and returned in network order (suitable for
  1084. * use in system calls).
  1085. }
  1086. type
  1087. PLongint = ^Longint;
  1088. { struct for gethostbyname() and gethostbyaddr() }
  1089. hostent = record
  1090. h_name : PChar; // official name of host
  1091. h_aliases : ^PChar; // alias list
  1092. h_addrtype : Longint; // host address type
  1093. h_length : Longint; // length of address
  1094. h_addr_list : ^PLongint; // list of addresses from name server
  1095. end;
  1096. phostent = ^hostent;
  1097. {
  1098. * Error return codes from gethostbyname(), gethostbyaddr() and res_* funcs
  1099. * (left in extern int h_errno).
  1100. }
  1101. const
  1102. NETDB_INTERNAL = -1; // see errno
  1103. NETDB_SUCCESS = 0; // no problem
  1104. HOST_NOT_FOUND = 1; // Authoritative Answer Host not found
  1105. TRY_AGAIN = 2; // Non-Authoritive Host not found, or SERVERFAIL
  1106. NO_RECOVERY = 3; // Non recoverable errors, FORMERR, REFUSED, NOTIMP
  1107. NO_DATA = 4; // Valid name, no data record of requested type
  1108. NO_ADDRESS = NO_DATA; // no address, look for MX record
  1109. type
  1110. { struct for getprotobyname() and getprotobynumber() }
  1111. protoent = record
  1112. p_name : PChar; // official protocol name
  1113. p_aliases : ^PChar; // alias list
  1114. p_proto : Longint; // protocol #
  1115. end;
  1116. pprotoent = ^protoent;
  1117. type
  1118. { struct for getservbyname() and getservbyport() }
  1119. servent = record
  1120. s_name : PChar; // official service name
  1121. s_aliases : ^PChar; // alias list
  1122. s_port : Longint; // port # (need ntohl() !!)
  1123. s_proto : PChar; // protocol to use
  1124. end;
  1125. pservent = ^servent;
  1126. {
  1127. * ioctl & ip trace support
  1128. }
  1129. const
  1130. SIOCGIFFLAGS = $6900 + 17; // get interface flags
  1131. { Interface Tracing Support }
  1132. SIOCGIFEFLAGS = $6900 + 150; // get interface enhanced flags
  1133. SIOCSIFEFLAGS = $6900 + 151; // set interface enhanced flags
  1134. SIOCGIFTRACE = $6900 + 152; // get interface trace data
  1135. SIOCSIFTRACE = $6900 + 153; // set interface trace data
  1136. { sorry, i skip other ioctl commands, see SYS\ioctl.h from toolkit for it.. }
  1137. IFF_UP = $1; // interface is up
  1138. IFF_BROADCAST = $2; // broadcast address valid
  1139. IFF_DEBUG = $4; // turn on debugging
  1140. IFF_LOOPBACK = $8; // is a loopback net
  1141. IFF_POINTOPOINT = $10; // interface is point-to-point link
  1142. IFF_LINK2 = $20; // was trailers, not used
  1143. IFF_NOTRAILERS = IFF_LINK2;
  1144. IFF_RUNNING = $40; // resources allocated
  1145. IFF_NOARP = $80; // no address resolution protocol
  1146. IFF_PROMISC = $100; // receive all packets
  1147. IFF_ALLMULTI = $200; // receive all multicast packets
  1148. IFF_BRIDGE = $1000; // support token ring routine field
  1149. IFF_SNAP = $2000; // support extended SAP header
  1150. IFF_DEFMTU = $400; // default mtu of 1500
  1151. IFF_RFC1469_BC = 1; // using broadcast
  1152. IFF_RFC1469_FA = 2; // using functional
  1153. IFF_RFC1469_MA = 3; // using multicast
  1154. IFF_ETHER = $4000; // Ethernet interface
  1155. IFF_LOOPBRD = $8000; // loop back broadcasts
  1156. IFF_MULTICAST = $800; // supports multicast
  1157. IFF_SIMPLEX = $10000; // can't hear own transmissions
  1158. IFF_OACTIVE = $20000; // transmission in progress
  1159. IFF_802_3 = $40000;
  1160. IFF_CANONICAL = $80000;
  1161. IFF_RUNNINGBLK = $100000; // threads waited for intf running
  1162. { Interface enhanced flags }
  1163. IFFE_PKTTRACE = $00000001; // trace datalink where possible
  1164. IFFE_IPTRACE = $00000002; // trace ONLY IP packets
  1165. type
  1166. { trace buffer struct }
  1167. pkt_trace_hdr = record
  1168. pt_htype : Word; // header type
  1169. pt_len : Word; // in: pt_buf len, out: packet len
  1170. pt_data : Pointer; // packet
  1171. pt_tstamp : Longint; // time stamp in milliseconds
  1172. end;
  1173. const
  1174. { physical protocols IDs }
  1175. HT_IP = $01; // IP
  1176. HT_ETHER = $06; // Ethernet
  1177. HT_ISO88023 = $07; // CSMA CD
  1178. HT_ISO88025 = $09; // Token Ring
  1179. HT_SLIP = $1c; // Serial Line IP
  1180. HT_PPP = $18; // PPP IP
  1181. const
  1182. IFNAMSIZ = 16; // interface name length
  1183. type
  1184. {
  1185. * Interface request structure used for socket
  1186. * ioctl's. All interface ioctl's must have parameter
  1187. * definitions which begin with ifr_name. The
  1188. * remainder may be interface specific.
  1189. }
  1190. ifreq = record
  1191. ifr_name : array[0..IFNAMSIZ-1] of Char;
  1192. case Byte of
  1193. 0: (ifr_addr : sockaddr); // address
  1194. 1: (ifr_dstaddr : sockaddr); // other end of p-to-p link
  1195. 2: (ifr_broadaddr : sockaddr); // broadcast address
  1196. 3: (ifr_flags : Word); // flags
  1197. 4: (ifr_metric : Longint); // metric
  1198. 5: (ifr_data : Pointer); // for use by interface
  1199. 6: (ifr_eflags : Longint); // eflags
  1200. end;
  1201. {
  1202. * Structure of an internet header, naked of options.
  1203. }
  1204. type
  1205. ip = record
  1206. hlen_ver : Byte; { lo 4 bits = header len/4
  1207. hi 4 bits = ip ver (4) }
  1208. ip_tos : Byte; { type of service }
  1209. ip_len : Word; { total packet length }
  1210. ip_id : Word; { identification }
  1211. ip_off : Word; { fragment offset field }
  1212. ip_ttl : Byte; { time to live }
  1213. ip_p : Byte; { protocol (see IPPROTO_* ) }
  1214. ip_sum : Word; { header checksum }
  1215. ip_src, ip_dst : Longint; { ip from / to addr }
  1216. end;
  1217. { in.h / inet.h const & func }
  1218. {
  1219. * Protocols
  1220. }
  1221. const
  1222. IPPROTO_IP = 0; { dummy for IP }
  1223. IPPROTO_ICMP = 1; { control message protocol }
  1224. IPPROTO_GGP = 3; { gateway^2 (deprecated) }
  1225. IPPROTO_TCP = 6; { tcp }
  1226. IPPROTO_EGP = 8; { exterior gateway protocol }
  1227. IPPROTO_PUP = 12; { pup }
  1228. IPPROTO_UDP = 17; { user datagram protocol }
  1229. IPPROTO_IDP = 22; { xns idp }
  1230. IPPROTO_RAW = 255; { raw IP packet }
  1231. IPPROTO_MAX = 256;
  1232. {
  1233. * Ports < IPPORT_RESERVED are reserved for
  1234. * privileged processes (e.g. root).
  1235. * Ports > IPPORT_USERRESERVED are reserved
  1236. * for servers, not necessarily privileged.
  1237. }
  1238. const
  1239. IPPORT_RESERVED = 1024;
  1240. IPPORT_USERRESERVED = 5000;
  1241. {
  1242. * Link numbers
  1243. }
  1244. const
  1245. IMPLINK_IP = 155;
  1246. IMPLINK_LOWEXPER = 156;
  1247. IMPLINK_HIGHEXPER = 158;
  1248. {
  1249. * Definitions of bits in internet address integers.
  1250. * On subnets, the decomposition of addresses to host and net parts
  1251. * is done according to subnet mask, not the masks here.
  1252. }
  1253. const
  1254. IN_CLASSA_NET = $ff000000;
  1255. IN_CLASSA_NSHIFT = 24;
  1256. IN_CLASSA_HOST = $00ffffff;
  1257. IN_CLASSA_MAX = 128;
  1258. IN_CLASSB_NET = $ffff0000;
  1259. IN_CLASSB_NSHIFT = 16;
  1260. IN_CLASSB_HOST = $0000ffff;
  1261. IN_CLASSB_MAX = 65536;
  1262. IN_CLASSC_NET = $ffffff00;
  1263. IN_CLASSC_NSHIFT = 8;
  1264. IN_CLASSC_HOST = $000000ff;
  1265. INADDR_ANY = $00000000;
  1266. INADDR_BROADCAST = $ffffffff; { must be masked }
  1267. INADDR_NONE = $ffffffff; { -1 return }
  1268. IN_LOOPBACKNET = 127; { official! }
  1269. {
  1270. * Socket address, internet style.
  1271. }
  1272. type
  1273. sockaddr_in = record
  1274. sin_family : Word;
  1275. sin_port : Word; { htons first!! }
  1276. sin_addr : Longint; {in_addr; hist reasons :)) }
  1277. sin_zero : array[0..7] of Byte; {must be zero}
  1278. end;
  1279. { * Internet address (a structure for historical reasons) }
  1280. type
  1281. in_addr = record
  1282. s_addr : Longint;
  1283. end;
  1284. {*
  1285. * Options for use with [gs]etsockopt at the IP level.
  1286. * }
  1287. const
  1288. IP_OPTIONS = 1; // buf/ip_opts; set/get IP options
  1289. IP_MULTICAST_IF = 2; // u_char; set/get IP multicast i/f
  1290. IP_MULTICAST_TTL = 3; // u_char; set/get IP multicast ttl
  1291. IP_MULTICAST_LOOP = 4; // u_char; set/get IP multicast loopback
  1292. IP_ADD_MEMBERSHIP = 5; // ip_mreq; add an IP group membership
  1293. IP_DROP_MEMBERSHIP = 6; // ip_mreq; drop an IP group membership
  1294. IP_HDRINCL = 7; // int; header is included with data
  1295. IP_TOS = 8; // int; IP type of service and preced.
  1296. IP_TTL = 9; // int; IP time to live
  1297. IP_RECVOPTS = 10; // bool; receive all IP opts w/dgram
  1298. IP_RECVRETOPTS = 11; // bool; receive IP opts for response
  1299. IP_RECVDSTADDR = 12; // bool; receive IP dst addr w/dgram
  1300. IP_RETOPTS = 13; // ip_opts; set/get IP options
  1301. IP_RECVTRRI = 14; // bool; receive token ring routing inf
  1302. {*
  1303. * Defaults and limits for options
  1304. * }
  1305. IP_DEFAULT_MULTICAST_TTL = 1; // normally limit m'casts to 1 hop
  1306. IP_DEFAULT_MULTICAST_LOOP = 1; // normally hear sends if a member
  1307. IP_MAX_MEMBERSHIPS = 20; // per socket; must fit in one mbuf
  1308. MAX_IN_MULTI = 16*IP_MAX_MEMBERSHIPS; // 320 max per os2
  1309. *)
  1310. (* List of not checked functions from SO32DLL.DLL
  1311. ³ 00011 ³ RECVFROM
  1312. ³ 00012 ³ SELECT
  1313. ³ 00013 ³ SEND
  1314. ³ 00014 ³ SENDTO
  1315. ³ 00015 ³ SETSOCKOPT
  1316. ³ 00016 ³ SOCKET
  1317. ³ 00017 ³ SOCLOSE
  1318. ³ 00018 ³ SO_CANCEL
  1319. ³ 00019 ³ SOABORT
  1320. ³ 00020 ³ SOCK_ERRNO
  1321. ³ 00021 ³ RECVMSG
  1322. ³ 00022 ³ SENDMSG
  1323. ³ 00023 ³ READV
  1324. ³ 00024 ³ WRITEV
  1325. ³ 00025 ³ SHUTDOWN
  1326. ³ 00026 ³ SOCK_INIT
  1327. ³ 00027 ³ ADDSOCKETTOLIST
  1328. ³ 00028 ³ REMOVESOCKETFROMLIST
  1329. ³ 00030 ³ PSOCK_ERRNO
  1330. ³ 00031 ³ GETINETVERSION
  1331. ³ 00032 ³ BSDSELECT
  1332. ³ 00035 ³ SET_ERRNO
  1333. ³ 00038 ³ WINSOCKCLEANUPSOCKETS
  1334. ³ 00039 ³ GETSOCKETFROMLIST
  1335. À´Done
  1336. *)