Update docs.
[iolib.git] / net.sockets / bsd.lisp
blob2b42ccdcc1f5803e439ada1e84592c026d35790a
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; bsd.lisp --- Bindings for BSD sockets.
4 ;;;
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>
9 ;;;
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:
17 ;;;
18 ;;; The above copyright notice and this permission notice shall be
19 ;;; included in all copies or substantial portions of the Software.
20 ;;;
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
36 ,@args)))
38 (defmacro define-socket-call (name return-type &body args)
39 `(deforeign ,name (errno-wrapper ,return-type
40 :error-generator signal-socket-error)
41 ,@args))
43 (defctype fd :int)
46 ;;;; sys/socket.h
48 (define-socket-call ("accept" %accept) :int
49 "Accept an incoming connection, returning the file descriptor."
50 (socket fd)
51 (address :pointer) ; sockaddr-foo
52 (addrlen :pointer))
54 (define-socket-call ("bind" %bind) :int
55 "Bind a socket to a particular local address."
56 (fd fd)
57 (address :pointer)
58 (addrlen socklen))
60 (define-socket-call ("connect" %connect) :int
61 "Create an outgoing connection on a given socket."
62 (socket fd)
63 (address :pointer) ; sockaddr-foo
64 (addrlen socklen))
66 (define-socket-call ("getpeername" %getpeername) :int
67 (socket fd)
68 (address :pointer)
69 (addrlen :pointer))
71 (define-socket-call ("getsockname" %getsockname) :int
72 (socket fd)
73 (address :pointer)
74 (addrlen :pointer))
76 (define-socket-call ("getsockopt" %getsockopt) :int
77 "Retrieve socket configuration."
78 (fd fd)
79 (level :int)
80 (optname :int)
81 (optval :pointer)
82 (optlen :pointer))
84 (define-socket-call ("listen" %listen) :int
85 "Mark a bound socket as listening for incoming connections."
86 (socket fd)
87 (backlog :int))
89 (define-socket-call ("recvfrom" %recvfrom) ssize
90 (socket fd)
91 (buffer :pointer)
92 (length size)
93 (flags :int)
94 (address :pointer)
95 (addrlen :pointer))
97 (define-socket-call ("sendto" %sendto) ssize
98 (socket fd)
99 (buffer :pointer)
100 (length size)
101 (flags :int)
102 (destaddr :pointer)
103 (destlen socklen))
105 (define-socket-call ("recvmsg" %recvmsg) ssize
106 (socket fd)
107 (message :pointer)
108 (flags :int))
110 (define-socket-call ("sendmsg" %sendmsg) ssize
111 (socket fd)
112 (message :pointer)
113 (flags :int))
115 (define-socket-call ("setsockopt" %setsockopt) :int
116 "Configure a socket."
117 (fd fd)
118 (level :int)
119 (optname :int)
120 (optval :pointer)
121 (optlen socklen))
123 (define-socket-call ("shutdown" %shutdown) :int
124 (socket fd)
125 (how :int))
127 ;;; SOCKET is emulated in winsock.lisp.
128 (define-socket-call ("socket" %socket) :int
129 "Create a BSD socket."
130 (domain :int) ; af-*
131 (type :int) ; sock-*
132 (protocol :int))
134 #-(and) ; unused
135 (define-socket-call ("sockatmark" %sockatmark) :int
136 (socket fd))
138 (define-socket-call ("socketpair" %%socketpair) :int
139 (domain :int) ; af-*
140 (type :int) ; sock-*
141 (protocol :int) ; usually 0 - "default protocol", whatever that is
142 (filedes :pointer))
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))))
150 ;;;; netinet/un.h
152 (defconstant unix-path-max
153 (- size-of-sockaddr-un (foreign-slot-offset 'sockaddr-un 'path)))
155 ;;;; net/if.h
157 (defcfun ("if_nametoindex" %if-nametoindex)
158 (errno-wrapper :unsigned-int :error-predicate zerop
159 :error-generator (lambda (r)
160 (declare (ignore r))
161 (nix::posix-error :enxio)))
162 (ifname :string))
164 (defcfun ("if_indextoname" %if-indextoname)
165 (errno-wrapper :string)
166 (ifindex :unsigned-int)
167 (ifname :pointer))
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
174 (ptr :pointer))