[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / resolv.c
blob02fe3123e303fc5fee0a6683ad0db4340d8396dc
1 /*
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.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <gpxe/in.h>
26 #include <gpxe/xfer.h>
27 #include <gpxe/open.h>
28 #include <gpxe/process.h>
29 #include <gpxe/resolv.h>
31 /** @file
33 * Name resolution
37 /***************************************************************************
39 * Name resolution interfaces
41 ***************************************************************************
44 /**
45 * Name resolution completed
47 * @v resolv Name resolution interface
48 * @v sa Completed socket address (if successful)
49 * @v rc Final status code
51 void resolv_done ( struct resolv_interface *resolv, struct sockaddr *sa,
52 int rc ) {
53 struct resolv_interface *dest = resolv_get_dest ( resolv );
55 resolv_unplug ( resolv );
56 dest->op->done ( dest, sa, rc );
57 resolv_put ( dest );
60 /**
61 * Ignore name resolution done() event
63 * @v resolv Name resolution interface
64 * @v sa Completed socket address (if successful)
65 * @v rc Final status code
67 void ignore_resolv_done ( struct resolv_interface *resolv __unused,
68 struct sockaddr *sa __unused, int rc __unused ) {
69 /* Do nothing */
72 /** Null name resolution interface operations */
73 struct resolv_interface_operations null_resolv_ops = {
74 .done = ignore_resolv_done,
77 /** Null name resolution interface */
78 struct resolv_interface null_resolv = {
79 .intf = {
80 .dest = &null_resolv.intf,
81 .refcnt = NULL,
83 .op = &null_resolv_ops,
86 /***************************************************************************
88 * Numeric name resolver
90 ***************************************************************************
93 /** A numeric name resolver */
94 struct numeric_resolv {
95 /** Reference counter */
96 struct refcnt refcnt;
97 /** Name resolution interface */
98 struct resolv_interface resolv;
99 /** Process */
100 struct process process;
101 /** Completed socket address */
102 struct sockaddr sa;
103 /** Overall status code */
104 int rc;
107 static void numeric_step ( struct process *process ) {
108 struct numeric_resolv *numeric =
109 container_of ( process, struct numeric_resolv, process );
111 resolv_done ( &numeric->resolv, &numeric->sa, numeric->rc );
112 process_del ( process );
115 static int numeric_resolv ( struct resolv_interface *resolv,
116 const char *name, struct sockaddr *sa ) {
117 struct numeric_resolv *numeric;
118 struct sockaddr_in *sin;
119 struct sockaddr_in6 *sin6;
121 /* Allocate and initialise structure */
122 numeric = zalloc ( sizeof ( *numeric ) );
123 if ( ! numeric )
124 return -ENOMEM;
125 ref_init ( &numeric->refcnt, NULL );
126 resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
127 process_init ( &numeric->process, numeric_step, &numeric->refcnt );
128 memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
130 DBGC ( numeric, "NUMERIC %p attempting to resolve \"%s\"\n",
131 numeric, name );
133 /* Attempt to resolve name */
134 sin = ( ( struct sockaddr_in * ) &numeric->sa );
135 sin->sin_family = AF_INET;
136 if ( inet_aton ( name, &sin->sin_addr ) == 0 ) {
137 sin6 = ( ( struct sockaddr_in6 * ) &numeric->sa );
138 sin6->sin_family = AF_INET6;
139 if ( inet6_aton ( name, &sin6->sin6_addr ) == 0 ) {
140 numeric->rc = -EINVAL;
144 /* Attach to parent interface, mortalise self, and return */
145 resolv_plug_plug ( &numeric->resolv, resolv );
146 ref_put ( &numeric->refcnt );
147 return 0;
150 struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
151 .name = "NUMERIC",
152 .resolv = numeric_resolv,
155 /***************************************************************************
157 * Name resolution multiplexer
159 ***************************************************************************
162 /** A name resolution multiplexer */
163 struct resolv_mux {
164 /** Reference counter */
165 struct refcnt refcnt;
166 /** Parent name resolution interface */
167 struct resolv_interface parent;
169 /** Child name resolution interface */
170 struct resolv_interface child;
171 /** Current child resolver */
172 struct resolver *resolver;
174 /** Socket address to complete */
175 struct sockaddr sa;
176 /** Name to be resolved
178 * Must be at end of structure
180 char name[0];
184 * Try current child name resolver
186 * @v mux Name resolution multiplexer
187 * @ret rc Return status code
189 static int resolv_mux_try ( struct resolv_mux *mux ) {
190 struct resolver *resolver = mux->resolver;
191 int rc;
193 DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
195 if ( ( rc = resolver->resolv ( &mux->child, mux->name,
196 &mux->sa ) ) != 0 ) {
197 DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
198 mux, resolver->name, strerror ( rc ) );
199 return rc;
202 return 0;
206 * Handle done() event from child name resolver
208 * @v resolv Child name resolution interface
209 * @v sa Completed socket address (if successful)
210 * @v rc Final status code
212 static void resolv_mux_done ( struct resolv_interface *resolv,
213 struct sockaddr *sa, int rc ) {
214 struct resolv_mux *mux =
215 container_of ( resolv, struct resolv_mux, child );
217 /* Unplug child */
218 resolv_unplug ( &mux->child );
220 /* If this resolution succeeded, stop now */
221 if ( rc == 0 ) {
222 DBGC ( mux, "RESOLV %p succeeded using method %s\n",
223 mux, mux->resolver->name );
224 goto finished;
227 /* Attempt next child resolver, if possible */
228 mux->resolver++;
229 if ( mux->resolver >= table_end ( RESOLVERS ) ) {
230 DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
231 goto finished;
233 if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
234 goto finished;
236 /* Next resolver is now running */
237 return;
239 finished:
240 resolv_done ( &mux->parent, sa, rc );
243 /** Name resolution multiplexer operations */
244 static struct resolv_interface_operations resolv_mux_child_ops = {
245 .done = resolv_mux_done,
249 * Start name resolution
251 * @v resolv Name resolution interface
252 * @v name Name to resolve
253 * @v sa Socket address to complete
254 * @ret rc Return status code
256 int resolv ( struct resolv_interface *resolv, const char *name,
257 struct sockaddr *sa ) {
258 struct resolv_mux *mux;
259 size_t name_len = ( strlen ( name ) + 1 );
260 int rc;
262 /* Allocate and initialise structure */
263 mux = zalloc ( sizeof ( *mux ) + name_len );
264 if ( ! mux )
265 return -ENOMEM;
266 ref_init ( &mux->refcnt, NULL );
267 resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
268 resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
269 mux->resolver = table_start ( RESOLVERS );
270 memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
271 memcpy ( mux->name, name, name_len );
273 DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
275 /* Start first resolver in chain. There will always be at
276 * least one resolver (the numeric resolver), so no need to
277 * check for the zero-resolvers-available case.
279 if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
280 goto err;
282 /* Attach parent interface, mortalise self, and return */
283 resolv_plug_plug ( &mux->parent, resolv );
284 ref_put ( &mux->refcnt );
285 return 0;
287 err:
288 ref_put ( &mux->refcnt );
289 return rc;
292 /***************************************************************************
294 * Named socket opening
296 ***************************************************************************
299 /** A named socket */
300 struct named_socket {
301 /** Reference counter */
302 struct refcnt refcnt;
303 /** Data transfer interface */
304 struct xfer_interface xfer;
305 /** Name resolution interface */
306 struct resolv_interface resolv;
307 /** Communication semantics (e.g. SOCK_STREAM) */
308 int semantics;
309 /** Stored local socket address, if applicable */
310 struct sockaddr local;
311 /** Stored local socket address exists */
312 int have_local;
316 * Finish using named socket
318 * @v named Named socket
319 * @v rc Reason for finish
321 static void named_done ( struct named_socket *named, int rc ) {
323 /* Close all interfaces */
324 resolv_nullify ( &named->resolv );
325 xfer_nullify ( &named->xfer );
326 xfer_close ( &named->xfer, rc );
330 * Handle close() event
332 * @v xfer Data transfer interface
333 * @v rc Reason for close
335 static void named_xfer_close ( struct xfer_interface *xfer, int rc ) {
336 struct named_socket *named =
337 container_of ( xfer, struct named_socket, xfer );
339 named_done ( named, rc );
342 /** Named socket opener data transfer interface operations */
343 static struct xfer_interface_operations named_xfer_ops = {
344 .close = named_xfer_close,
345 .vredirect = ignore_xfer_vredirect,
346 .window = no_xfer_window,
347 .alloc_iob = default_xfer_alloc_iob,
348 .deliver_iob = xfer_deliver_as_raw,
349 .deliver_raw = ignore_xfer_deliver_raw,
353 * Handle done() event
355 * @v resolv Name resolution interface
356 * @v sa Completed socket address (if successful)
357 * @v rc Final status code
359 static void named_resolv_done ( struct resolv_interface *resolv,
360 struct sockaddr *sa, int rc ) {
361 struct named_socket *named =
362 container_of ( resolv, struct named_socket, resolv );
364 /* Redirect if name resolution was successful */
365 if ( rc == 0 ) {
366 rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
367 named->semantics, sa,
368 ( named->have_local ?
369 &named->local : NULL ) );
372 /* Terminate resolution */
373 named_done ( named, rc );
376 /** Named socket opener name resolution interface operations */
377 static struct resolv_interface_operations named_resolv_ops = {
378 .done = named_resolv_done,
382 * Open named socket
384 * @v semantics Communication semantics (e.g. SOCK_STREAM)
385 * @v peer Peer socket address to complete
386 * @v name Name to resolve
387 * @v local Local socket address, or NULL
388 * @ret rc Return status code
390 int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
391 struct sockaddr *peer, const char *name,
392 struct sockaddr *local ) {
393 struct named_socket *named;
394 int rc;
396 /* Allocate and initialise structure */
397 named = zalloc ( sizeof ( *named ) );
398 if ( ! named )
399 return -ENOMEM;
400 ref_init ( &named->refcnt, NULL );
401 xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
402 resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
403 named->semantics = semantics;
404 if ( local ) {
405 memcpy ( &named->local, local, sizeof ( named->local ) );
406 named->have_local = 1;
409 DBGC ( named, "RESOLV %p opening named socket \"%s\"\n",
410 named, name );
412 /* Start name resolution */
413 if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
414 goto err;
416 /* Attach parent interface, mortalise self, and return */
417 xfer_plug_plug ( &named->xfer, xfer );
418 ref_put ( &named->refcnt );
419 return 0;
421 err:
422 ref_put ( &named->refcnt );
423 return rc;