⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dns_client.c

📁 DNS 的实现代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* ############################################################################ (c) Copyright Virata Limited 2001## Virata Limited Confidential and Proprietary## The following software source code ("Software") is strictly confidential and# is proprietary to Virata Limited ("Virata").  It may only be read, used,# copied, adapted, modified or otherwise dealt with by you if you have# entered into a confidentiality agreement with Virata and then subject to the# terms of that confidentiality agreement and any other applicable agreement# between you and Virata.  If you are in any doubt as to whether you are# entitled to access, read, use, copy, adapt, modify or otherwise deal with# the Software or whether you are entitled to disclose the Software to any# other person you should contact Virata.  If you have not entered into a# confidentiality agreement with Virata granting access to this Software you# should forthwith return all media, copies and printed listings containing# the Software to Virata.## Virata reserves the right to take legal action against you should you breach# the above provisions.## If you are unsure, or to report violations, please contact# support@virata.com# ##########################################################################*//* * file:  dns_client.c * * description:  this file contains the main() for the dns client process. *               it handles all incoming messages.  also included are routines *               to handle TELL commands and the configuration file. *//* * List of New Routines: *     ipstring2sockaddr() *     get_nameserver6() *     cmd_nslookup6() */#include "config.h"#include "atypes.h"#include "kernel.h"#include "messages.h"#include "dns_resolv.h"#include "dns_client_query.h"#include "dns_client_resource.h"#include "dns_client_cache.h"#include "netlib.h"#include "cyanlib.h"#include <errno.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#include <command.h>#include <timelib.h>#include <netdb.h>#include <dns_client_statics.h>#include "dns_client_util6.h"#define NSLISTSTR      "nameserver [ip_address]|delete <ipaddress>"#define SEARCHLISTSTR  "search <domain_1 domain_2 . . . domain_n>"#define DISPLAYSTR     "show domain information"#define NSLOOKUPSTR    "nslookup argument (hostname or IP address)"#define NSLOOKUP6STR    "nslookup6 argument (hostname or IPv6 address)"#define DNSCACHELIST   "lists all the elements of the cache"#define FLUSHSTR       "flush_cache all|[argument]"#define CACHESTR       "cache [flush [all]|[argument]]|[show]"#define XXCACHESTR     "xx_cache [flush [all]|[argument]]|[show]"#ifdef DNS_DEBUG#define DNSRESOURCESTR "show DNS Client resource stats"#endif#define MAXIPLEN       16#define MAXIPSECTION   3#define MAXSECTVAL     255#define MAXIPSTRLEN    15#ifdef DNS_DEBUGextern void print_resource_pool(void);extern char *dns_client_inetaddr(const struct sockaddr_storage *);#endifstatic void dns_message_handler(ATMOS_MESSAGE *pmsg);static BOOL cmd_ns_list(PTR, const char *);static BOOL cmd_search_list(PTR, const char *);static BOOL cmd_show(PTR, const char *);static BOOL cmd_nslookup(PTR, const char *);static BOOL cmd_cache(PTR, const char *);static BOOL cmd_XX_cache(PTR, const char *);#ifdef DNS_DEBUGstatic BOOL cmd_pool_stats(PTR, const char *);static BOOL cmd_toggle(PTR, const char *);#endifstatic void config_reset(void);static void config_save(void);static void config_print(int);static char *string_cleanup(const char *);static void ns_list_delete(struct sockaddr_storage *);static int  validate_ipaddr_str(const char *) ;static int  dns_ipsection_test(const char *);static BOOL in_current_list(char [][], char *, int);static int  init_dns_resources(DnsDomain *);static DnsDomain  dnsdomain;    /* copy of DNS config information *//* * MAPI helper routines */static int   add_nameserver(const char *);static int   delete_nameserver(const char *);static int   new_searchlist(const char *);static int   get_nameserver(int, char *);static int   get_searchlist_entry(int, char *);U32  ipstring2long(const char *ipstr);/* * New Routines *//* * ipstring2sockaddr -- * *     Converts an IP in the form of a string to a sockaddr_storage. *     Here it is assumed an IPv6 address must contain a colon, while *     IPv4 address CANNOT contain a colon. * * PARAMETERS *     ipstr  - source string with address *     dest   - destination sockaddr_storage structure * * RETURNS *     dest  on success, *     NULL  on failure */static struct sockaddr_storage *ipstring2sockaddr(IN const char *ipStr, OUT struct sockaddr_storage *dest);/* * get_nameserver6 -- * *     This routine retrieves an IPv6 nameserver from the existing list. *     Note, this routine will return only a IPv6 nameserver. * * PARAMETERS *     index      - index of the nameserver in the list to return *     nsaddr_str - result name * * RETURNS  ESUCCESS (nameserver in string buffer), or *          EINVAL (nameserver not found) */static int get_nameserver6(IN int index, OUT char *nsAddrStr);/* * cmd_nslookup6 -- *     Performs standard or inverse DNS query for IPv6 address. *     If string parameter is a valid IPv6 address inverse *     name resolution is invoked. Otherwise, standard name *     resolution is invoked. * * PARAMETERS *     context - unused *     cmdStr  - command line string */static BOOL cmd_nslookup6(PTR context, const char *cmdStr);#ifdef DNS_DEBUGint  dns_verbosity = 5;         /* verbosity level, used for debugging only */#endif /* console commmands */static const CmdEntry console_cmds[] =    {        { 0, "nameserver",   cmd_ns_list,        NSLISTSTR },        { 0, "search",       cmd_search_list,    SEARCHLISTSTR },        { 0, "show",         cmd_show,           DISPLAYSTR },        { 0, "nslookup",     cmd_nslookup,       NSLOOKUPSTR },        { 0, "nslookup6",    cmd_nslookup6,      NSLOOKUP6STR },#ifdef DNS_DEBUG        { 0, "toggle",       cmd_toggle,        "toggle verbosity" },        { 0, "stats",        cmd_pool_stats,     DNSRESOURCESTR },#endif        { 0, "cache",        cmd_cache,          CACHESTR },        { 0, "xx_cache",     cmd_XX_cache,          XXCACHESTR }    };static int ncmds = sizeof(console_cmds) / sizeof(console_cmds[0]);/* functions called through confighandle */static ConfigFunctionTable config_cmds =    {        config_reset,        config_save,        config_print,    };int main(void){    if ( init_dns_resources(&dnsdomain) < 0 ) {  /* initialize our resource pools */        dprintf("%C error initializing DNS resources\n");        exit(1);    }#ifdef NO_ISFS_CONFIGURATION    WARN_NO_ISFS_CONFIG ("//isfs/initdnsclient");#else    /* read saved configuration */    InterpretProcessCommands(0, NULL, console_cmds, ncmds, FALSE,                             atmos_pcb_current_get_name());#endif    /* register to receive config messages */    ConfigRegister();    /* register this process with the console, and offer help */    console_register_process(atmos_pcb_current_get_name(), TRUE);        /* Register socket handler for current process */    if (socket_register_message_handler(dns_message_handler) != ESUCCESS)    {        kprintf("%C: Failed to register socket handler\n");                atmos_exitprocess();    }    /* main message loop */        while ( 1 ) { /* forever loop */                /* Wait for message */        ATMOS_MESSAGE *pmsg = awaitmessage();        /* Process received message */        dns_message_handler(pmsg);        /* Check and, if it's necessary, wait for network event */        dns_client_udp_receive();        /* We are not waiting for network event now */        } /* end forever loop */    atmos_exitprocess();}   /* end main() *//* dns_message_handler -- *     Handler of DNS messages. It should be called for each DNS  *     message. It's called from main() function, registered as  *     message handler in Socket API and may be called from  *     atmos_select() function to process DNS or unknown message. *  * PARAMETERS: *     pmsg - ATMOS message pointer *  * RETURNS: *     N/A */static voiddns_message_handler(ATMOS_MESSAGE *pmsg){#ifdef DNS_DEBUG    foreground_output_begin();#endif    switch(pmsg->code) {        case MSG_N_TELL: {                        MSG_D_TELL(ptell, pmsg);            DNSC_DEBUG("MSG_D_TELL\n");            foreground_output_begin();            InterpretCommand(0, NULL, console_cmds, ncmds, FALSE, ptell->cmd);            foreground_output_end();            sendreply(pmsg);            break;        }        case MSG_N_CONFIG_REQUEST:            DNSC_DEBUG("MSG_N_CONFIG_REQUEST\n");            foreground_output_begin();            ConfigRequest(pmsg, &config_cmds);            foreground_output_end();            break;        case MSG_N_DNS_GET_NODEBYNAME:            DNSC_DEBUG("MSG_N_DNS_GET_NODEBYNAME\n");            dns_getnodebyname(pmsg, &dnsdomain);            break;        case MSG_N_DNS_GET_HOSTBYADDR6:            DNSC_DEBUG("MSG_N_DNS_GET_HOSTBYADDR6\n");            dns_gethostbyaddr6(pmsg, &dnsdomain);  /* reply sent to sender when */            break;                                 /* the DNS results become available */        case MSG_N_DNS_GET_ADDRBYNAME:            DNSC_DEBUG("MSG_N_DNS_GET_ADDRBYNAME\n");            dns_gethostbyname(pmsg, &dnsdomain);  /* reply sent to sender when */            break;                                /* the DNS results become available */        case MSG_N_DNS_GET_HOSTBYADDR:            DNSC_DEBUG("MSG_N_DNS_GET_HOSTBYADDR\n");            dns_gethostbyaddr(pmsg, &dnsdomain);  /* reply sent to sender when */            break;                                /* the DNS results become available */        case MSG_N_DNS_FLUSH_CACHE: {            MSG_D_DNS_FLUSH_CACHE(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_FLUSH_CACHE\n");            dns_flush_resolver_cache();            dmsg->error = ESUCCESS;            sendreply(pmsg);            break;        }        case MSG_N_DNS_FLUSH_CACHE_NAME6: {            MSG_D_DNS_FLUSH_CACHE_NAME6(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_FLUSH_CACHE_NAME6\n");            dns_flush_resolver_entry6(dmsg->hostname);            dmsg->error = ESUCCESS;            sendreply(pmsg);            break;        }        case MSG_N_DNS_FLUSH_CACHE_NAME: {            MSG_D_DNS_FLUSH_CACHE_NAME(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_FLUSH_CACHE_NAME\n");            dns_flush_resolver_entry(dmsg->hostname);            dmsg->error = ESUCCESS;            sendreply(pmsg);            break;        }        case MSG_N_DNS_FLUSH_CACHE_IP6: {            MSG_D_DNS_FLUSH_CACHE_IP6(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_FLUSH_CACHE_IP6\n");            dns_flush_resolver_entry_ip6(dmsg->ip6addr);            dmsg->error = ESUCCESS;            sendreply(pmsg);            break;        }        case MSG_N_DNS_FLUSH_CACHE_IP: {            MSG_D_DNS_FLUSH_CACHE_IP(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_FLUSH_CACHE_IP\n");            dns_flush_resolver_entry_ip(dmsg->ipaddr);            dmsg->error = ESUCCESS;            sendreply(pmsg);            break;        }        case MSG_N_DNS_ADD_NAMESERVER: {            MSG_D_DNS_ADD_NAMESERVER(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_ADD_NAMESERVER\n");            dmsg->error = add_nameserver(dmsg->ipaddr);            sendreply(pmsg);            break;        }        case MSG_N_DNS_DELETE_NAMESERVER: {            MSG_D_DNS_DELETE_NAMESERVER(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_DELETE_NAMESERVER\n");            dmsg->error = delete_nameserver(dmsg->ipaddr);            sendreply(pmsg);            break;        }        case MSG_N_DNS_NEW_SEARCHLIST: {            MSG_D_DNS_NEW_SEARCHLIST(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_NEW_SEARCHLIST\n");            dmsg->error = new_searchlist(dmsg->liststr);            sendreply(pmsg);            break;        }        case MSG_N_DNS_GET_NAMESERVER6: {            MSG_D_DNS_GET_NAMESERVER6(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_GET_NAMESERVER6\n");            dmsg->error = get_nameserver6(dmsg->index, dmsg->nsaddr_str);            sendreply(pmsg);            break;        }        case MSG_N_DNS_GET_NAMESERVER: {            MSG_D_DNS_GET_NAMESERVER(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_GET_NAMESERVER\n");            dmsg->error = get_nameserver(dmsg->index, dmsg->nsaddr_str);            sendreply(pmsg);            break;        }        case MSG_N_DNS_GET_SEARCHLIST_ENTRY: {            MSG_D_DNS_GET_SEARCHLIST_ENTRY(dmsg, pmsg);            DNSC_DEBUG("MSG_N_DNS_GET_SEARCHLIST_ENTRY\n");            dmsg->error = get_searchlist_entry(dmsg->index, dmsg->slent_str);            sendreply(pmsg);            break;        }        case MSG_R_DNS_GET_ADDRBYNAME:            DNSC_DEBUG("MSG_R_DNS_GET_ADDRBYNAME\n");            foreground_output_begin();            dns_host_query_reply(pmsg);            foreground_output_end();            break;        case MSG_R_DNS_GET_HOSTBYADDR:            DNSC_DEBUG("MSG_R_DNS_GET_HOSTBYADDR\n");            foreground_output_begin();            dns_addr_query_reply(pmsg);            foreground_output_end();            break;        case MSG_R_DNS_GET_NODEBYNAME:            DNSC_DEBUG("MSG_R_DNS_GET_NODEBYNAME\n");            foreground_output_begin();            dns_node_query_reply(pmsg);            foreground_output_end();            break;        case MSG_R_DNS_GET_HOSTBYADDR6:            DNSC_DEBUG("MSG_R_DNS_GET_HOSTBYADDR6\n");            foreground_output_begin();            dns_addr6_query_reply(pmsg);

⌨️ 快捷键说明

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