socketsh.inc 9.7 KB

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