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" :int
49 "Accept an incoming connection, returning the file descriptor."
51 (address :pointer
) ; sockaddr-foo
54 (define-socket-call "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" :int
71 (define-socket-call "getsockname" :int
76 (define-socket-call "getsockopt" :int
77 "Retrieve socket configuration."
84 (define-socket-call "listen" :int
85 "Mark a bound socket as listening for incoming connections."
89 (define-socket-call "recv" ssize
95 (define-socket-call "recvfrom" ssize
104 (define-socket-call "recvmsg" ssize
109 (define-socket-call "send" ssize
116 (define-socket-call "sendmsg" ssize
121 (define-socket-call "sendto" ssize
129 (define-socket-call "setsockopt" :int
130 "Configure a socket."
137 (define-socket-call ("shutdown" %shutdown
) :int
141 ;;; SOCKET is emulated in winsock.lisp.
142 (define-socket-call "socket" :int
143 "Create a BSD socket."
149 (define-socket-call "sockatmark" :int
153 (define-socket-call ("socketpair" %socketpair
) :int
160 (defun socketpair (domain type protocol
)
161 (with-foreign-object (filedes :int
2)
162 (%socketpair domain type protocol filedes
)
163 (values (mem-aref filedes
:int
0)
164 (mem-aref filedes
:int
1))))
168 (defconstant unix-path-max
169 (- size-of-sockaddr-un
(foreign-slot-offset 'sockaddr-un
'path
)))
173 (defcfun "if_nametoindex"
174 (errno-wrapper :unsigned-int
:error-predicate zerop
)
177 (define-socket-call "if_indextoname" :string
178 (ifindex :unsigned-int
)
181 (define-socket-call "if_nameindex" :pointer
182 "Return all network interface names and indexes")
184 (define-socket-call "if_freenameindex" :void