#| Copyright (c) 2006 Risto Laakso All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (in-package :nio) (declaim (optimize (debug 3) (speed 3) (space 0))) ;;;; FFI (defconstant +af-inet+ 2) (defconstant +af-inet6+ 30) (defconstant +sock-stream+ 1) (defconstant +sock-dgram+ 2) (defcstruct sockaddr-in (len :uint8) (family :uint8) (port :uint16) (addr :uint32) (zero :char :count 8)) (defconstant +sockaddr-in-len+ #.(+ 1 1 2 4 8)) (defcstruct sockaddr-in6 (len :uint8) (family :uint8) (port :uint16) (flowinfo :uint32) (addr :uint16 :count 8) (scope-id :uint32)) (defconstant +sockaddr-in6-len+ #.(+ 1 1 2 4 16 4)) (defcfun ("socket" %socket) :int (domain :int) (type :int) (protocol :int)) (defcfun ("inet_pton" %inet-pton) :int (af :int) (src :string) (dst :pointer)) (defcfun ("htons" %htons) :uint16 (host-value :uint16)) (defcfun ("bind" %bind) :int (socket :int) (sockaddr :pointer) (socklent :long)) (defcfun ("listen" %listen) :int (socket :int) (backlog :int)) (defcfun ("accept" %accept) :int (socket :int) (sockaddr :pointer) (socklen :pointer)) ;;TODO put backlog on config (defun start-listen (socket-fd &optional (backlog 1000)) (%listen socket-fd backlog)) ;;;; IPv4 (defun make-inet-socket (&optional (type :tcp)) (%socket +af-inet+ (ecase type (:tcp +sock-stream+) (:udp +sock-dgram+)) 0)) (defun bind-inet-socket (socket-fd port &optional (addr "127.0.0.1")) (with-foreign-object (sa 'sockaddr-in) (memzero sa +sockaddr-in-len+) ;; init struct (setf (foreign-slot-value sa 'sockaddr-in 'len) +sockaddr-in-len+ (foreign-slot-value sa 'sockaddr-in 'port) (%htons port) (foreign-slot-value sa 'sockaddr-in 'family) +af-inet+) ;; set addr (if (/= (%inet-pton +af-inet+ addr (foreign-slot-pointer sa 'sockaddr-in 'addr)) 1) (error "inet_pton: Bad address ~A!" addr)) ;; bind (if (= (%bind socket-fd sa +sockaddr-in-len+) 0) t nil))) ;;;; IPv6 (defun make-inet6-socket (&optional (type :tcp)) (%socket +af-inet6+ (ecase type (:tcp +sock-stream+) (:udp +sock-dgram+)) 0)) (defun bind-inet6-socket (socket-fd port &optional (addr "::1")) (with-foreign-object (sa 'sockaddr-in6) (memzero sa +sockaddr-in6-len+) ;; init struct (setf (foreign-slot-value sa 'sockaddr-in6 'len) +sockaddr-in6-len+ (foreign-slot-value sa 'sockaddr-in6 'port) (%htons port) (foreign-slot-value sa 'sockaddr-in6 'family) +af-inet6+) ;; set addr (if (/= (%inet-pton +af-inet6+ addr (foreign-slot-pointer sa 'sockaddr-in6 'addr)) 1) (error "inet_pton: Bad address ~A!" addr)) ;; bind (if (= (%bind socket-fd sa +sockaddr-in6-len+) 0) t nil))) ;;;; SOCKET I/O (defclass async-socket-fd (async-fd) ((family :initform :unknown :initarg :family) (remote-host :initform nil :initarg :remote-host) (remote-port :initform nil :initarg :remote-port))) (defun socket-accept (socket-fd) "Accept connection from SOCKET-FD. Allocates and returns socket structure denoting the connection." (flet ((parse-inet6-addr (addr) (let ((client-addr (foreign-slot-value addr 'sockaddr-in6 'addr))) (loop for i from 0 to 7 for hex = (mem-aref client-addr :unsigned-short i) collect hex into res finally (return res)))) (parse-inet4-addr (addr) (let ((client-addr (foreign-slot-value addr 'sockaddr-in 'addr))) (loop for i from 0 to 3 for hex = (logand (ash client-addr (- (* i 8))) #xFF) collect hex into res finally (return (nreverse res)))))) (with-foreign-object (addr 'sockaddr-in6) (let ((len (foreign-alloc :unsigned-long :initial-element +sockaddr-in6-len+))) ;; accept connection (let* ((res (%accept socket-fd addr len)) (async-socket-fd (make-instance 'async-socket-fd :read-fd res :write-fd res))) (unless (< res 0) (let ((len-value (mem-ref len :unsigned-int))) ;; parse sockaddr struct for remote client info (with-slots (family remote-host remote-port) async-socket-fd (cond ((= len-value +sockaddr-in6-len+) (setf family :inet6 remote-port (foreign-slot-value addr 'sockaddr-in6 'port) remote-host (parse-inet6-addr addr))) ((= len-value +sockaddr-in-len+) (setf family :inet4 remote-port (foreign-slot-value addr 'sockaddr-in 'port) remote-host (parse-inet4-addr addr))))) (foreign-free len) (if (>= res 0) async-socket-fd nil) ))))))) (defun remote-info (async-socket-fd) "Return FAMILY, REMOTE-HOST and REMOTE-PORT in list." (with-slots (family remote-host remote-port) async-socket-fd (list family remote-host remote-port)))