dns.c

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

C
1,215
字号
                        break;

                    case DNS_TYPE_PTR :

                        /* The answer has the correct type and class. Get the name. */
                        name_size = DNS_Unpack_Domain_Name (data,
                                                            rr_ptr->dns_rdata,
                                                            (CHAR *)pkt);
                        break;

                    default :
                        return -1;
                }

                /* Get the time to live for this RR. */
                *ttl = longswap (rr_ptr->dns_ttl);

                /* Indicate an answer was found. */
                answer_received = 1;

                break;
			}

            /* Copy the length of this answer. */
            memcpy (&length, &rr_ptr->dns_rdlength, 2);

            /* Point to the next answer, if any.  The rdlength field is the
               length of the data section of the RR.  Add 10 for the sizes of
               the type, class, ttl, and rdlength.
            */
            p_ptr += (10 + intswap ((int16)length));
		}
	}

    NU_Deallocate_Memory(name);

    if (answer_received)
        return (NU_SUCCESS);
    else
        return (-1);

}  /* DNS_Extract_Data */

/******************************************************************************/
/* FUNCTION                                                                   */
/*                                                                            */
/*   DNS_Add_Host                                                             */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*    This function adds a new host to the "hosts file".                      */
/*    A block of memory is alocated each time we want to add a new host.      */
/*    This can be wasteful of memory because Nucleus PLUS has a minimum       */
/*    allocation size of 50 bytes.  The alternative would have been to        */
/*    allocate enough memory for several of these at one time.  The problem   */
/*    with that is the RFC defines the maximum size of a name at 255 bytes.   */
/*    We would have to assume that a maximum sized name would have to be      */
/*    stored and allocate memory accordingly.  Since most names are nowhere   */
/*    close to 255 bytes this method would have been even more wasteful.      */
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*    Glen Johnson,      Accelerated Technology Inc.                          */
/*                                                                            */
/* CALLED BY                                                                  */
/*                                                                            */
/*    DNS_Find_Host_By_Addr     Find a host by address.                       */
/*    DNS_Find_Host_By_Name     Find a host by name.                          */
/*                                                                            */
/* CALLS                                                                      */
/*                                                                            */
/*    dll_enqueue               Add an item to a list.                        */
/*    NU_Allocate_Memory        Allocate memory.                              */
/*    NU_Retrieve_Clock         Get the current clock tick.                   */
/*                                                                            */
/* INPUTS                                                                     */
/*                                                                            */
/*    name                      The name of the host to add.                  */
/*    ip_addr                   The IP address of the host to add.            */
/*    ttl                       The Time To Live for this entry.              */
/*                                                                            */
/* OUTPUTS                                                                    */
/*                                                                            */
/*    NULL is returned if the host can not be added, otherwise a host pointer.*/
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*    NAME                DATE        REMARKS                                 */
/*                                                                            */
/*    Glen Johnson      06/17/97    Created initial version for NET 3.1       */
/*                                                                            */
/******************************************************************************/
DNS_HOST *DNS_Add_Host(CHAR *name, CHAR *ip_addr, UNSIGNED ttl)
{
    INT             size;
    DNS_HOST        *dns_host;
    UNSIGNED        time;

    /* Get the size of the name. */
    size = strlen(name) + 1;

    /* Retrieve the current time. */
    time = NU_Retrieve_Clock();

    /* Before allocating memory for a new entry, check to see if the TTL of an
       old entry has expired.  If so re-use that entry. */
    for ( dns_host = DNS_Hosts.dns_head;
          dns_host;
          dns_host = dns_host->dns_next )
    {
        /* First check to see if this entry has a ttl.  A ttl of 0 is used to
           indicate permanent entries.
        */
        if (dns_host->dns_ttl)

            /* Check to see if the ttl has expired. */
            if (INT32_CMP(dns_host->dns_ttl, time) <= 0)

                /* Good so far.  Check to see if the name will fit in this
                   entry. */
                if (dns_host->dns_name_size >= size)
                    break;
    }

    /* Did we find an entry that can be re-used. */
    if (!dns_host)
    {
        /* A new host structure must be created. */
        /* We need to compute the size of the block of memory to allocate.  The
           size will include the size of a host structure + length of the name +
           1 (for the null terminator).  We may want to reuse this entry later
           for other hosts.  So allocate a minimum number of bytes for the name.
           This will make it more likely that this entry can be reused.
        */

        /* If the size is less than the minimum, then bump it up. */
        if (size < DNS_MIN_NAME_ALLOC)
            size = DNS_MIN_NAME_ALLOC;

        /* Allocate a block of memory for the new DNS host structure and for the
           name. */
        if (NU_Allocate_Memory (&System_Memory, (VOID **)&dns_host,
                                size + sizeof(DNS_HOST),
                                NU_NO_SUSPEND) != NU_SUCCESS)
        {
            return (NU_NULL);
        }

        /* The name will be stored immediately after the structure. */
        dns_host->dns_name = (CHAR *)(dns_host + 1);

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

    }

    /* Copy the name and IP address. */
    strcpy(dns_host->dns_name, name);
    memcpy(dns_host->dns_ipaddr, ip_addr, 4);

    /* TTL is specified in seconds.  So multiply by the number ticks per
       second. */
    ttl *= TICKSPERSEC;

    /* Initialize ttl. */
    dns_host->dns_ttl = ttl + NU_Retrieve_Clock();

    /* TTL value of 0 is used to indicate that the entry is permanent.  So if by
       some chance the value computed is 0, add 1. */
    if (!dns_host->dns_ttl)
        dns_host->dns_ttl++;

    /* Initialize the size of the name of this host. */
    dns_host->dns_name_size = size;

    return (dns_host);

} /* DNS_Add_Host */

/******************************************************************************/
/* FUNCTION                                                                   */
/*                                                                            */
/*   DNS_Addr_To_String                                                       */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*    This function takes an IP address and converts it to a character string.*/
/*                                                                            */
/* AUTHOR                                                                     */
/*                                                                            */
/*    Glen Johnson,      Accelerated Technology Inc.                          */
/*                                                                            */
/* CALLED BY                                                                  */
/*                                                                            */
/*    DNS_Build_Query           Create a DNS query packet.                    */
/*                                                                            */
/* CALLS                                                                      */
/*                                                                            */
/*    itoa                      Convert an intger to a ascii string.          */
/*                                                                            */
/* INPUTS                                                                     */
/*                                                                            */
/*    addr                      The address of to convert.                    */
/*    new_name                  The address in ascii format.                  */
/*                                                                            */
/* OUTPUTS                                                                    */
/*                                                                            */
/*    NU_SUCCESS                Indicates success.                            */
/*                                                                            */
/* HISTORY                                                                    */
/*                                                                            */
/*    NAME                DATE        REMARKS                                 */
/*                                                                            */
/*    Glen Johnson      06/17/97    Created initial version for NET 3.1       */
/*                                                                            */
/******************************************************************************/
STATUS  DNS_Addr_To_String(CHAR *addr, CHAR *new_name)
{
    INT         i;
    CHAR        *ptr = new_name;
    uchar       *a = (uchar *)addr;

    /* We need to add each octet of the address to the name in reverse order. */
    for (i = 3; i >= 0; i--)
    {
        /* Convert the octet to a character string. */
        itoa(a[i], ptr, 10);

        /* Move past the string just added. */
        ptr += strlen(ptr);

        /* Add the dot. */
        *ptr++ = '.';
    }

    strcpy(ptr, "IN-ADDR.ARPA");

    return NU_SUCCESS;

} /* DNS_Addr_To_String */

#endif /* DNS_INCLUDED */

⌨️ 快捷键说明

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