2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gpxe/xfer.h>
25 #include <gpxe/open.h>
26 #include <gpxe/process.h>
27 #include <gpxe/resolv.h>
35 /***************************************************************************
37 * Name resolution interfaces
39 ***************************************************************************
43 * Name resolution completed
45 * @v resolv Name resolution interface
46 * @v sa Completed socket address (if successful)
47 * @v rc Final status code
49 void resolv_done ( struct resolv_interface
*resolv
, struct sockaddr
*sa
,
51 struct resolv_interface
*dest
= resolv_get_dest ( resolv
);
53 dest
->op
->done ( dest
, sa
, rc
);
54 resolv_unplug ( resolv
);
59 * Ignore name resolution done() event
61 * @v resolv Name resolution interface
62 * @v sa Completed socket address (if successful)
63 * @v rc Final status code
65 void ignore_resolv_done ( struct resolv_interface
*resolv __unused
,
66 struct sockaddr
*sa __unused
, int rc __unused
) {
70 /** Null name resolution interface operations */
71 struct resolv_interface_operations null_resolv_ops
= {
72 .done
= ignore_resolv_done
,
75 /** Null name resolution interface */
76 struct resolv_interface null_resolv
= {
78 .dest
= &null_resolv
.intf
,
81 .op
= &null_resolv_ops
,
84 /***************************************************************************
86 * Numeric name resolver
88 ***************************************************************************
91 /** A numeric name resolver */
92 struct numeric_resolv
{
93 /** Reference counter */
95 /** Name resolution interface */
96 struct resolv_interface resolv
;
98 struct process process
;
99 /** Completed socket address */
101 /** Overall status code */
105 static void numeric_step ( struct process
*process
) {
106 struct numeric_resolv
*numeric
=
107 container_of ( process
, struct numeric_resolv
, process
);
109 resolv_done ( &numeric
->resolv
, &numeric
->sa
, numeric
->rc
);
110 process_del ( process
);
113 static int numeric_resolv ( struct resolv_interface
*resolv
,
114 const char *name
, struct sockaddr
*sa
) {
115 struct numeric_resolv
*numeric
;
116 struct sockaddr_in
*sin
;
118 /* Allocate and initialise structure */
119 numeric
= malloc ( sizeof ( *numeric
) );
122 memset ( numeric
, 0, sizeof ( *numeric
) );
123 resolv_init ( &numeric
->resolv
, &null_resolv_ops
, &numeric
->refcnt
);
124 process_init ( &numeric
->process
, numeric_step
, &numeric
->refcnt
);
125 memcpy ( &numeric
->sa
, sa
, sizeof ( numeric
->sa
) );
127 DBGC ( numeric
, "NUMERIC %p attempting to resolve \"%s\"\n",
130 /* Attempt to resolve name */
131 sin
= ( ( struct sockaddr_in
* ) &numeric
->sa
);
132 sin
->sin_family
= AF_INET
;
133 if ( inet_aton ( name
, &sin
->sin_addr
) == 0 )
134 numeric
->rc
= -EINVAL
;
136 /* Attach to parent interface, mortalise self, and return */
137 resolv_plug_plug ( &numeric
->resolv
, resolv
);
138 ref_put ( &numeric
->refcnt
);
142 struct resolver numeric_resolver
__resolver ( RESOLV_NUMERIC
) = {
144 .resolv
= numeric_resolv
,
147 /***************************************************************************
149 * Name resolution multiplexer
151 ***************************************************************************
154 /** Registered name resolvers */
155 static struct resolver resolvers
[0]
156 __table_start ( struct resolver
, resolvers
);
157 static struct resolver resolvers_end
[0]
158 __table_end ( struct resolver
, resolvers
);
160 /** A name resolution multiplexer */
162 /** Reference counter */
163 struct refcnt refcnt
;
164 /** Parent name resolution interface */
165 struct resolv_interface parent
;
167 /** Child name resolution interface */
168 struct resolv_interface child
;
169 /** Current child resolver */
170 struct resolver
*resolver
;
172 /** Socket address to complete */
174 /** Name to be resolved
176 * Must be at end of structure
182 * Try current child name resolver
184 * @v mux Name resolution multiplexer
185 * @ret rc Return status code
187 static int resolv_mux_try ( struct resolv_mux
*mux
) {
188 struct resolver
*resolver
= mux
->resolver
;
191 DBGC ( mux
, "RESOLV %p trying method %s\n", mux
, resolver
->name
);
193 if ( ( rc
= resolver
->resolv ( &mux
->child
, mux
->name
,
194 &mux
->sa
) ) != 0 ) {
195 DBGC ( mux
, "RESOLV %p could not use method %s: %s\n",
196 mux
, resolver
->name
, strerror ( rc
) );
204 * Handle done() event from child name resolver
206 * @v resolv Child name resolution interface
207 * @v sa Completed socket address (if successful)
208 * @v rc Final status code
210 static void resolv_mux_done ( struct resolv_interface
*resolv
,
211 struct sockaddr
*sa
, int rc
) {
212 struct resolv_mux
*mux
=
213 container_of ( resolv
, struct resolv_mux
, child
);
216 resolv_unplug ( &mux
->child
);
218 /* If this resolution succeeded, stop now */
220 DBGC ( mux
, "RESOLV %p succeeded using method %s\n",
221 mux
, mux
->resolver
->name
);
225 /* Attempt next child resolver, if possible */
227 if ( mux
->resolver
>= resolvers_end
) {
228 DBGC ( mux
, "RESOLV %p failed to resolve name\n", mux
);
231 if ( ( rc
= resolv_mux_try ( mux
) ) != 0 )
234 /* Next resolver is now running */
238 resolv_done ( &mux
->parent
, sa
, rc
);
241 /** Name resolution multiplexer operations */
242 static struct resolv_interface_operations resolv_mux_child_ops
= {
243 .done
= resolv_mux_done
,
247 * Start name resolution
249 * @v resolv Name resolution interface
250 * @v name Name to resolve
251 * @v sa Socket address to complete
252 * @ret rc Return status code
254 int resolv ( struct resolv_interface
*resolv
, const char *name
,
255 struct sockaddr
*sa
) {
256 struct resolv_mux
*mux
;
257 size_t name_len
= ( strlen ( name
) + 1 );
260 /* Allocate and initialise structure */
261 mux
= malloc ( sizeof ( *mux
) + name_len
);
264 memset ( mux
, 0, sizeof ( *mux
) );
265 resolv_init ( &mux
->parent
, &null_resolv_ops
, &mux
->refcnt
);
266 resolv_init ( &mux
->child
, &resolv_mux_child_ops
, &mux
->refcnt
);
267 mux
->resolver
= resolvers
;
268 memcpy ( &mux
->sa
, sa
, sizeof ( mux
->sa
) );
269 memcpy ( mux
->name
, name
, name_len
);
271 DBGC ( mux
, "RESOLV %p attempting to resolve \"%s\"\n", mux
, name
);
273 /* Start first resolver in chain. There will always be at
274 * least one resolver (the numeric resolver), so no need to
275 * check for the zero-resolvers-available case.
277 if ( ( rc
= resolv_mux_try ( mux
) ) != 0 )
280 /* Attach parent interface, mortalise self, and return */
281 resolv_plug_plug ( &mux
->parent
, resolv
);
282 ref_put ( &mux
->refcnt
);
286 ref_put ( &mux
->refcnt
);
290 /***************************************************************************
292 * Named socket opening
294 ***************************************************************************
297 /** A named socket */
298 struct named_socket
{
299 /** Reference counter */
300 struct refcnt refcnt
;
301 /** Data transfer interface */
302 struct xfer_interface xfer
;
303 /** Name resolution interface */
304 struct resolv_interface resolv
;
305 /** Communication semantics (e.g. SOCK_STREAM) */
307 /** Stored local socket address, if applicable */
308 struct sockaddr local
;
309 /** Stored local socket address exists */
314 * Handle seek() event
316 * @v xfer Data transfer interface
317 * @v offset Offset to new position
318 * @v whence Basis for new position
319 * @ret rc Return status code
321 static int resolv_xfer_seek ( struct xfer_interface
*xfer __unused
,
322 off_t offset __unused
, int whence __unused
) {
323 /* Never ready to accept data */
327 /** Named socket opener data transfer interface operations */
328 static struct xfer_interface_operations named_xfer_ops
= {
329 .close
= ignore_xfer_close
,
330 .vredirect
= ignore_xfer_vredirect
,
331 .request
= ignore_xfer_request
,
332 .seek
= resolv_xfer_seek
,
333 .alloc_iob
= default_xfer_alloc_iob
,
334 .deliver_iob
= xfer_deliver_as_raw
,
335 .deliver_raw
= ignore_xfer_deliver_raw
,
339 * Handle done() event
341 * @v resolv Name resolution interface
342 * @v sa Completed socket address (if successful)
343 * @v rc Final status code
345 static void named_resolv_done ( struct resolv_interface
*resolv
,
346 struct sockaddr
*sa
, int rc
) {
347 struct named_socket
*named
=
348 container_of ( resolv
, struct named_socket
, resolv
);
350 /* Unplug resolver and nullify data transfer interface */
351 resolv_unplug ( &named
->resolv
);
352 xfer_nullify ( &named
->xfer
);
354 /* Redirect if name resolution was successful */
356 rc
= xfer_redirect ( &named
->xfer
, LOCATION_SOCKET
,
357 named
->semantics
, sa
,
358 ( named
->have_local
?
359 &named
->local
: NULL
) );
362 /* Close data transfer interface if redirection failed */
364 xfer_close ( &named
->xfer
, rc
);
366 /* Unplug data transfer interface */
367 xfer_unplug ( &named
->xfer
);
370 /** Named socket opener name resolution interface operations */
371 static struct resolv_interface_operations named_resolv_ops
= {
372 .done
= named_resolv_done
,
378 * @v semantics Communication semantics (e.g. SOCK_STREAM)
379 * @v peer Peer socket address to complete
380 * @v name Name to resolve
381 * @v local Local socket address, or NULL
382 * @ret rc Return status code
384 int xfer_open_named_socket ( struct xfer_interface
*xfer
, int semantics
,
385 struct sockaddr
*peer
, const char *name
,
386 struct sockaddr
*local
) {
387 struct named_socket
*named
;
390 /* Allocate and initialise structure */
391 named
= malloc ( sizeof ( *named
) );
394 memset ( named
, 0, sizeof ( *named
) );
395 xfer_init ( &named
->xfer
, &named_xfer_ops
, &named
->refcnt
);
396 resolv_init ( &named
->resolv
, &named_resolv_ops
, &named
->refcnt
);
397 named
->semantics
= semantics
;
399 memcpy ( &named
->local
, local
, sizeof ( named
->local
) );
400 named
->have_local
= 1;
403 DBGC ( named
, "RESOLV %p opening named socket \"%s\"\n",
406 /* Start name resolution */
407 if ( ( rc
= resolv ( &named
->resolv
, name
, peer
) ) != 0 )
410 /* Attach parent interface, mortalise self, and return */
411 xfer_plug_plug ( &named
->xfer
, xfer
);
412 ref_put ( &named
->refcnt
);
416 ref_put ( &named
->refcnt
);