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 resolv_unplug ( resolv );
54 dest->op->done ( dest, sa, rc );
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 = zalloc ( sizeof ( *numeric ) );
122 resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
123 process_init ( &numeric->process, numeric_step, &numeric->refcnt );
124 memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
126 DBGC ( numeric, "NUMERIC %p attempting to resolve \"%s\"\n",
129 /* Attempt to resolve name */
130 sin = ( ( struct sockaddr_in * ) &numeric->sa );
131 sin->sin_family = AF_INET;
132 if ( inet_aton ( name, &sin->sin_addr ) == 0 )
133 numeric->rc = -EINVAL;
135 /* Attach to parent interface, mortalise self, and return */
136 resolv_plug_plug ( &numeric->resolv, resolv );
137 ref_put ( &numeric->refcnt );
141 struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
143 .resolv = numeric_resolv,
146 /***************************************************************************
148 * Name resolution multiplexer
150 ***************************************************************************
153 /** A name resolution multiplexer */
155 /** Reference counter */
156 struct refcnt refcnt;
157 /** Parent name resolution interface */
158 struct resolv_interface parent;
160 /** Child name resolution interface */
161 struct resolv_interface child;
162 /** Current child resolver */
163 struct resolver *resolver;
165 /** Socket address to complete */
167 /** Name to be resolved
169 * Must be at end of structure
175 * Try current child name resolver
177 * @v mux Name resolution multiplexer
178 * @ret rc Return status code
180 static int resolv_mux_try ( struct resolv_mux *mux ) {
181 struct resolver *resolver = mux->resolver;
184 DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
186 if ( ( rc = resolver->resolv ( &mux->child, mux->name,
187 &mux->sa ) ) != 0 ) {
188 DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
189 mux, resolver->name, strerror ( rc ) );
197 * Handle done() event from child name resolver
199 * @v resolv Child name resolution interface
200 * @v sa Completed socket address (if successful)
201 * @v rc Final status code
203 static void resolv_mux_done ( struct resolv_interface *resolv,
204 struct sockaddr *sa, int rc ) {
205 struct resolv_mux *mux =
206 container_of ( resolv, struct resolv_mux, child );
209 resolv_unplug ( &mux->child );
211 /* If this resolution succeeded, stop now */
213 DBGC ( mux, "RESOLV %p succeeded using method %s\n",
214 mux, mux->resolver->name );
218 /* Attempt next child resolver, if possible */
220 if ( mux->resolver >= table_end ( RESOLVERS ) ) {
221 DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
224 if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
227 /* Next resolver is now running */
231 resolv_done ( &mux->parent, sa, rc );
234 /** Name resolution multiplexer operations */
235 static struct resolv_interface_operations resolv_mux_child_ops = {
236 .done = resolv_mux_done,
240 * Start name resolution
242 * @v resolv Name resolution interface
243 * @v name Name to resolve
244 * @v sa Socket address to complete
245 * @ret rc Return status code
247 int resolv ( struct resolv_interface *resolv, const char *name,
248 struct sockaddr *sa ) {
249 struct resolv_mux *mux;
250 size_t name_len = ( strlen ( name ) + 1 );
253 /* Allocate and initialise structure */
254 mux = zalloc ( sizeof ( *mux ) + name_len );
257 resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
258 resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
259 mux->resolver = table_start ( RESOLVERS );
260 memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
261 memcpy ( mux->name, name, name_len );
263 DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
265 /* Start first resolver in chain. There will always be at
266 * least one resolver (the numeric resolver), so no need to
267 * check for the zero-resolvers-available case.
269 if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
272 /* Attach parent interface, mortalise self, and return */
273 resolv_plug_plug ( &mux->parent, resolv );
274 ref_put ( &mux->refcnt );
278 ref_put ( &mux->refcnt );
282 /***************************************************************************
284 * Named socket opening
286 ***************************************************************************
289 /** A named socket */
290 struct named_socket {
291 /** Reference counter */
292 struct refcnt refcnt;
293 /** Data transfer interface */
294 struct xfer_interface xfer;
295 /** Name resolution interface */
296 struct resolv_interface resolv;
297 /** Communication semantics (e.g. SOCK_STREAM) */
299 /** Stored local socket address, if applicable */
300 struct sockaddr local;
301 /** Stored local socket address exists */
305 /** Named socket opener data transfer interface operations */
306 static struct xfer_interface_operations named_xfer_ops = {
307 .close = ignore_xfer_close,
308 .vredirect = ignore_xfer_vredirect,
309 .window = no_xfer_window,
310 .alloc_iob = default_xfer_alloc_iob,
311 .deliver_iob = xfer_deliver_as_raw,
312 .deliver_raw = ignore_xfer_deliver_raw,
316 * Handle done() event
318 * @v resolv Name resolution interface
319 * @v sa Completed socket address (if successful)
320 * @v rc Final status code
322 static void named_resolv_done ( struct resolv_interface *resolv,
323 struct sockaddr *sa, int rc ) {
324 struct named_socket *named =
325 container_of ( resolv, struct named_socket, resolv );
327 /* Unplug resolver and nullify data transfer interface */
328 resolv_unplug ( &named->resolv );
329 xfer_nullify ( &named->xfer );
331 /* Redirect if name resolution was successful */
333 rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
334 named->semantics, sa,
335 ( named->have_local ?
336 &named->local : NULL ) );
339 /* Close data transfer interface if redirection failed */
341 xfer_close ( &named->xfer, rc );
343 /* Unplug data transfer interface */
344 xfer_unplug ( &named->xfer );
347 /** Named socket opener name resolution interface operations */
348 static struct resolv_interface_operations named_resolv_ops = {
349 .done = named_resolv_done,
355 * @v semantics Communication semantics (e.g. SOCK_STREAM)
356 * @v peer Peer socket address to complete
357 * @v name Name to resolve
358 * @v local Local socket address, or NULL
359 * @ret rc Return status code
361 int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
362 struct sockaddr *peer, const char *name,
363 struct sockaddr *local ) {
364 struct named_socket *named;
367 /* Allocate and initialise structure */
368 named = zalloc ( sizeof ( *named ) );
371 xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
372 resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
373 named->semantics = semantics;
375 memcpy ( &named->local, local, sizeof ( named->local ) );
376 named->have_local = 1;
379 DBGC ( named, "RESOLV %p opening named socket \"%s\"\n",
382 /* Start name resolution */
383 if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
386 /* Attach parent interface, mortalise self, and return */
387 xfer_plug_plug ( &named->xfer, xfer );
388 ref_put ( &named->refcnt );
392 ref_put ( &named->refcnt );