dns.c revision a734c64bff58bda2fa48c2795453e092167b0ff7
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* Portions copyright (C) 2004 Anselm M. Hoffmeister
* <stockholm@users.sourceforge.net>.
*
* 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 <stdio.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/settings.h>
#include <ipxe/features.h>
/** @file
*
* DNS protocol
*
*/
/* Disambiguate the various error causes */
#define EINFO_ENXIO_NO_RECORD \
#define EINFO_ENXIO_NO_NAMESERVER \
/** The DNS server */
static struct sockaddr_tcpip nameserver = {
};
/** The local domain */
static char *localdomain;
/** A DNS request */
struct dns_request {
/** Reference counter */
/** Name resolution interface */
/** Data transfer interface */
/** Retry timer */
struct retry_timer timer;
/** Socket address to fill in with resolved address */
/** Current query packet */
/** Location of query info structure within current packet
*
* The query info structure is located immediately after the
* compressed name.
*/
struct dns_query_info *qinfo;
/** Recursion counter */
unsigned int recursion;
};
/**
* Mark DNS request as complete
*
* @v dns DNS request
* @v rc Return status code
*/
/* Stop the retry timer */
/* Shut down interfaces */
}
/**
* Compare DNS reply name against the query name from the original request
*
* @v dns DNS request
* @v reply DNS reply
* @v rname Reply name
* @ret zero Names match
* @ret non-zero Names do not match
*/
const struct dns_header *reply,
const char *rname ) {
int i;
while ( 1 ) {
/* Obtain next section of rname */
while ( ( *rname ) & 0xc0 ) {
}
/* Check that lengths match */
return -1;
/* If length is zero, we have reached the end */
if ( ! *qname )
return 0;
/* Check that data matches */
for ( i = *qname + 1; i > 0 ; i-- ) {
return -1;
}
}
}
/**
* Skip over a (possibly compressed) DNS name
*
* @v name DNS name
* @ret name Next DNS name
*/
static const char * dns_skip_name ( const char *name ) {
while ( 1 ) {
if ( ! *name ) {
/* End of name */
return ( name + 1);
}
if ( *name & 0xc0 ) {
/* Start of a compressed name */
return ( name + 2 );
}
/* Uncompressed name portion */
}
}
/**
* Find an RR in a reply packet corresponding to our query
*
* @v dns DNS request
* @v reply DNS reply
* @ret rr DNS RR, or NULL if not found
*/
const struct dns_header *reply ) {
int i, cmp;
const char *p = ( ( char * ) reply ) + sizeof ( struct dns_header );
union dns_rr_info *rr_info;
/* Skip over the questions section */
p = dns_skip_name ( p ) + sizeof ( struct dns_query_info );
}
/* Process the answers section */
p = dns_skip_name ( p );
rr_info = ( ( union dns_rr_info * ) p );
if ( cmp == 0 )
return rr_info;
}
return NULL;
}
/**
* Append DHCP domain name if available and name is not fully qualified
*
* @v string Name as a NUL-terminated string
* @ret fqdn Fully-qualified domain name, malloc'd copy
*
* The caller must free fqdn which is allocated even if the name is already
* fully qualified.
*/
static char * dns_qualify_name ( const char *string ) {
char *fqdn;
/* Leave unchanged if already fully-qualified or no local domain */
/* Append local domain to name */
return fqdn;
}
/**
* Convert a standard NUL-terminated string to a DNS name
*
* @v string Name as a NUL-terminated string
* @v buf Buffer in which to place DNS name
* @ret next Byte following constructed DNS name
*
* DNS names consist of "<length>element" pairs.
*/
char *length_byte;
char c;
length_byte = buf++;
*length_byte = 0;
do {
c = *(string++);
if ( ( c == '.' ) || ( c == '\0' ) ) {
if ( *length_byte ) {
length_byte = buf++;
*length_byte = 0;
}
} else {
*(buf++) = c;
(*length_byte)++;
}
} while ( c );
return buf;
}
/**
* Convert an uncompressed DNS name to a NUL-terminated string
*
* @v name DNS name
* @ret string NUL-terminated string
*
* Produce a printable version of a DNS name. Used only for debugging.
*/
static inline char * dns_unmake_name ( char *name ) {
char *p;
unsigned int len;
p = name;
while ( ( len = *p ) ) {
*(p++) = '.';
p += len;
}
return name + 1;
}
/**
* Decompress a DNS name
*
* @v reply DNS replay
* @v name DNS name
* @v buf Buffer into which to decompress DNS name
* @ret next Byte following decompressed DNS name
*/
int i, len;
do {
/* Obtain next section of name */
while ( ( *name ) & 0xc0 ) {
}
/* Copy data */
for ( i = len + 1 ; i > 0 ; i-- ) {
}
} while ( len );
return buf;
}
/**
* Send next packet in DNS request
*
* @v dns DNS request
*/
static unsigned int qid = 0;
/* Increment query ID */
/* Start retransmission timer */
/* Send the data */
}
/**
* Handle DNS retransmission timer expiry
*
* @v timer Retry timer
* @v fail Failure indicator
*/
struct dns_request *dns =
if ( fail ) {
} else {
dns_send_packet ( dns );
}
}
/**
* Receive new data
*
* @v dns DNS request
* @v iobuf I/O buffer
* @v meta Data transfer metadata
* @ret rc Return status code
*/
union dns_rr_info *rr_info;
struct sockaddr_in *sin;
int rc;
/* Sanity check */
goto done;
}
/* Check reply ID matches query ID */
goto done;
}
/* Stop the retry timer. After this point, each code path
* must either restart the timer by calling dns_send_packet(),
* or mark the DNS operation as complete by calling
* dns_done()
*/
/* Search through response for useful answers. Do this
* multiple times, to take advantage of useful nameservers
* which send us e.g. the CNAME *and* the A record for the
* pointed-to name.
*/
case htons ( DNS_TYPE_A ):
/* Found the target A record */
/* Return resolved address */
/* Mark operation as complete */
rc = 0;
goto done;
case htons ( DNS_TYPE_CNAME ):
/* Found a CNAME record; update query and recurse */
/* Terminate the operation if we recurse too far */
dns );
rc = 0;
goto done;
}
break;
default:
break;
}
}
/* Determine what to do next based on the type of query we
* issued and the reponse we received
*/
switch ( qtype ) {
case htons ( DNS_TYPE_A ):
/* We asked for an A record and got nothing;
* try the CNAME.
*/
dns_send_packet ( dns );
rc = 0;
goto done;
case htons ( DNS_TYPE_CNAME ):
/* We asked for a CNAME record. If we got a response
* (i.e. if the next A query is already set up), then
* issue it, otherwise abort.
*/
dns_send_packet ( dns );
rc = 0;
goto done;
} else {
rc = 0;
goto done;
}
default:
assert ( 0 );
goto done;
}
done:
/* Free I/O buffer */
return rc;
}
/**
* Receive new data
*
* @v dns DNS request
* @v rc Reason for close
*/
if ( ! rc )
rc = -ECONNABORTED;
}
/** DNS socket interface operations */
static struct interface_operation dns_socket_operations[] = {
};
/** DNS socket interface descriptor */
static struct interface_descriptor dns_socket_desc =
/** DNS resolver interface operations */
static struct interface_operation dns_resolv_op[] = {
};
/** DNS resolver interface descriptor */
static struct interface_descriptor dns_resolv_desc =
/**
* Resolve name using DNS
*
* @v resolv Name resolution interface
* @v name Name to resolve
* @v sa Socket address to fill in
* @ret rc Return status code
*/
struct dns_request *dns;
char *fqdn;
int rc;
/* Fail immediately if no DNS servers */
if ( ! nameserver.st_family ) {
DBG ( "DNS not attempting to resolve \"%s\": "
"no DNS servers\n", name );
goto err_no_nameserver;
}
/* Ensure fully-qualified domain name if DHCP option was given */
if ( ! fqdn ) {
goto err_qualify_name;
}
/* Allocate DNS structure */
if ( ! dns ) {
goto err_alloc_dns;
}
/* Create query */
DNS_FLAG_RD );
/* Open UDP connection */
( struct sockaddr * ) &nameserver,
NULL ) ) != 0 ) {
goto err_open_socket;
}
/* Send first DNS packet */
dns_send_packet ( dns );
/* Attach parent interface, mortalise self, and return */
return 0;
return rc;
}
/** DNS name resolver */
.name = "DNS",
.resolv = dns_resolv,
};
/******************************************************************************
*
* Settings
*
******************************************************************************
*/
/** DNS server setting */
.name = "dns",
.description = "DNS server",
.tag = DHCP_DNS_SERVERS,
.type = &setting_type_ipv4,
};
/** Domain name setting */
.name = "domain",
.description = "DNS domain",
.tag = DHCP_DOMAIN_NAME,
.type = &setting_type_string,
};
/**
* Apply DNS settings
*
* @ret rc Return status code
*/
static int apply_dns_settings ( void ) {
struct sockaddr_in *sin_nameserver =
( struct sockaddr_in * ) &nameserver;
int len;
/* Fetch DNS server address */
nameserver.st_family = 0;
&sin_nameserver->sin_addr ) ) >= 0 ){
DBG ( "DNS using nameserver %s\n",
}
/* Get local domain DHCP option */
free ( localdomain );
&localdomain ) ) < 0 ) {
DBG ( "DNS could not fetch local domain: %s\n",
}
if ( localdomain )
return 0;
}
/** DNS settings applicator */
};