dns.c
来自「mcf5307实验源代码」· C语言 代码 · 共 1,215 行 · 第 1/5 页
C
1,215 行
#endif /* DNS_INCLUDED */
return (NU_NULL);
} /* DNS_Find_Host_By_Name */
/******************************************************************************/
/* FUNCTION */
/* */
/* DNS_Find_Host_By_Addr */
/* */
/* DESCRIPTION */
/* */
/* This function searches the "hosts" file for a host with a matching addr.*/
/* If the host is not found DNS is used to resolve the host address */
/* if possible. */
/* */
/* AUTHOR */
/* */
/* Glen Johnson, Accelerated Technology Inc. */
/* */
/* CALLED BY */
/* */
/* NU_Get_Host_By_Addr Retrieve host information based on an address.*/
/* */
/* 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 */
/* */
/* addr The address 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_Addr(CHAR *addr)
{
DNS_HOST *host;
CHAR name[255];
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 (*(uint32 *)host->dns_ipaddr == *(uint32 *)addr)
{
/* 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, addr, &ttl, DNS_TYPE_PTR) == NU_SUCCESS)
return (DNS_Add_Host(name, addr, ttl));
#endif /* DNS_INCLUDED */
return (NU_NULL);
} /* DNS_Find_Host_By_Addr */
#if DNS_INCLUDED
/******************************************************************************/
/* FUNCTION */
/* */
/* DNS_Resolve */
/* */
/* DESCRIPTION */
/* */
/* This routine will handle domain based name lookup. It can hadle both */
/* queries for names and quiries for IP addresses. Either the name and ttl*/
/* are returned or an IP address and ttl are returned. */
/* */
/* AUTHOR */
/* */
/* Glen Johnson, Accelerated Technology Inc. */
/* */
/* CALLED BY */
/* */
/* DNS_Find_Host_By_Name Given a name find a host. */
/* DNS_Find_Host_By_Addr Given an address find a host. */
/* */
/* CALLS */
/* */
/* NU_Deallocate_Memory Deallocate a block of memory. */
/* DNS_Build_Query Create a DNS query packet. */
/* DNS_Query Send the DNS query. */
/* DNS_Extract_Data Extract an IP address or name from the */
/* response. */
/* */
/* INPUTS */
/* */
/* name A host name. */
/* ip_addr The host IP address, filled in by this */
/* function. */
/* ttl Time To Live, length of time the host info. */
/* can be cached. Filled in here. */
/* type Type of query, name or address. */
/* */
/* OUTPUTS */
/* */
/* NU_SUCCESS Indicates successful operation. */
/* a negative value Indicates failure. */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* Glen Johnson 06/16/97 Created initial version for NET 3.1 */
/* */
/******************************************************************************/
STATUS DNS_Resolve(CHAR *name, CHAR *ip_addr, UNSIGNED *ttl, INT type)
{
STATUS stat;
CHAR *buffer;
int16 q_size;
/* Is there a DNS Server. */
if ( !(*(uint32 *)DNS_Server) )
{
/* SGJ: Make this a more meanigful return value. */
return (-1);
}
/* Build the DNS query. */
switch (type)
{
case DNS_TYPE_A :
if ((q_size = DNS_Build_Query(name, (VOID **)&buffer, type)) < 0)
return -1;
break;
case DNS_TYPE_PTR :
if ((q_size = DNS_Build_Query(ip_addr, (VOID **)&buffer, type)) < 0)
return -1;
break;
default:
return -1;
}
/* Query the DNS server. Upon returning the buffer will contain the
response to our query. */
if (DNS_Query(buffer, q_size) < 0)
{
NU_Deallocate_Memory(buffer);
return -1;
}
/* Now process the response. */
switch (type)
{
case DNS_TYPE_A :
stat = DNS_Extract_Data ((DNS_PKT_HEADER *)buffer, ip_addr, ttl,
type);
break;
case DNS_TYPE_PTR :
stat = DNS_Extract_Data ((DNS_PKT_HEADER *)buffer, name, ttl, type);
break;
default:
return -1;
}
/* Deallocate the memory buffer. */
NU_Deallocate_Memory(buffer);
if (stat != NU_SUCCESS)
return (NU_DNS_ERROR);
else
return (stat);
} /* DNS_Resolve */
/******************************************************************************/
/* FUNCTION */
/* */
/* DNS_Build_Query */
/* */
/* DESCRIPTION */
/* */
/* This function will build a DNS query packet. */
/* */
/* AUTHOR */
/* */
/* Glen Johnson, Accelerated Technology Inc. */
/* */
/* CALLED BY */
/* */
/* DNS_Resolve Resolve a host IP address. */
/* */
/* CALLS */
/* */
/* NU_Allocate_Memory Allocate memory. */
/* DNS_Pack_Domain_Name Convert a host name into format expected by */
/* a DNS server. */
/* DNS_Addr_To_String Convert an IP address into a character string.*/
/* intswap Swap the bytes in an integer. */
/* */
/* INPUTS */
/* */
/* name A host name. */
/* buffer A pointer to a buffer pointer. */
/* type A query type. */
/* */
/* OUTPUTS */
/* */
/* > 0 The size of the packed name. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?