📄 dns.c
字号:
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,
(UNSIGNED)(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(&DNS_Hosts, 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 *= SCK_Ticks_Per_Second;
/* 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.
*
* INPUTS
*
* addr The address of to convert.
* new_name The address in ascii format.
*
* OUTPUTS
*
* NU_SUCCESS Indicates success.
*
******************************************************************************/
STATUS DNS_Addr_To_String(CHAR *addr, CHAR *new_name)
{
INT i;
CHAR *ptr = new_name;
UINT8 *a = (UINT8 *)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. */
NU_ITOA(((INT)((UINT8)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 */
/****************************************************************************
* FUNCTION
*
* NU_Add_DNS_Server
*
* DESCRIPTION
*
* Add a new DNS server to the list of DNS servers to use.
*
* INPUTS
*
* new_dns_server The IP address of the DNS server to add.
* where
*
* OUTPUTS
*
* NU_SUCCESS Indicates successful operation.
* NU_QUEUE_FULL Indicates the DNS server list is full.
* NU_INVALID_PARM Indicates that one of the parameters was a
* NULL pointer.
*
******************************************************************************/
STATUS NU_Add_DNS_Server (UINT8 *new_dns_server, INT where)
{
DNS_SERVER *current_server, *temp;
STATUS ret_status;
UINT32 server_ip;
NU_SUPERV_USER_VARIABLES
server_ip = IP_ADDR(new_dns_server);
/* If this is not a class A, B, or C address, or if it is 0 then it is not
a valid IP address. */
if ( (!IP_CLASSA_ADDR(server_ip)
&& !IP_CLASSB_ADDR(server_ip)
&& !IP_CLASSC_ADDR(server_ip))
|| server_ip == 0)
{
ret_status = NU_INVALID_PARM;
}
else if ((where != DNS_ADD_TO_FRONT) && (where != DNS_ADD_TO_END))
{
ret_status = NU_INVALID_PARM;
}
/* Check the last entry on the list. If there is at least one unused
entry on the list it will be at the end. */
else if (*(UINT32 *)DNS_Servers.dnss_tail->dnss_ip != 0)
{
ret_status = NU_QUEUE_FULL;
}
else
{
/* Switch to supervisor mode. */
NU_SUPERVISOR_MODE();
/* Remove the last one from the list. After initializing it we will
put it back in its new location. */
current_server = DNS_Servers.dnss_tail;
DLL_Remove(&DNS_Servers, current_server);
/* Initialize the DNS server IP address. */
((UINT32 *)current_server->dnss_ip)[0] = server_ip;
if (where == DNS_ADD_TO_FRONT)
{
/* Insert the current server at the front of the list. */
DLL_Insert(&DNS_Servers, current_server, DNS_Servers.dnss_head);
}
else
{
/* Walk the list until either the last node in the list is reached
or the last node that is in use is reached, whichever occurs
first. */
for ( temp = DNS_Servers.dnss_head;
temp && (*(UINT32 *)temp->dnss_ip != 0);
temp = temp->dnss_next);
/* If all of the nodes in the list are in use. Enqueue this at the
end. Else insert it into the proper position. */
if (temp == NU_NULL)
DLL_Enqueue(&DNS_Servers, current_server);
else
DLL_Insert(&DNS_Servers, current_server, temp);
}
/* Switch back to user mode. */
NU_USER_MODE();
ret_status = NU_SUCCESS;
}
/* Return */
return (ret_status);
} /* NU_Add_DNS_Server */
/****************************************************************************
* FUNCTION
*
* NU_Delete_DNS_Server
*
* DESCRIPTION
*
*
* INPUTS
*
* dns_ip
*
* OUTPUTS
*
* NU_SUCCESS
* NU_INVALID_PARM
*
******************************************************************************/
STATUS NU_Delete_DNS_Server ( UINT8 *dns_ip )
{
UINT32 server;
DNS_SERVER *temp;
STATUS ret_status;
NU_SUPERV_USER_VARIABLES
server = IP_ADDR(dns_ip);
for ( temp = DNS_Servers.dnss_head;
temp && (*(UINT32 *)temp->dnss_ip != server);
temp = temp->dnss_next);
if (!temp)
{
ret_status = NU_INVALID_PARM;
}
else
{
/* Switch to supervisor mode. */
NU_SUPERVISOR_MODE();
/* Remove the server from the list. */
DLL_Remove(&DNS_Servers, temp);
/* Clear the IP address. */
*(UINT32 *)temp->dnss_ip = 0;
/* Add it back to the end of the list from which it can be reused. */
DLL_Enqueue(&DNS_Servers, temp);
/* Switch back to user mode. */
NU_USER_MODE();
ret_status = NU_SUCCESS;
}
return ret_status;
} /* NU_Delete_DNS_Server */
/****************************************************************************
* FUNCTION
*
* NU_Get_DNS_Servers
*
* DESCRIPTION
*
*
* INPUTS
*
* dest
* size
*
* OUTPUTS
*
* NU_SUCCESS Indicates success.
* NU_INVALID_PARM
* INT Index into array of IP addresses.
*
******************************************************************************/
INT NU_Get_DNS_Servers(UINT8 *dest, INT size)
{
/* The memory area provided by the application will be treated as an array
of 32 bit integers (IP addresses). */
UINT32 *dns_array = (UINT32 *)dest;
INT ret_status;
DNS_SERVER *srv_ptr;
INT i;
NU_SUPERV_USER_VARIABLES
if ( (size < IP_ADDR_LEN) || (dns_array == NU_NULL))
{
ret_status = NU_INVALID_PARM;
}
else
{
/* Switch to supervisor mode. */
NU_SUPERVISOR_MODE();
for ( srv_ptr = DNS_Servers.dnss_head, i = 0;
srv_ptr && *(UINT32 *)srv_ptr->dnss_ip != 0 && size >= IP_ADDR_LEN;
srv_ptr = srv_ptr->dnss_next, size -= IP_ADDR_LEN, i++)
{
dns_array[i] = IP_ADDR(srv_ptr->dnss_ip);
}
/* Switch back to user mode. */
NU_USER_MODE();
ret_status = i;
}
/* return pointer to beginning of arrays */
return (ret_status);
} /* NU_Get_DNS_Servers */
#endif /* INCLUDE_DNS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -