0.7.12.28
[sbcl.git] / contrib / sb-bsd-sockets / inet.lisp
1 (in-package :sb-bsd-sockets)
2
3 #|| <h2>INET-domain sockets</h2>
4
5 <p>The TCP and UDP sockets that you know and love.  Some representation issues:
6 <ul>
7 <li>These functions do not accept hostnames directly: see <a href="#name-service">name resolution</a>
8 <li>Internet <b>addresses</b> are represented by vectors of <tt>(unsigned-byte 8)</tt> - viz. <tt>#(127 0 0 1)</tt>.  <b>Ports</b> are just integers: <tt>6010</tt>.  No conversion between network- and host-order data is needed from the user of this package.
9 <li><b><i>socket addresses</i></b> are represented by the two values for <b>address</b> and <b>port</b>, so for example, <tt>(<a href="#SOCKET-CONNECT">socket-connect</a> s #(192.168.1.1) 80)</tt>
10 </ul>
11
12 |#
13
14 ;;; Our class and constructor
15
16 (eval-when (:compile-toplevel :load-toplevel :execute)
17   (defclass inet-socket (socket)
18     ((family :initform sockint::AF-INET))))
19
20 ;;; XXX should we *...* this?
21 (defparameter inet-address-any (vector 0 0 0 0))
22
23 ;;; binding a socket to an address and port.  Doubt that anyone's
24 ;;; actually using this much, to be honest.
25
26 (defun make-inet-address (dotted-quads)
27   "Return a vector of octets given a string DOTTED-QUADS in the format
28 \"127.0.0.1\""
29   (coerce
30    (mapcar #'parse-integer
31            (split dotted-quads nil '(#\.)))
32    'vector))
33
34 ;;; getprotobyname only works in the internet domain, which is why this
35 ;;; is here
36 (defun get-protocol-by-name (name)      ;exported
37   "Returns the network protocol number associated with the string NAME,
38 using getprotobyname(2) which typically looks in NIS or /etc/protocols"
39   ;; for extra brownie points, could return canonical protocol name
40   ;; and aliases as extra values
41   (let ((ent (sockint::foreign-vector (sockint::getprotobyname name) 1
42                                       sockint::size-of-protoent)))
43     (sockint::protoent-proto ent)))
44
45
46 ;;; sockaddr protocol
47 ;;; (1) sockaddrs are represented as the semi-foreign array-of-octets
48 ;;; thing
49 ;;; (2) a protocol provides make-sockaddr-for, size-of-sockaddr,
50 ;;; bits-of-sockaddr
51
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))))
54     (when (and host port)
55       ;; port and host are represented in C as "network-endian" unsigned
56       ;; integers of various lengths.  This is stupid.  The value of the
57       ;; integer doesn't matter (and will change depending on your
58       ;; machine's endianness); what the bind(2) call is interested in
59       ;; is the pattern of bytes within that integer.
60       
61       ;; We have no truck with such dreadful type punning.  Octets to
62       ;; octets, dust to dust.
63       
64       (setf (sockint::sockaddr-in-family sockaddr) sockint::af-inet)
65       (setf (sockint::sockaddr-in-port sockaddr 0) (ldb (byte 8 8) port))
66       (setf (sockint::sockaddr-in-port sockaddr 1) (ldb (byte 8 0) port))
67       
68       (setf (sockint::sockaddr-in-addr sockaddr 0) (elt host 0))
69       (setf (sockint::sockaddr-in-addr sockaddr 1) (elt host 1))
70       (setf (sockint::sockaddr-in-addr sockaddr 2) (elt host 2))
71       (setf (sockint::sockaddr-in-addr sockaddr 3) (elt host 3)))
72     sockaddr))
73
74 (defmethod size-of-sockaddr ((socket inet-socket))
75   sockint::size-of-sockaddr-in)
76
77 (defmethod bits-of-sockaddr ((socket inet-socket) sockaddr)
78   "Returns address and port of SOCKADDR as multiple values"
79   (values
80    (vector
81     (sockint::sockaddr-in-addr sockaddr 0) 
82     (sockint::sockaddr-in-addr sockaddr 1) 
83     (sockint::sockaddr-in-addr sockaddr 2) 
84     (sockint::sockaddr-in-addr sockaddr 3))
85    (+ (* 256 (sockint::sockaddr-in-port sockaddr 0))
86       (sockint::sockaddr-in-port sockaddr 1))))  
87    
88
89 (defun make-inet-socket (type protocol)
90   "Make an INET socket.  Deprecated in favour of make-instance"
91   (make-instance 'inet-socket :type type :protocol protocol))
92
93
94