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/resolv.h>
32 static struct async_operations resolv_async_operations;
34 /** Registered name resolvers */
35 static struct resolver resolvers[0]
36 __table_start ( struct resolver, resolvers );
37 static struct resolver resolvers_end[0]
38 __table_end ( struct resolver, resolvers );
41 * Start name resolution
43 * @v name Host name to resolve
44 * @v sa Socket address to fill in
45 * @v parent Parent asynchronous operation
46 * @ret rc Return status code
48 int resolv ( const char *name, struct sockaddr *sa, struct async *parent ) {
49 struct resolution *resolution;
50 struct resolver *resolver;
51 struct sockaddr_in *sin = ( struct sockaddr_in * ) sa;
55 /* Allocate and populate resolution structure */
56 resolution = malloc ( sizeof ( *resolution ) );
59 async_init ( &resolution->async, &resolv_async_operations, parent );
61 /* Check for a dotted quad IP address first */
62 if ( inet_aton ( name, &in ) != 0 ) {
63 DBGC ( resolution, "RESOLV %p saw valid IP address %s\n",
65 sin->sin_family = AF_INET;
67 async_done ( &resolution->async, 0 );
71 /* Start up all resolvers */
72 for ( resolver = resolvers ; resolver < resolvers_end ; resolver++ ) {
73 if ( ( rc = resolver->resolv ( name, sa,
74 &resolution->async ) ) != 0 ) {
75 DBGC ( resolution, "RESOLV %p could not start %s: "
76 "%s\n", resolution, resolver->name,
78 /* Continue to try other resolvers */
81 (resolution->pending)++;
83 if ( ! resolution->pending )
89 async_uninit ( &resolution->async );
95 * Handle child name resolution completion
97 * @v async Name resolution asynchronous operation
100 static void resolv_sigchld ( struct async *async,
101 enum signal signal __unused ) {
102 struct resolution *resolution =
103 container_of ( async, struct resolution, async );
106 /* If this child succeeded, kill all the others and return */
107 async_wait ( async, &rc, 1 );
109 async_signal_children ( async, SIGKILL );
110 async_done ( async, 0 );
114 /* If we have no children left, return failure */
115 if ( --(resolution->pending) == 0 )
116 async_done ( async, -ENXIO );
120 * Free name resolution structure
122 * @v async Asynchronous operation
124 static void resolv_reap ( struct async *async ) {
125 free ( container_of ( async, struct resolution, async ) );
128 /** Name resolution asynchronous operations */
129 static struct async_operations resolv_async_operations = {
132 [SIGCHLD] = resolv_sigchld,