1.0.39.12: remove darwin-langinfo
[sbcl.git] / contrib / sb-bsd-sockets / inet.lisp
index f6a7c17..869b80f 100644 (file)
@@ -1,34 +1,68 @@
 (in-package :sb-bsd-sockets)
 
-#|| <h2>INET-domain sockets</h2>
-
-<p>The TCP and UDP sockets that you know and love.  Some representation issues:
-<ul>
-<li>These functions do not accept hostnames directly: see <a href="#name-service">name resolution</a>
-<li>Internet <b>addresses</b> are represented by sequences 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.
-<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>
-</ul>
-
-|#
-
 ;;; Our class and constructor
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (defclass inet-socket (socket)
-    ((family :initform sockint::AF-INET))))
+    ((family :initform sockint::AF-INET))
+    (:documentation "Class representing TCP and UDP sockets.
+
+Examples:
+
+ (make-instance 'inet-socket :type :stream :protocol :tcp)
+
+ (make-instance 'inet-socket :type :datagram :protocol :udp)
+")))
 
 ;;; XXX should we *...* this?
 (defparameter inet-address-any (vector 0 0 0 0))
 
+(defmethod socket-namestring ((socket inet-socket))
+  (ignore-errors
+    (multiple-value-bind (addr port) (socket-name socket)
+      (format nil "~{~A~^.~}:~A" (coerce addr 'list) port))))
+
+(defmethod socket-peerstring ((socket inet-socket))
+  (ignore-errors
+    (multiple-value-bind (addr port) (socket-peername socket)
+      (format nil "~{~A~^.~}:~A" (coerce addr 'list) port))))
+
 ;;; binding a socket to an address and port.  Doubt that anyone's
 ;;; actually using this much, to be honest.
 
 (defun make-inet-address (dotted-quads)
   "Return a vector of octets given a string DOTTED-QUADS in the format
-\"127.0.0.1\""
-  (map 'vector
-       #'parse-integer
-       (split dotted-quads nil '(#\.))))
+\"127.0.0.1\". Signals an error if the string is malformed."
+  (declare (type string dotted-quads))
+  (labels ((oops ()
+             (error "~S is not a string designating an IP address."
+                    dotted-quads))
+           (check (x)
+             (if (typep x '(unsigned-byte 8))
+                 x
+                 (oops))))
+    (let* ((s1 (position #\. dotted-quads))
+           (s2 (if s1 (position #\. dotted-quads :start (1+ s1)) (oops)))
+           (s3 (if s2 (position #\. dotted-quads :start (1+ s2)) (oops)))
+           (u0 (parse-integer dotted-quads :end s1))
+           (u1 (parse-integer dotted-quads :start (1+ s1) :end s2))
+           (u2 (parse-integer dotted-quads :start (1+ s2) :end s3)))
+      (multiple-value-bind (u3 end) (parse-integer dotted-quads :start (1+ s3) :junk-allowed t)
+        (unless (= end (length dotted-quads))
+          (oops))
+        (let ((vector (make-array 4 :element-type '(unsigned-byte 8))))
+          (setf (aref vector 0) (check u0)
+                (aref vector 1) (check u1)
+                (aref vector 2) (check u2)
+                (aref vector 3) (check u3))
+          vector)))))
+
+(define-condition unknown-protocol ()
+  ((name :initarg :name
+         :reader unknown-protocol-name))
+  (:report (lambda (c s)
+             (format s "Protocol not found: ~a" (prin1-to-string
+                                                 (unknown-protocol-name c))))))
 
 ;;; getprotobyname only works in the internet domain, which is why this
 ;;; is here
@@ -38,6 +72,8 @@ using getprotobyname(2) which typically looks in NIS or /etc/protocols"
   ;; for extra brownie points, could return canonical protocol name
   ;; and aliases as extra values
   (let ((ent (sockint::getprotobyname name)))
+    (if (sb-alien::null-alien ent)
+        (error 'unknown-protocol :name name))
     (sockint::protoent-proto ent)))
 
 ;;; our protocol provides make-sockaddr-for, size-of-sockaddr,
@@ -55,7 +91,7 @@ using getprotobyname(2) which typically looks in NIS or /etc/protocols"
 
       ;; We have no truck with such dreadful type punning.  Octets to
       ;; octets, dust to dust.
-      
+
       (setf (sockint::sockaddr-in-family sockaddr) sockint::af-inet)
       (setf (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 0) (ldb (byte 8 8) port))
       (setf (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 1) (ldb (byte 8 0) port))
@@ -76,11 +112,11 @@ using getprotobyname(2) which typically looks in NIS or /etc/protocols"
   "Returns address and port of SOCKADDR as multiple values"
   (values
    (coerce (loop for i from 0 below 4
-                collect (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) i))
-          '(vector (unsigned-byte 8) 4))
+                 collect (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) i))
+           '(vector (unsigned-byte 8) 4))
    (+ (* 256 (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 0))
       (sb-alien:deref (sockint::sockaddr-in-port sockaddr) 1))))
-   
+
 (defun make-inet-socket (type protocol)
   "Make an INET socket.  Deprecated in favour of make-instance"
   (make-instance 'inet-socket :type type :protocol protocol))