sockets.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 Yuri Prokushev
  4. Copyright (c) 2005 Soren Ager
  5. Sockets implementation for OS/2
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$MODE ObjFPC}
  13. { $DEFINE notUnix} // To make ssockets.pp compile
  14. unit Sockets;
  15. interface
  16. uses
  17. so32dll, ctypes;
  18. const
  19. AF_UNSPEC = so32dll.AF_UNSPEC; // unspecified
  20. AF_LOCAL = so32dll.AF_LOCAL; // local to host (pipes, portals)
  21. AF_UNIX = so32dll.AF_UNIX;
  22. AF_OS2 = so32dll.AF_OS2;
  23. AF_INET = so32dll.AF_INET; // internetwork: UDP, TCP, etc.
  24. AF_IMPLINK = so32dll.AF_IMPLINK; // arpanet imp addresses
  25. AF_PUP = so32dll.AF_PUP; // pup protocols: e.g. BSP
  26. AF_CHAOS = so32dll.AF_CHAOS; // mit CHAOS protocols
  27. AF_NS = so32dll.AF_NS; // XEROX NS protocols
  28. AF_ISO = so32dll.AF_ISO; // ISO protocols
  29. AF_OSI = so32dll.AF_OSI;
  30. AF_ECMA = so32dll.AF_ECMA; // european computer manufacturers
  31. AF_DATAKIT = so32dll.AF_DATAKIT; // datakit protocols
  32. AF_CCITT = so32dll.AF_CCITT; // CCITT protocols, X.25 etc
  33. AF_SNA = so32dll.AF_SNA; // IBM SNA
  34. AF_DECnet = so32dll.AF_DECnet; // DECnet
  35. AF_DLI = so32dll.AF_DLI; // DEC Direct data link interface
  36. AF_LAT = so32dll.AF_LAT; // LAT
  37. AF_HYLINK = so32dll.AF_HYLINK; // NSC Hyperchannel
  38. AF_APPLETALK = so32dll.AF_APPLETALK; // Apple Talk
  39. AF_NB = so32dll.AF_NB; // Netbios
  40. AF_NETBIOS = so32dll.AF_NETBIOS; // Netbios
  41. AF_LINK = so32dll.AF_LINK; // Link layer interface
  42. pseudo_AF_XTP = so32dll.pseudo_AF_XTP; // eXpress Transfer Protocol (no AF)
  43. AF_COIP = so32dll.AF_COIP; // connection-oriented IP, aka ST II
  44. AF_CNT = so32dll.AF_CNT; // Computer Network Technology
  45. pseudo_AF_RTIP = so32dll.pseudo_AF_RTIP; // Help Identify RTIP packets
  46. AF_IPX = so32dll.AF_IPX; // Novell Internet Protocol
  47. AF_SIP = so32dll.AF_SIP; // Simple Internet Protocol
  48. AF_INET6 = so32dll.AF_INET6;
  49. pseudo_AF_PIP = so32dll.pseudo_AF_PIP; // Help Identify PIP packets
  50. AF_ROUTE = so32dll.AF_ROUTE; // Internal Routing Protocol
  51. AF_FWIP = so32dll.AF_FWIP; // firewall support
  52. AF_IPSEC = so32dll.AF_IPSEC; // IPSEC and encryption techniques
  53. AF_DES = so32dll.AF_DES; // DES
  54. AF_MD5 = so32dll.AF_MD5;
  55. AF_CDMF = so32dll.AF_CDMF;
  56. AF_MAX = so32dll.AF_MAX;
  57. // PF_LOCAL = so32dll.PF_LOCAL;
  58. PF_OS2 = so32dll.PF_OS2;
  59. PF_IMPLINK = so32dll.PF_IMPLINK;
  60. PF_PUP = so32dll.PF_PUP;
  61. PF_CHAOS = so32dll.PF_CHAOS;
  62. PF_NS = so32dll.PF_NS;
  63. PF_ISO = so32dll.PF_ISO;
  64. PF_OSI = so32dll.PF_OSI;
  65. PF_ECMA = so32dll.PF_ECMA;
  66. PF_DATAKIT = so32dll.PF_DATAKIT;
  67. PF_CCITT = so32dll.PF_CCITT;
  68. PF_SNA = so32dll.PF_SNA;
  69. PF_DECnet = so32dll.PF_DECnet;
  70. PF_DLI = so32dll.PF_DLI;
  71. PF_LAT = so32dll.PF_LAT;
  72. PF_HYLINK = so32dll.PF_HYLINK;
  73. PF_APPLETALK = so32dll.PF_APPLETALK;
  74. PF_NETBIOS = so32dll.PF_NB;
  75. PF_NB = so32dll.PF_NB;
  76. PF_ROUTE = so32dll.PF_ROUTE;
  77. PF_LINK = so32dll.PF_LINK;
  78. PF_XTP = so32dll.PF_XTP; // really just proto family, no AF
  79. PF_COIP = so32dll.PF_COIP;
  80. PF_CNT = so32dll.PF_CNT;
  81. PF_SIP = so32dll.PF_SIP;
  82. PF_INET6 = so32dll.PF_INET6;
  83. PF_IPX = so32dll.PF_IPX; // same format as AF_NS
  84. PF_RTIP = so32dll.PF_RTIP; // same format as AF_INET
  85. PF_PIP = so32dll.PF_PIP;
  86. PF_MAX = so32dll.PF_MAX;
  87. EsockEINTR = SOCEINTR;
  88. EsockEBADF = SOCEBADF;
  89. EsockEFAULT = SOCEFAULT;
  90. EsockEINVAL = SOCEINVAL;
  91. EsockEACCESS = SOCEACCES;
  92. EsockEMFILE = SOCEMFILE;
  93. EsockEMSGSIZE = SOCEMSGSIZE;
  94. EsockENOBUFS = SOCENOBUFS;
  95. EsockENOTCONN = SOCENOTCONN;
  96. EsockENOTSOCK = SOCENOTSOCK;
  97. EsockEPROTONOSUPPORT = SOCEPROTONOSUPPORT;
  98. EsockEWOULDBLOCK = SOCEWOULDBLOCK;
  99. (***************************************************************************)
  100. (* *)
  101. (* Option flags per-socket *)
  102. (* *)
  103. (***************************************************************************)
  104. const
  105. // turn on debugging info recording
  106. SO_DEBUG = $0001;
  107. // socket has had listen()
  108. SO_ACCEPTCONN = $0002;
  109. // allow local address reuse
  110. SO_REUSEADDR = $0004;
  111. // keep connections alive
  112. SO_KEEPALIVE = $0008;
  113. // just use interface addresses
  114. SO_DONTROUTE = $0010;
  115. // permit sending of broadcast msgs
  116. SO_BROADCAST = $0020;
  117. // bypass hardware when possible
  118. SO_USELOOPBACK = $0040;
  119. // linger on close if data present
  120. SO_LINGER = $0080;
  121. // leave received OOB data in line
  122. SO_OOBINLINE = $0100;
  123. // limited broadcast sent on all IFs
  124. SO_L_BROADCAST = $0200;
  125. // set if shut down called for rcv
  126. SO_RCV_SHUTDOWN = $0400;
  127. // set if shutdown called for send
  128. SO_SND_SHUTDOWN = $0800;
  129. // allow local address & port reuse
  130. SO_REUSEPORT = $1000;
  131. // allow t/tcp on socket
  132. SO_TTCP = $2000;
  133. (***************************************************************************)
  134. (* *)
  135. (* Additional options, not kept in so_options *)
  136. (* *)
  137. (***************************************************************************)
  138. // send buffer size
  139. SO_SNDBUF = $1001;
  140. // receive buffer size
  141. SO_RCVBUF = $1002;
  142. // send low-water mark
  143. SO_SNDLOWAT = $1003;
  144. // receive low-water mark
  145. SO_RCVLOWAT = $1004;
  146. // send timeout
  147. SO_SNDTIMEO = $1005;
  148. // receive timeout
  149. SO_RCVTIMEO = $1006;
  150. // get error status and clear
  151. SO_ERROR = $1007;
  152. // get socket type
  153. SO_TYPE = $1008;
  154. // get socket options
  155. SO_OPTIONS = $1010;
  156. (***************************************************************************)
  157. (* *)
  158. (* Level number for (get/set)sockopt() to apply to socket itself *)
  159. (* *)
  160. (***************************************************************************)
  161. // options for socket level
  162. SOL_SOCKET = $ffff;
  163. (***************************************************************************)
  164. (* *)
  165. (* Definitions for sysctl call. The sysctl call uses a hierarchical name *)
  166. (* for objects that can be examined or modified. The name is expressed as *)
  167. (* a sequence of integers. Like a file path name, the meaning of each *)
  168. (* component depends on its place in the hierarchy. The top-level and kern *)
  169. (* identifiers are defined here, and other identifiers are defined in the *)
  170. (* respective subsystem header files. *)
  171. (* *)
  172. (***************************************************************************)
  173. // largest number of components supported
  174. CTL_MAXNAME = 12;
  175. // name is a node
  176. CTLTYPE_NODE =1;
  177. // name describes an integer
  178. CTLTYPE_INT =2;
  179. // name describes a string
  180. CTLTYPE_STRING =3;
  181. // name describes a 64-bit number
  182. CTLTYPE_QUAD =4;
  183. // name describes a structure
  184. CTLTYPE_STRUCT =5;
  185. // inetcfg sysctl code
  186. CTLTYPE_INETCFG =6;
  187. // inetver sysctl code
  188. CTLTYPE_INEVER =7;
  189. (*
  190. * Top-level identifiers
  191. *)
  192. // "high kernel": proc, limits
  193. CTL_KERN = 1;
  194. // network, see socket.h
  195. CTL_NET = 4;
  196. // OS/2 specific codes
  197. CTL_OS2 = 9;
  198. {
  199. /*
  200. * PF_ROUTE - Routing table
  201. *
  202. * Three additional levels are defined:
  203. * Fourth: address family, 0 is wildcard
  204. * Fifth: type of info, defined below
  205. * Sixth: flag(s) to mask with for NET_RT_FLAGS
  206. */
  207. }
  208. // dump; may limit to a.f.
  209. NET_RT_DUMP = 1;
  210. // by flags, e.g. RESOLVING
  211. NET_RT_FLAGS = 2;
  212. // survey interface list
  213. NET_RT_IFLIST = 3;
  214. NET_RT_MAXID = 4;
  215. (***************************************************************************)
  216. (* *)
  217. (* Maximum queue length specifiable by listen *)
  218. (* *)
  219. (***************************************************************************)
  220. // Maximum queue length specifiable by listen
  221. SOMAXCONN = 1024;
  222. // process out-of-band data
  223. MSG_OOB = $1;
  224. // peek at incoming message
  225. MSG_PEEK = $2;
  226. // send without using routing tables
  227. MSG_DONTROUTE = $4;
  228. // send without using routing tables
  229. MSG_FULLREAD = $8;
  230. // data completes record
  231. MSG_EOR = $10;
  232. // data discarded before delivery
  233. MSG_TRUNC = $20;
  234. // control data lost before delivery
  235. MSG_CTRUNC = $40;
  236. // wait for full request or error
  237. MSG_WAITALL = $80;
  238. // this message should be nonblocking
  239. MSG_DONTWAIT = $100;
  240. MSG_EOF = $200;
  241. // mem mapped io
  242. MSG_MAPIO = $400;
  243. (***************************************************************************)
  244. (* *)
  245. (* "Socket"-level control message types *)
  246. (* *)
  247. (***************************************************************************)
  248. // access rights (array of int)
  249. SCM_RIGHTS = $01;
  250. // * bsd select definitions
  251. {
  252. * Select uses bit masks of file descriptors in longs. These macros
  253. * manipulate such bit fields (the filesystem macros use chars).
  254. * FD_SETSIZE may be defined by the user, but the default here should
  255. * be enough for most uses.
  256. }
  257. FD_SETSIZE = 64;
  258. {
  259. * ioctl & ip trace support
  260. }
  261. FIONREAD = (Ord('f') SHL 8) OR 127;
  262. FIONBIO = (Ord('f') SHL 8) OR 126;
  263. FIOASYNC = (Ord('f') SHL 8) OR 125;
  264. FIOTCPCKSUM = (Ord('f') SHL 8) OR 128;
  265. FIONSTATUS = (Ord('f') SHL 8) OR 120;
  266. FIONURG = (Ord('f') SHL 8) OR 121;
  267. SIOCSHIWAT = (Ord('s') SHL 8) OR 0;
  268. SIOCGHIWAT = (Ord('s') SHL 8) OR 1;
  269. SIOCSLOWAT = (Ord('s') SHL 8) OR 2;
  270. SIOCGLOWAT = (Ord('s') SHL 8) OR 3;
  271. SIOCATMARK = (Ord('s') SHL 8) OR 7;
  272. SIOCSPGRP = (Ord('s') SHL 8) OR 8;
  273. SIOCGPGRP = (Ord('s') SHL 8) OR 9;
  274. SIOCSHOSTID = (Ord('s') SHL 8) OR 10;
  275. SIOCADDRT = (Ord('r') SHL 8) OR 10;
  276. SIOCDELRT = (Ord('r') SHL 8) OR 11;
  277. SIOMETRIC1RT = (Ord('r') SHL 8) OR 12;
  278. SIOMETRIC2RT = (Ord('r') SHL 8) OR 13;
  279. SIOMETRIC3RT = (Ord('r') SHL 8) OR 14;
  280. SIOMETRIC4RT = (Ord('r') SHL 8) OR 15;
  281. SIOCREGADDNET = (Ord('r') SHL 8) OR 12;
  282. SIOCREGDELNET = (Ord('r') SHL 8) OR 13;
  283. SIOCREGROUTES = (Ord('r') SHL 8) OR 14;
  284. SIOCFLUSHROUTES=(Ord('r') SHL 8) OR 15;
  285. SIOCSIFADDR = (Ord('i') SHL 8) OR 12;
  286. SIOCGIFADDR = (Ord('i') SHL 8) OR 13;
  287. SIOCSIFDSTADDR= (Ord('i') SHL 8) OR 14;
  288. SIOCGIFDSTADDR= (Ord('i') SHL 8) OR 15;
  289. SIOCSIFFLAGS = (Ord('i') SHL 8) OR 16;
  290. SIOCGIFFLAGS = (Ord('i') SHL 8) OR 17;
  291. SIOCGIFBRDADDR= (Ord('i') SHL 8) OR 18;
  292. SIOCSIFBRDADDR= (Ord('i') SHL 8) OR 19;
  293. SIOCGIFCONF = (Ord('i') SHL 8) OR 20;
  294. SIOCGIFNETMASK= (Ord('i') SHL 8) OR 21;
  295. SIOCSIFNETMASK= (Ord('i') SHL 8) OR 22;
  296. SIOCGIFMETRIC = (Ord('i') SHL 8) OR 23;
  297. SIOCSIFMETRIC = (Ord('i') SHL 8) OR 24;
  298. SIOCSIFSETSIG = (Ord('i') SHL 8) OR 25;
  299. SIOCSIFCLRSIG = (Ord('i') SHL 8) OR 26;
  300. SIOCSIFBRD = (Ord('i') SHL 8) OR 27; { SINGLE-rt bcst. using old # for bkw cmpt }
  301. SIOCSIFALLRTB = (Ord('i') SHL 8) OR 63; { added to configure all-route broadcst }
  302. SIOCGIFLOAD =(Ord('i') SHL 8) OR 27;
  303. SIOCSIFFILTERSRC=(Ord('i') SHL 8) OR 28;
  304. SIOCGIFFILTERSRC=(Ord('i') SHL 8) OR 29;
  305. SIOCSARP = (Ord('i') SHL 8) OR 30;
  306. SIOCGARP = (Ord('i') SHL 8) OR 31;
  307. SIOCDARP = (Ord('i') SHL 8) OR 32;
  308. SIOCSIFSNMPSIG= (Ord('i') SHL 8) OR 33;
  309. SIOCSIFSNMPCLR= (Ord('i') SHL 8) OR 34;
  310. SIOCSIFSNMPCRC= (Ord('i') SHL 8) OR 35;
  311. SIOCSIFPRIORITY=(Ord('i') SHL 8) OR 36;
  312. SIOCGIFPRIORITY=(Ord('i') SHL 8) OR 37;
  313. SIOCSIFFILTERDST=(Ord('i') SHL 8) OR 38;
  314. SIOCGIFFILTERDST=(Ord('i') SHL 8) OR 39;
  315. SIOCSIF802_3 = (Ord('i') SHL 8) OR 40;
  316. SIOCSIFNO802_3= (Ord('i') SHL 8) OR 41;
  317. SIOCSIFNOREDIR= (Ord('i') SHL 8) OR 42;
  318. SIOCSIFYESREDIR= (Ord('i') SHL 8) OR 43;
  319. SIOCSIFMTU = (Ord('i') SHL 8) OR 45;
  320. SIOCSIFFDDI = (Ord('i') SHL 8) OR 46;
  321. SIOCSIFNOFDDI = (Ord('i') SHL 8) OR 47;
  322. SIOCSRDBRD = (Ord('i') SHL 8) OR 48;
  323. SIOCSARP_TR = (Ord('i') SHL 8) OR 49;
  324. SIOCGARP_TR = (Ord('i') SHL 8) OR 50;
  325. { multicast ioctls }
  326. SIOCADDMULTI = (Ord('i') SHL 8) OR 51; { add m'cast addr }
  327. SIOCDELMULTI = (Ord('i') SHL 8) OR 52; { del m'cast addr }
  328. SIOCMULTISBC = (Ord('i') SHL 8) OR 61; { use broadcast to send IP multicast }
  329. SIOCMULTISFA = (Ord('i') SHL 8) OR 62; { use functional addr to send IP multicast }
  330. {$IFDEF SLBOOTP}
  331. SIOCGUNIT = (Ord('i') SHL 8) OR 70; { Used to retreive unit number on }
  332. { serial interface }
  333. {$ENDIF}
  334. SIOCSIFSPIPE = (Ord('i') SHL 8) OR 71; { used to set pipe size on interface }
  335. { this is used as tcp send buffer size }
  336. SIOCSIFRPIPE = (Ord('i') SHL 8) OR 72; { used to set pipe size on interface }
  337. { this is used as tcp recv buffer size }
  338. SIOCSIFTCPSEG = (Ord('i') SHL 8) OR 73; { set the TCP segment size on interface }
  339. SIOCSIFUSE576 = (Ord('i') SHL 8) OR 74; { enable/disable the automatic change of mss to 576 }
  340. { if going through a router }
  341. SIOCGIFVALID = (Ord('i') SHL 8) OR 75; { to check if the interface is Valid or not }
  342. { sk June 14 1995 }
  343. SIOCGIFBOUND = (Ord('i') SHL 8) OR 76; { ioctl to return bound/shld bind ifs }
  344. { Interface Tracing Support }
  345. SIOCGIFEFLAGS = (Ord('i') SHL 8) OR 150;
  346. SIOCSIFEFLAGS = (Ord('i') SHL 8) OR 151;
  347. SIOCGIFTRACE = (Ord('i') SHL 8) OR 152;
  348. SIOCSIFTRACE = (Ord('i') SHL 8) OR 153;
  349. {$IFDEF SLSTATS}
  350. SIOCSSTAT = (Ord('i') SHL 8) OR 154;
  351. SIOCGSTAT = (Ord('i') SHL 8) OR 155;
  352. {$ENDIF}
  353. { NETSTAT stuff }
  354. SIOSTATMBUF = (Ord('n') SHL 8) OR 40;
  355. SIOSTATTCP = (Ord('n') SHL 8) OR 41;
  356. SIOSTATUDP = (Ord('n') SHL 8) OR 42;
  357. SIOSTATIP = (Ord('n') SHL 8) OR 43;
  358. SIOSTATSO = (Ord('n') SHL 8) OR 44;
  359. SIOSTATRT = (Ord('n') SHL 8) OR 45;
  360. SIOFLUSHRT = (Ord('n') SHL 8) OR 46;
  361. SIOSTATICMP = (Ord('n') SHL 8) OR 47;
  362. SIOSTATIF = (Ord('n') SHL 8) OR 48;
  363. SIOSTATAT = (Ord('n') SHL 8) OR 49;
  364. SIOSTATARP = (Ord('n') SHL 8) OR 50;
  365. SIOSTATIF42 = (Ord('n') SHL 8) OR 51;
  366. {*
  367. * User-settable options (used with setsockopt).
  368. *}
  369. TCP_NODELAY = $01; // don't delay send to coalesce packets
  370. TCP_MAXSEG = $02; // set maximum segment size
  371. TCP_MSL = $03; // MSL HACK
  372. TCP_TIMESTMP = $04; // RFC 1323 (RTTM TimeStamp)
  373. TCP_WINSCALE = $05; // RFC 1323 (Window Scale)
  374. TCP_CC = $06; // RFC 1644 (Connection Count)
  375. IFF_UP = $1; // interface is up
  376. IFF_BROADCAST = $2; // broadcast address valid
  377. IFF_DEBUG = $4; // turn on debugging
  378. IFF_LOOPBACK = $8; // is a loopback net
  379. IFF_POINTOPOINT = $10; // interface is point-to-point link
  380. IFF_LINK2 = $20; // was trailers, not used
  381. IFF_NOTRAILERS = IFF_LINK2;
  382. IFF_RUNNING = $40; // resources allocated
  383. IFF_NOARP = $80; // no address resolution protocol
  384. IFF_PROMISC = $100; // receive all packets
  385. IFF_ALLMULTI = $200; // receive all multicast packets
  386. IFF_BRIDGE = $1000; // support token ring routine field
  387. IFF_SNAP = $2000; // support extended SAP header
  388. IFF_DEFMTU = $400; // default mtu of 1500
  389. IFF_RFC1469_BC = 1; // using broadcast
  390. IFF_RFC1469_FA = 2; // using functional
  391. IFF_RFC1469_MA = 3; // using multicast
  392. IFF_ETHER = $4000; // Ethernet interface
  393. IFF_LOOPBRD = $8000; // loop back broadcasts
  394. IFF_MULTICAST = $800; // supports multicast
  395. IFF_SIMPLEX = $10000; // can't hear own transmissions
  396. IFF_OACTIVE = $20000; // transmission in progress
  397. IFF_802_3 = $40000;
  398. IFF_CANONICAL = $80000;
  399. IFF_RUNNINGBLK = $100000; // threads waited for intf running
  400. { Interface enhanced flags }
  401. IFFE_PKTTRACE = $00000001; // trace datalink where possible
  402. IFFE_IPTRACE = $00000002; // trace ONLY IP packets
  403. { physical protocols IDs }
  404. HT_IP = $01; // IP
  405. HT_ETHER = $06; // Ethernet
  406. HT_ISO88023 = $07; // CSMA CD
  407. HT_ISO88025 = $09; // Token Ring
  408. HT_SLIP = $1c; // Serial Line IP
  409. HT_PPP = $18; // PPP IP
  410. IFNAMSIZ = 16; // interface name length
  411. { in.h / inet.h const & func }
  412. {
  413. * Protocols
  414. }
  415. IPPROTO_IP = 0; { dummy for IP }
  416. IPPROTO_ICMP = 1; { control message protocol }
  417. IPPROTO_GGP = 3; { gateway^2 (deprecated) }
  418. IPPROTO_TCP = 6; { tcp }
  419. IPPROTO_EGP = 8; { exterior gateway protocol }
  420. IPPROTO_PUP = 12; { pup }
  421. IPPROTO_UDP = 17; { user datagram protocol }
  422. IPPROTO_IDP = 22; { xns idp }
  423. IPPROTO_RAW = 255; { raw IP packet }
  424. IPPROTO_MAX = 256;
  425. {
  426. * Ports < IPPORT_RESERVED are reserved for
  427. * privileged processes (e.g. root).
  428. * Ports > IPPORT_USERRESERVED are reserved
  429. * for servers, not necessarily privileged.
  430. }
  431. IPPORT_RESERVED = 1024;
  432. IPPORT_USERRESERVED = 5000;
  433. {
  434. * Link numbers
  435. }
  436. IMPLINK_IP = 155;
  437. IMPLINK_LOWEXPER = 156;
  438. IMPLINK_HIGHEXPER = 158;
  439. {
  440. * Definitions of bits in internet address integers.
  441. * On subnets, the decomposition of addresses to host and net parts
  442. * is done according to subnet mask, not the masks here.
  443. }
  444. IN_CLASSA_NET = $ff000000;
  445. IN_CLASSA_NSHIFT = 24;
  446. IN_CLASSA_HOST = $00ffffff;
  447. IN_CLASSA_MAX = 128;
  448. IN_CLASSB_NET = $ffff0000;
  449. IN_CLASSB_NSHIFT = 16;
  450. IN_CLASSB_HOST = $0000ffff;
  451. IN_CLASSB_MAX = 65536;
  452. IN_CLASSC_NET = $ffffff00;
  453. IN_CLASSC_NSHIFT = 8;
  454. IN_CLASSC_HOST = $000000ff;
  455. INADDR_BROADCAST = $ffffffff; { must be masked }
  456. IN_LOOPBACKNET = 127; { official! }
  457. {*
  458. * Options for use with [gs]etsockopt at the IP level.
  459. * }
  460. IP_OPTIONS = 1; // buf/ip_opts; set/get IP options
  461. IP_MULTICAST_IF = 2; // u_char; set/get IP multicast i/f
  462. IP_MULTICAST_TTL = 3; // u_char; set/get IP multicast ttl
  463. IP_MULTICAST_LOOP = 4; // u_char; set/get IP multicast loopback
  464. IP_ADD_MEMBERSHIP = 5; // ip_mreq; add an IP group membership
  465. IP_DROP_MEMBERSHIP = 6; // ip_mreq; drop an IP group membership
  466. IP_HDRINCL = 7; // int; header is included with data
  467. IP_TOS = 8; // int; IP type of service and preced.
  468. IP_TTL = 9; // int; IP time to live
  469. IP_RECVOPTS = 10; // bool; receive all IP opts w/dgram
  470. IP_RECVRETOPTS = 11; // bool; receive IP opts for response
  471. IP_RECVDSTADDR = 12; // bool; receive IP dst addr w/dgram
  472. IP_RETOPTS = 13; // ip_opts; set/get IP options
  473. IP_RECVTRRI = 14; // bool; receive token ring routing inf
  474. IP_DEFAULT_MULTICAST_TTL = 1; // normally limit m'casts to 1 hop
  475. IP_DEFAULT_MULTICAST_LOOP = 1; // normally hear sends if a member
  476. IP_MAX_MEMBERSHIPS = 20; // per socket; must fit in one mbuf
  477. MAX_IN_MULTI = 16*IP_MAX_MEMBERSHIPS; // 320 max per os2
  478. type
  479. cushort=word;
  480. cuint16=word;
  481. cuint32=cardinal;
  482. size_t =cuint32;
  483. ssize_t=cuint16;
  484. cint =longint;
  485. pcint =^cint;
  486. tsocklen=cint;
  487. psocklen=^tsocklen;
  488. function InitEMXHandles: boolean;
  489. (* This procedure shall be called before touching any socket. Once called, *)
  490. (* it forces dynamic loading of emx.dll and all functions start with socket *)
  491. (* handles compatible to EMX in order to allow interworking with external *)
  492. (* libraries using EMX libc (e.g. OpenSSL compiled with EMX port of GCC). *)
  493. (* It returns true in case of successful initialization, false otherwise. *)
  494. function CheckEMXHandles: boolean;
  495. (* This function checks whether EMX compatible socket handles are used. *)
  496. function EMXSocket (ANativeSocket: cInt): cInt;
  497. function NativeSocket (AEMXSocket: cInt): cInt;
  498. // OS/2 stack based on BSD stack
  499. {$DEFINE BSD}
  500. {$I socketsh.inc}
  501. INVALID_SOCKET = TSocket(not(0));
  502. SOCKET_ERROR = -1;
  503. Implementation
  504. uses
  505. DosCalls;
  506. {Include filerec and textrec structures}
  507. {$I filerec.inc}
  508. {$I textrec.inc}
  509. {******************************************************************************
  510. Basic Socket Functions
  511. ******************************************************************************}
  512. const
  513. EMXHandles: boolean = false;
  514. EMXSysCall: pointer = nil;
  515. EMXLibHandle: THandle = THandle (-1);
  516. function CheckEMXHandles: boolean;
  517. begin
  518. CheckEMXHandles := EMXHandles;
  519. end;
  520. function InitEMXHandles: boolean;
  521. const
  522. EMXLib: string [8] = 'emx.dll'#0;
  523. CBufLen = 260;
  524. var
  525. CBuf: array [1..CBufLen] of char;
  526. begin
  527. if not EMXHandles then
  528. begin
  529. if DosLoadModule (@CBuf [1], SizeOf (CBuf), @EMXLib [1], EMXLibHandle) = 0
  530. then
  531. begin
  532. if DosQueryProcAddr (EMXLibHandle, 2, nil, EMXSysCall) = 0 then
  533. EMXHandles := true;
  534. end;
  535. InitEMXHandles := EMXHandles;
  536. end;
  537. end;
  538. {$ASMMODE INTEL}
  539. function EMXSocket (ANativeSocket: cInt): cInt; assembler;
  540. asm
  541. or EMXHandles, 0
  542. jz @EMXSocketEnd
  543. mov edx, eax
  544. mov eax, 7F54h
  545. mov ecx, 0
  546. call EMXSysCall
  547. @EMXSocketEnd:
  548. end;
  549. function NativeSocket (AEMXSocket: cInt): cInt; assembler;
  550. asm
  551. or EMXHandles, 0
  552. jz @NativeSocketEnd
  553. push ebx
  554. mov ebx, eax
  555. mov eax, 7F3Bh
  556. call EMXSysCall
  557. pop ebx
  558. @NativeSocketEnd:
  559. end;
  560. function SocketError: cint;
  561. begin
  562. SocketError := so32dll.Sock_ErrNo;
  563. end;
  564. Function Socket(Domain,SocketType,Protocol:Longint):Longint;
  565. begin
  566. Socket := fpSocket (Domain, SocketType, Protocol);
  567. end;
  568. Function Send(Sock:Longint;Const Buf;BufLen,Flags:Longint):Longint;
  569. begin
  570. Send:=fpSend(Sock,@Buf,BufLen,Flags);
  571. end;
  572. Function SendTo(Sock:Longint;Const Buf;BufLen,Flags:Longint;Var Addr; AddrLen : Longint):Longint;
  573. begin
  574. SendTo:=fpSendTo(Sock,@Buf,BufLen,Flags,@Addr,AddrLen);
  575. end;
  576. Function Recv(Sock:Longint;Var Buf;BufLen,Flags:Longint):Longint;
  577. begin
  578. Sock := NativeSocket (Sock);
  579. Recv:=so32dll.Recv(Sock,Buf,BufLen,Flags);
  580. end;
  581. Function RecvFrom(Sock : Longint; Var Buf; Buflen,Flags : Longint; Var Addr; var AddrLen : longInt) : longint;
  582. begin
  583. Sock := NativeSocket (Sock);
  584. RecvFrom:=so32dll.RecvFrom(Sock,Buf,BufLen,Flags,so32dll.SockAddr(Addr),AddrLen);
  585. end;
  586. Function Bind(Sock:Longint;Const Addr;AddrLen:Longint):Boolean;
  587. begin
  588. Bind:=fpBind(Sock,@Addr,AddrLen)=0;
  589. end;
  590. Function Listen(Sock,MaxConnect:Longint):Boolean;
  591. begin
  592. Sock := NativeSocket (Sock);
  593. Listen := so32dll.Listen(Sock,MaxConnect) = 0;
  594. end;
  595. Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  596. begin
  597. Sock := NativeSocket (Sock);
  598. Accept:=so32dll.Accept(Sock,so32dll.SockAddr(Addr), AddrLen);
  599. end;
  600. Function Connect(Sock:Longint;const Addr; Addrlen:Longint):Boolean;
  601. begin
  602. Connect:=fpConnect(Sock,@Addr,AddrLen)=0;
  603. end;
  604. Function Shutdown(Sock:Longint;How:Longint):Longint;
  605. begin
  606. ShutDown:=fpShutDown(Sock,How);
  607. end;
  608. Function GetSocketName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  609. begin
  610. Sock := NativeSocket (Sock);
  611. GetSocketName:=so32dll.GetSockName(Sock, so32dll.SockAddr(Addr),AddrLen);
  612. end;
  613. Function GetPeerName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  614. begin
  615. Sock := NativeSocket (Sock);
  616. GetPeerName:=so32dll.GetPeerName(Sock,so32dll.SockAddr(Addr),AddrLen);
  617. end;
  618. Function SetSocketOptions(Sock,Level,OptName:Longint;Const OptVal;optlen:longint):Longint;
  619. begin
  620. SetSocketOptions:=fpSetSockOpt(Sock,Level,OptName,@OptVal,OptLen);
  621. end;
  622. Function GetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;Var optlen:longint):Longint;
  623. begin
  624. Sock := NativeSocket (Sock);
  625. GetSocketOptions:=so32dll.GetSockOpt(Sock,Level,OptName,OptVal,OptLen);
  626. end;
  627. Function SocketPair(Domain,SocketType,Protocol:Longint;var Pair:TSockArray):Longint;
  628. begin
  629. {!!TODO!!
  630. SocketPair:=so32dll.socketpair(Domain,SocketType,Protocol,Pair);}
  631. //SocketCall(Socket_Sys_SocketPair,Domain,SocketType,Protocol,longint(@Pair),0,0);
  632. SocketPair:=-1;
  633. end;
  634. { mimic the linux fpWrite/fpRead calls for the file/text socket wrapper }
  635. function fpWrite(handle : longint;Const bufptr;size : dword) : dword;
  636. begin
  637. fpWrite := dword(fpsend(handle, @bufptr, size, 0));
  638. if fpWrite = dword(-1) then
  639. fpWrite := 0;
  640. end;
  641. function fpRead(handle : longint;var bufptr;size : dword) : dword;
  642. var
  643. d : dword;
  644. begin
  645. Handle := NativeSocket (Handle);
  646. d:=dword(so32dll.os2_ioctl(handle,FIONREAD,d,SizeOf(d)));
  647. if d=dword(-1) then
  648. fpRead:=0
  649. else
  650. begin
  651. if size>d then
  652. size:=d;
  653. fpRead := dword(so32dll.recv(handle, bufptr, size, 0));
  654. if fpRead = dword(-1) then
  655. fpRead := 0
  656. end;
  657. end;
  658. {$i sockets.inc}
  659. function fpsocket (domain:cint; xtype:cint; protocol: cint):cint;
  660. begin
  661. if EMXHandles then
  662. fpSocket := EMXSocket (so32dll.Socket (Domain, xtype, Protocol))
  663. else
  664. fpSocket:=so32dll.Socket(Domain,xtype,Protocol);
  665. end;
  666. function fpsend (s:cint; msg:pointer; len:size_t; flags:cint):ssize_t;
  667. begin
  668. S := NativeSocket (S);
  669. fpSend:=so32dll.Send(S,msg^,len,flags);
  670. end;
  671. function fpsendto (s:cint; msg:pointer; len:size_t; flags:cint; tox :psockaddr; tolen: tsocklen):ssize_t;
  672. begin
  673. S := NativeSocket (S);
  674. // Dubious construct, this should be checked. (IPV6 fails ?)
  675. fpSendTo:=so32dll.SendTo(S,msg^,Len,Flags,so32dll.SockAddr(tox^),toLen);
  676. end;
  677. function fprecv (s:cint; buf: pointer; len: size_t; flags: cint):ssize_t;
  678. begin
  679. S := NativeSocket (S);
  680. fpRecv:=so32dll.Recv(S,Buf,Len,Flags);
  681. end;
  682. function fprecvfrom (s:cint; buf: pointer; len: size_t; flags: cint; from : psockaddr; fromlen : psocklen):ssize_t;
  683. begin
  684. S := NativeSocket (S);
  685. fpRecvFrom:=so32dll.RecvFrom(S,Buf,Len,Flags,so32dll.SockAddr(from^),FromLen^);
  686. end;
  687. function fpconnect (s:cint; name : psockaddr; namelen : tsocklen):cint;
  688. begin
  689. S := NativeSocket (S);
  690. fpConnect:=so32dll.Connect(S,so32dll.SockAddr(name^),nameLen);
  691. end;
  692. function fpshutdown (s:cint; how:cint):cint;
  693. begin
  694. S := NativeSocket (S);
  695. fpShutDown:=so32dll.ShutDown(S,How);
  696. end;
  697. function fpbind (s:cint; addrx : psockaddr; addrlen : tsocklen):cint;
  698. begin
  699. S := NativeSocket (S);
  700. fpbind:=so32dll.Bind(S,so32dll.SockAddr(Addrx^),AddrLen);
  701. end;
  702. function fplisten (s:cint; backlog : cint):cint;
  703. begin
  704. S := NativeSocket (S);
  705. fplisten:=so32dll.Listen(S,backlog);
  706. end;
  707. function fpaccept (s:cint; addrx : psockaddr; addrlen : psocklen):cint;
  708. begin
  709. S := NativeSocket (S);
  710. fpAccept:=so32dll.Accept(S,so32dll.SockAddr(Addrx^),longint(@AddrLen));
  711. end;
  712. function fpgetsockname (s:cint; name : psockaddr; namelen : psocklen):cint;
  713. begin
  714. S := NativeSocket (S);
  715. fpGetSockName:=so32dll.GetSockName(S,so32dll.SockAddr(name^),nameLen^);
  716. end;
  717. function fpgetpeername (s:cint; name : psockaddr; namelen : psocklen):cint;
  718. begin
  719. S := NativeSocket (S);
  720. fpGetPeerName:=so32dll.GetPeerName(S,so32dll.SockAddr(name^),NameLen^);
  721. end;
  722. function fpgetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : psocklen):cint;
  723. begin
  724. S := NativeSocket (S);
  725. fpGetSockOpt:=so32dll.GetSockOpt(S,Level,OptName,OptVal,OptLen^);
  726. end;
  727. function fpsetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen :tsocklen):cint;
  728. begin
  729. S := NativeSocket (S);
  730. fpSetSockOpt:=so32dll.SetSockOpt(S,Level,OptName,OptVal,OptLen);
  731. end;
  732. function fpsocketpair (d:cint; xtype:cint; protocol:cint; sv:pcint):cint;
  733. begin
  734. fpsocketpair:=-1;
  735. end;
  736. Function CloseSocket(Sock:Longint):Longint;
  737. begin
  738. Sock := NativeSocket (Sock);
  739. CloseSocket:=so32dll.soclose (Sock);
  740. end;
  741. Begin
  742. so32dll.sock_init;
  743. End.