iPXE
Data Structures | Functions | Variables
nslookup.c File Reference

Standalone name resolution. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ipxe/resolv.h>
#include <ipxe/tcpip.h>
#include <ipxe/monojob.h>
#include <ipxe/settings.h>
#include <usr/nslookup.h>

Go to the source code of this file.

Data Structures

struct  nslookup
 A name resolution request. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static void nslookup_close (struct nslookup *nslookup, int rc)
 Terminate name resolution.
static void nslookup_resolv_done (struct nslookup *nslookup, struct sockaddr *sa)
 Handle resolved name.
static int resolv_setting (struct interface *job, const char *name, const char *setting_name)
 Initiate standalone name resolution.
int nslookup (const char *name, const char *setting_name)
 Perform (blocking) standalone name resolution.

Variables

static struct interface_operation nslookup_resolver_operations []
 Name resolution resolver interface operations.
static struct interface_descriptor nslookup_resolver_desc
 Name resolution resolver interface descriptor.
static struct interface_operation nslookup_job_operations []
 Name resolution job control interface operations.
static struct interface_descriptor nslookup_job_desc
 Name resolution job control interface descriptor.

Detailed Description

Standalone name resolution.

Definition in file nslookup.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
static void nslookup_close ( struct nslookup nslookup,
int  rc 
) [static]

Terminate name resolution.

Parameters:
nslookupName resolution request
rcReason for termination

Definition at line 58 of file nslookup.c.

References intf_shutdown(), nslookup::job, and nslookup::resolver.

Referenced by nslookup_resolv_done().

                                                                 {

        /* Shut down interfaces */
        intf_shutdown ( &nslookup->resolver, rc );
        intf_shutdown ( &nslookup->job, rc );
}
static void nslookup_resolv_done ( struct nslookup nslookup,
struct sockaddr sa 
) [static]

Handle resolved name.

Parameters:
nslookupName resolution request
saCompleted socket address

Definition at line 71 of file nslookup.c.

References AF_INET, AF_INET6, autovivify_child_settings(), data, ENOTSUP, len, nslookup_close(), parse_setting_name(), rc, sockaddr::sa_family, nslookup::setting_name, sin, sin6, sockaddr_in6::sin6_addr, sockaddr_in::sin_addr, store_setting(), and setting::type.

                                                         {
        struct sockaddr_in *sin;
        struct sockaddr_in6 *sin6;
        const struct setting_type *default_type;
        struct settings *settings;
        struct setting setting;
        void *data;
        size_t len;
        int rc;

        /* Extract address */
        switch ( sa->sa_family ) {
        case AF_INET:
                sin = ( ( struct sockaddr_in * ) sa );
                data = &sin->sin_addr;
                len = sizeof ( sin->sin_addr );
                default_type = &setting_type_ipv4;
                break;
        case AF_INET6:
                sin6 = ( ( struct sockaddr_in6 * ) sa );
                data = &sin6->sin6_addr;
                len = sizeof ( sin6->sin6_addr );
                default_type = &setting_type_ipv6;
                break;
        default:
                rc = -ENOTSUP;
                goto err;
        }

        /* Parse specified setting name */
        if ( ( rc = parse_setting_name ( nslookup->setting_name,
                                         autovivify_child_settings, &settings,
                                         &setting ) ) != 0 )
                goto err;

        /* Apply default type if necessary */
        if ( ! setting.type )
                setting.type = default_type;

        /* Store in specified setting */
        if ( ( rc = store_setting ( settings, &setting, data, len ) ) != 0 )
                goto err;

 err:
        /* Terminate name resolution */
        nslookup_close ( nslookup, rc );
}
static int resolv_setting ( struct interface job,
const char *  name,
const char *  setting_name 
) [static]

Initiate standalone name resolution.

Parameters:
jobParent interface
nameName to resolve
setting_nameSetting name
Return values:
rcReturn status code

Definition at line 149 of file nslookup.c.

References ENOMEM, intf_init(), intf_plug_plug(), nslookup::job, memset(), nslookup(), NULL, rc, ref_init, ref_put, nslookup::refcnt, resolv(), nslookup::resolver, nslookup::setting_name, strcpy(), strlen(), and zalloc().

Referenced by nslookup().

                                                       {
        struct nslookup *nslookup;
        struct sockaddr sa;
        char *setting_name_copy;
        int rc;

        /* Allocate and initialise structure */
        nslookup = zalloc ( sizeof ( *nslookup ) + strlen ( setting_name )
                            + 1 /* NUL */ );
        if ( ! nslookup )
                return -ENOMEM;
        ref_init ( &nslookup->refcnt, NULL );
        intf_init ( &nslookup->job, &nslookup_job_desc, &nslookup->refcnt );
        intf_init ( &nslookup->resolver, &nslookup_resolver_desc,
                    &nslookup->refcnt );
        setting_name_copy = ( ( void * ) ( nslookup + 1 ) );
        strcpy ( setting_name_copy, setting_name );
        nslookup->setting_name = setting_name_copy;

        /* Start name resolution */
        memset ( &sa, 0, sizeof ( sa ) );
        if ( ( rc = resolv ( &nslookup->resolver, name, &sa ) ) != 0 )
                goto err_resolv;

        /* Attach parent interface, mortalise self, and return */
        intf_plug_plug ( &nslookup->job, job );
        ref_put ( &nslookup->refcnt );
        return 0;

 err_resolv:
        ref_put ( &nslookup->refcnt );
        return rc;
}
int nslookup ( const char *  name,
const char *  setting_name 
)

Perform (blocking) standalone name resolution.

Parameters:
nameName to resolve
setting_nameSetting name
Return values:
rcReturn status code

Definition at line 191 of file nslookup.c.

References monojob, monojob_wait(), NULL, printf(), rc, resolv_setting(), and strerror().

Referenced by resolv_setting().

                                                            {
        int rc;

        /* Perform name resolution */
        if ( ( rc = resolv_setting ( &monojob, name, setting_name ) ) == 0 )
                rc = monojob_wait ( NULL, 0 );
        if ( rc != 0 ) {
                printf ( "Could not resolve %s: %s\n", name, strerror ( rc ) );
                return rc;
        }

        return 0;
}

Variable Documentation

Initial value:

Name resolution resolver interface operations.

Definition at line 121 of file nslookup.c.

Initial value:

Name resolution resolver interface descriptor.

Definition at line 127 of file nslookup.c.

Initial value:

Name resolution job control interface operations.

Definition at line 132 of file nslookup.c.

Initial value:

Name resolution job control interface descriptor.

Definition at line 137 of file nslookup.c.