1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; bsd.lisp --- Bindings for BSD sockets.
5 ;;; Copyright (C) 2005-2006, Matthew Backes <lucca@accela.net>
6 ;;; Copyright (C) 2005-2006, Dan Knapp <dankna@accela.net> and
7 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@common-lisp.net>
8 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
10 ;;; Permission is hereby granted, free of charge, to any person
11 ;;; obtaining a copy of this software and associated documentation
12 ;;; files (the "Software"), to deal in the Software without
13 ;;; restriction, including without limitation the rights to use, copy,
14 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
15 ;;; of the Software, and to permit persons to whom the Software is
16 ;;; furnished to do so, subject to the following conditions:
18 ;;; The above copyright notice and this permission notice shall be
19 ;;; included in all copies or substantial portions of the Software.
21 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 ;;; DEALINGS IN THE SOFTWARE.
30 (in-package :net.sockets
)
32 (defmacro deforeign
(name-and-opts return-type
&body args
)
33 (multiple-value-bind (lisp-name c-name options
)
34 (cffi::parse-name-and-options name-and-opts
)
35 `(defcfun (,c-name
,lisp-name
,@options
) ,return-type
38 (defmacro define-socket-call
(name return-type
&body args
)
39 `(deforeign ,name
(errno-wrapper ,return-type
40 :error-generator signal-socket-error
)
48 (define-socket-call ("accept" %accept
) :int
49 "Accept an incoming connection, returning the file descriptor."
51 (address :pointer
) ; sockaddr-foo
54 (define-socket-call ("bind" %bind
) :int
55 "Bind a socket to a particular local address."
60 (define-socket-call ("connect" %connect
) :int
61 "Create an outgoing connection on a given socket."
63 (address :pointer
) ; sockaddr-foo
66 (define-socket-call ("getpeername" %getpeername
) :int
71 (define-socket-call ("getsockname" %getsockname
) :int
76 (define-socket-call ("getsockopt" %getsockopt
) :int
77 "Retrieve socket configuration."
84 (define-socket-call ("listen" %listen
) :int
85 "Mark a bound socket as listening for incoming connections."
89 (define-socket-call ("recvfrom" %recvfrom
) ssize
97 (define-socket-call ("sendto" %sendto
) ssize
105 (define-socket-call ("recvmsg" %recvmsg
) ssize
110 (define-socket-call ("sendmsg" %sendmsg
) ssize
115 (define-socket-call ("setsockopt" %setsockopt
) :int
116 "Configure a socket."
123 (define-socket-call ("shutdown" %shutdown
) :int
127 ;;; SOCKET is emulated in winsock.lisp.
128 (define-socket-call ("socket" %socket
) :int
129 "Create a BSD socket."
135 (define-socket-call ("sockatmark" %sockatmark
) :int
138 (define-socket-call ("socketpair" %%socketpair
) :int
141 (protocol :int
) ; usually 0 - "default protocol", whatever that is
144 (defun %socketpair
(domain type protocol
)
145 (with-foreign-object (filedes :int
2)
146 (%%socketpair domain type protocol filedes
)
147 (values (mem-aref filedes
:int
0)
148 (mem-aref filedes
:int
1))))
152 (defconstant unix-path-max
153 (- size-of-sockaddr-un
(foreign-slot-offset 'sockaddr-un
'path
)))
157 (defcfun ("if_nametoindex" %if-nametoindex
)
158 (errno-wrapper :unsigned-int
:error-predicate zerop
159 :error-generator
(lambda (r)
161 (nix::posix-error
:enxio
)))
164 (defcfun ("if_indextoname" %if-indextoname
)
165 (errno-wrapper :string
)
166 (ifindex :unsigned-int
)
169 (defcfun ("if_nameindex" %if-nameindex
)
170 (errno-wrapper :pointer
)
171 "Return all network interface names and indexes")
173 (defcfun ("if_freenameindex" %if-freenameindex
) :void