dns.c

来自「mcf5307实验源代码」· C语言 代码 · 共 1,215 行 · 第 1/5 页

C
1,215
字号
/****************************************************************************/
/*                                                                          */
/*      Copyright (c) 1993, 1997 By Accelerated Technology, Inc.            */
/*                                                                          */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the subject */
/* matter of this material.  All manufacturing, reproduction, use and sales */
/* rights pertaining to this subject matter are governed by the license     */
/* agreement.  The recipient of this software implicity accepts the terms   */
/* of the license.                                                          */
/*                                                                          */
/****************************************************************************/
/******************************************************************************/
/*                                                                            */
/* FILENAME                                                 VERSION           */
/*                                                                            */
/*  DNS                                                        3.2            */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*  This file contains the Domain Name System (DNS) component.  Given a       */
/*  name this component will discover the matching IP address.                */
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*  Glen Johnson,      Accelerated Technology Inc.                            */
/*                                                                            */
/* DATA STRUCTURES                                                            */
/*                                                                            */
/*  DNS_Hosts               A list of hosts names and IP addresses.           */
/*                                                                            */
/* FUNCTIONS                                                                  */
/*                                                                            */
/*  DNS_Initialize              Initialize the DNS component.                 */
/*  DNS_Resolve                 Resolve a host's IP address.                  */
/*  DNS_Build_Query             Build a DNS query packet.                     */
/*  DNS_Query                   Send a DNS query packet.                      */
/*  DNS_Pack_Domain_Name        Convert a name to the format expected by a    */
/*                                format expected by a DNS server.            */
/*  DNS_Unpack_Domain_Name      Upack a domain name.                          */
/*  DNS_Extract_Data            Get info (IP address or name) from the DNS    */
/*                                response.                                   */
/*  DNS_Find_Host_By_Name       Search for a host.                            */
/*  DNS_Add_Host                Add a host to the "hosts file".               */
/*  DNS_Find_Host_By_Addr       Search for a host.                            */
/*  DNS_Addr_To_String          Convert an IP address to ascii string.        */
/*                                                                            */
/* DEPENDENCIES                                                               */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*  NAME                DATE        REMARKS                                   */
/*                                                                            */
/*  Glen Johnson        06/16/97    Created initial version.                  */
/*                                                                            */
/******************************************************************************/

#include "nucleus.h"
#include "externs.h"
#include "data.h"
#include "tcp.h"
#include "dns.h"

#if DNS_INCLUDED

/* IP address for a DNS server. */
CHAR DNS_Server[4];

#endif

/* This is the list of hosts.  If a host can not be found in this list, DNS will
   be used to retrieve the information.  The new host will be added to the list.
*/
DNS_HOST_LIST   DNS_Hosts;


/******************************************************************************/
/* FUNCTION                                                                   */
/*                                                                            */
/*   DNS_Initialize                                                           */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*    This function initializes the DNS component.                            */
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*    Glen Johnson,      Accelerated Technology Inc.                          */
/*                                                                            */
/* CALLED BY                                                                  */
/*                                                                            */
/*    protinit                  Initializes the protocol stack.               */
/*                                                                            */
/* CALLS                                                                      */
/*                                                                            */
/*    NU_Allocate_Memory        Allocate memory.                              */
/*    dll_enqueue               Add an item to a list.                        */
/*                                                                            */
/* INPUTS                                                                     */
/*                                                                            */
/*                                                                            */
/* OUTPUTS                                                                    */
/*                                                                            */
/*    NU_SUCCESS                Indicates successful operation.               */
/*    NU_MEM_ALLOC              Indicates not enough dynamic memory was       */
/*                                available.                                  */
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*    NAME                DATE        REMARKS                                 */
/*                                                                            */
/*    Glen Johnson      06/16/97    Created initial version for NET 3.1       */
/*                                                                            */
/******************************************************************************/
STATUS DNS_Initialize(VOID)
{
    struct host         *hst;
    INT                 host_count;
    INT                 i;
    DNS_HOST            *dns_host;

#if DNS_INCLUDED

    /* Initialize the DNS Server's IP address. */
    DNS_Server[0] = (CHAR)DNS_IP_Addr_1;
    DNS_Server[1] = (CHAR)DNS_IP_Addr_2;
    DNS_Server[2] = (CHAR)DNS_IP_Addr_3;
    DNS_Server[3] = (CHAR)DNS_IP_Addr_4;

#endif /* DNS_INCLUDED */

    /* Count the number of hosts. */
    for ( hst = hostTable, host_count = 0;
          hst->name[0];
          hst++, host_count++ );

    /* Allocate a block of memeory to build the hosts list with. */
    if (NU_Allocate_Memory (&System_Memory, (VOID **)&dns_host,
                            sizeof (DNS_HOST) * host_count,
                            NU_NO_SUSPEND) != NU_SUCCESS)
    {
        return (NU_MEM_ALLOC);
    }

    /* Build the HOST list. */
    for (i = 0; i < host_count; i++, dns_host++)
    {
        /* Setup the name and IP address. */
        dns_host->dns_name = hostTable[i].name;

        memcpy(dns_host->dns_ipaddr, hostTable[i].address, 4);

        /* Set the TTL.  A TTL of 0 indicates that an entry is permanent. */
        dns_host->dns_ttl = 0;

        /* Setup the length of the name.  Account for the null terminator. */
        dns_host->dns_name_size = strlen(hostTable[i].name) + 1;

        /* Add this host to the list. */
        dll_enqueue((tqe_t *) &DNS_Hosts, (tqe_t *) dns_host);

    }

    return (NU_SUCCESS);

} /* DNS_Initialize */

/******************************************************************************/
/* FUNCTION                                                                   */
/*                                                                            */
/*   DNS_Find_Host_By_Name                                                    */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*    This function searches the "hosts" file for a host with a matching name.*/
/*    If the host is not found DNS is used to resolve the host name if        */
/*    if possible.                                                            */
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*    Glen Johnson,      Accelerated Technology Inc.                          */
/*                                                                            */
/* CALLED BY                                                                  */
/*                                                                            */
/*    NU_Get_Host_By_Name       Retrieve host information based on a name.    */
/*                                                                            */
/* CALLS                                                                      */
/*                                                                            */
/*    DNS_Add_Host              Add a host to the "hosts file"                */
/*    DNS_Resolve               Resolve a host name or address                */
/*    NU_Retrieve_Clock         Get the current clock tick.                   */
/*                                                                            */
/* INPUTS                                                                     */
/*                                                                            */
/*    name                      The name of the host to find.                 */
/*                                                                            */
/* OUTPUTS                                                                    */
/*                                                                            */
/*    NULL is returned if the host is not found, otherwise a host pointer.    */
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*    NAME                DATE        REMARKS                                 */
/*                                                                            */
/*    Glen Johnson      06/17/97    Created initial version for NET 3.1       */
/*                                                                            */
/******************************************************************************/
DNS_HOST *DNS_Find_Host_By_Name(CHAR *name)
{
    DNS_HOST        *host;
    CHAR            ip_addr[4];
    UNSIGNED        ttl;


    /* Search for a matching host. */
    for ( host = DNS_Hosts.dns_head; host ; host = host->dns_next)
    {
        /* Is this one we're looking for. */
        if (stricmp((const char *)host->dns_name, name) == 0)
        {
            /* We found a match.  If this is a permanent entry, return it. */
            if (!host->dns_ttl)
                return host;
            else
            {
                /* If the ttl has not expired return the host.  Else this entry
                   should be updated, so break. */
                if (INT32_CMP(host->dns_ttl, NU_Retrieve_Clock()) > 0)
                    return host;
                else
                    break;
            }
        }
    }

#if DNS_INCLUDED

    /* If the host was not found in the local cache or it had expired, then try
       to resolve it through DNS. */
    if (DNS_Resolve(name, ip_addr, &ttl, DNS_TYPE_A) == NU_SUCCESS)
        return (DNS_Add_Host(name, ip_addr, ttl));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?