socketsh.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Type
  18. {$ifdef SOCK_HAS_SINLEN}
  19. sa_family_t=cuchar;
  20. {$else}
  21. sa_family_t=cushort;
  22. {$endif}
  23. Const
  24. { Socket types }
  25. SOCK_STREAM = 1; { stream (connection) socket }
  26. SOCK_DGRAM = 2; { datagram (conn.less) socket }
  27. SOCK_RAW = 3; { raw socket }
  28. SOCK_RDM = 4; { reliably-delivered message }
  29. SOCK_SEQPACKET = 5; { sequential packet socket }
  30. { Protocol families }
  31. PF_UNSPEC = 0; { Unspecified }
  32. PF_LOCAL = 1; { Local to host (pipes and file-domain) }
  33. PF_UNIX = PF_LOCAL; { Old BSD name for PF_LOCAL }
  34. PF_FILE = PF_LOCAL; { Another non-standard name for PF_LOCAL }
  35. PF_INET = 2; { IP protocol family }
  36. { Address families }
  37. AF_UNSPEC = PF_UNSPEC;
  38. AF_LOCAL = PF_LOCAL;
  39. AF_UNIX = PF_UNIX;
  40. AF_FILE = PF_FILE;
  41. AF_INET = PF_INET;
  42. { Flags for send, recv etc. }
  43. MSG_OOB = $0001; { Process out-of-band data}
  44. MSG_PEEK = $0002; { Peek at incoming messages }
  45. MSG_DONTROUTE= $0004; { Don't use local routing }
  46. MSG_TRYHARD = MSG_DONTROUTE;
  47. MSG_CTRUNC = $0008; { Control data lost before delivery }
  48. MSG_PROXY = $0010; { Supply or ask second address }
  49. MSG_TRUNC = $0020;
  50. MSG_DONTWAIT = $0040; { Non-blocking I/O }
  51. MSG_EOR = $0080; { End of record }
  52. MSG_WAITALL = $0100; { Wait for a full request }
  53. MSG_FIN = $0200;
  54. MSG_SYN = $0400;
  55. MSG_CONFIRM = $0800; { Confirm path validity }
  56. MSG_RST = $1000;
  57. MSG_ERRQUERE = $2000; { Fetch message from error queue }
  58. MSG_NOSIGNAL = $4000; { Do not generate SIGPIPE }
  59. MSG_MORE = $8000; { Sender will send more }
  60. const
  61. { Two constants to determine whether part of soket is for in or output }
  62. S_IN = 0;
  63. S_OUT = 1;
  64. Type
  65. in_addr = packed record
  66. case boolean of
  67. true: (s_addr : cuint32); // inaddr_t=cuint32
  68. false: (s_bytes : packed array[1..4] of byte);
  69. end;
  70. TIn_addr = in_addr;
  71. pin_addr = ^in_addr;
  72. in_addrbytes = packed array [1..4] of byte;
  73. TSockAddr = packed Record
  74. {$ifdef SOCK_HAS_SINLEN}
  75. sa_len : cuchar;
  76. {$endif}
  77. sa_family : sa_family_t;
  78. sa_data : array [0..13] of char;
  79. end;
  80. PSockAddr = ^TSockAddr;
  81. Sockaddr = TSockAddr; // Kylix compat
  82. TInetSockAddr = packed Record
  83. case boolean of
  84. false : (
  85. {$ifdef SOCK_HAS_SINLEN}
  86. sin_len : cuchar;
  87. {$endif}
  88. sin_family : sa_family_t;
  89. sin_port : cushort;
  90. sin_addr : in_addr;
  91. xpad : array [0..7] of char; { to get to the size of sockaddr... }
  92. );
  93. true: (
  94. {$ifdef SOCK_HAS_SINLEN}
  95. len : cuchar;
  96. {$endif}
  97. family : sa_family_t;
  98. port : cushort;
  99. addr : cardinal;
  100. pad : array [0..7] of char; { to get to the size of sockaddr... }
  101. );
  102. end;
  103. pInetSockAddr = ^TInetSockAddr;
  104. TInAddr = TInetSockAddr;
  105. Tin6_addr = packed record
  106. case byte of
  107. 0: (u6_addr8 : array[0..15] of byte);
  108. 1: (u6_addr16 : array[0..7] of Word);
  109. 2: (u6_addr32 : array[0..3] of Cardinal);
  110. 3: (s6_addr8 : array[0..15] of shortint);
  111. 4: (s6_addr : array[0..15] of shortint);
  112. 5: (s6_addr16 : array[0..7] of smallint);
  113. 6: (s6_addr32 : array[0..3] of LongInt);
  114. end;
  115. pIn6_Addr=^TIn6_addr;
  116. TInetSockAddr6 = packed Record
  117. {$ifdef SOCKET_HAS_SINLEN} // as per RFC 2553
  118. sin6_len : byte;
  119. {$endif}
  120. sin6_family : sa_family_t;
  121. sin6_port : cuint16;
  122. sin6_flowinfo : cuint32;
  123. sin6_addr : Tin6_addr;
  124. sin6_scope_id : cuint32;
  125. end;
  126. sockaddr_in6 = TInetSockAddr6;
  127. psockaddr_in6 = ^sockaddr_in6;
  128. TSockPairArray = Array[0..1] of Longint;
  129. TSockArray = Array[1..2] of Longint; //legacy
  130. Var
  131. SocketError:cint;
  132. function fpsocket (domain:cint; xtype:cint; protocol: cint):cint;
  133. function fprecv (s:cint; buf: pointer; len: size_t; flags: cint):ssize_t;
  134. function fprecvfrom (s:cint; buf: pointer; len: size_t; flags: cint; from : psockaddr; fromlen : psocklen):ssize_t;
  135. function fpsend (s:cint; msg:pointer; len:size_t; flags:cint):ssize_t;
  136. function fpsendto (s:cint; msg:pointer; len:size_t; flags:cint; tox :psockaddr; tolen: tsocklen):ssize_t;
  137. function fpbind (s:cint; addrx : psockaddr; addrlen : tsocklen):cint;
  138. function fplisten (s:cint; backlog : cint):cint;
  139. function fpaccept (s:cint; addrx : psockaddr; addrlen : psocklen):cint;
  140. function fpconnect (s:cint; name : psockaddr; namelen : tsocklen):cint;
  141. function fpshutdown (s:cint; how:cint):cint;
  142. function fpgetsockname (s:cint; name : psockaddr; namelen : psocklen):cint;
  143. function fpgetpeername (s:cint; name : psockaddr; namelen : psocklen):cint;
  144. function fpgetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : psocklen):cint;
  145. function fpsetsockopt (s:cint; level:cint; optname:cint; optval:pointer; optlen : tsocklen):cint;
  146. function fpsocketpair (d:cint; xtype:cint; protocol:cint; sv:pcint):cint;
  147. {Basic Socket Functions}
  148. Function Socket(Domain,SocketType,Protocol:Longint):Longint;
  149. Function CloseSocket(Sock:Longint):Longint;
  150. Function Send(Sock:Longint;Const Buf;BufLen,Flags:Longint):Longint;
  151. Function SendTo(Sock:Longint;Const Buf;BufLen,Flags:Longint;Var Addr; AddrLen : Longint):Longint;
  152. Function Recv(Sock:Longint;Var Buf;BufLen,Flags:Longint):Longint;
  153. Function RecvFrom(Sock : Longint; Var Buf; Buflen,Flags : Longint; Var Addr; var AddrLen : longInt) : longint;
  154. Function Bind(Sock:Longint;Const Addr;AddrLen:Longint):Boolean;
  155. Function Listen (Sock,MaxConnect:Longint):Boolean;
  156. Function Accept(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  157. Function Connect(Sock:Longint;Const Addr;Addrlen:Longint):boolean;
  158. Function Shutdown(Sock:Longint;How:Longint):Longint;
  159. Function GetSocketName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  160. Function GetPeerName(Sock:Longint;Var Addr;Var Addrlen:Longint):Longint;
  161. Function SetSocketOptions(Sock,Level,OptName:Longint;const OptVal;optlen:longint):Longint;
  162. Function GetSocketOptions(Sock,Level,OptName:Longint;Var OptVal;Var optlen:longint):Longint;
  163. Function SocketPair(Domain,SocketType,Protocol:Longint;var Pair:TSockArray):Longint;
  164. {Text Support}
  165. Procedure Sock2Text(Sock:Longint;Var SockIn,SockOut:Text);
  166. {Untyped File Support}
  167. Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);
  168. {Better Pascal Calling, Overloaded Functions!}
  169. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
  170. Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
  171. Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
  172. Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
  173. { Utility routines}
  174. function htonl( host : longint):longint; {$ifdef HASINLINE} inline; {$endif}
  175. Function NToHl( Net : Longint) : Longint; {$ifdef HASINLINE} inline; {$endif}
  176. function htons( host : word):word; {$ifdef HASINLINE} inline; {$endif}
  177. Function NToHs( Net : word):word; {$ifdef HASINLINE} inline; {$endif}
  178. function NetAddrToStr (Entry : in_addr) : AnsiString;
  179. function HostAddrToStr(Entry : in_addr) : AnsiString;
  180. function StrToHostAddr(IP : AnsiString) : in_addr ;
  181. function StrToNetAddr (IP : AnsiString) : in_addr;
  182. { these for are for netdb legacy compat}
  183. Function HostToNet (Host : in_addr) : in_addr;
  184. Function NetToHost (Net : in_addr) : in_addr;
  185. Function HostToNet (Host : Longint) : Longint;
  186. Function NetToHost (Net : Longint) : Longint;
  187. Function ShortHostToNet(Host : Word) : Word;
  188. Function ShortNetToHost(Net : Word) : Word;
  189. // ipv6
  190. function HostAddrToStr6(Entry : Tin6_addr) : AnsiString;
  191. function StrToHostAddr6(IP : String) : Tin6_addr; // not implemented?!?
  192. function NetAddrToStr6 (Entry: Tin6_addr) : AnsiString;
  193. function StrToNetAddr6 (IP : AnsiString) : TIn6_Addr;
  194. CONST
  195. NoAddress : in_addr = (s_addr:0);
  196. NoNet : in_addr = (s_addr:0);
  197. NoAddress6: Tin6_addr = (u6_addr16:(0,0,0,0,0,0,0,0));
  198. NoNet6 : Tin6_addr = (u6_addr16:(0,0,0,0,0,0,0,0));
  199. {
  200. $Log$
  201. Revision 1.26 2005-02-18 13:10:10 marco
  202. * noadress and friend, ipv4 changed to an enum.
  203. Revision 1.25 2005/02/14 17:13:26 peter
  204. * truncate log
  205. Revision 1.24 2005/02/13 21:10:31 marco
  206. * in_addr_bytes to easily put an array over the byte type
  207. Revision 1.23 2005/02/13 19:59:57 marco
  208. * More htons like functionality. IPV6 string support still missing
  209. Revision 1.22 2005/02/12 17:34:56 marco
  210. * some kylix stuf
  211. }