Hash :
d55d1aa0
Author :
Thomas de Grivel
Date :
2017-05-27T21:38:38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
;;
;; cffi-socket - Common Lisp wrapper for BSD sockets
;;
;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(in-package :cffi-socket)
;; Sockets
(deftype file-descriptor ()
`(unsigned-byte (1- (* 8 (foreign-type-size :int)))))
(defcfun ("socket" c-socket) :int
(domain :int)
(type :int)
(protocol :int))
(defun socket (domain type protocol)
(let ((s (c-socket domain type protocol)))
(when (< s 0)
(error-errno "socket"))
s))
(defmacro with-socket ((var domain type protocol) &body body)
`(let ((,var (socket ,domain ,type ,protocol)))
(unwind-protect (progn ,@body)
(unistd:close ,var))))
(defcstruct sockaddr
(sa-family sa-family-t)
(sa-data :char :count 14))
(defcfun ("bind" c-bind) :int
(sockfd :int)
(addr (:pointer (:struct sockaddr)))
(addrlen :int))
(defcfun "htons" uint16-t
(hostshort uint16-t))
;; IP
(defcstruct sockaddr-in
(sin-family sa-family-t)
(sin-port in-port-t)
(sin-addr uint32-t)
(sin-zero :unsigned-char :count #.(- (foreign-type-size '(:struct sockaddr))
(foreign-type-size 'sa-family-t)
(foreign-type-size 'in-port-t)
(foreign-type-size 'uint32-t))))
(defun inet-addr-from-string (x &key (start 0) (end (length x)))
(ignore-errors
(let ((addr 0))
(flet ((eat-byte ()
(let ((b 0))
(loop while (< start end)
for c = (char x start)
while (char<= #\0 c #\9)
do (setf b (+ (* 10 b) (- (char-code c) (char-code #\0))))
do (incf start))
(setf addr (logior (ash addr 8) b)))))
(dotimes (_ 3)
(eat-byte)
(assert (char= #\. (char x start)))
(incf start))
(eat-byte)
(assert (= end start)))
addr)))
(defun inet-addr (x)
(etypecase x
((unsigned-byte 32) x)
(string (or (inet-addr-from-string x)))))
(defun bind-inet (sockfd addr port)
(with-foreign-object (addr-in '(:struct sockaddr))
(with-foreign-slots ((sin-family sin-port sin-addr)
addr-in (:struct sockaddr-in))
(let ((inet-addr (inet-addr addr)))
(setf sin-family +af-inet+
sin-port (htons port)
sin-addr inet-addr)))
(let ((r (c-bind sockfd addr-in (foreign-type-size
'(:struct sockaddr-in)))))
(when (< r 0)
(error-errno "bind"))
r)))
(defcfun ("listen" c-listen) :int
(sockfd :int)
(backlog :int))
(defun listen (sockfd backlog)
(let ((r (c-listen sockfd backlog)))
(when (< r 0)
(error-errno "listen"))
r))
(defcfun ("accept" c-accept) :int
(sockfd :int)
(addr :pointer)
(addrlen (:pointer :int)))
(defun accept (sockfd)
(with-foreign-object (addr '(:struct sockaddr-in))
(with-foreign-object (addrlen :int)
(setf (mem-ref addrlen :int) (foreign-type-size '(:struct sockaddr-in)))
(let ((s (c-accept sockfd addr addrlen)))
(cond ((<= 0 s)
(values s addr))
((or (= errno +eagain+)
(= errno +ewouldblock+))
nil)
(t
(error-errno "accept")))))))
(defmacro with-accept ((fd-var &optional (addr-var (gensym))) listening-fd
&body body)
(declare (type symbol fd-var addr-var))
`(multiple-value-bind (,fd-var ,addr-var) (accept ,listening-fd)
(when ,fd-var
(unwind-protect (progn ,@body)
(unistd:close ,fd-var)))))
(defcfun ("recv" c-recv) ssize-t
(sockfd :int)
(buf :pointer)
(len size-t)
(flags :int))
(defun recv (sockfd buffer flags &key (start 0) (end (length buffer)))
(declare (type (array (unsigned-byte 8)) buffer))
(let ((len (- end start)))
(with-foreign-pointer (buf len)
(let ((r (c-recv sockfd buf len flags)))
(dotimes (i len)
(setf (aref buffer start) (mem-aref buf :unsigned-char i))
(incf start))
(when (< r 0)
(error-errno "recv"))
r))))
(defun recv-sequence (sockfd buffer flags &key (start 0) (end (length buffer)))
(loop while (< start end)
for r = (recv sockfd buffer flags :start start :end end)
do (when (= r 0) (error "end of file"))
do (incf start r)))
(defcfun ("send" c-send) ssize-t
(sockfd :int)
(buf :pointer)
(len size-t)
(flags :int))
(defun send (sockfd buffer flags &key (start 0) (end (length buffer)))
(declare (type (array (unsigned-byte 8)) buffer))
(let ((len (- end start)))
(with-foreign-pointer (buf len)
(dotimes (i len)
(setf (mem-aref buf :unsigned-char i) (aref buffer start))
(incf start))
(let ((r (c-send sockfd buf len flags)))
(when (< r 0)
(error-errno "send"))
r))))
(defun send-sequence (sockfd buffer flags &key (start 0) (end (length buffer)))
(loop while (< start end)
for r = (send sockfd buffer flags :start start :end end)
do (when (= r 0) (error "end of file"))
do (incf start r)))
(defcfun ("shutdown" c-shutdown) :int
(sockfd :int)
(how :int))
(defun shutdown (sockfd &optional read write)
(when (or read write)
(c-shutdown sockfd (cond ((and read write) +shut-rdwr+)
(read +shut-rd+)
(write +shut-wr+)))))