Remove trailing whitespace.
[iolib/alendvai.git] / net.sockets / bsd.lisp
blobb2dcfb51882b205f4c73d169d6289f79755959f2
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Bindings for BSD sockets.
4 ;;;
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
12 ,@args)))
14 (defmacro define-socket-call (name return-type &body args)
15 `(deforeign ,name (errno-wrapper ,return-type
16 :error-generator signal-socket-error)
17 ,@args))
19 (defctype fd :int)
22 ;;;; sys/socket.h
24 (define-socket-call ("accept" %accept) :int
25 "Accept an incoming connection, returning the file descriptor."
26 (socket fd)
27 (address :pointer) ; sockaddr-foo
28 (addrlen :pointer))
30 (define-socket-call ("bind" %bind) :int
31 "Bind a socket to a particular local address."
32 (fd fd)
33 (address :pointer)
34 (addrlen socklen))
36 (define-socket-call ("connect" %connect) :int
37 "Create an outgoing connection on a given socket."
38 (socket fd)
39 (address :pointer) ; sockaddr-foo
40 (addrlen socklen))
42 (define-socket-call ("getpeername" %getpeername) :int
43 (socket fd)
44 (address :pointer)
45 (addrlen :pointer))
47 (define-socket-call ("getsockname" %getsockname) :int
48 (socket fd)
49 (address :pointer)
50 (addrlen :pointer))
52 (define-socket-call ("getsockopt" %getsockopt) :int
53 "Retrieve socket configuration."
54 (fd fd)
55 (level :int)
56 (optname :int)
57 (optval :pointer)
58 (optlen :pointer))
60 (define-socket-call ("listen" %listen) :int
61 "Mark a bound socket as listening for incoming connections."
62 (socket fd)
63 (backlog :int))
65 (define-socket-call ("recvfrom" %recvfrom) ssize
66 (socket fd)
67 (buffer :pointer)
68 (length size)
69 (flags :int)
70 (address :pointer)
71 (addrlen :pointer))
73 (define-socket-call ("sendto" %sendto) ssize
74 (socket fd)
75 (buffer :pointer)
76 (length size)
77 (flags :int)
78 (destaddr :pointer)
79 (destlen socklen))
81 (define-socket-call ("recvmsg" %recvmsg) ssize
82 (socket fd)
83 (message :pointer)
84 (flags :int))
86 (define-socket-call ("sendmsg" %sendmsg) ssize
87 (socket fd)
88 (message :pointer)
89 (flags :int))
91 (define-socket-call ("setsockopt" %setsockopt) :int
92 "Configure a socket."
93 (fd fd)
94 (level :int)
95 (optname :int)
96 (optval :pointer)
97 (optlen socklen))
99 (define-socket-call ("shutdown" %shutdown) :int
100 (socket fd)
101 (how :int))
103 ;;; SOCKET is emulated in winsock.lisp.
104 (define-socket-call ("socket" %socket) :int
105 "Create a BSD socket."
106 (domain :int) ; af-*
107 (type :int) ; sock-*
108 (protocol :int))
110 #-(and) ; unused
111 (define-socket-call ("sockatmark" %sockatmark) :int
112 (socket fd))
114 (define-socket-call ("socketpair" %%socketpair) :int
115 (domain :int) ; af-*
116 (type :int) ; sock-*
117 (protocol :int) ; usually 0 - "default protocol", whatever that is
118 (filedes :pointer))
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))))
126 ;;;; netinet/un.h
128 (defconstant unix-path-max
129 (- size-of-sockaddr-un (foreign-slot-offset 'sockaddr-un 'path)))
131 ;;;; net/if.h
133 (defcfun ("if_nametoindex" %if-nametoindex)
134 (errno-wrapper :unsigned-int :error-predicate zerop
135 :error-generator (lambda (r)
136 (declare (ignore r))
137 (nix::posix-error :enxio)))
138 (ifname :string))
140 (defcfun ("if_indextoname" %if-indextoname)
141 (errno-wrapper :string)
142 (ifindex :unsigned-int)
143 (ifname :pointer))
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
150 (ptr :pointer))