socketsh.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$IFNDEF ver1_0}
  12. {$IFNDEF ver1_9_4}
  13. {$INLINE ON}
  14. {$define HASINLINE}
  15. {$endif}
  16. {$ENDIF}
  17. {$ifdef FreeBSD}
  18. {$DEFINE SOCK_HAS_SINLEN} // BSD definition of scoketaddr
  19. {$endif}
  20. Type
  21. {$ifdef SOCK_HAS_SINLEN}
  22. sa_family_t=cuchar;
  23. {$else}
  24. sa_family_t=cushort;
  25. {$endif}
  26. Const
  27. { Socket types }
  28. SOCK_STREAM = 1; { stream (connection) socket }
  29. SOCK_DGRAM = 2; { datagram (conn.less) socket }
  30. SOCK_RAW = 3; { raw socket }
  31. SOCK_RDM = 4; { reliably-delivered message }
  32. SOCK_SEQPACKET = 5; { sequential packet socket }
  33. { Protocol families }
  34. PF_UNSPEC = 0; { Unspecified }
  35. PF_LOCAL = 1; { Local to host (pipes and file-domain) }
  36. PF_UNIX = PF_LOCAL; { Old BSD name for PF_LOCAL }
  37. PF_FILE = PF_LOCAL; { Another non-standard name for PF_LOCAL }
  38. PF_INET = 2; { IP protocol family }
  39. { Address families }
  40. AF_UNSPEC = PF_UNSPEC;
  41. AF_LOCAL = PF_LOCAL;
  42. AF_UNIX = PF_UNIX;
  43. AF_FILE = PF_FILE;
  44. AF_INET = PF_INET;
  45. { Flags for send, recv etc. }
  46. MSG_OOB = $0001; { Process out-of-band data}
  47. MSG_PEEK = $0002; { Peek at incoming messages }
  48. MSG_DONTROUTE= $0004; { Don't use local routing }
  49. MSG_TRYHARD = MSG_DONTROUTE;
  50. MSG_CTRUNC = $0008; { Control data lost before delivery }
  51. MSG_PROXY = $0010; { Supply or ask second address }
  52. MSG_TRUNC = $0020;
  53. MSG_DONTWAIT = $0040; { Non-blocking I/O }
  54. MSG_EOR = $0080; { End of record }
  55. MSG_WAITALL = $0100; { Wait for a full request }
  56. MSG_FIN = $0200;
  57. MSG_SYN = $0400;
  58. MSG_CONFIRM = $0800; { Confirm path validity }
  59. MSG_RST = $1000;
  60. MSG_ERRQUERE = $2000; { Fetch message from error queue }
  61. MSG_NOSIGNAL = $4000; { Do not generate SIGPIPE }
  62. MSG_MORE = $8000; { Sender will send more }
  63. const
  64. { Two constants to determine whether part of soket is for in or output }
  65. S_IN = 0;
  66. S_OUT = 1;
  67. Type
  68. in_addr = packed record
  69. s_addr : cuint32; // inaddr_t=cuint32
  70. end;
  71. TSockAddr = packed Record
  72. {$ifdef SOCK_HAS_SINLEN}
  73. sa_len : cuchar;
  74. {$endif}
  75. sa_family : sa_family_t;
  76. sa_data : array [0..13] of char;
  77. end;
  78. pSockAddr = ^TSockAddr;
  79. TInetSockAddr = packed Record
  80. case boolean of
  81. false : (
  82. {$ifdef SOCK_HAS_SINLEN}
  83. sin_len : cuchar;
  84. {$endif}
  85. sin_family : sa_family_t;
  86. sin_port : cushort;
  87. sin_addr : in_addr;
  88. xpad : array [0..7] of char; { to get to the size of sockaddr... }
  89. );
  90. true: (
  91. {$ifdef SOCK_HAS_SINLEN}
  92. len : cuchar;
  93. {$endif}
  94. family : sa_family_t;
  95. port : cushort;
  96. addr : cardinal;
  97. pad : array [0..7] of char; { to get to the size of sockaddr... }
  98. );
  99. end;
  100. pInetSockAddr = ^TInetSockAddr;
  101. Tin6_addr = packed record
  102. case byte of
  103. 0: (u6_addr8 : array[0..15] of byte);
  104. 1: (u6_addr16 : array[0..7] of Word);
  105. 2: (u6_addr32 : array[0..3] of Cardinal);
  106. 3: (s6_addr8 : array[0..15] of shortint);
  107. 4: (s6_addr : array[0..15] of shortint);
  108. 5: (s6_addr16 : array[0..7] of smallint);
  109. 6: (s6_addr32 : array[0..3] of LongInt);
  110. end;
  111. pIn6_Addr=^TIn6_addr;
  112. TInetSockAddr6 = packed Record
  113. {$ifdef SOCKET_HAS_SINLEN} // as per RFC 2553
  114. sin6_len : byte;
  115. {$endif}
  116. sin6_family : sa_family_t;
  117. sin6_port : cuint16;
  118. sin6_flowinfo : cuint32;
  119. sin6_addr : Tin6_addr;
  120. sin6_scope_id : cuint32;
  121. end;
  122. sockaddr_in6 = TInetSockAddr6;
  123. psockaddr_in6 = ^sockaddr_in6;
  124. TSockPairArray = Array[0..1] of Longint;
  125. TSockArray = Array[1..2] of Longint; //legacy
  126. Var
  127. SocketError:cint;
  128. function fpsocket (domain:cint; xtype:cint; protocol: cint):cint; maybelibc
  129. function fprecv (s:cint; buf: pointer; len: size_t; flags: cint):ssize_t; maybelibc
  130. function fprecvfrom (s:cint; buf: pointer; len: size_t; flags: cint; from : psockaddr; fromlen : psocklen):ssize_t; maybelibc
  131. function fpsend (s:cint; msg:pointer; len:size_t; flags:cint):ssize_t; maybelibc
  132. function fpsendto (s:cint; msg:pointer; len:size_t; flags:cint; tox :psockaddr; tolen: tsocklen):ssize_t; maybelibc
  133. function fpbind (s:cint; addrx : psockaddr; addrlen : tsocklen):cint; maybelibc
  134. function fplisten (s:cint; backlog : cint):cint; maybelibc
  135. function fpaccept (s:cint; addrx : psockaddr; addrlen : psocklen):cint; maybelibc
  136. function fpconnect (s:cint; name : psockaddr; namelen : tsocklen):cint; maybelibc
  137. function fpshutdown (s:cint; how:cint):cint; maybelibc
  138. function fpgetsockname (s:cint; name : psockaddr; namelen : psocklen):cint; maybelibc
  139. function fpgetpeername (s:cint; name : psockaddr; namelen : psocklen):cint; maybelibc
  140. function fpgetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : psocklen):cint; maybelibc
  141. function fpsetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : tsocklen):cint; maybelibc
  142. function fpsocketpair (d:cint; xtype:cint; protocol:cint; sv:pcint):cint; maybelibc
  143. {Basic Socket Functions}
  144. Function Socket(Domain,SocketType,Protocol:Longint):Longint;
  145. Function CloseSocket(Sock:Longint):Longint;
  146. Function Send(Sock:Longint;Const Buf;BufLen,Flags:Longint):Longint;
  147. Function SendTo(Sock:Longint;Const Buf;BufLen,Flags:Longint;Var Addr; AddrLen : Longint):Longint;
  148. Function Recv(Sock:Longint;Var Buf;BufLen,Flags:Longint):Longint;
  149. Function RecvFrom(Sock : Longint; Var Buf; Buflen,Flags : Longint; Var Addr; var AddrLen : longInt) : longint;
  150. Function Bind(Sock:Longint;Const Addr;AddrLen:Longint):Boolean;
  151. Function Listen (Sock,MaxConnect:Longint):Boolean;
  152. Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  153. Function Connect(Sock:Longint;Const Addr;Addrlen:Longint):boolean;
  154. Function Shutdown(Sock:Longint;How:Longint):Longint;
  155. Function GetSocketName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  156. Function GetPeerName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  157. Function SetSocketOptions(Sock,Level,OptName:Longint;const OptVal;optlen:longint):Longint;
  158. Function GetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;Var optlen:longint):Longint;
  159. Function SocketPair(Domain,SocketType,Protocol:Longint;var Pair:TSockArray):Longint;
  160. {Text Support}
  161. Procedure Sock2Text(Sock:Longint;Var SockIn,SockOut:Text);
  162. {Untyped File Support}
  163. Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);
  164. {Better Pascal Calling, Overloaded Functions!}
  165. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
  166. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
  167. Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
  168. Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
  169. function htonl( host : longint):longint; {$ifdef HASINLINE} inline; {$endif}
  170. Function NToHl (Net : Longint) : Longint; {$ifdef HASINLINE} inline; {$endif}
  171. function htons( host : word):word; {$ifdef HASINLINE} inline; {$endif}
  172. Function NToHs (Net : word):word; {$ifdef HASINLINE} inline; {$endif}
  173. {
  174. $Log$
  175. Revision 1.18 2004-11-01 19:39:19 peter
  176. * disable inline for 1.9.4
  177. Revision 1.17 2004/11/01 17:35:22 marco
  178. * typo fixed, aarght
  179. Revision 1.16 2004/11/01 17:29:47 marco
  180. * inline problems fixed
  181. Revision 1.15 2004/11/01 16:23:15 marco
  182. * htons etc
  183. Revision 1.14 2004/03/16 19:15:57 marco
  184. * sockaddr is now a union between the old and new struct as grace period
  185. Revision 1.13 2004/03/16 18:03:37 marco
  186. * first changes sockets units
  187. Revision 1.12 2003/11/23 11:00:07 michael
  188. + Added IPV6 patch from Johannes Berg
  189. Revision 1.11 2003/11/22 16:28:56 michael
  190. + Added several constants
  191. Revision 1.10 2003/11/09 21:43:15 michael
  192. + Added some missing socket options and the shut_* constants
  193. Revision 1.9 2003/03/23 17:47:15 armin
  194. * CloseSocket added
  195. Revision 1.8 2002/09/07 15:07:46 peter
  196. * old logs removed and tabs fixed
  197. Revision 1.7 2002/02/04 21:29:34 michael
  198. + merged missing sendto/rcvfrom functions
  199. }