david/ipxe
Archived
1
0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/core/resolv.c

141 lines
3.8 KiB
C
Raw Normal View History

2007-01-18 23:38:13 +01:00
/*
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <gpxe/in.h>
#include <gpxe/resolv.h>
2005-04-29 15:26:31 +02:00
2007-01-18 23:38:13 +01:00
/** @file
*
* Name resolution
*
*/
static struct async_operations resolv_async_operations;
/** Registered name resolvers */
static struct resolver resolvers[0]
2007-01-18 23:38:13 +01:00
__table_start ( struct resolver, resolvers );
static struct resolver resolvers_end[0]
2007-01-18 23:38:13 +01:00
__table_end ( struct resolver, resolvers );
2005-04-29 15:26:31 +02:00
2007-01-18 23:38:13 +01:00
/**
* Start name resolution
2005-04-29 15:26:31 +02:00
*
2007-01-18 23:38:13 +01:00
* @v name Host name to resolve
* @v sa Socket address to fill in
* @v parent Parent asynchronous operation
* @ret rc Return status code
2005-04-29 15:26:31 +02:00
*/
2007-01-18 23:38:13 +01:00
int resolv ( const char *name, struct sockaddr *sa, struct async *parent ) {
struct resolution *resolution;
2005-04-29 15:26:31 +02:00
struct resolver *resolver;
2007-01-18 23:38:13 +01:00
struct sockaddr_in *sin = ( struct sockaddr_in * ) sa;
struct in_addr in;
int rc = -ENXIO;
/* Allocate and populate resolution structure */
resolution = malloc ( sizeof ( *resolution ) );
if ( ! resolution )
return -ENOMEM;
memset ( resolution, 0, sizeof ( *resolution ) );
2007-01-18 23:38:13 +01:00
async_init ( &resolution->async, &resolv_async_operations, parent );
2005-04-29 15:26:31 +02:00
/* Check for a dotted quad IP address first */
2007-01-18 23:38:13 +01:00
if ( inet_aton ( name, &in ) != 0 ) {
DBGC ( resolution, "RESOLV %p saw valid IP address %s\n",
resolution, name );
sin->sin_family = AF_INET;
sin->sin_addr = in;
async_done ( &resolution->async, 0 );
return 0;
2005-04-30 16:41:37 +02:00
}
2005-04-29 15:26:31 +02:00
2007-01-18 23:38:13 +01:00
/* Start up all resolvers */
2005-04-29 15:26:31 +02:00
for ( resolver = resolvers ; resolver < resolvers_end ; resolver++ ) {
2007-01-18 23:38:13 +01:00
if ( ( rc = resolver->resolv ( name, sa,
&resolution->async ) ) != 0 ) {
DBGC ( resolution, "RESOLV %p could not start %s: "
"%s\n", resolution, resolver->name,
strerror ( rc ) );
/* Continue to try other resolvers */
continue;
2005-04-30 16:41:37 +02:00
}
2007-01-18 23:38:13 +01:00
(resolution->pending)++;
2005-04-29 15:26:31 +02:00
}
2007-01-18 23:38:13 +01:00
if ( ! resolution->pending )
goto err;
2005-04-29 15:26:31 +02:00
return 0;
2007-01-18 23:38:13 +01:00
err:
async_uninit ( &resolution->async );
free ( resolution );
return rc;
2005-04-29 15:26:31 +02:00
}
2007-01-18 23:38:13 +01:00
/**
* Handle child name resolution completion
*
* @v async Name resolution asynchronous operation
* @v signal SIGCHLD
*/
static void resolv_sigchld ( struct async *async,
enum signal signal __unused ) {
struct resolution *resolution =
container_of ( async, struct resolution, async );
int rc;
/* Reap the child */
2007-01-18 23:38:13 +01:00
async_wait ( async, &rc, 1 );
/* If this child succeeded, kill all the others. They should
* immediately die (invoking resolv_sigchld() again, which
* won't do anything because the exit status is non-zero and
* the pending count won't reach zero until this instance
* completes).
*/
if ( rc == 0 )
2007-01-18 23:38:13 +01:00
async_signal_children ( async, SIGKILL );
/* When we have no children left, exit */
2007-01-18 23:38:13 +01:00
if ( --(resolution->pending) == 0 )
async_done ( async, rc );
2007-01-18 23:38:13 +01:00
}
/**
* Free name resolution structure
*
* @v async Asynchronous operation
*/
static void resolv_reap ( struct async *async ) {
free ( container_of ( async, struct resolution, async ) );
}
/** Name resolution asynchronous operations */
static struct async_operations resolv_async_operations = {
.reap = resolv_reap,
.signal = {
[SIGKILL] = async_signal_children,
2007-01-18 23:38:13 +01:00
[SIGCHLD] = resolv_sigchld,
},
};