so32dll.pas 55 KB

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