From bb9313c336417fedcdee98d5eaef3db75d6026dc Mon Sep 17 00:00:00 2001
From: Andreas Fuchs <asf@boinkor.net>
Date: Mon, 31 May 2004 20:01:07 +0000
Subject: [PATCH] 0.8.10.71: 	Export socket conditions and accessors;
 improve EAGAIN support

	... Export all socket errors and the parent condition
	    SOCKET-ERROR from the sb-bsd-sockets package

	... Also export socket-* accessors (except socket-stream)

	... We now return NIL from socket-connect and socket-receive
	    when connect(2) or recvfrom(2) fails with errno set to EAGAIN.

	These fixes were suggested by Tony Martinez.
---
 contrib/sb-bsd-sockets/constants.lisp  |    1 +
 contrib/sb-bsd-sockets/defpackage.lisp |   21 +++++++-----------
 contrib/sb-bsd-sockets/sockets.lisp    |   38 ++++++++++++++++++--------------
 version.lisp-expr                      |    2 +-
 4 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/contrib/sb-bsd-sockets/constants.lisp b/contrib/sb-bsd-sockets/constants.lisp
index fa17043..97a9fa8 100644
--- a/contrib/sb-bsd-sockets/constants.lisp
+++ b/contrib/sb-bsd-sockets/constants.lisp
@@ -65,6 +65,7 @@
  (:integer EAGAIN "EAGAIN")
  (:integer EBADF "EBADF")
  (:integer ECONNREFUSED "ECONNREFUSED")
+ (:integer ETIMEDOUT "ETIMEDOUT")
  (:integer EINTR "EINTR")
  (:integer EINVAL "EINVAL")
  (:integer ENOBUFS "ENOBUFS")
diff --git a/contrib/sb-bsd-sockets/defpackage.lisp b/contrib/sb-bsd-sockets/defpackage.lisp
index 344136d..a99bea2 100644
--- a/contrib/sb-bsd-sockets/defpackage.lisp
+++ b/contrib/sb-bsd-sockets/defpackage.lisp
@@ -35,8 +35,9 @@
            socket-bind socket-accept socket-connect
            socket-send socket-receive socket-recv
            socket-name socket-peername socket-listen
-           socket-close socket-file-descriptor socket-make-stream
-           get-protocol-by-name
+           socket-close socket-file-descriptor
+	   socket-family socket-protocol socket-type
+	   socket-make-stream get-protocol-by-name
 
            get-host-by-name get-host-by-address
            host-ent
@@ -53,17 +54,11 @@
 	   ;; all socket options are also exported, by code in
 	   ;; sockopt.lisp
 
-           bad-file-descriptor-error
-           address-in-use-error
-           interrupted-error
-           invalid-argument-error
-           out-of-memory-error
-           operation-not-supported-error
-           operation-not-permitted-error
-           protocol-not-supported-error
-	   socket-type-not-supported-error
-           network-unreachable-error
-           
+	   socket-error
+
+	   ;; other errno-based socket errors are exported by code in
+	   ;; sockets.lisp
+	   
            make-inet-address
 
            non-blocking-mode
diff --git a/contrib/sb-bsd-sockets/sockets.lisp b/contrib/sb-bsd-sockets/sockets.lisp
index caaccc8..c189e5c 100644
--- a/contrib/sb-bsd-sockets/sockets.lisp
+++ b/contrib/sb-bsd-sockets/sockets.lisp
@@ -88,15 +88,17 @@ values"))
     (let ((fd (sockint::accept (socket-file-descriptor socket)
 			       sockaddr
 			       (size-of-sockaddr socket))))
-      (apply #'values
-	     (if (= fd -1)
-		 (socket-error "accept")
-		 (let ((s (make-instance (class-of socket)
-			     :type (socket-type socket)
-			     :protocol (socket-protocol socket)
-			     :descriptor fd)))
-		   (sb-ext:finalize s (lambda () (sockint::close fd)))))
-	     (multiple-value-list (bits-of-sockaddr socket sockaddr))))))
+      (cond
+	((and (= fd -1) (= sockint::EAGAIN (sb-unix::get-errno)))
+	 nil)
+	((= fd -1) (socket-error "accept"))
+	(t (apply #'values
+		  (let ((s (make-instance (class-of socket)
+			      :type (socket-type socket)
+			      :protocol (socket-protocol socket)
+			      :descriptor fd)))
+		    (sb-ext:finalize s (lambda () (sockint::close fd))))
+		  (multiple-value-list (bits-of-sockaddr socket sockaddr))))))))
     
 (defgeneric socket-connect (socket &rest address)
   (:documentation "Perform the connect(2) call to connect SOCKET to a
@@ -182,11 +184,13 @@ small"))
 					flags
 					sockaddr
 					(sb-alien:cast sa-len (* integer)))))
-		(when (= len -1) (socket-error "recvfrom"))
-		(loop for i from 0 below len
-		      do (setf (elt buffer i) (sb-alien:deref copy-buffer i)))
-		(apply #'values buffer len (multiple-value-list
-					    (bits-of-sockaddr socket sockaddr)))))
+		(cond
+		  ((and (= len -1) (= sockint::EAGAIN (sb-unix::get-errno))) nil)
+		  ((= len -1) (socket-error "recvfrom"))
+		  (t (loop for i from 0 below len
+			   do (setf (elt buffer i) (sb-alien:deref copy-buffer i)))
+		     (apply #'values buffer len (multiple-value-list
+						 (bits-of-sockaddr socket sockaddr)))))))
 	  (sb-alien:free-alien copy-buffer))))))
 
 
@@ -278,19 +282,19 @@ SB-SYS:MAKE-FD-STREAM."))
   `(progn
      (define-condition ,name (socket-error)
        ((symbol :reader socket-error-symbol :initform (quote ,symbol))))
+     (export ',name)
      (push (cons ,symbol (quote ,name)) *conditions-for-errno*)))
 
 (defparameter *conditions-for-errno* nil)
 ;;; this needs the rest of the list adding to it, really.  They also
-;;; need
-;;; - conditions to be exported in the DEFPACKAGE form
-;;; - symbols to be added to constants.ccon
+;;; need symbols to be added to constants.ccon
 ;;; I haven't yet thought of a non-kludgey way of keeping all this in
 ;;; the same place
 (define-socket-condition sockint::EADDRINUSE address-in-use-error)
 (define-socket-condition sockint::EAGAIN interrupted-error)
 (define-socket-condition sockint::EBADF bad-file-descriptor-error)
 (define-socket-condition sockint::ECONNREFUSED connection-refused-error)
+(define-socket-condition sockint::ETIMEDOUT operation-timeout-error)
 (define-socket-condition sockint::EINTR interrupted-error)
 (define-socket-condition sockint::EINVAL invalid-argument-error)
 (define-socket-condition sockint::ENOBUFS no-buffers-error)
diff --git a/version.lisp-expr b/version.lisp-expr
index 68093b7..e10b284 100644
--- a/version.lisp-expr
+++ b/version.lisp-expr
@@ -17,4 +17,4 @@
 ;;; checkins which aren't released. (And occasionally for internal
 ;;; versions, especially for internal versions off the main CVS
 ;;; branch, it gets hairier, e.g. "0.pre7.14.flaky4.13".)
-"0.8.10.70"
+"0.8.10.71"
-- 
1.7.10.4