Moved GRAY-STREAM implementation to the new IO.STREAMS package.
[iolib.git] / sockets / common.lisp
bloba444fe8d09e3a1ee4986e910705aaf1336f2ca96
1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
4 ;;
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)
24 ;;;;;;;;;;;;;
25 ;;; ;;;
26 ;;; Types ;;;
27 ;;; ;;;
28 ;;;;;;;;;;;;;
30 (deftype ipv4-array ()
31 '(ub8-sarray 4))
32 (deftype ipv6-array ()
33 '(ub16-sarray 8))
36 (define-modify-macro coercef (type-spec) coerce)
38 ;;;
39 ;;; Byte-swap functions
40 ;;;
42 (defun htons (short)
43 (check-type short ub16 "a 16-bit unsigned number")
44 #+little-endian
45 (logior (ash (logand (the ub16 short) #x00FF) 8)
46 (ash (logand (the ub16 short) #xFF00) -8))
47 #+big-endian short)
49 (defun ntohs (short)
50 (htons short))
52 (defun htonl (long)
53 (check-type long ub32 "a 32-bit unsigned number")
54 #+little-endian
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))
59 #+big-endian long)
61 (defun ntohl (long)
62 (htonl long))
64 ;;;
65 ;;; Conversion between address formats
66 ;;;
68 (defun copy-simple-array-ub16-to-alien-vector (lisp-vec alien-vec)
69 (declare (type ipv6-array lisp-vec))
70 (dotimes (i 8)
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
77 :initial-element 0)))
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)
82 (aref addr 1)))
83 ;; setting the last two bytes
84 (setf (aref ipv6addr 7) (+ (ash (aref addr 2) 8)
85 (aref addr 3)))
87 ipv6addr))
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)
95 (aref vector 3)))
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))
104 vector))
106 (defun in6-addr-to-ipv6-array (in6-addr)
107 (let ((vector (make-array 8 :element-type 'ub16)))
108 (dotimes (i 8)
109 (setf (aref vector i)
110 (ntohs (mem-aref in6-addr :uint16 i))))
111 vector))
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)
119 (let ((tmp port))
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))))
124 sin)
126 (defun make-sockaddr-in6 (sin6 ub16-vector &optional (port 0))
127 (et:bzero sin6 et:size-of-sockaddr-in6)
128 (let ((tmp port))
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))))
133 sin6)
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)
141 (loop
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)))))
145 sun)
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)))
155 (ntohs et:port))))
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))
161 (ntohs et:port))))
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)))
166 (abstract nil))
167 (if (zerop (mem-aref et:path :uint8 0))
168 ;; abstract address
169 (progn
170 (setf abstract t)
171 (loop
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
179 :name name
180 :abstract abstract))))
182 (defun sockaddr-storage->sockaddr (ss)
183 (with-foreign-slots ((et:family) ss et:sockaddr-storage)
184 (ecase et:family
185 (#.et:af-inet
186 (sockaddr-in->sockaddr ss))
187 (#.et:af-inet6
188 (sockaddr-in6->sockaddr ss))
189 (#.et:af-local
190 (sockaddr-un->sockaddr ss)))))
192 (defun sockaddr->sockaddr-storage (ss sockaddr &optional (port 0))
193 (etypecase sockaddr
194 (ipv4addr
195 (make-sockaddr-in ss (name sockaddr) port))
196 (ipv6addr
197 (make-sockaddr-in6 ss (name sockaddr) port))
198 (localaddr
199 (make-sockaddr-un ss (name sockaddr)))))
202 ;;; Misc
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))
211 (let ((parsed
212 (if (stringp value)
213 (ignore-errors (parse-integer value :radix radix
214 :junk-allowed nil))
215 value)))
216 (if parsed
217 ;; if it's a number and its type is ok return it
218 (and (ecase type
219 (:any t)
220 (:ub8 (typep parsed 'ub8))
221 (:ub16 (typep parsed 'ub16))
222 (:ub32 (typep parsed 'ub32)))
223 parsed)
224 ;; otherwise nil
225 nil)))
227 (defun c->lisp-bool (val)
228 (if (zerop val) nil t))
230 (defun lisp->c-bool (val)
231 (if val 1 0))
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))