sockets.pas 29 KB

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