Removed definitions related to the libc resolver
[iolib.git] / sockets / bsd.lisp
blob80a8a4565ae0d528d71dc05f6b37df3de3be150a
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" :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" :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" :int
67 (socket fd)
68 (address :pointer)
69 (addrlen :pointer))
71 (define-socket-call "getsockname" :int
72 (socket fd)
73 (address :pointer)
74 (addrlen :pointer))
76 (define-socket-call "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" :int
85 "Mark a bound socket as listening for incoming connections."
86 (socket fd)
87 (backlog :int))
89 (define-socket-call "recv" ssize
90 (socket fd)
91 (buffer :pointer)
92 (length size)
93 (flags :int))
95 (define-socket-call "recvfrom" ssize
96 (socket fd)
97 (buffer :pointer)
98 (length size)
99 (flags :int)
100 (address :pointer)
101 (addrlen :pointer))
103 #-(and) ; unused
104 (define-socket-call "recvmsg" ssize
105 (socket fd)
106 (message :pointer)
107 (flags :int))
109 (define-socket-call "send" ssize
110 (socket fd)
111 (buffer :pointer)
112 (length size)
113 (flags :int))
115 #-(and) ; unused
116 (define-socket-call "sendmsg" ssize
117 (socket fd)
118 (message :pointer)
119 (flags :int))
121 (define-socket-call "sendto" ssize
122 (socket fd)
123 (buffer :pointer)
124 (length size)
125 (flags :int)
126 (destaddr :pointer)
127 (destlen socklen))
129 (define-socket-call "setsockopt" :int
130 "Configure a socket."
131 (fd fd)
132 (level :int)
133 (optname :int)
134 (optval :pointer)
135 (optlen socklen))
137 (define-socket-call ("shutdown" %shutdown) :int
138 (socket fd)
139 (how :int))
141 ;;; SOCKET is emulated in winsock.lisp.
142 (define-socket-call "socket" :int
143 "Create a BSD socket."
144 (domain :int) ; af-*
145 (type :int) ; sock-*
146 (protocol :int))
148 #-(and) ; unused
149 (define-socket-call "sockatmark" :int
150 (socket fd))
152 #-(and) ; unused
153 (define-socket-call ("socketpair" %socketpair) :int
154 (domain :int) ; af-*
155 (type :int) ; sock-*
156 (protocol :int)
157 (filedes :pointer))
159 #-(and) ; unused
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))))
166 ;;;; netinet/un.h
168 (defconstant unix-path-max
169 (- size-of-sockaddr-un (foreign-slot-offset 'sockaddr-un 'path)))
171 ;;;; net/if.h
173 (defcfun "if_nametoindex"
174 (errno-wrapper :unsigned-int :error-predicate zerop)
175 (ifname :string))
177 (define-socket-call "if_indextoname" :string
178 (ifindex :unsigned-int)
179 (ifname :pointer))
181 (define-socket-call "if_nameindex" :pointer
182 "Return all network interface names and indexes")
184 (define-socket-call "if_freenameindex" :void
185 (ptr :pointer))