Rename WITH-SOCKET to WITH-OPEN-SOCKET.
[iolib.git] / sockets / make-socket.lisp
blob809279f46b59a6c706101ff6ec1c641d2658b894
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 ;;; FIXME: protocol is sort of misinterpreted.
27 ;;;
28 ;;; CREATE-SOCKET is a a bit of a confusing name as it is too similar
29 ;;; to MAKE-SOCKET. I thought about different names and considered
30 ;;; the option of moving this "factory" code could be moved into
31 ;;; MAKE-INSTANCE methods. However, AFAICT, MAKE-SOCKET can achieve
32 ;;; the exact same effect as CREATE-SOCKET when some of the parameters
33 ;;; are ommitted so I have opted to remove CREATE-SOCKET from the
34 ;;; export list. --luis
35 (defun create-socket (&key (family :internet) (type :stream) (connect :active)
36 (protocol :default) (ipv6 *ipv6*)
37 (external-format :default))
38 (when (or (null family) (eq family :internet))
39 (setf family (if ipv6 :ipv6 :ipv4)))
40 (let ((class (select-socket-type family type connect protocol)))
41 (make-instance class
42 :family family
43 :external-format external-format)))
45 (defmacro %close-on-error ((obj) &body body)
46 (with-unique-names (flag)
47 `(let ((,flag t))
48 (unwind-protect (multiple-value-prog1 (progn ,@body) (setf ,flag nil))
49 (when (and ,obj ,flag) (close ,obj :abort t))))))
51 (defun convert-or-lookup-inet-address (address &optional (ipv6 *ipv6*))
52 "If ADDRESS is an inet-address designator, it is converted, if
53 necessary, to an INET-ADDRESS object and returned. Otherwise it
54 is assumed to be a host name which is then looked up in order to
55 return its primary address as the first return value and the
56 remaining address list as the second return value."
57 (or (ignore-errors (ensure-address address :internet))
58 (let ((addresses (lookup-host address :ipv6 ipv6)))
59 (values (car addresses) (cdr addresses)))))
61 (declaim (inline %make-internet-stream-socket))
62 (defun %make-internet-stream-socket (args connect ef)
63 (let (socket address)
64 (destructuring-bind (&key local-host (local-port 0) remote-host (remote-port 0)
65 backlog reuse-address keepalive nodelay family)
66 args
67 (ecase connect
68 (:active
69 (%close-on-error (socket)
70 (setf socket (create-socket :family family :type :stream
71 :connect :active :external-format ef))
72 (when keepalive (set-socket-option socket :keep-alive :value t))
73 (when nodelay (set-socket-option socket :tcp-nodelay :value t))
74 (when local-host
75 (setf address (convert-or-lookup-inet-address local-host))
76 (bind-address socket address :port local-port
77 :reuse-address reuse-address))
78 (when remote-host
79 (setf address (convert-or-lookup-inet-address remote-host))
80 (connect socket address :port remote-port))))
81 (:passive
82 (%close-on-error (socket)
83 (setf socket (create-socket :family family :type :stream
84 :connect :passive :external-format ef))
85 (when local-host
86 (setf address (convert-or-lookup-inet-address local-host))
87 (bind-address socket address :port local-port
88 :reuse-address reuse-address)
89 (socket-listen socket :backlog backlog))))))
90 (values socket)))
92 (declaim (inline %make-local-stream-socket))
93 (defun %make-local-stream-socket (args connect ef)
94 (let (socket)
95 (destructuring-bind (&key local-filename remote-filename backlog family)
96 args
97 (ecase connect
98 (:active
99 (assert remote-filename)
100 (%close-on-error (socket)
101 (setf socket (create-socket :family family :type :stream
102 :connect :active :external-format ef))
103 (when local-filename
104 (bind-address socket (make-address local-filename)))
105 (connect socket (make-address remote-filename))))
106 (:passive
107 (assert local-filename)
108 (%close-on-error (socket)
109 (setf socket (create-socket :family family :type :stream
110 :connect :passive
111 :external-format ef))
112 (bind-address socket (make-address local-filename))
113 (socket-listen socket :backlog backlog)))))
114 (values socket)))
116 (declaim (inline %make-internet-datagram-socket))
117 (defun %make-internet-datagram-socket (args ef)
118 (let (socket address)
119 (destructuring-bind (&key local-host (local-port 0)
120 remote-host (remote-port 0)
121 reuse-address broadcast family)
122 args
123 (%close-on-error (socket)
124 (setf socket (create-socket :family family :type :datagram
125 :connect :active :external-format ef))
126 (when broadcast (set-socket-option socket :broadcast :value t))
127 (when local-host
128 (setf address (convert-or-lookup-inet-address local-host))
129 (bind-address socket address :port local-port
130 :reuse-address reuse-address))
131 (when remote-host
132 (setf address (convert-or-lookup-inet-address remote-host))
133 (connect socket address :port remote-port))))
134 (values socket)))
136 (declaim (inline %make-local-datagram-socket))
137 (defun %make-local-datagram-socket (args ef)
138 (let (socket address)
139 (destructuring-bind (&key local-filename remote-filename family) args
140 (%close-on-error (socket)
141 (setf socket (create-socket :family family :type :datagram
142 :connect :active :external-format ef))
143 (when local-filename
144 (bind-address socket (make-address address)))
145 (when remote-filename
146 (connect socket (make-address address)))))
147 (values socket)))
149 ;;; Changed ADDRESS-FAMILY to FAMILY and accept :IPV4 and :IPV6 as
150 ;;; arguments so we can create an IPv4 socket with
151 ;;; (make-socket :family :ipv4)
152 ;;; instead of
153 ;;; (make-socket #|:family :internet|# :ipv6 nil)
154 ;;; This is not compatible with Allegro's MAKE-SOCKET behaviour.
155 ;;; Is the renaming and the acceptance of extra values a problem?
156 ;;; (We need to be careful with *IPV6* for starters.)
157 (defun make-socket (&rest args &key (family :internet) (type :stream)
158 (connect :active) (ipv6 *ipv6*) format eol
159 (external-format :default) scope-id &allow-other-keys)
160 "Creates a socket instance of the appropriate subclass of SOCKET."
161 (declare (ignore format eol scope-id))
162 (check-type family (member :internet :local :ipv4 :ipv6))
163 (check-type type (member :stream :datagram))
164 (check-type connect (member :active :passive))
165 (remf args :ipv6)
166 (remf args :external-format)
167 (remf args :type)
168 (remf args :connect)
169 (let ((*ipv6* (if (eq family :ipv4) nil ipv6))
170 (address-family (if (or (eq family :ipv4) (eq family :ipv6))
171 :internet
172 family)))
173 (cond
174 ((and (eq address-family :internet) (eq type :stream))
175 (%make-internet-stream-socket args connect external-format))
176 ((and (eq address-family :local) (eq type :stream))
177 (%make-local-stream-socket args connect external-format))
178 ((and (eq address-family :internet) (eq type :datagram))
179 (%make-internet-datagram-socket args external-format))
180 ((and (eq address-family :local) (eq type :datagram))
181 (%make-local-datagram-socket args external-format)))))
183 (defmacro with-open-socket ((var &rest args) &body body)
184 "VAR is bound to a socket created by passing ARGS to
185 MAKE-SOCKET and BODY is executed as implicit PROGN. The socket
186 is automatically closed upon exit."
187 `(with-open-stream (,var (make-socket ,@args)) ,@body))