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

Name resolution. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/process.h>
#include <ipxe/socket.h>
#include <ipxe/resolv.h>

Go to the source code of this file.

Data Structures

struct  numeric_resolv
 A numeric name resolver. More...
struct  resolv_mux
 A name resolution multiplexer. More...
struct  named_socket
 A named socket. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void resolv_done (struct interface *intf, struct sockaddr *sa)
 Name resolved.
static void numeric_step (struct numeric_resolv *numeric)
static int numeric_resolv (struct interface *resolv, const char *name, struct sockaddr *sa)
struct resolver numeric_resolver __resolver (RESOLV_NUMERIC)
static int resmux_try (struct resolv_mux *mux)
 Try current child name resolver.
static void resmux_close (struct resolv_mux *mux, int rc)
 Close name resolution multiplexer.
static void resmux_child_close (struct resolv_mux *mux, int rc)
 Child finished resolution.
int resolv (struct interface *resolv, const char *name, struct sockaddr *sa)
 Start name resolution.
static void named_close (struct named_socket *named, int rc)
 Terminate named socket opener.
static size_t named_window (struct named_socket *named __unused)
 Check flow control window.
static void named_resolv_done (struct named_socket *named, struct sockaddr *sa)
 Name resolved.
int xfer_open_named_socket (struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
 Open named socket.

Variables

static struct process_descriptor numeric_process_desc
static struct interface_operation resmux_child_op []
 Name resolution multiplexer child interface operations.
static struct interface_descriptor resmux_child_desc
 Name resolution multiplexer child interface descriptor.
static struct interface_operation resmux_parent_op []
 Name resolution multiplexer parent interface operations.
static struct interface_descriptor resmux_parent_desc
 Name resolution multiplexer parent interface descriptor.
static struct interface_operation named_xfer_ops []
 Named socket opener data transfer interface operations.
static struct interface_descriptor named_xfer_desc
 Named socket opener data transfer interface descriptor.
static struct interface_operation named_resolv_op []
 Named socket opener resolver interface operations.
static struct interface_descriptor named_resolv_desc
 Named socket opener resolver interface descriptor.

Detailed Description

Name resolution.

Definition in file resolv.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void resolv_done ( struct interface intf,
struct sockaddr sa 
)

Name resolved.

Parameters:
intfObject interface
saCompleted socket address (if successful)

Definition at line 55 of file resolv.c.

References DBGC, dest, INTF_COL, intf_get_dest_op, INTF_INTF_DBG, INTF_INTF_FMT, intf_object(), intf_put(), op, resolv_done(), and resolv_done_TYPE.

Referenced by dns_resolved(), numeric_step(), and resolv_done().

                                                                 {
        struct interface *dest;
        resolv_done_TYPE ( void * ) *op =
                intf_get_dest_op ( intf, resolv_done, &dest );
        void *object = intf_object ( dest );

        DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " resolv_done\n",
               INTF_INTF_DBG ( intf, dest ) );

        if ( op ) {
                op ( object, sa );
        } else {
                /* Default is to ignore resolutions */
        }

        intf_put ( dest );
}
static void numeric_step ( struct numeric_resolv numeric) [static]

Definition at line 94 of file resolv.c.

References intf_shutdown(), numeric_resolv::rc, numeric_resolv::resolv, resolv_done(), and numeric_resolv::sa.

                                                            {

        if ( numeric->rc == 0 )
                resolv_done ( &numeric->resolv, &numeric->sa );
        intf_shutdown ( &numeric->resolv, numeric->rc );
}
static int numeric_resolv ( struct interface resolv,
const char *  name,
struct sockaddr sa 
) [static]

Definition at line 104 of file resolv.c.

References ENOMEM, intf_init(), intf_plug_plug(), memcpy(), NULL, null_intf_desc, numeric_resolv::process, process_init(), numeric_resolv::rc, ref_init, ref_put, numeric_resolv::refcnt, numeric_resolv::resolv, numeric_resolv::sa, sock_aton(), and zalloc().

                                                                    {
        struct numeric_resolv *numeric;

        /* Allocate and initialise structure */
        numeric = zalloc ( sizeof ( *numeric ) );
        if ( ! numeric )
                return -ENOMEM;
        ref_init ( &numeric->refcnt, NULL );
        intf_init ( &numeric->resolv, &null_intf_desc, &numeric->refcnt );
        process_init ( &numeric->process, &numeric_process_desc,
                       &numeric->refcnt );
        memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );

        /* Attempt to resolve name */
        numeric->rc = sock_aton ( name, &numeric->sa );

        /* Attach to parent interface, mortalise self, and return */
        intf_plug_plug ( &numeric->resolv, resolv );
        ref_put ( &numeric->refcnt );
        return 0;
}
struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC  ) [read]
static int resmux_try ( struct resolv_mux mux) [static]

Try current child name resolver.

Parameters:
muxName resolution multiplexer
Return values:
rcReturn status code

Definition at line 166 of file resolv.c.

References resolv_mux::child, DBGC, resolver::name, resolv_mux::name, rc, resolver::resolv, resolv_mux::resolver, resolv_mux::sa, and strerror().

Referenced by resmux_child_close(), and resolv().

                                                 {
        struct resolver *resolver = mux->resolver;
        int rc;

        DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );

        if ( ( rc = resolver->resolv ( &mux->child, mux->name,
                                       &mux->sa ) ) != 0 ) {
                DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
                       mux, resolver->name, strerror ( rc ) );
                return rc;
        }

        return 0;
}
static void resmux_close ( struct resolv_mux mux,
int  rc 
) [static]

Close name resolution multiplexer.

Parameters:
muxName resolution multiplexer
rcReason for close

Definition at line 188 of file resolv.c.

References resolv_mux::child, intf_shutdown(), and resolv_mux::parent.

Referenced by resmux_child_close().

                                                            {

        /* Shut down all interfaces */
        intf_shutdown ( &mux->child, rc );
        intf_shutdown ( &mux->parent, rc );
}
static void resmux_child_close ( struct resolv_mux mux,
int  rc 
) [static]

Child finished resolution.

Parameters:
muxName resolution multiplexer
rcReturn status code

Definition at line 201 of file resolv.c.

References resolv_mux::child, DBGC, intf_restart(), resolver::name, resmux_close(), resmux_try(), resolv_mux::resolver, RESOLVERS, and table_end.

                                                                  {

        /* Restart child interface */
        intf_restart ( &mux->child, rc );

        /* If this resolution succeeded, stop now */
        if ( rc == 0 ) {
                DBGC ( mux, "RESOLV %p succeeded using method %s\n",
                       mux, mux->resolver->name );
                goto finished;
        }

        /* Attempt next child resolver, if possible */
        mux->resolver++;
        if ( mux->resolver >= table_end ( RESOLVERS ) ) {
                DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
                goto finished;
        }
        if ( ( rc = resmux_try ( mux ) ) != 0 )
                goto finished;

        /* Next resolver is now running */
        return;

 finished:
        resmux_close ( mux, rc );
}
int resolv ( struct interface resolv,
const char *  name,
struct sockaddr sa 
)

Start name resolution.

Parameters:
resolvName resolution interface
nameName to resolve
saSocket address to complete
Return values:
rcReturn status code

Definition at line 257 of file resolv.c.

References resolv_mux::child, DBGC, ENOMEM, intf_init(), intf_plug_plug(), memcpy(), resolv_mux::name, NULL, resolv_mux::parent, rc, ref_init, ref_put, resolv_mux::refcnt, resmux_try(), resolv_mux::resolver, RESOLVERS, resolv_mux::sa, strlen(), table_start, and zalloc().

Referenced by comboot_resolv(), resolv_setting(), and xfer_open_named_socket().

                                   {
        struct resolv_mux *mux;
        size_t name_len = ( strlen ( name ) + 1 );
        int rc;

        /* Allocate and initialise structure */
        mux = zalloc ( sizeof ( *mux ) + name_len );
        if ( ! mux )
                return -ENOMEM;
        ref_init ( &mux->refcnt, NULL );
        intf_init ( &mux->parent, &resmux_parent_desc, &mux->refcnt );
        intf_init ( &mux->child, &resmux_child_desc, &mux->refcnt );
        mux->resolver = table_start ( RESOLVERS );
        if ( sa )
                memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
        memcpy ( mux->name, name, name_len );

        DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );

        /* Start first resolver in chain.  There will always be at
         * least one resolver (the numeric resolver), so no need to
         * check for the zero-resolvers-available case.
         */
        if ( ( rc = resmux_try ( mux ) ) != 0 )
                goto err;

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

 err:
        ref_put ( &mux->refcnt );
        return rc;      
}
static void named_close ( struct named_socket named,
int  rc 
) [static]

Terminate named socket opener.

Parameters:
namedNamed socket
rcReason for termination

Definition at line 323 of file resolv.c.

References intf_shutdown(), named_socket::resolv, and named_socket::xfer.

Referenced by named_resolv_done().

                                                               {
        /* Shut down interfaces */
        intf_shutdown ( &named->resolv, rc );
        intf_shutdown ( &named->xfer, rc );
}
static size_t named_window ( struct named_socket *named  __unused) [static]

Check flow control window.

Parameters:
namedNamed socket
Return values:
lenLength of window

Definition at line 335 of file resolv.c.

                                                                   {
        /* Not ready for data until we have redirected away */
        return 0;
}
static void named_resolv_done ( struct named_socket named,
struct sockaddr sa 
) [static]

Name resolved.

Parameters:
namedNamed socket
saCompleted socket address

Definition at line 357 of file resolv.c.

References DBGC, named_socket::have_local, intf_nullify(), intf_unplug(), named_socket::local, LOCATION_SOCKET, named_close(), NULL, rc, named_socket::semantics, strerror(), named_socket::xfer, and xfer_redirect().

                                                      {
        int rc;

        /* Nullify data transfer interface */
        intf_nullify ( &named->xfer );

        /* Redirect data-xfer interface */
        if ( ( rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
                                    named->semantics, sa,
                                    ( named->have_local ?
                                      &named->local : NULL ) ) ) != 0 ) {
                /* Redirection failed - do not unplug data-xfer interface */
                DBGC ( named, "NAMED %p could not redirect: %s\n",
                       named, strerror ( rc ) );
        } else {
                /* Redirection succeeded - unplug data-xfer interface */
                DBGC ( named, "NAMED %p redirected successfully\n", named );
                intf_unplug ( &named->xfer );
        }

        /* Terminate named socket opener */
        named_close ( named, rc );
}
int xfer_open_named_socket ( struct interface xfer,
int  semantics,
struct sockaddr peer,
const char *  name,
struct sockaddr local 
)

Open named socket.

Parameters:
semanticsCommunication semantics (e.g. SOCK_STREAM)
peerPeer socket address to complete
nameName to resolve
localLocal socket address, or NULL
Return values:
rcReturn status code

Definition at line 402 of file resolv.c.

References DBGC, ENOMEM, named_socket::have_local, intf_init(), intf_plug_plug(), named_socket::local, memcpy(), NULL, rc, ref_init, ref_put, named_socket::refcnt, resolv(), named_socket::resolv, named_socket::semantics, named_socket::xfer, and zalloc().

Referenced by apply_syslogs_settings(), create_pinger(), ftp_open(), http_connect(), iscsi_open_connection(), nfs_connect(), slam_open(), start_ntp(), tcp_open_uri(), tftp_reopen(), and udp_open_uri().

                                                      {
        struct named_socket *named;
        int rc;

        /* Allocate and initialise structure */
        named = zalloc ( sizeof ( *named ) );
        if ( ! named )
                return -ENOMEM;
        ref_init ( &named->refcnt, NULL );
        intf_init ( &named->xfer, &named_xfer_desc, &named->refcnt );
        intf_init ( &named->resolv, &named_resolv_desc, &named->refcnt );
        named->semantics = semantics;
        if ( local ) {
                memcpy ( &named->local, local, sizeof ( named->local ) );
                named->have_local = 1;
        }

        DBGC ( named, "NAMED %p opening \"%s\"\n",
               named, name );

        /* Start name resolution */
        if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
                goto err;

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

 err:
        ref_put ( &named->refcnt );
        return rc;
}

Variable Documentation

Initial value:

Definition at line 101 of file resolv.c.

Initial value:

Name resolution multiplexer child interface operations.

Definition at line 230 of file resolv.c.

Initial value:
        INTF_DESC_PASSTHRU ( struct resolv_mux, child, resmux_child_op,
                             parent )

Name resolution multiplexer child interface descriptor.

Definition at line 235 of file resolv.c.

Initial value:

Name resolution multiplexer parent interface operations.

Definition at line 240 of file resolv.c.

Initial value:
        INTF_DESC_PASSTHRU ( struct resolv_mux, parent, resmux_parent_op,
                             child )

Name resolution multiplexer parent interface descriptor.

Definition at line 245 of file resolv.c.

Initial value:

Named socket opener data transfer interface operations.

Definition at line 341 of file resolv.c.

Initial value:

Named socket opener data transfer interface descriptor.

Definition at line 347 of file resolv.c.

Initial value:

Named socket opener resolver interface operations.

Definition at line 383 of file resolv.c.

Initial value:

Named socket opener resolver interface descriptor.

Definition at line 389 of file resolv.c.