sockets.tex 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The SOCKETS unit.}
  22. This chapter describes the SOCKETS unit for Free Pascal.
  23. it was written for \linux by Micha\"el Van Canneyt.
  24. The chapter is divided in 2 sections:
  25. \begin{itemize}
  26. \item The first section lists types, constants and variables from the
  27. interface part of the unit.
  28. \item The second section describes the functions defined in the unit.
  29. \end{itemize}
  30. \section {Types, Constants and variables : }
  31. The following constants identify the different socket types, as needed in
  32. the \seef{Socket} call.
  33. \begin{verbatim}
  34. SOCK_STREAM = 1; { stream (connection) socket }
  35. SOCK_DGRAM = 2; { datagram (conn.less) socket }
  36. SOCK_RAW = 3; { raw socket }
  37. SOCK_RDM = 4; { reliably-delivered message }
  38. SOCK_SEQPACKET = 5; { sequential packet socket }
  39. SOCK_PACKET =10;
  40. \end{verbatim}
  41. The following constants determine the socket domain, they are used in the
  42. \seef{Socket} call.
  43. \begin{verbatim}
  44. AF_UNSPEC = 0;
  45. AF_UNIX = 1; { Unix domain sockets }
  46. AF_INET = 2; { Internet IP Protocol }
  47. AF_AX25 = 3; { Amateur Radio AX.25 }
  48. AF_IPX = 4; { Novell IPX }
  49. AF_APPLETALK = 5; { Appletalk DDP }
  50. AF_NETROM = 6; { Amateur radio NetROM }
  51. AF_BRIDGE = 7; { Multiprotocol bridge }
  52. AF_AAL5 = 8; { Reserved for Werner's ATM }
  53. AF_X25 = 9; { Reserved for X.25 project }
  54. AF_INET6 = 10; { IP version 6 }
  55. AF_MAX = 12;
  56. \end{verbatim}
  57. The following constants determine the protocol family, they are used in the
  58. \seef{Socket} call.
  59. \begin{verbatim}
  60. PF_UNSPEC = AF_UNSPEC;
  61. PF_UNIX = AF_UNIX;
  62. PF_INET = AF_INET;
  63. PF_AX25 = AF_AX25;
  64. PF_IPX = AF_IPX;
  65. PF_APPLETALK = AF_APPLETALK;
  66. PF_NETROM = AF_NETROM;
  67. PF_BRIDGE = AF_BRIDGE;
  68. PF_AAL5 = AF_AAL5;
  69. PF_X25 = AF_X25;
  70. PF_INET6 = AF_INET6;
  71. PF_MAX = AF_MAX;
  72. \end{verbatim}
  73. The following types are used to store different kinds of eddresses for the
  74. \seef{Bind}, \seef{Recv} and \seef{Send} calls.
  75. \begin{verbatim}
  76. TSockAddr = packed Record
  77. family:word;
  78. data :array [0..13] of char;
  79. end;
  80. TUnixSockAddr = packed Record
  81. family:word;
  82. path:array[0..108] of char;
  83. end;
  84. TInetSockAddr = packed Record
  85. family:Word;
  86. port :Word;
  87. addr :Cardinal;
  88. pad :array [1..8] of byte;
  89. end;
  90. \end{verbatim}
  91. The following type is returned by the \seef{SocketPair} call.
  92. \begin{verbatim}
  93. TSockArray = Array[1..2] of Longint;
  94. \end{verbatim}
  95. \section {Functions and Procedures}
  96. \function{Socket}{(Domain,SocketType,Protocol:Longint)}{Longint}
  97. {\var{Socket} creates a new socket in domain \var{Domain}, from type
  98. \var{SocketType} using protocol \var{Protocol}.
  99. The Domain, Socket type and Protocol can be specified using predefined
  100. constants (see the section on constants for available constants)
  101. If succesfull, the function returns a socket descriptor, which can be passed
  102. to a subsequent \seef{Bind} call. If unsuccesfull, the function returns -1.
  103. }
  104. {Errors are returned in \var{SocketError}, and include the follwing:
  105. \begin{description}
  106. \item[SYS\_EPROTONOSUPPORT]
  107. The protocol type or the specified protocol is not
  108. supported within this domain.
  109. \item[SYS\_EMFILE]
  110. The per-process descriptor table is full.
  111. \item[SYS\_ENFILE]
  112. The system file table is full.
  113. \item[SYS\_EACCESS]
  114. Permission to create a socket of the specified
  115. type and/or protocol is denied.
  116. \item[SYS\_ENOBUFS]
  117. Insufficient buffer space is available. The
  118. socket cannot be created until sufficient
  119. resources are freed.
  120. \end{description}}
  121. {\seef{SocketPair}, \seem{socket}{2}}
  122. for an example, see \seef{Accept}.
  123. \function{Send}{(Sock:Longint;Var Addr;AddrLen,Flags:Longint)}{Longint}
  124. {\var{Send} sends \var{AddrLen} bytes starting from address \var{Addr}
  125. to socket \var{Sock}. \var{Sock} must be in a connected state.
  126. The function returns the number of bytes sent, or -1 if a detectable
  127. error occurred.
  128. \var{Flags} can be one of the following:
  129. \begin{description}
  130. \item [1] : Process out-of band data.
  131. \item [4] : Bypass routing, use a direct interface.
  132. \end{description}
  133. }
  134. {Errors are reported in \var{SocketError}, and include the following:
  135. \begin{description}
  136. \item[SYS\_EBADF] The socket descriptor is invalid.
  137. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  138. \item[SYS\_EFAULT] The address is outside your address space.
  139. \item[SYS\_EMSGSIZE] The message cannot be sent atomically.
  140. \item[SYS\_EWOULDBLOCK] The requested operation would block the process.
  141. \item[SYS\_ENOBUFS] The system doesn't have enough free buffers available.
  142. \end{description}
  143. }{\seef{Recv}, \seem{send}{2}}
  144. \function{Recv}{(Sock:Longint;Var Addr;AddrLen,Flags:Longint)}{Longint}
  145. {\var{Recv} reads at most \var{Addrlen} bytes from socket \var{Sock} into
  146. address \var{Addr}. The socket must be in a connected state.
  147. \var{Flags} can be one of the following:
  148. \begin{description}
  149. \item [1] : Process out-of band data.
  150. \item [4] : Bypass routing, use a direct interface.
  151. \item [??] : Wait for full request or report an error.
  152. \end{description}
  153. The functions returns the number of bytes actually read from the socket, or
  154. -1 if a detectable error occurred.}
  155. {Errors are reported in \var{SocketError}, and include the following:
  156. \begin{description}
  157. \item[SYS\_EBADF] The socket descriptor is invalid.
  158. \item[SYS\_ENOTCONN] The socket isn't connected.
  159. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  160. \item[SYS\_EFAULT] The address is outside your address space.
  161. \item[SYS\_EMSGSIZE] The message cannot be sent atomically.
  162. \item[SYS\_EWOULDBLOCK] The requested operation would block the process.
  163. \item[SYS\_ENOBUFS] The system doesn't have enough free buffers available.
  164. \end{description}
  165. }{\seef{Send}}
  166. \function{Bind}{(Sock:Longint;Var Addr;AddrLen:Longint)}{Boolean}
  167. {\var{Bind} binds the socket \var{Sock} to address \var{Addr}. \var{Addr}
  168. has length \var{Addrlen}.
  169. The function returns \var{True} if the call was succesful, \var{False} if
  170. not.
  171. }
  172. {Errors are returned in \var{SocketError} and include the following:
  173. \begin{description}
  174. \item[SYS\_EBADF] The socket descriptor is invalid.
  175. \item[SYS\_EINVAL] The socket is already bound to an address,
  176. \item[SYS\_EACCESS] Address is protected and you don't have permission to
  177. open it.
  178. \end{description}
  179. More arrors can be found in the Unix man pages.
  180. }{\seef{Socket}}
  181. \functionl{Bind}{AltBind}{(Sock:longint;const addr:string)}{boolean}
  182. {This is an alternate form of the \var{Bind} command.
  183. This form of the \var{Bind} command is equivalent to subsequently
  184. calling \seep{Str2UnixSockAddr} and the regular \seef{Bind} function.
  185. The function returns \var{True} if successfull, \var{False} otherwise.
  186. }
  187. {Errors are those of the regular \seef{Bind} command.}
  188. {\seef{Bind}}
  189. \function{Listen}{(Sock,MaxConnect:Longint)}{Boolean}
  190. {\var{Listen} listens for up to \var{MaxConnect} connections from socket
  191. \var{Sock}. The socket \var{Sock} must be of type \var{SOCK\_STREAM} or
  192. \var{Sock\_SEQPACKET}.
  193. The function returns \var{True} if a connection was accepted, \var{False}
  194. if an error occurred.
  195. }
  196. {Errors are reported in \var{SocketError}, and include the following:
  197. \begin{description}
  198. \item[SYS\_EBADF] The socket descriptor is invalid.
  199. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  200. \item[SYS\_EOPNOTSUPP] The socket type doesn't support the \var{Listen}
  201. operation.
  202. \end{description}
  203. }{\seef{Socket}, \seef{Bind}, \seef{Connect}}
  204. \function{Accept}{(Sock:Longint;Var Addr;Var Addrlen:Longint)}{Longint}
  205. {\var{Accept} accepts a connection from a socket \var{Sock}, which was
  206. listening for a connection. The accepted socket may NOT be used to accept
  207. more connections. The original socket remains open.
  208. The \var{Accept} call fills the address of the connecting entity in \var{Addr},
  209. and sets its length in \var{Addrlen}. \var{Addr} should be pointing to
  210. enough space, and \var{Addrlen} should be set to the amount of space
  211. available, prior to the call.
  212. }
  213. {Errors are reported in \var{SocketError}, and include the following:
  214. \begin{description}
  215. \item[SYS\_EBADF] The socket descriptor is invalid.
  216. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  217. \item[SYS\_EOPNOTSUPP] The socket type doesn't support the \var{Listen}
  218. operation.
  219. \item[SYS\_EFAULT] \var{Addr} points outside your address space.
  220. \item[SYS\_EWOULDBLOCK] The requested operation would block the process.
  221. \end{description}
  222. }
  223. {\seef{Listen}, \seef{Connect}}
  224. \input{sockex/sock_svr.tex}
  225. \functionl{Accept}{AltAAccept}{(Sock:longint;var addr:string;var SockIn,SockOut:text)}{Boolean}
  226. { This is an alternate form of the \seef{Accept} command. It is equivalent
  227. to subsequently calling the regular \seef{Accept}
  228. function and the \seep{Sock2Text} function.
  229. The function returns \var{True} if successfull, \var{False} otherwise.
  230. }
  231. {The errors are those of \seef{Accept}.}
  232. {\seef{Accept}}
  233. \functionl{Accept}{AltBAccept}{(Sock:longint;var addr:string;var SockIn,SockOut:File)}{Boolean}
  234. { This is an alternate form of the \seef{Accept} command.
  235. It is equivalent
  236. to subsequently calling the regular \seef{Accept} function and the
  237. \seep{Sock2File} function.
  238. The function returns \var{True} if successfull, \var{False} otherwise.
  239. }
  240. {The errors are those of \seef{Accept}.}
  241. {\seef{Accept}}
  242. \function{Connect}{(Sock:Longint;Var Addr;Addrlen:Longint)}{Boolean}
  243. {\var{Connect} opens a connection to a peer, whose address is described by
  244. var{Addr}. \var{AddrLen} contains the length of the address.
  245. The type of \var{Addr} depends on the kind of connection you're trying to
  246. make, but is generally one of \var{TSockAddr} or \var{TUnixSockAddr}.
  247. }
  248. {Errors are reported in \var{SocketError}.}
  249. {\seef{Listen}, \seef{Bind},\seef{Accept}}
  250. \input{sockex/sock_cli.tex}
  251. \functionl{Connect}{AltAConnect}{(Sock:longint;const addr:string;var SockIn,SockOut:text)}{Boolean}
  252. { This is an alternate form of the \seef{Connect} command.
  253. It is equivalent
  254. to subsequently calling the regular \seef{Connect} function and the
  255. \seep{Sock2Text} function.
  256. The function returns \var{True} if successfull, \var{False} otherwise.
  257. }{The errors are those of \seef{Connect}.}
  258. {\seef{Connect}}
  259. \functionl{Connect}{AltBConnect}{(Sock:longint;const addr:string;var SockIn,SockOut:file)}{Boolean}
  260. { This is an alternate form of the \seef{Connect} command.
  261. It is equivalent
  262. to subsequently calling the regular \seef{Connect} function and the
  263. \seep{Sock2File} function.
  264. The function returns \var{True} if successfull, \var{False} otherwise.
  265. }{The errors are those of \seef{Connect}.}
  266. {\seef{Connect}}
  267. \function{Shutdown}{(Sock:Longint;How:Longint)}{Longint}
  268. {\var{ShutDown} closes one end of a full duplex socket connection, described
  269. by \var{Sock}. \var{How} determines how the connection will be shut down,
  270. and can be one of the following:
  271. \begin{description}
  272. \item[0] : Further receives are disallowed.
  273. \item[1] : Further sends are disallowed.
  274. \item[2] : Sending nor receiving are allowed.
  275. \end{description}
  276. On succes, the function returns 0, on error -1 is returned.
  277. }
  278. {\var{SocketError} is used to report errors, and includes the following:
  279. \begin{description}
  280. \item[SYS\_EBADF] The socket descriptor is invalid.
  281. \item[SYS\_ENOTCONN] The socket isn't connected.
  282. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  283. \end{description}
  284. }{\seef{Socket}, \seef{Connect}}
  285. \function{GetSocketName}{(Sock:Longint;Var Addr;Var Addrlen:Longint)}{Longint}
  286. {\var{GetSockName} returns the current name of the specified socket
  287. \var{Sock}. \var{Addr} should point to enough space to store the name, the
  288. amount of space pointed to should be set in \var{Addrlen}.
  289. When the function returns succesfully, \var{Addr} will be filled with the
  290. name, and \var{Addrlen} will be set to the length of \var{Addr}.}
  291. {Errors are reported in \var{SocketError}, and include the following:
  292. \begin{description}
  293. \item[SYS\_EBADF] The socket descriptor is invalid.
  294. \item[SYS\_ENOBUFS] The system doesn't have enough buffers to perform the
  295. operation.
  296. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  297. \item[SYS\_EFAULT] \var{Addr} points outside your address space.
  298. \end{description}
  299. }{\seef{Bind}}
  300. \function{GetPeerName}{(Sock:Longint;Var Addr;Var Addrlen:Longint)}{Longint}
  301. {\var{GetPeerName} returns the name of the entity connected to the
  302. specified socket \var{Sock}. The Socket must be connected for this call to
  303. work.
  304. \var{Addr} should point to enough space to store the name, the
  305. amount of space pointed to should be set in \var{Addrlen}.
  306. When the function returns succesfully, \var{Addr} will be filled with the
  307. name, and \var{Addrlen} will be set to the length of \var{Addr}.
  308. }
  309. {Errors are reported in \var{SocketError}, and include the following:
  310. \begin{description}
  311. \item[SYS\_EBADF] The socket descriptor is invalid.
  312. \item[SYS\_ENOBUFS] The system doesn't have enough buffers to perform the
  313. operation.
  314. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  315. \item[SYS\_EFAULT] \var{Addr} points outside your address space.
  316. \item[SYS\_ENOTCONN] The socket isn't connected.
  317. \end{description}
  318. }{\seef{Connect}, \seef{Socket}, \seem{connect}{2}}
  319. \function{SetSocketOptions}{(Sock,Level,OptName:Longint;Var OptVal;optlen:longint)}{Longint}
  320. {\var{SetSocketOptions} sets the connection options for socket \var{Sock}.
  321. The socket may be manipulated at different levels, indicated by \var{Level},
  322. which can be one of the following:
  323. \begin{description}
  324. \item[SOL\_SOCKET] To manipulate the socket itself.
  325. \item[XXX] set \var{Level} to \var{XXX}, the protocol number of the protocol
  326. which should interprete the option.
  327. \end{description}
  328. For more information on this call, refer to the unix manual page \seem{setsockopt}{2}.
  329. }
  330. {Errors are reported in \var{SocketError}, and include the following:
  331. \begin{description}
  332. \item[SYS\_EBADF] The socket descriptor is invalid.
  333. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  334. \item[SYS\_EFAULT] \var{OptVal} points outside your address space.
  335. \end{description}
  336. }
  337. {\seef{GetSocketOptions}}
  338. \function{GetSocketOptions}{(Sock,Level,OptName:Longint;Var OptVal;optlen:longint)}{Longint}
  339. {\var{GetSocketOptions} gets the connection options for socket \var{Sock}.
  340. The socket may be obtained from different levels, indicated by \var{Level},
  341. which can be one of the following:
  342. \begin{description}
  343. \item[SOL\_SOCKET] From the socket itself.
  344. \item[XXX] set \var{Level} to \var{XXX}, the protocol number of the protocol
  345. which should interprete the option.
  346. \end{description}
  347. For more information on this call, refer to the unix manual page \seem{getsockopt}{2}.
  348. }
  349. {Errors are reported in \var{SocketError}, and include the following:
  350. \begin{description}
  351. \item[SYS\_EBADF] The socket descriptor is invalid.
  352. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  353. \item[SYS\_EFAULT] \var{OptVal} points outside your address space.
  354. \end{description}
  355. }
  356. {\seef{GetSocketOptions}}
  357. \function{SocketPair}{(Domain,SocketType,Protocol:Longint;var Pair:TSockArray)}{Longint}
  358. {\var{SocketPair} creates 2 sockets in domain \var{Domain}, from type
  359. \var{SocketType} and using protocol \var{Protocol}.
  360. The pair is returned in \var{Pair}, and they are indistinguishable.
  361. The function returns -1 upon error and 0 upon success.
  362. }
  363. {Errors are reported in \var{SocketError}, and are the same as in \seef{Socket}}
  364. \procedure{Sock2Text}{(Sock:Longint;Var SockIn,SockOut: Text)}
  365. {\var{Sock2Text} transforms a socket \var{Sock} into 2 Pascal file
  366. descriptors of type \var{Text}, one for reading from the socket
  367. (\var{SockIn}), one for writing to the socket (\var{SockOut}).}
  368. {None.}
  369. {\seef{Socket}, \seep{Sock2File}}
  370. \procedure{Sock2File}{(Sock:Longint;Var SockIn,SockOut:File)}
  371. {\var{Sock2File} transforms a socket \var{Sock} into 2 Pascal file
  372. descriptors of type \var{File}, one for reading from the socket
  373. (\var{SockIn}), one for writing to the socket (\var{SockOut}).}
  374. {None.}
  375. {\seef{Socket}, \seep{Sock2Text}}
  376. \procedure{Str2UnixSockAddr}{(const addr:string;var t:TUnixSockAddr;var len:longint)}
  377. {\var{Str2UnixSockAddr} transforms a Unix socket address in a string to a
  378. \var{TUnixSockAddr} sturcture which can be passed to the \seef{Bind} call.
  379. }
  380. {None.}
  381. {\seef{Socket}, \seef{Bind}}