PageRenderTime 75ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/srfi-basic-socket-interface.texi

http://github.com/marcomaggi/vicare
Unknown | 741 lines | 549 code | 192 blank | 0 comment | 0 complexity | 52ac820360d32cc3b128927a9488c664 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0
  1. @node srfi basic-socket
  2. @section @ansrfi{106} basic socket interface
  3. @cindex @ansrfi{106} basic socket interface
  4. @cindex @library{srfi :106}, library
  5. @cindex @library{srfi :106 socket}, library
  6. @cindex Library @library{srfi :106}
  7. @cindex Library @library{srfi :106 socket}
  8. The library @library{srfi :106} is by Takashi Kato as the reference
  9. implementation for @ansrfi{106}; see:
  10. @center @url{http://srfi.schemers.org/srfi-106/srfi-106.html}
  11. @noindent
  12. for more details.
  13. @menu
  14. * srfi basic-socket abstract:: Abstract.
  15. * srfi basic-socket rationale:: Rationale.
  16. * srfi basic-socket spec:: Specification.
  17. * srfi basic-socket examples:: Examples.
  18. * srfi basic-socket refs:: References.
  19. * srfi basic-socket copyright:: Copyright.
  20. @end menu
  21. @c page
  22. @node srfi basic-socket abstract
  23. @subsection Abstract
  24. This document specifies basic socket interfaces.
  25. @c page
  26. @node srfi basic-socket rationale
  27. @subsection Rationale
  28. Many Scheme implementations have their own socket @api{}s however there
  29. are no portable way to write socket programs. Therefore programmers
  30. need to provide implementation dependent layers for their programs.
  31. This document specifies high and middle range of socket interfaces which
  32. are commonly used for socket programming. It should make it easier to
  33. write portable programs that need to send or receive data from their
  34. socket.
  35. @c page
  36. @node srfi basic-socket spec
  37. @subsection Specification
  38. @menu
  39. * srfi basic-socket spec intro:: Introduction.
  40. * srfi basic-socket spec cons:: Constructors and predicates.
  41. * srfi basic-socket spec ops:: Socket operations.
  42. * srfi basic-socket spec port:: Port conversion.
  43. * srfi basic-socket spec control:: Control features.
  44. * srfi basic-socket spec flag:: Flag operations.
  45. * srfi basic-socket spec const:: Constants.
  46. @end menu
  47. @c page
  48. @node srfi basic-socket spec intro
  49. @subsubsection Introduction
  50. All procedures defined in this @srfi{} may raise an error when the
  51. procedure fails because of a connection problem or other socket related
  52. problems. This document does not specify which condition should be
  53. raised.
  54. Names defined in this document:
  55. @table @strong
  56. @item Constructors and predicates
  57. @example
  58. make-client-socket make-server-socket
  59. socket?
  60. @end example
  61. @item Socket operations
  62. @example
  63. socket-accept
  64. socket-send socket-recv
  65. socket-shutdown socket-close
  66. @end example
  67. @item Port conversion
  68. @example
  69. socket-input-port
  70. socket-output-port
  71. @end example
  72. @item Control feature
  73. @example
  74. call-with-socket
  75. @end example
  76. @item Flag operations
  77. @example
  78. address-family address-info
  79. socket-domain ip-protocol
  80. message-type shutdown-method
  81. socket-merge-flags socket-purge-flags
  82. @end example
  83. @item Constant values
  84. @example
  85. *af-unspec* *af-inet* *af-inet6*
  86. *sock-stream *sock-dgram*
  87. *ai-canonname* *ai-numerichost*
  88. *ai-v4mapped* *ai-all* *ai-addrconfig*
  89. *ipproto-ip* *ipproto-tcp* *ipproto-udp*
  90. *msg-peek* *msg-oob* *msg-waitall*
  91. *shut-rd* *shut-wr* *shut-rdwr*
  92. @end example
  93. @end table
  94. The procedure description uses following notation:
  95. @table @var
  96. @item socket
  97. A socket object.
  98. @item bv
  99. A bytevector.
  100. @item obj
  101. Any value.
  102. @end table
  103. @c page
  104. @node srfi basic-socket spec cons
  105. @subsubsection Constructors and predicates
  106. The following bindings are exported by the libraries @library{srfi :106}
  107. and @library{srfi :106 socket}.
  108. @defun make-client-socket @var{node} @var{service}
  109. @defunx make-client-socket @var{node} @var{service} @var{ai-family}
  110. @defunx make-client-socket @var{node} @var{service} @var{ai-family} @var{ai-socktype}
  111. @defunx make-client-socket @var{node} @var{service} @var{ai-family} @var{ai-socktype} @var{ai-flags}
  112. @defunx make-client-socket @var{node} @var{service} @var{ai-family} @var{ai-socktype} @var{ai-flags} @var{ai-protocol}
  113. Return a client socket connected to an Internet address.
  114. The Internet address is identified by @var{node} and @var{service}.
  115. @var{node} and @var{service} must be strings. Example values for
  116. @var{node}: @samp{"localhost"}, @samp{127.0.0.1}. Example values for
  117. @var{service}: @samp{"http"}, @samp{"80"}.
  118. The optional arguments may specify the created socket's behaviour. If
  119. the optional arguments are omitted, then the following value should be
  120. used as default:
  121. @table @var
  122. @item ai-family
  123. Defaults to: @code{*af-inet*}.
  124. @item ai-socktype
  125. Defaults to: @code{*sock-stream*}.
  126. @item ai-flags
  127. Defaults to: @code{(socket-merge-flags *ai-v4mapped* *ai-addrconfig*)}.
  128. @item ai-protocol
  129. Defaults to: @code{*ipproto-ip*}.
  130. @end table
  131. The returned socket may not be closed automatically so it is the users'
  132. responsibility to close it explicitly.
  133. @quotation
  134. @strong{For Vicare:} whenever the returned socket object is garbage
  135. collected, the function @func{socket-close} is automatically applied to
  136. it.
  137. @end quotation
  138. @end defun
  139. @defun make-server-socket @var{service}
  140. @defunx make-server-socket @var{service} @var{ai-family}
  141. @defunx make-server-socket @var{service} @var{ai-family} @var{ai-socktype}
  142. @defunx make-server-socket @var{service} @var{ai-family} @var{ai-socktype} @var{ai-protocol}
  143. Return a server socket waiting for connection.
  144. The @var{node} argument is the same as the one of
  145. @func{make-client-socket}. The optional arguments may specify the
  146. created socket's behaviour. If the optional arguments are omitted, then
  147. the following value should be used as default:
  148. @table @var
  149. @item ai-family
  150. Defaults to: @code{*af-inet*}.
  151. @item @var{ai-socktype}
  152. Defaults to: @code{*sock-stream*}.
  153. @item @var{ai-protocol}
  154. Defaults to: @code{*ipproto-ip*}.
  155. @end table
  156. The returned socket may not be closed automatically so it is the users'
  157. responsibility to close it explicitly.
  158. @quotation
  159. @strong{For Vicare:} whenever the returned socket object is garbage
  160. collected, the function @func{socket-close} is automatically applied to
  161. it.
  162. @end quotation
  163. @end defun
  164. @defun socket? @var{obj}
  165. Return @true{} if @var{obj} is a socket object, @false{} otherwise.
  166. @end defun
  167. @c page
  168. @node srfi basic-socket spec ops
  169. @subsubsection Socket operations
  170. The following bindings are exported by the libraries @library{srfi :106}
  171. and @library{srfi :106 socket}.
  172. @defun socket-accept @var{socket}
  173. Wait for an incoming connection request, and return a fresh connected
  174. client socket.
  175. @end defun
  176. @defun socket-send @var{socket} @var{bv}
  177. @defunx socket-send @var{socket} @var{bv} @var{flags}
  178. Send a binary data block to a socket and return the sent data size.
  179. @var{flags} may specify the procedure's behaviour. If @var{flags} is
  180. omitted: the default value must be the result of evaluating the form:
  181. @example
  182. (message-type none)
  183. @end example
  184. @end defun
  185. @defun socket-recv @var{socket} @var{size}
  186. @defunx socket-recv @var{socket} @var{size} @var{flags}
  187. Receive a binary data block from a socket. If a zero--length bytevector
  188. is returned: it means the peer connection is closed.
  189. @var{flags} may specify the procedure's behaviour. If @var{flags} is
  190. omitted, the default value must be the result of evaluating the form:
  191. @example
  192. (message-type none)
  193. @end example
  194. @end defun
  195. @defun socket-shutdown @var{socket} @var{how}
  196. Shutdown a socket. @var{how} must be one of the following constants:
  197. @code{*shut-rd*}, @code{*shut-wr*}, @code{*shut-rdwr*}.
  198. @end defun
  199. @defun socket-close @var{socket}
  200. Close a socket. The procedure should not shutdown the given socket: to
  201. shutdown a socket @func{socket-shutdown} should be called explicitly.
  202. @quotation
  203. @strong{For Vicare:} it is safe to apply multiple times this function to
  204. the same @var{socket} object; the first time the socket is closed,
  205. subsequent times nothing happens. This function is automatically
  206. applied to every socket object returned by @func{make-client-socket} and
  207. @func{make-server-socket} whenever such objects are garbage collected.
  208. @end quotation
  209. @end defun
  210. @defun socket-descriptor @var{socket}
  211. This function is a Vicare extension. Return an exact integer
  212. representing the underlying socket descriptor; such integer can be used
  213. as argument to every @posix{} function accepting socket descriptors.
  214. @end defun
  215. @c page
  216. @node srfi basic-socket spec port
  217. @subsubsection Port conversion
  218. The following bindings are exported by the libraries @library{srfi :106}
  219. and @library{srfi :106 socket}.
  220. @defun socket-input-port @var{socket}
  221. @defunx socket-output-port @var{socket}
  222. Return a fresh binary input or output port associated with a
  223. @var{socket}, respectively. Whenever the returned port is closed: the
  224. associated socket must @strong{not} be closed along.
  225. @quotation
  226. @strong{For Vicare:} it is fine to use @func{transcoded-port} from
  227. @rsixlibrary{io ports} to put a textual port on top of the returned
  228. binary ports. Example:
  229. @example
  230. (import (vicare)
  231. (prefix (srfi :106) srfi.))
  232. (with-compensations
  233. (define socket
  234. (compensate
  235. (srfi.make-client-socket "reddit.com" "http")
  236. (with
  237. (srfi.socket-shutdown socket
  238. (srfi.shutdown-method read write))
  239. (srfi.socket-close socket))))
  240. (define in-port
  241. (compensate
  242. (transcoded-port (srfi.socket-input-port socket)
  243. (native-transcoder))
  244. (with
  245. (close-port in-port))))
  246. (define ou-port
  247. (compensate
  248. (transcoded-port (srfi.socket-output-port socket)
  249. (native-transcoder))
  250. (with
  251. (close-port ou-port))))
  252. ---)
  253. @end example
  254. It is also fine to use the returned ports as @var{port} argument to the
  255. functions @func{select-port}, @func{select-port-readable?},
  256. @func{select-port-writable?}, @func{select-port-exceptional?}.
  257. @end quotation
  258. @end defun
  259. @c page
  260. @node srfi basic-socket spec control
  261. @subsubsection Control features
  262. The following bindings are exported by the libraries @library{srfi :106}
  263. and @library{srfi :106 socket}.
  264. @defun call-with-socket @var{socket} @var{proc}
  265. Call a given procedure with a given socket as an argument. If
  266. @var{proc} returns: @func{call-with-socket} returns the result of
  267. @var{proc} and @var{socket} is automatically closed. If @var{proc} does
  268. not return: then @var{socket} is not closed automatically.
  269. @end defun
  270. @c page
  271. @node srfi basic-socket spec flag
  272. @subsubsection Flag operations
  273. The following bindings must be implemented as macros:
  274. @code{address-family}, @code{address-info}, @code{socket-domain},
  275. @code{ip-protocol}, @code{message-type} and @code{shutdown-method}.
  276. The following bindings are exported by the libraries @library{srfi :106}
  277. and @library{srfi :106 socket}.
  278. @deffn Syntax address-family @meta{name}
  279. Return a proper address family from the given @meta{name}.
  280. Implementations must support at least following names and must have the
  281. described behaviour.
  282. @table @code
  283. @item inet
  284. Returns @code{*af-inet*}.
  285. @item inet6
  286. Returns @code{*af-inet6*}.
  287. @item unspec
  288. Returns @code{*af-unspec*}.
  289. @end table
  290. Implementations may support more names such as @code{unix} or
  291. @code{local} or other names.
  292. @end deffn
  293. @deffn Syntax address-info @meta{name} @dots{}
  294. Return merged address info flags from given @meta{name}.
  295. Implementations must support at least following names and must have the
  296. described behaviour.
  297. @table @code
  298. @item canoname
  299. @itemx canonname
  300. Returns @code{*ai-canonname*}.
  301. @item numerichost
  302. Returns @code{*ai-numerichost*}.
  303. @item v4mapped
  304. Returns @code{*ai-v4mapped*}.
  305. @item all
  306. Returns @code{*ai-all*}.
  307. @item addrconfig
  308. Returns @code{*ai-addrconfig*}.
  309. @end table
  310. Implementations may support more names.
  311. @end deffn
  312. @deffn Syntax socket-domain @meta{name}
  313. Return socket domain flags from the given @meta{name}. Implementations
  314. must support at least following names and must have the described
  315. behaviour.
  316. @table @code
  317. @item stream
  318. Returns @code{*sock-stream*}.
  319. @item datagram
  320. Returns @code{*sock-dgram*}.
  321. @end table
  322. Implementations may support more names.
  323. @end deffn
  324. @deffn Syntax ip-protocol @meta{name}
  325. Return ip-protocol flag from given @meta{name}. Implementations must
  326. support at least following names and must have the described behaviour.
  327. @table @code
  328. @item ip
  329. Returns @code{*ipproto-ip*}.
  330. @item tcp
  331. Returns @code{*ipproto-tcp*}.
  332. @item udp
  333. Returns @code{*ipproto-udp*}.
  334. @end table
  335. Implementations may support more names.
  336. @end deffn
  337. @deffn Syntax message-type @meta{name} @dots{}
  338. Return message type flag from given @var{name}. The flag can be used
  339. both by @func{socket-recv} and @func{socket-send}. Implementations must
  340. support at least following names and must have the described behaviour.
  341. @table @code
  342. @item none
  343. Returns no flag.
  344. @item peek
  345. Returns @code{*msg-peek*}.
  346. @item oob
  347. Returns @code{*msg-oob*}.
  348. @item wait-all
  349. Returns @code{*msg-waitall*}.
  350. @end table
  351. Implementations may support more names.
  352. @end deffn
  353. @deffn Syntax shutdown-method @meta{name} @dots{}
  354. Return shutdown method flags from given @meta{names}. Implementations
  355. must support at least following names and must have the described
  356. behaviour.
  357. @table @code
  358. @item read
  359. Returns @code{*shut-rd*}.
  360. @item write
  361. Returns @code{*shut-wr*}.
  362. @end table
  363. If @func{shutdown-method} is given both @code{read} and @code{write},
  364. then it must return @code{*shut-rdwr*}.
  365. @end deffn
  366. @deffn Syntax socket-merge-flags @meta{flags} @dots{}
  367. Merge given @meta{flags} and returns a new flag.
  368. @end deffn
  369. @deffn Syntax socket-purge-flags @meta{base-flag} @meta{flag} @dots{}
  370. Remove @meta{flag} from @meta{base-flag} if it exists and return a new
  371. flag.
  372. @end deffn
  373. @c page
  374. @node srfi basic-socket spec const
  375. @subsubsection Constants
  376. Implementations must support following constant variables. All constant
  377. variable must be consistent with @posix{}'s[1] definition. The
  378. following bindings are exported by the libraries @library{srfi :106} and
  379. @library{srfi :106 socket}.
  380. @c ------------------------------------------------------------
  381. @subsubheading Address family
  382. @table @code
  383. @item *af-inet*
  384. Internet domain sockets for use with IPv4 addresses. This must behave
  385. the same as @posix{}'s @code{AF_INET}.
  386. @item *af-inet6*
  387. Internet domain sockets for use with IPv6 addresses. This must behave
  388. the same as @posix{}'s @code{AF_INET6}.
  389. @item *af-unspec*
  390. Unspecified. This must behave the same as @posix{}'s @code{AF_UNSPEC}.
  391. @end table
  392. @c ------------------------------------------------------------
  393. @subsubheading Socket domain
  394. @table @code
  395. @item *sock-stream*
  396. Byte--stream socket. This must behave the same as @posix{}'s
  397. @code{SOCK_STREAM}.
  398. @item *sock-dgram*
  399. Datagram socket. This must behave the same as @posix{}'s
  400. @code{SOCK_DGRAM}.
  401. @end table
  402. @c ------------------------------------------------------------
  403. @subsubheading Address info
  404. @table @code
  405. @item *ai-canonname*
  406. This must behave the same as @posix{}'s @code{AI_CANONNAME}.
  407. @item *ai-numerichost*
  408. This must behave the same as @posix{}'s @code{AI_NUMERICHOST}.
  409. @item *ai-v4mapped*
  410. This must behave the same as @posix{}'s @code{AI_V4MAPPED}.
  411. @item *ai-all*
  412. This must behave the same as @posix{}'s @code{AI_ALL}.
  413. @item *ai-addrconfig*
  414. This must behave the same as @posix{}'s @code{AI_ADDRCONFIG}.
  415. @end table
  416. @c ------------------------------------------------------------
  417. @subsubheading IP protocol
  418. @table @code
  419. @item *ipproto-ip*
  420. Internet protocol. This must behave the same as @posix{}'s
  421. @code{IPPROTO_IP}.
  422. @item *ipproto-tcp*
  423. Transmission control protocol. This must behave the same as @posix{}'s
  424. @code{IPPROTO_TCP}.
  425. @item *ipproto-udp*
  426. User datagram protocol. This must behave the same as @posix{}'s
  427. @code{IPPROTO_UDP}.
  428. @end table
  429. @c ------------------------------------------------------------
  430. @subsubheading Message type
  431. @table @code
  432. @item *msg-peek*
  433. For @code{socket-recv}. Peeks at an incoming message. The data is
  434. treated as unread and the next @code{socket-recv} shall still return
  435. this data. This must behave the same as @posix{}'s @code{MSG_PEEK}.
  436. @item *msg-oob*
  437. For both @code{socket-recv} and @code{socket-send}. Requests/sends
  438. out--of--band data. This must behave the same as @posix{}'s
  439. @code{MSG_OOB}.
  440. @item *msg-waitall*
  441. For @code{socket-recv}. On sockets created with @code{*sock-stream*}
  442. flag, this requests the procedure block until the full amount of data
  443. ban be returned. This must behave the same as @posix{}'s
  444. @code{MSG_WAITALL}.
  445. @end table
  446. @c ------------------------------------------------------------
  447. @subsubheading Shutdown method
  448. @table @code
  449. @item *shut-rd*
  450. Disables further receive operation. This must behave the same as
  451. @posix{}'s @code{SHUT_RD}.
  452. @item *shut-wr*
  453. Disables further send operations. This must behave the same as
  454. @posix{}'s @code{SHUT_WR}.
  455. @item *shut-rdwr*
  456. Disables further send and receive operations. This must behave the same
  457. as @posix{}'s @code{SHUT_RDWR}.
  458. @end table
  459. @c page
  460. @node srfi basic-socket examples
  461. @subsection Examples
  462. Simple echo server:
  463. @example
  464. (import (vicare)
  465. (prefix (srfi :106 socket)
  466. srfi.))
  467. (define (server-run master-socket)
  468. ;;Handle the first pending connection. If an
  469. ;;exception is raised ignore it.
  470. (guard (E (else
  471. (debug-print (condition-message E))))
  472. (srfi.call-with-socket
  473. (srfi.socket-accept master-socket)
  474. (lambda (server-socket)
  475. (with-compensations
  476. (define in
  477. (compensate
  478. (transcoded-port
  479. (srfi.socket-input-port server-socket)
  480. (native-transcoder))
  481. (with
  482. (close-port in))))
  483. (define ou
  484. (compensate
  485. (transcoded-port
  486. (srfi.socket-output-port server-socket)
  487. (native-transcoder))
  488. (with
  489. (close-port ou))))
  490. (push-compensation
  491. (srfi.socket-shutdown server-socket)
  492. (srfi.socket-close server-socket))
  493. (let loop ((line (read-line in)))
  494. (unless (eof-object? line)
  495. (put-string ou (string-append line "\r\n"))
  496. (flush-output-port ou)
  497. (loop (read-line in))))))))
  498. ;;Handle next pending connection.
  499. (server-run master-socket))
  500. (define echo-master-socket
  501. (srfi.make-server-socket "8080"))
  502. (server-run echo-master-socket)
  503. @end example
  504. Simple echo client:
  505. @example
  506. (import (rnrs)
  507. (prefix (srfi :106 socket)
  508. srfi.))
  509. (define client-socket
  510. (srfi.make-client-socket "localhost" "8080"
  511. (srfi.address-family inet)
  512. (srfi.socket-domain stream)
  513. (srfi.address-info v4mapped addrconfig)
  514. (srfi.ip-protocol ip)))
  515. (srfi.socket-send client-socket (string->utf8 "hello\r\n"))
  516. (display (utf8->string
  517. (srfi.socket-recv client-socket
  518. (string-length "hello\r\n"))))
  519. (flush-output-port (current-output-port))
  520. (srfi.socket-shutdown client-socket
  521. (srfi.shutdown-method read write))
  522. (srfi.socket-close client-socket)
  523. @end example
  524. @c page
  525. @node srfi basic-socket refs
  526. @subsection References
  527. [1] The Open Group Base Specifications Issue 7:
  528. @center @url{http://pubs.opengroup.org/onlinepubs/9699919799/nframe.html}
  529. @c page
  530. @node srfi basic-socket copyright
  531. @subsection Copyright
  532. Copyright @copyright{} Takashi Kato (2012) @email{ktakashi@@ymail.com}.@*
  533. All Rights Reserved.
  534. Permission is hereby granted, free of charge, to any person obtaining a
  535. copy of this software and associated documentation files (the
  536. ``Software''), to deal in the Software without restriction, including
  537. without limitation the rights to use, copy, modify, merge, publish,
  538. distribute, sublicense, and/or sell copies of the Software, and to
  539. permit persons to whom the Software is furnished to do so, subject to
  540. the following conditions:
  541. The above copyright notice and this permission notice shall be included
  542. in all copies or substantial portions of the Software.
  543. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  544. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  545. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  546. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  547. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  548. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  549. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.