1 (in-package :sb-bsd-sockets)
3 ;;; Our class and constructor
5 (eval-when (:compile-toplevel :load-toplevel :execute)
6 (defclass inet-socket (socket)
7 ((family :initform sockint::AF-INET))
8 (:documentation "Class representing TCP and UDP sockets.
12 (make-instance 'inet-socket :type :stream :protocol :tcp)
14 (make-instance 'inet-socket :type :datagram :protocol :udp)
17 ;;; XXX should we *...* this?
18 (defparameter inet-address-any (vector 0 0 0 0))
20 ;;; binding a socket to an address and port. Doubt that anyone's
21 ;;; actually using this much, to be honest.
23 (defun make-inet-address (dotted-quads)
24 "Return a vector of octets given a string DOTTED-QUADS in the format
28 (split dotted-quads nil '(#\.))))
30 (define-condition unknown-protocol ()
32 :reader unknown-protocol-name))
33 (:report (lambda (c s)
34 (format s "Protocol not found: ~a" (prin1-to-string
35 (unknown-protocol-name c))))))
37 ;;; getprotobyname only works in the internet domain, which is why this
39 (defun get-protocol-by-name (name) ;exported
40 "Returns the network protocol number associated with the string NAME,
41 using getprotobyname(2) which typically looks in NIS or /etc/protocols"
42 ;; for extra brownie points, could return canonical protocol name
43 ;; and aliases as extra values
44 (let ((ent (sockint::getprotobyname name)))
45 (if (sb-alien::null-alien ent)
46 (error 'unknown-protocol :name name))
47 (sockint::protoent-proto ent)))
49 ;;; our protocol provides make-sockaddr-for, size-of-sockaddr,
52 (defmethod make-sockaddr-for ((socket inet-socket) &optional sockaddr &rest address &aux (host (first address)) (port (second address)))
53 (let ((sockaddr (or sockaddr (sockint::allocate-sockaddr-in))))
55 (setf host (coerce host '(simple-array (unsigned-byte 8) (4))))
56 ;; port and host are represented in C as "network-endian" unsigned
57 ;; integers of various lengths. This is stupid. The value of the
58 ;; integer doesn't matter (and will change depending on your
59 ;; machine's endianness); what the bind(2) call is interested in
60 ;; is the pattern of bytes within that integer.
62 ;; We have no truck with such dreadful type punning. Octets to
63 ;; octets, dust to dust.
65 (setf (sockint::sockaddr-in-family sockaddr) sockint::af-inet)
66 (setf (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 0) (ldb (byte 8 8) port))
67 (setf (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 1) (ldb (byte 8 0) port))
69 (setf (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) 0) (elt host 0))
70 (setf (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) 1) (elt host 1))
71 (setf (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) 2) (elt host 2))
72 (setf (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) 3) (elt host 3)))
75 (defmethod free-sockaddr-for ((socket inet-socket) sockaddr)
76 (sockint::free-sockaddr-in sockaddr))
78 (defmethod size-of-sockaddr ((socket inet-socket))
79 sockint::size-of-sockaddr-in)
81 (defmethod bits-of-sockaddr ((socket inet-socket) sockaddr)
82 "Returns address and port of SOCKADDR as multiple values"
84 (coerce (loop for i from 0 below 4
85 collect (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) i))
86 '(vector (unsigned-byte 8) 4))
87 (+ (* 256 (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 0))
88 (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 1))))
90 (defun make-inet-socket (type protocol)
91 "Make an INET socket. Deprecated in favour of make-instance"
92 (make-instance 'inet-socket :type type :protocol protocol))