Skip to content

Commit

Permalink
Handle SSL errors properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Aug 11, 2024
1 parent b91e8e4 commit ceb9030
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
54 changes: 34 additions & 20 deletions src/ev/socket.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,51 @@
(cffi:with-pointer-to-vector-data (data-sap data)
(let* ((len (length data))
(completedp nil)
(ssl-handle (socket-ssl-handle socket))
(n
#+woo-no-ssl
(wsys:write fd data-sap len)
#-woo-no-ssl
(if (socket-ssl-handle socket)
(cl+ssl::ssl-write (socket-ssl-handle socket)
(if ssl-handle
(cl+ssl::ssl-write ssl-handle
data-sap
len)
(wsys:write fd data-sap len))))
(declare (type fixnum len)
(type fixnum n))
(case n
(-1
(let ((errno (wsys:errno)))
(return-from flush-buffer
(cond
((or (= errno wsys:EWOULDBLOCK)
(= errno wsys:EINTR))
nil)
((or (= errno wsys:ECONNABORTED)
(= errno wsys:ECONNREFUSED)
(= errno wsys:ECONNRESET)
(= errno wsys:EPIPE)
(= errno wsys:ENOTCONN))
(vom:error "Connection is already closed (Code: ~D)" errno)
(close-socket socket)
t)
(t
(vom:error "Unexpected error (Code: ~D)" errno)
(close-socket socket)
t)))))
(if ssl-handle
#+woo-no-ssl (close-socket socket)
#-woo-no-ssl
(let ((errno (cl+ssl::ssl-get-error ssl-handle n)))
(declare (type fixnum errno))
(cond
((or (= errno cl+ssl::+ssl-error-zero-return+)
(= errno cl+ssl::+ssl-error-ssl+))
(close-socket socket))
((= errno cl+ssl::+ssl-error-want-write+))
(t
(vom:error "Unexpected error (Code: ~D)" errno)
(close-socket socket))))
(let ((errno (wsys:errno)))
(return-from flush-buffer
(cond
((or (= errno wsys:EWOULDBLOCK)
(= errno wsys:EINTR))
nil)
((or (= errno wsys:ECONNABORTED)
(= errno wsys:ECONNREFUSED)
(= errno wsys:ECONNRESET)
(= errno wsys:EPIPE)
(= errno wsys:ENOTCONN))
(vom:error "Connection is already closed (Code: ~D)" errno)
(close-socket socket)
t)
(t
(vom:error "Unexpected error (Code: ~D)" errno)
(close-socket socket)
t))))))
(otherwise
(setf (socket-last-activity socket) (lev:ev-now *evloop*))
(if (= n len)
Expand Down
44 changes: 29 additions & 15 deletions src/ev/tcp.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,35 @@
(declare (type fixnum n))
(case n
(-1
(let ((errno (wsys:errno)))
(cond
((or (= errno wsys:EWOULDBLOCK)
(= errno wsys:EINTR)))
((or (= errno wsys:ECONNABORTED)
(= errno wsys:ECONNREFUSED)
(= errno wsys:ECONNRESET))
(vom:error "Connection is already closed (Code: ~D)" errno)
(close-socket socket))
((= errno wsys:EAGAIN)
;; Just to nothing
)
(t
(vom:error "Unexpected error (Code: ~D)" errno)
(close-socket socket))))
(if ssl-handle
#+woo-no-ssl (close-socket socket)
#-woo-no-ssl
(let ((errno (cl+ssl::ssl-get-error ssl-handle n)))
(declare (type fixnum errno))
(cond
((or (= errno cl+ssl::+ssl-error-zero-return+)
(= errno cl+ssl::+ssl-error-ssl+))
(close-socket socket))
((= errno cl+ssl::+ssl-error-want-read+))
(t
(vom:error "Unexpected error (Code: ~D)" errno)
(close-socket socket))))
(let ((errno (wsys:errno)))
(declare (type fixnum errno))
(cond
((or (= errno wsys:EWOULDBLOCK)
(= errno wsys:EINTR)))
((or (= errno wsys:ECONNABORTED)
(= errno wsys:ECONNREFUSED)
(= errno wsys:ECONNRESET))
(vom:error "Connection is already closed (Code: ~D)" errno)
(close-socket socket))
((= errno wsys:EAGAIN)
;; Just to nothing
)
(t
(vom:error "Unexpected error (Code: ~D)" errno)
(close-socket socket)))))
(return))
(0
;; EOF
Expand Down

0 comments on commit ceb9030

Please sign in to comment.