sockets.tex 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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{Accept}{(Sock:Longint;Var Addr;Var Addrlen:Longint)}{Longint}
  97. {\var{Accept} accepts a connection from a socket \var{Sock}, which was
  98. listening for a connection. The accepted socket may NOT be used to accept
  99. more connections. The original socket remains open.
  100. The \var{Accept} call fills the address of the connecting entity in \var{Addr},
  101. and sets its length in \var{Addrlen}. \var{Addr} should be pointing to
  102. enough space, and \var{Addrlen} should be set to the amount of space
  103. available, prior to the call.
  104. }
  105. {Errors are reported in \var{SocketError}, and include the following:
  106. \begin{description}
  107. \item[SYS\_EBADF] The socket descriptor is invalid.
  108. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  109. \item[SYS\_EOPNOTSUPP] The socket type doesn't support the \var{Listen}
  110. operation.
  111. \item[SYS\_EFAULT] \var{Addr} points outside your address space.
  112. \item[SYS\_EWOULDBLOCK] The requested operation would block the process.
  113. \end{description}
  114. }
  115. {\seef{Listen}, \seef{Connect}}
  116. \latex{\inputlisting{sockex/sock_svr.pp}}
  117. \html{\input{sockex/sock_svr.tex}}
  118. \functionl{Accept}{AltAAccept}{(Sock:longint;var addr:string;var SockIn,SockOut:text)}{Boolean}
  119. { This is an alternate form of the \seef{Accept} command. It is equivalent
  120. to subsequently calling the regular \seef{Accept}
  121. function and the \seep{Sock2Text} function.
  122. The function returns \var{True} if successfull, \var{False} otherwise.
  123. }
  124. {The errors are those of \seef{Accept}.}
  125. {\seef{Accept}}
  126. \functionl{Accept}{AltBAccept}{(Sock:longint;var addr:string;var SockIn,SockOut:File)}{Boolean}
  127. { This is an alternate form of the \seef{Accept} command.
  128. It is equivalent
  129. to subsequently calling the regular \seef{Accept} function and the
  130. \seep{Sock2File} function.
  131. The \var{Addr} parameter contains the name of the unix socket file to be
  132. opened.
  133. The function returns \var{True} if successfull, \var{False} otherwise.
  134. }
  135. {The errors are those of \seef{Accept}.}
  136. {\seef{Accept}}
  137. \functionl{Accept}{AltCAccept}{(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File)}{Boolean}
  138. { This is an alternate form of the \seef{Accept} command.
  139. It is equivalent
  140. to subsequently calling the regular \seef{Accept} function and the
  141. \seep{Sock2File} function.
  142. The \var{Addr} parameter contains the parameters of the internet socket that
  143. should be opened.
  144. The function returns \var{True} if successfull, \var{False} otherwise.
  145. }
  146. {The errors are those of \seef{Accept}.}
  147. {\seef{Accept}}
  148. \function{Bind}{(Sock:Longint;Var Addr;AddrLen:Longint)}{Boolean}
  149. {\var{Bind} binds the socket \var{Sock} to address \var{Addr}. \var{Addr}
  150. has length \var{Addrlen}.
  151. The function returns \var{True} if the call was succesful, \var{False} if
  152. not.
  153. }
  154. {Errors are returned in \var{SocketError} and include the following:
  155. \begin{description}
  156. \item[SYS\_EBADF] The socket descriptor is invalid.
  157. \item[SYS\_EINVAL] The socket is already bound to an address,
  158. \item[SYS\_EACCESS] Address is protected and you don't have permission to
  159. open it.
  160. \end{description}
  161. More arrors can be found in the Unix man pages.
  162. }{\seef{Socket}}
  163. \functionl{Bind}{AltBind}{(Sock:longint;const addr:string)}{boolean}
  164. {This is an alternate form of the \var{Bind} command.
  165. This form of the \var{Bind} command is equivalent to subsequently
  166. calling \seep{Str2UnixSockAddr} and the regular \seef{Bind} function.
  167. The function returns \var{True} if successfull, \var{False} otherwise.
  168. }
  169. {Errors are those of the regular \seef{Bind} command.}
  170. {\seef{Bind}}
  171. \function{Connect}{(Sock:Longint;Var Addr;Addrlen:Longint)}{Boolean}
  172. {\var{Connect} opens a connection to a peer, whose address is described by
  173. var{Addr}. \var{AddrLen} contains the length of the address.
  174. The type of \var{Addr} depends on the kind of connection you're trying to
  175. make, but is generally one of \var{TSockAddr} or \var{TUnixSockAddr}.
  176. }
  177. {Errors are reported in \var{SocketError}.}
  178. {\seef{Listen}, \seef{Bind},\seef{Accept}}
  179. \latex{\inputlisting{sockex/sock_cli.pp}}
  180. \html{\input{sockex/sock_cli.tex}}
  181. \functionl{Connect}{AltAConnect}{(Sock:longint;const addr:string;var SockIn,SockOut:text)}{Boolean}
  182. { This is an alternate form of the \seef{Connect} command.
  183. It is equivalent
  184. to subsequently calling the regular \seef{Connect} function and the
  185. \seep{Sock2Text} function.
  186. The function returns \var{True} if successfull, \var{False} otherwise.
  187. }{The errors are those of \seef{Connect}.}
  188. {\seef{Connect}}
  189. \functionl{Connect}{AltBConnect}{(Sock:longint;const addr:string;var SockIn,SockOut:file)}{Boolean}
  190. { This is an alternate form of the \seef{Connect} command. The parameter
  191. \var{addr} contains the name of the unix socket file to be opened.
  192. It is equivalent to subsequently calling the regular \seef{Connect}
  193. function and the \seep{Sock2File} function.
  194. The function returns \var{True} if successfull, \var{False} otherwise.
  195. }{The errors are those of \seef{Connect}.}
  196. {\seef{Connect}}
  197. \functionl{Connect}{AltCConnect}{(Sock:longint;const addr: TInetSockAddr;var SockIn,SockOut:file)}{Boolean}
  198. { This is another alternate form of the \seef{Connect} command.
  199. It is equivalent
  200. to subsequently calling the regular \seef{Connect} function and the
  201. \seep{Sock2File} function. The \var{Addr} parameter contains the parameters
  202. of the internet socket to connect to.
  203. The function returns \var{True} if successfull, \var{False} otherwise.
  204. }{The errors are those of \seef{Connect}.}
  205. {\seef{Connect}}
  206. \latex{\inputlisting{sockex/pfinger.pp}}
  207. \html{\input{sockex/pfinger.tex}}
  208. \function{GetPeerName}{(Sock:Longint;Var Addr;Var Addrlen:Longint)}{Longint}
  209. {\var{GetPeerName} returns the name of the entity connected to the
  210. specified socket \var{Sock}. The Socket must be connected for this call to
  211. work.
  212. \var{Addr} should point to enough space to store the name, the
  213. amount of space pointed to should be set in \var{Addrlen}.
  214. When the function returns succesfully, \var{Addr} will be filled with the
  215. name, and \var{Addrlen} will be set to the length of \var{Addr}.
  216. }
  217. {Errors are reported in \var{SocketError}, and include the following:
  218. \begin{description}
  219. \item[SYS\_EBADF] The socket descriptor is invalid.
  220. \item[SYS\_ENOBUFS] The system doesn't have enough buffers to perform the
  221. operation.
  222. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  223. \item[SYS\_EFAULT] \var{Addr} points outside your address space.
  224. \item[SYS\_ENOTCONN] The socket isn't connected.
  225. \end{description}
  226. }{\seef{Connect}, \seef{Socket}, \seem{connect}{2}}
  227. \function{GetSocketName}{(Sock:Longint;Var Addr;Var Addrlen:Longint)}{Longint}
  228. {\var{GetSockName} returns the current name of the specified socket
  229. \var{Sock}. \var{Addr} should point to enough space to store the name, the
  230. amount of space pointed to should be set in \var{Addrlen}.
  231. When the function returns succesfully, \var{Addr} will be filled with the
  232. name, and \var{Addrlen} will be set to the length of \var{Addr}.}
  233. {Errors are reported in \var{SocketError}, and include the following:
  234. \begin{description}
  235. \item[SYS\_EBADF] The socket descriptor is invalid.
  236. \item[SYS\_ENOBUFS] The system doesn't have enough buffers to perform the
  237. operation.
  238. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  239. \item[SYS\_EFAULT] \var{Addr} points outside your address space.
  240. \end{description}
  241. }{\seef{Bind}}
  242. \function{GetSocketOptions}{(Sock,Level,OptName:Longint;Var OptVal;optlen:longint)}{Longint}
  243. {\var{GetSocketOptions} gets the connection options for socket \var{Sock}.
  244. The socket may be obtained from different levels, indicated by \var{Level},
  245. which can be one of the following:
  246. \begin{description}
  247. \item[SOL\_SOCKET] From the socket itself.
  248. \item[XXX] set \var{Level} to \var{XXX}, the protocol number of the protocol
  249. which should interprete the option.
  250. \end{description}
  251. For more information on this call, refer to the unix manual page \seem{getsockopt}{2}.
  252. }
  253. {Errors are reported in \var{SocketError}, and include the following:
  254. \begin{description}
  255. \item[SYS\_EBADF] The socket descriptor is invalid.
  256. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  257. \item[SYS\_EFAULT] \var{OptVal} points outside your address space.
  258. \end{description}
  259. }
  260. {\seef{GetSocketOptions}}
  261. \function{Listen}{(Sock,MaxConnect:Longint)}{Boolean}
  262. {\var{Listen} listens for up to \var{MaxConnect} connections from socket
  263. \var{Sock}. The socket \var{Sock} must be of type \var{SOCK\_STREAM} or
  264. \var{Sock\_SEQPACKET}.
  265. The function returns \var{True} if a connection was accepted, \var{False}
  266. if an error occurred.
  267. }
  268. {Errors are reported in \var{SocketError}, and include the following:
  269. \begin{description}
  270. \item[SYS\_EBADF] The socket descriptor is invalid.
  271. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  272. \item[SYS\_EOPNOTSUPP] The socket type doesn't support the \var{Listen}
  273. operation.
  274. \end{description}
  275. }{\seef{Socket}, \seef{Bind}, \seef{Connect}}
  276. \function{Recv}{(Sock:Longint;Var Addr;AddrLen,Flags:Longint)}{Longint}
  277. {\var{Recv} reads at most \var{Addrlen} bytes from socket \var{Sock} into
  278. address \var{Addr}. The socket must be in a connected state.
  279. \var{Flags} can be one of the following:
  280. \begin{description}
  281. \item [1] : Process out-of band data.
  282. \item [4] : Bypass routing, use a direct interface.
  283. \item [??] : Wait for full request or report an error.
  284. \end{description}
  285. The functions returns the number of bytes actually read from the socket, or
  286. -1 if a detectable error occurred.}
  287. {Errors are reported in \var{SocketError}, and include the following:
  288. \begin{description}
  289. \item[SYS\_EBADF] The socket descriptor is invalid.
  290. \item[SYS\_ENOTCONN] The socket isn't connected.
  291. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  292. \item[SYS\_EFAULT] The address is outside your address space.
  293. \item[SYS\_EMSGSIZE] The message cannot be sent atomically.
  294. \item[SYS\_EWOULDBLOCK] The requested operation would block the process.
  295. \item[SYS\_ENOBUFS] The system doesn't have enough free buffers available.
  296. \end{description}
  297. }{\seef{Send}}
  298. \function{Send}{(Sock:Longint;Var Addr;AddrLen,Flags:Longint)}{Longint}
  299. {\var{Send} sends \var{AddrLen} bytes starting from address \var{Addr}
  300. to socket \var{Sock}. \var{Sock} must be in a connected state.
  301. The function returns the number of bytes sent, or -1 if a detectable
  302. error occurred.
  303. \var{Flags} can be one of the following:
  304. \begin{description}
  305. \item [1] : Process out-of band data.
  306. \item [4] : Bypass routing, use a direct interface.
  307. \end{description}
  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\_ENOTSOCK] The descriptor is not a socket.
  313. \item[SYS\_EFAULT] The address is outside your address space.
  314. \item[SYS\_EMSGSIZE] The message cannot be sent atomically.
  315. \item[SYS\_EWOULDBLOCK] The requested operation would block the process.
  316. \item[SYS\_ENOBUFS] The system doesn't have enough free buffers available.
  317. \end{description}
  318. }{\seef{Recv}, \seem{send}{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{Shutdown}{(Sock:Longint;How:Longint)}{Longint}
  339. {\var{ShutDown} closes one end of a full duplex socket connection, described
  340. by \var{Sock}. \var{How} determines how the connection will be shut down,
  341. and can be one of the following:
  342. \begin{description}
  343. \item[0] : Further receives are disallowed.
  344. \item[1] : Further sends are disallowed.
  345. \item[2] : Sending nor receiving are allowed.
  346. \end{description}
  347. On succes, the function returns 0, on error -1 is returned.
  348. }
  349. {\var{SocketError} is used to report errors, and includes the following:
  350. \begin{description}
  351. \item[SYS\_EBADF] The socket descriptor is invalid.
  352. \item[SYS\_ENOTCONN] The socket isn't connected.
  353. \item[SYS\_ENOTSOCK] The descriptor is not a socket.
  354. \end{description}
  355. }{\seef{Socket}, \seef{Connect}}
  356. \procedure{Sock2File}{(Sock:Longint;Var SockIn,SockOut:File)}
  357. {\var{Sock2File} transforms a socket \var{Sock} into 2 Pascal file
  358. descriptors of type \var{File}, one for reading from the socket
  359. (\var{SockIn}), one for writing to the socket (\var{SockOut}).}
  360. {None.}
  361. {\seef{Socket}, \seep{Sock2Text}}
  362. \procedure{Sock2Text}{(Sock:Longint;Var SockIn,SockOut: Text)}
  363. {\var{Sock2Text} transforms a socket \var{Sock} into 2 Pascal file
  364. descriptors of type \var{Text}, one for reading from the socket
  365. (\var{SockIn}), one for writing to the socket (\var{SockOut}).}
  366. {None.}
  367. {\seef{Socket}, \seep{Sock2File}}
  368. \function{Socket}{(Domain,SocketType,Protocol:Longint)}{Longint}
  369. {\var{Socket} creates a new socket in domain \var{Domain}, from type
  370. \var{SocketType} using protocol \var{Protocol}.
  371. The Domain, Socket type and Protocol can be specified using predefined
  372. constants (see the section on constants for available constants)
  373. If succesfull, the function returns a socket descriptor, which can be passed
  374. to a subsequent \seef{Bind} call. If unsuccesfull, the function returns -1.
  375. }
  376. {Errors are returned in \var{SocketError}, and include the follwing:
  377. \begin{description}
  378. \item[SYS\_EPROTONOSUPPORT]
  379. The protocol type or the specified protocol is not
  380. supported within this domain.
  381. \item[SYS\_EMFILE]
  382. The per-process descriptor table is full.
  383. \item[SYS\_ENFILE]
  384. The system file table is full.
  385. \item[SYS\_EACCESS]
  386. Permission to create a socket of the specified
  387. type and/or protocol is denied.
  388. \item[SYS\_ENOBUFS]
  389. Insufficient buffer space is available. The
  390. socket cannot be created until sufficient
  391. resources are freed.
  392. \end{description}}
  393. {\seef{SocketPair}, \seem{socket}{2}}
  394. for an example, see \seef{Accept}.
  395. \function{SocketPair}{(Domain,SocketType,Protocol:Longint;var Pair:TSockArray)}{Longint}
  396. {\var{SocketPair} creates 2 sockets in domain \var{Domain}, from type
  397. \var{SocketType} and using protocol \var{Protocol}.
  398. The pair is returned in \var{Pair}, and they are indistinguishable.
  399. The function returns -1 upon error and 0 upon success.
  400. }
  401. {Errors are reported in \var{SocketError}, and are the same as in \seef{Socket}}
  402. \procedure{Str2UnixSockAddr}{(const addr:string;var t:TUnixSockAddr;var len:longint)}
  403. {\var{Str2UnixSockAddr} transforms a Unix socket address in a string to a
  404. \var{TUnixSockAddr} sturcture which can be passed to the \seef{Bind} call.
  405. }
  406. {None.}
  407. {\seef{Socket}, \seef{Bind}}