socketsh.inc 8.1 KB

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