Fix definition of resolver errors.
[iolib.git] / sockets / make-socket.lisp
blob5abb43c7f3e8b5550d775ff20060611793e2adb3
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; make-socket.lisp --- Socket creation.
4 ;;;
5 ;;; Copyright (C) 2006-2007, 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 (defun create-socket (&key (family :internet) (type :stream) (connect :active)
27 (protocol :default) (ipv6 *ipv6*)
28 (external-format :default))
29 (when (or (null family) (eq family :internet))
30 (setf family (if ipv6 :ipv6 :ipv4)))
31 (let ((class (select-socket-type family type connect protocol)))
32 (make-instance class
33 :family family
34 :external-format external-format)))
36 (defmacro %with-close-on-error ((var value) &body body)
37 "Bind VAR to VALUE, execute BODY as implicit PROGN and return VAR.
38 On error call CLOSE with :ABORT T on VAR."
39 (with-unique-names (errorp)
40 `(let ((,var ,value) (,errorp t))
41 (unwind-protect (prog1 (locally ,@body ,var) (setf ,errorp nil))
42 (when (and ,var ,errorp) (close ,var :abort t))))))
44 (defun convert-or-lookup-inet-address (address &optional (ipv6 *ipv6*))
45 "If ADDRESS is an inet-address designator, it is converted, if
46 necessary, to an INET-ADDRESS object and returned. Otherwise it
47 is assumed to be a host name which is then looked up in order to
48 return its primary address as the first return value and the
49 remaining address list as the second return value."
50 (or (ignore-parse-errors (ensure-address address :internet))
51 (let ((addresses (lookup-host address :ipv6 ipv6)))
52 (values (car addresses) (cdr addresses)))))
54 (defun %make-internet-stream-socket (args connect ef)
55 (destructuring-bind (&key family local-host (local-port 0) remote-host (remote-port 0)
56 (backlog *default-backlog-size*) reuse-address keepalive nodelay)
57 args
58 (ecase connect
59 (:active
60 (%with-close-on-error (socket (create-socket :family family :type :stream
61 :connect :active :external-format ef))
62 (when keepalive (set-socket-option socket :keep-alive :value t))
63 (when nodelay (set-socket-option socket :tcp-nodelay :value t))
64 (when local-host
65 (bind-address socket (convert-or-lookup-inet-address local-host)
66 :port local-port
67 :reuse-address reuse-address))
68 (when remote-host
69 (connect socket (convert-or-lookup-inet-address remote-host)
70 :port remote-port))))
71 (:passive
72 (%with-close-on-error (socket (create-socket :family family :type :stream
73 :connect :passive :external-format ef))
74 (when local-host
75 (bind-address socket (convert-or-lookup-inet-address local-host)
76 :port local-port
77 :reuse-address reuse-address)
78 (socket-listen socket :backlog backlog)))))))
80 (defun %make-local-stream-socket (args connect ef)
81 (destructuring-bind (&key family local-filename remote-filename (backlog *default-backlog-size*))
82 args
83 (ecase connect
84 (:active
85 (assert remote-filename)
86 (%with-close-on-error (socket (create-socket :family family :type :stream
87 :connect :active :external-format ef))
88 (when local-filename
89 (bind-address socket (make-address local-filename)))
90 (connect socket (make-address remote-filename))))
91 (:passive
92 (assert local-filename)
93 (%with-close-on-error (socket (create-socket :family family :type :stream
94 :connect :passive :external-format ef))
95 (bind-address socket (make-address local-filename))
96 (socket-listen socket :backlog backlog))))))
98 (defun %make-internet-datagram-socket (args ef)
99 (destructuring-bind (&key family local-host (local-port 0) remote-host
100 (remote-port 0) reuse-address broadcast)
101 args
102 (%with-close-on-error (socket (create-socket :family family :type :datagram
103 :connect :active :external-format ef))
104 (when broadcast (set-socket-option socket :broadcast :value t))
105 (when local-host
106 (bind-address socket (convert-or-lookup-inet-address local-host)
107 :port local-port
108 :reuse-address reuse-address))
109 (when remote-host
110 (connect socket (convert-or-lookup-inet-address remote-host)
111 :port remote-port)))))
113 (defun %make-local-datagram-socket (args ef)
114 (destructuring-bind (&key family local-filename remote-filename) args
115 (%with-close-on-error (socket (create-socket :family family :type :datagram
116 :connect :active :external-format ef))
117 (when local-filename
118 (bind-address socket (ensure-address local-filename :local)))
119 (when remote-filename
120 (connect socket (ensure-address remote-filename :local))))))
122 ;;; Changed ADDRESS-FAMILY to FAMILY and accept :IPV4 and :IPV6 as
123 ;;; arguments so we can create an IPv4 socket with
124 ;;; (make-socket :family :ipv4)
125 ;;; instead of
126 ;;; (make-socket #|:family :internet|# :ipv6 nil)
127 ;;; This is not compatible with Allegro's MAKE-SOCKET behaviour.
128 ;;; Is the renaming and the acceptance of extra values a problem?
129 ;;; (We need to be careful with *IPV6* for starters.)
130 (defun make-socket (&rest args &key (family :internet) (type :stream)
131 (connect :active) (ipv6 *ipv6*) format eol
132 (external-format :default) scope-id &allow-other-keys)
133 "Creates a socket instance of the appropriate subclass of SOCKET."
134 (declare (ignore format eol scope-id))
135 (check-type family (member :internet :local :ipv4 :ipv6))
136 (check-type type (member :stream :datagram))
137 (check-type connect (member :active :passive))
138 (remf args :ipv6)
139 (remf args :external-format)
140 (remf args :type)
141 (remf args :connect)
142 (let ((*ipv6* (if (eq family :ipv4) nil ipv6))
143 (address-family (if (or (eq family :ipv4) (eq family :ipv6))
144 :internet
145 family)))
146 (cond
147 ((and (eq address-family :internet) (eq type :stream))
148 (%make-internet-stream-socket args connect external-format))
149 ((and (eq address-family :local) (eq type :stream))
150 (%make-local-stream-socket args connect external-format))
151 ((and (eq address-family :internet) (eq type :datagram))
152 (%make-internet-datagram-socket args external-format))
153 ((and (eq address-family :local) (eq type :datagram))
154 (%make-local-datagram-socket args external-format)))))
156 (defmacro with-open-socket ((var &rest args) &body body)
157 "VAR is bound to a socket created by passing ARGS to
158 MAKE-SOCKET and BODY is executed as implicit PROGN. The socket
159 is automatically closed upon exit."
160 `(with-open-stream (,var (make-socket ,@args)) ,@body))
162 (defmacro with-accept-connection ((var passive-socket &rest args) &body body)
163 "VAR is bound to a socket created by passing PASSIVE-SOCKET and ARGS to
164 ACCEPT-CONNECTION and BODY is executed as implicit PROGN. The socket
165 is automatically closed upon exit."
166 `(with-open-stream (,var (accept-connection ,passive-socket ,@args)) ,@body))