1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- Bindings for BSD sockets.
6 (in-package :net.sockets
)
8 (defmacro deforeign
(name-and-opts return-type
&body args
)
9 (multiple-value-bind (lisp-name c-name options
)
10 (cffi::parse-name-and-options name-and-opts
)
11 `(defcfun (,c-name
,lisp-name
,@options
) ,return-type
14 (defmacro define-socket-call
(name return-type
&body args
)
15 `(deforeign ,name
(errno-wrapper ,return-type
16 :error-generator signal-socket-error
)
24 (define-socket-call ("accept" %accept
) :int
25 "Accept an incoming connection, returning the file descriptor."
27 (address :pointer
) ; sockaddr-foo
30 (define-socket-call ("bind" %bind
) :int
31 "Bind a socket to a particular local address."
36 (define-socket-call ("connect" %connect
) :int
37 "Create an outgoing connection on a given socket."
39 (address :pointer
) ; sockaddr-foo
42 (define-socket-call ("getpeername" %getpeername
) :int
47 (define-socket-call ("getsockname" %getsockname
) :int
52 (define-socket-call ("getsockopt" %getsockopt
) :int
53 "Retrieve socket configuration."
60 (define-socket-call ("listen" %listen
) :int
61 "Mark a bound socket as listening for incoming connections."
65 (define-socket-call ("recvfrom" %recvfrom
) ssize
73 (define-socket-call ("sendto" %sendto
) ssize
81 (define-socket-call ("recvmsg" %recvmsg
) ssize
86 (define-socket-call ("sendmsg" %sendmsg
) ssize
91 (define-socket-call ("setsockopt" %setsockopt
) :int
99 (define-socket-call ("shutdown" %shutdown
) :int
103 ;;; SOCKET is emulated in winsock.lisp.
104 (define-socket-call ("socket" %socket
) :int
105 "Create a BSD socket."
111 (define-socket-call ("sockatmark" %sockatmark
) :int
114 (define-socket-call ("socketpair" %%socketpair
) :int
117 (protocol :int
) ; usually 0 - "default protocol", whatever that is
120 (defun %socketpair
(domain type protocol
)
121 (with-foreign-object (filedes :int
2)
122 (%%socketpair domain type protocol filedes
)
123 (values (mem-aref filedes
:int
0)
124 (mem-aref filedes
:int
1))))
128 (defconstant unix-path-max
129 (- size-of-sockaddr-un
(foreign-slot-offset 'sockaddr-un
'path
)))
133 (defcfun ("if_nametoindex" %if-nametoindex
)
134 (errno-wrapper :unsigned-int
:error-predicate zerop
135 :error-generator
(lambda (r)
137 (nix::posix-error
:enxio
)))
140 (defcfun ("if_indextoname" %if-indextoname
)
141 (errno-wrapper :string
)
142 (ifindex :unsigned-int
)
145 (defcfun ("if_nameindex" %if-nameindex
)
146 (errno-wrapper :pointer
)
147 "Return all network interface names and indexes")
149 (defcfun ("if_freenameindex" %if-freenameindex
) :void