Windows port. Lots of rough edges, passes the most important tests.
[iolib.git] / sockets / common.lisp
blob198e3711de28c693ddaa02b46741514bc4ddbf54
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; common.lisp --- Various helpers for bsd-sockets.
4 ;;;
5 ;;; Copyright (C) 2006-2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :net.sockets)
26 ;;;; Types
28 (deftype ipv4-array () '(ub8-sarray 4))
29 (deftype ipv6-array () '(ub16-sarray 8))
31 ;;;; Byte-swap functions
33 (defun htons (short)
34 (check-type short ub16 "a 16-bit unsigned number")
35 #+little-endian
36 (logior (ash (logand (the ub16 short) #x00FF) 8)
37 (ash (logand (the ub16 short) #xFF00) -8))
38 #+big-endian short)
40 (defun ntohs (short)
41 (htons short))
43 (defun htonl (long)
44 (check-type long ub32 "a 32-bit unsigned number")
45 #+little-endian
46 (logior (ash (logand (the ub32 long) #x000000FF) 24)
47 (ash (logand (the ub32 long) #x0000FF00) 8)
48 (ash (logand (the ub32 long) #x00FF0000) -8)
49 (ash (logand (the ub32 long) #xFF000000) -24))
50 #+big-endian long)
52 (defun ntohl (long)
53 (htonl long))
55 ;;;; Conversion between address formats
57 (defun copy-simple-array-ub16-to-alien-vector (lisp-vec alien-vec)
58 (declare (type ipv6-array lisp-vec))
59 (dotimes (i 8)
60 (setf (mem-aref alien-vec :uint16 i)
61 (htons (aref lisp-vec i)))))
63 (defun map-ipv4-vector-to-ipv6 (addr)
64 (declare (type ipv4-array addr))
65 (let ((ipv6addr (make-array 8 :element-type 'ub16
66 :initial-element 0)))
67 ;; setting the IPv4 marker
68 (setf (aref ipv6addr 5) #xFFFF)
69 ;; setting the first two bytes
70 (setf (aref ipv6addr 6) (+ (ash (aref addr 0) 8)
71 (aref addr 1)))
72 ;; setting the last two bytes
73 (setf (aref ipv6addr 7) (+ (ash (aref addr 2) 8)
74 (aref addr 3)))
75 ipv6addr))
77 ;;; From CLOCC's PORT library.
78 (defun vector-to-integer (vector)
79 "Convert a vector to a 32-bit unsigned integer."
80 (coercef vector 'ipv4-array)
81 (+ (ash (aref vector 0) 24)
82 (ash (aref vector 1) 16)
83 (ash (aref vector 2) 8)
84 (aref vector 3)))
86 (defun integer-to-vector (ipaddr)
87 "Convert a 32-bit unsigned integer to a vector."
88 (check-type ipaddr ub32)
89 (let ((vector (make-array 4 :element-type 'ub8)))
90 (setf (aref vector 0) (ldb (byte 8 24) ipaddr)
91 (aref vector 1) (ldb (byte 8 16) ipaddr)
92 (aref vector 2) (ldb (byte 8 8) ipaddr)
93 (aref vector 3) (ldb (byte 8 0) ipaddr))
94 vector))
96 (defun in6-addr-to-ipv6-array (in6-addr)
97 (let ((vector (make-array 8 :element-type 'ub16)))
98 (dotimes (i 8)
99 (setf (aref vector i)
100 (ntohs (mem-aref in6-addr :uint16 i))))
101 vector))
103 ;;;; Constructors for SOCKADDR_* structs
105 (defun make-sockaddr-in (sin ub8-vector &optional (port 0))
106 (declare (type ipv4-array ub8-vector) (type ub16 port))
107 (nix:bzero sin size-of-sockaddr-in)
108 (with-foreign-slots ((family addr port) sin sockaddr-in)
109 (setf family af-inet)
110 (setf addr (htonl (vector-to-integer ub8-vector)))
111 (setf port (htons port)))
112 (values sin))
114 (defmacro with-sockaddr-in ((var address &optional (port 0)) &body body)
115 `(with-foreign-object (,var 'sockaddr-in)
116 (make-sockaddr-in ,var ,address ,port)
117 ,@body))
119 (defun make-sockaddr-in6 (sin6 ub16-vector &optional (port 0))
120 (declare (type ipv6-array ub16-vector) (type ub16 port))
121 (nix:bzero sin6 size-of-sockaddr-in6)
122 (with-foreign-slots ((family addr port) sin6 sockaddr-in6)
123 (setf family af-inet6)
124 (copy-simple-array-ub16-to-alien-vector ub16-vector addr)
125 (setf port (htons port)))
126 (values sin6))
128 (defmacro with-sockaddr-in6 ((var address &optional port) &body body)
129 `(with-foreign-object (,var 'sockaddr-in6)
130 (make-sockaddr-in6 ,var ,address ,port)
131 ,@body))
133 #-windows
134 (defun make-sockaddr-un (sun string)
135 (declare (type string string))
136 (nix:bzero sun size-of-sockaddr-un)
137 (with-foreign-slots ((family path) sun sockaddr-un)
138 (setf family af-local)
139 (with-foreign-string (c-string string)
140 (loop :for off :below (1- unix-path-max)
141 :do (setf (mem-aref path :uint8 off)
142 (mem-aref c-string :uint8 off)))))
143 (values sun))
145 #-windows
146 (defmacro with-sockaddr-un ((var address) &body body)
147 `(with-foreign-object (,var 'sockaddr-un)
148 (make-sockaddr-un ,var ,address)
149 ,@body))
151 ;;;; Conversion functions for SOCKADDR_* structs
153 (defun sockaddr-in->sockaddr (sin)
154 (with-foreign-slots ((addr port) sin sockaddr-in)
155 (values (make-instance 'ipv4-address
156 :name (integer-to-vector (ntohl addr)))
157 (ntohs port))))
159 (defun sockaddr-in6->sockaddr (sin6)
160 (with-foreign-slots ((addr port) sin6 sockaddr-in6)
161 (values (make-instance 'ipv6-address
162 :name (in6-addr-to-ipv6-array addr))
163 (ntohs port))))
165 #-windows
166 (defun sockaddr-un->sockaddr (sun)
167 (with-foreign-slots ((path) sun sockaddr-un)
168 (let ((name (make-string (1- unix-path-max)))
169 (abstract nil))
170 (if (zerop (mem-aref path :uint8 0))
171 ;; abstract address
172 (progn
173 (setf abstract t)
174 (loop :for sindex :from 0 :below (1- unix-path-max)
175 :for pindex :from 1 :below unix-path-max
176 :do (setf (schar name sindex)
177 (code-char (mem-aref path :uint8 pindex)))))
178 ;; address is in the filesystem
179 (setf name (foreign-string-to-lisp path)))
180 (make-instance 'local-address
181 :name name
182 :abstract abstract))))
184 (defun sockaddr-storage->sockaddr (ss)
185 (with-foreign-slots ((family) ss sockaddr-storage)
186 (ecase family
187 (#.af-inet (sockaddr-in->sockaddr ss))
188 (#.af-inet6 (sockaddr-in6->sockaddr ss))
189 #-windows (#.af-local (sockaddr-un->sockaddr ss)))))
191 (defun sockaddr->sockaddr-storage (ss sockaddr &optional (port 0))
192 (etypecase sockaddr
193 (ipv4-address (make-sockaddr-in ss (address-name sockaddr) port))
194 (ipv6-address (make-sockaddr-in6 ss (address-name sockaddr) port))
195 #-windows (local-address (make-sockaddr-un ss (address-name sockaddr)))))
197 ;;;; Misc
199 (defmacro check-bounds (sequence start end)
200 (alexandria:with-unique-names (length)
201 `(let ((,length (length ,sequence)))
202 (unless ,end
203 (setq ,end ,length))
204 (unless (<= ,start ,end ,length)
205 (error "Wrong sequence bounds. start: ~S end: ~S" ,start ,end)))))
207 (defun %to-octets (buff ef start end)
208 (babel:string-to-octets buff :start start :end end
209 :encoding (babel:external-format-encoding ef)))
211 (defun parse-number-or-nil (value &optional (type :any) (radix 10))
212 (check-type value (or string unsigned-byte))
213 (let ((parsed
214 (if (stringp value)
215 (ignore-errors (parse-integer value :radix radix
216 :junk-allowed nil))
217 value)))
218 (and parsed
219 ;; if it's a number and its type is ok return it
220 (typep parsed (ecase type
221 (:any t) (:ub8 'ub8)
222 (:ub16 'ub16) (:ub32 'ub32)))
223 (values parsed))))
225 (defun lisp->c-bool (val)
226 (if val 1 0))
228 (defmacro with-socklen ((var value) &body body)
229 `(with-foreign-object (,var 'socklen)
230 (setf (mem-ref ,var 'socklen) ,value)
231 ,@body))