1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
5 ;; This code is free software; you can redistribute it and/or
6 ;; modify it under the terms of the version 2.1 of
7 ;; the GNU Lesser General Public License as published by
8 ;; the Free Software Foundation, as clarified by the
9 ;; preamble found here:
10 ;; http://opensource.franz.com/preamble.html
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU Lesser General
18 ;; Public License along with this library; if not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA
22 (in-package :net.sockets
)
30 (deftype ipv4-array
()
32 (deftype ipv6-array
()
36 (define-modify-macro coercef
(type-spec) coerce
)
39 ;;; Byte-swap functions
43 (check-type short ub16
"a 16-bit unsigned number")
45 (logior (ash (logand (the ub16 short
) #x00FF
) 8)
46 (ash (logand (the ub16 short
) #xFF00
) -
8))
53 (check-type long ub32
"a 32-bit unsigned number")
55 (logior (ash (logand (the ub32 long
) #x000000FF
) 24)
56 (ash (logand (the ub32 long
) #x0000FF00
) 8)
57 (ash (logand (the ub32 long
) #x00FF0000
) -
8)
58 (ash (logand (the ub32 long
) #xFF000000
) -
24))
65 ;;; Conversion between address formats
68 (defun copy-simple-array-ub16-to-alien-vector (lisp-vec alien-vec
)
69 (declare (type ipv6-array lisp-vec
))
71 (setf (mem-aref alien-vec
:uint16 i
)
72 (htons (aref lisp-vec i
)))))
74 (defun map-ipv4-vector-to-ipv6 (addr)
75 (declare (type ipv4-array addr
))
76 (let ((ipv6addr (make-array 8 :element-type
'ub16
78 ;; setting the IPv4 marker
79 (setf (aref ipv6addr
5) #xFFFF
)
80 ;; setting the first two bytes
81 (setf (aref ipv6addr
6) (+ (ash (aref addr
0) 8)
83 ;; setting the last two bytes
84 (setf (aref ipv6addr
7) (+ (ash (aref addr
2) 8)
89 ;; From CLOCC's PORT library
90 (defun vector-to-ipaddr (vector)
91 (coercef vector
'ipv4-array
)
92 (+ (ash (aref vector
0) 24)
93 (ash (aref vector
1) 16)
94 (ash (aref vector
2) 8)
97 (defun ipaddr-to-vector (ipaddr)
98 (check-type ipaddr ub32
)
99 (let ((vector (make-array 4 :element-type
'ub8
)))
100 (setf (aref vector
0) (ldb (byte 8 24) ipaddr
)
101 (aref vector
1) (ldb (byte 8 16) ipaddr
)
102 (aref vector
2) (ldb (byte 8 8) ipaddr
)
103 (aref vector
3) (ldb (byte 8 0) ipaddr
))
106 (defun in6-addr-to-ipv6-array (in6-addr)
107 (let ((vector (make-array 8 :element-type
'ub16
)))
109 (setf (aref vector i
)
110 (ntohs (mem-aref in6-addr
:uint16 i
))))
114 ;;; Constructors for SOCKADDR_* structs
117 (defun make-sockaddr-in (sin ub8-vector
&optional
(port 0))
118 (et:bzero sin et
:size-of-sockaddr-in
)
120 (with-foreign-slots ((et:family et
:addr et
:port
) sin et
:sockaddr-in
)
121 (setf et
:family et
:af-inet
)
122 (setf et
:addr
(htonl (vector-to-ipaddr ub8-vector
)))
123 (setf et
:port
(htons tmp
))))
126 (defun make-sockaddr-in6 (sin6 ub16-vector
&optional
(port 0))
127 (et:bzero sin6 et
:size-of-sockaddr-in6
)
129 (with-foreign-slots ((et:family et
:addr et
:port
) sin6 et
:sockaddr-in6
)
130 (setf et
:family et
:af-inet6
)
131 (copy-simple-array-ub16-to-alien-vector ub16-vector et
:addr
)
132 (setf et
:port
(htons tmp
))))
135 (defun make-sockaddr-un (sun string
)
136 (check-type string string
)
137 (et:bzero sun et
:size-of-sockaddr-un
)
138 (with-foreign-slots ((et:family et
:path
) sun et
:sockaddr-un
)
139 (setf et
:family et
:af-local
)
140 (with-foreign-string (c-string string
)
142 :for off
:below
(1- et
:unix-path-max
)
143 :do
(setf (mem-aref et
:path
:uint8 off
)
144 (mem-aref c-string
:uint8 off
)))))
148 ;;; Conversion functions for SOCKADDR_* structs
151 (defun sockaddr-in->sockaddr
(sin)
152 (with-foreign-slots ((et:addr et
:port
) sin et
:sockaddr-in
)
153 (values (make-instance 'ipv4addr
154 :name
(ipaddr-to-vector (ntohl et
:addr
)))
157 (defun sockaddr-in6->sockaddr
(sin6)
158 (with-foreign-slots ((et:addr et
:port
) sin6 et
:sockaddr-in6
)
159 (values (make-instance 'ipv6addr
160 :name
(in6-addr-to-ipv6-array et
:addr
))
163 (defun sockaddr-un->sockaddr
(sun)
164 (with-foreign-slots ((et:path
) sun et
:sockaddr-un
)
165 (let ((name (make-string (1- et
:unix-path-max
)))
167 (if (zerop (mem-aref et
:path
:uint8
0))
172 :for sindex
:from
0 :below
(1- et
:unix-path-max
)
173 :for pindex
:from
1 :below et
:unix-path-max
174 :do
(setf (schar name sindex
)
175 (code-char (mem-aref et
:path
:uint8 pindex
)))))
176 ;; address is in the filesystem
177 (setf name
(foreign-string-to-lisp et
:path
)))
178 (make-instance 'localaddr
180 :abstract abstract
))))
182 (defun sockaddr-storage->sockaddr
(ss)
183 (with-foreign-slots ((et:family
) ss et
:sockaddr-storage
)
186 (sockaddr-in->sockaddr ss
))
188 (sockaddr-in6->sockaddr ss
))
190 (sockaddr-un->sockaddr ss
)))))
192 (defun sockaddr->sockaddr-storage
(ss sockaddr
&optional
(port 0))
195 (make-sockaddr-in ss
(name sockaddr
) port
))
197 (make-sockaddr-in6 ss
(name sockaddr
) port
))
199 (make-sockaddr-un ss
(name sockaddr
)))))
205 (defun %to-octets
(buff ef start end
)
206 (io.encodings
:string-to-octets buff
:external-format ef
207 :start start
:end end
))
209 (defun parse-number-or-nil (value &optional
(type :any
) (radix 10))
210 (check-type value
(or string unsigned-byte
))
213 (ignore-errors (parse-integer value
:radix radix
217 ;; if it's a number and its type is ok return it
220 (:ub8
(typep parsed
'ub8
))
221 (:ub16
(typep parsed
'ub16
))
222 (:ub32
(typep parsed
'ub32
)))
227 (defun c->lisp-bool
(val)
228 (if (zerop val
) nil t
))
230 (defun lisp->c-bool
(val)
233 (defun addrerr-value (keyword)
234 (foreign-enum-value 'et
:addrinfo-errors keyword
))
236 (defun unixerr-value (keyword)
237 (foreign-enum-value 'et
:errno-values keyword
))