connection: Allow users to pass extra SOCK_ flags to 'socket'.

Ludovic Court??sTue May 23 20:44:48+0200 2023

c0f3f1b

connection: Allow users to pass extra SOCK_ flags to 'socket'. In particular, this lets users pass SOCK_NONBLOCK. * netlink/connection.scm (open-socket): Add 'flags' parameter and honor it. (connect): Add #:flags and pass it to 'open-socket'. (connect-route): Add #:flags and pass it to 'connect'. * doc/guile-netlink.texi (Netlink Connections): Adjust accordingly. Signed-off-by: Julien Lepiller <julien@lepiller.eu>

doc/guile-netlink.texi

240240
the set of broadcast groups to which the connection subscribes.
241241
@end deffn
242242
243-
@deffn {Scheme Procedure} connect @var{proto} @var{addr}
243+
@cindex non-blocking socket
244+
@deffn {Scheme Procedure} connect @var{proto} @var{addr} [#:flags 0]
244245
Creates a netlink socket for @var{proto} and binds it to @var{addr}.
245246
246247
@var{proto} is the integer representing the protocol.  For instance, rtnetlink

248249
@code{(netlink constant)}).
249250
250251
@var{addr} is a bytevector, as returned by @code{get-addr}.
252+
253+
@var{flags} is a set of additional flags to pass as the second argument
254+
to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}.
251255
@end deffn
252256
253-
@deffn {Scheme Procedure} connect-route [#:groups @code{0}]
257+
@deffn {Scheme Procedure} connect-route [#:groups 0] [#:flags 0]
254258
This procedure is a wrapper for @code{connect} that creates a socket for the
255259
rtnetlink protocol, binds it to the kernel and returns it.  By passing the
256260
optional @var{groups} keyword, you can select broadcast groups to subscribe to.
261+
262+
@var{flags} is a set of additional flags to pass as the second argument
263+
to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}.
257264
@end deffn
258265
259266
@deffn {Scheme Procedure} send-msg @var{msg} @var{sock} [#:@var{addr}]

netlink/connection.scm

7474
                      #:waiter (lambda () (current-read-waiter))))
7575
7676
;; define simple functions to open/close sockets
77-
(define (open-socket proto)
78-
  (socket AF_NETLINK (logior SOCK_RAW SOCK_CLOEXEC) proto))
77+
(define (open-socket proto flags)
78+
  (socket AF_NETLINK (logior SOCK_RAW SOCK_CLOEXEC flags) proto))
7979
8080
(define (close-socket sock)
8181
  (issue-deprecation-warning

102102
    (list '* size_t)
103103
    (list content size)))
104104
105-
(define* (connect proto addr)
106-
  (let ((sock (open-socket proto)))
105+
(define* (connect proto addr #:key (flags 0))
106+
  (let ((sock (open-socket proto flags)))
107107
    (ffi-bind sock
108108
              (bytevector->pointer addr)
109109
              12)
110110
    sock))
111111
112-
(define* (connect-route #:key (groups 0))
113-
  (connect NETLINK_ROUTE (get-addr AF_NETLINK 0 groups)))
112+
(define* (connect-route #:key (groups 0) (flags 0))
113+
  (connect NETLINK_ROUTE (get-addr AF_NETLINK 0 groups)
114+
           #:flags flags))
114115
115116
(define* (send-msg msg sock #:key (addr (get-addr AF_NETLINK 0 0)))
116117
  (unless (message? msg)