Update docs.
[iolib/alendvai.git] / net.sockets / conditions.lisp
blobcc278732e505538d84ae38cdccd1b7ecaa5cd8b3
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; conditions.lisp --- Socket conditions.
4 ;;;
5 ;;; Copyright (C) 2006-2008, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :net.sockets)
26 (defgeneric error-code (err)
27 (:method ((err system-error))
28 (osicat-sys:system-error-code err)))
30 (defgeneric error-identifier (err)
31 (:method ((err system-error))
32 (osicat-sys:system-error-identifier err)))
34 (defgeneric error-message (err)
35 (:method ((err system-error))
36 (osicat-sys:system-error-message err)))
38 ;;;; Socket Errors
40 (define-condition socket-error (nix:posix-error) ())
42 (defmethod print-object ((socket-error socket-error) stream)
43 (print-unreadable-object (socket-error stream :type t :identity nil)
44 (let ((code (osicat-sys:system-error-code socket-error)))
45 (format stream "~S ~S ~S"
46 (or code "[Unknown code]")
47 (error-identifier socket-error)
48 (if code (nix:strerror code)
49 "[Can't get error string.]")))))
51 (defvar *socket-error-map* (make-hash-table :test 'eq))
53 (defmacro define-socket-error (name identifier &optional documentation)
54 `(progn
55 (setf (gethash ,identifier *socket-error-map*) ',name)
56 (define-condition ,name (,(nix::get-posix-error-condition identifier)
57 socket-error) ()
58 (:default-initargs :code ,(foreign-enum-value 'socket-error-values
59 identifier)
60 :identifier ,identifier)
61 (:documentation ,(or documentation (nix:strerror identifier))))))
63 (defun lookup-socket-error (keyword)
64 (gethash keyword *socket-error-map*))
66 (define-condition unknown-socket-error (socket-error) ()
67 (:documentation "Error signalled upon finding an unknown socket error."))
69 (define-socket-error socket-invalid-argument :einval)
70 (define-socket-error socket-address-in-use-error :eaddrinuse)
71 (define-socket-error socket-address-not-available-error :eaddrnotavail)
72 (define-socket-error socket-network-down-error :enetdown)
73 (define-socket-error socket-network-reset-error :enetreset)
74 (define-socket-error socket-network-unreachable-error :enetunreach)
75 (define-socket-error socket-no-network-error :enonet)
76 (define-socket-error socket-connection-aborted-error :econnaborted)
77 (define-socket-error socket-connection-reset-error :econnreset)
78 (define-socket-error socket-connection-refused-error :econnrefused)
79 (define-socket-error socket-connection-timeout-error :etimedout)
80 (define-socket-error socket-connection-in-progress-error :einprogress)
81 (define-socket-error socket-endpoint-shutdown-error :eshutdown)
82 (define-socket-error socket-no-buffer-space-error :enobufs)
83 (define-socket-error socket-host-down-error :ehostdown)
84 (define-socket-error socket-host-unreachable-error :ehostunreach)
85 (define-socket-error socket-already-connected-error :eisconn)
86 (define-socket-error socket-not-connected-error :enotconn)
87 (define-socket-error socket-option-not-supported-error :enoprotoopt)
88 (define-socket-error socket-operation-not-supported-error :eopnotsupp)
90 (defun %socket-error (id code)
91 (when-let ((err (lookup-socket-error id)))
92 (error err :identifier id :code code)))
94 ;;; Used in the ERRNO-WRAPPER foreign type.
95 (defun signal-socket-error (&optional return-value)
96 (declare (ignore return-value))
97 (let* ((errno (nix:get-errno))
98 (kw (foreign-enum-keyword 'socket-error-values
99 errno :errorp nil)))
100 (or (%socket-error kw errno)
101 (error (nix::make-posix-error errno)))))