📄 dns_client.c
字号:
if ((cnt = atoi(num)) > MAXSECTVAL) return -1; return 0;} /* end dns_ipsection_test() *//* * Following routines support the MAPI access methods. * They allow encapsulation of the DNS client internals * in this module. *//* * add_nameserver() * * this routine adds an IP nameserver to the existing list. * The reason it isn't a wrapper around cmd_ns_list() is * because that doesn't support error codes. * * return: ESUCCESS, or * EINVAL (parameter missing), * EFAULT (bad IP address), * ENOSPC (nameserver list full), * EEXIST (nameserver already in list), * E2BIG (only one parameter allowed), */intadd_nameserver(const char *ipaddr){ char *tmp, *tok; struct sockaddr_storage ip; int i, rc = ESUCCESS; DNSC_DEBUG("entry - %s\n", ipaddr); /* * The incoming string is supposed to be a dotted-decimal * IP address. */ tmp = string_cleanup(ipaddr); if ( 0 == strlen(tmp) ) { rc = EINVAL; } else { /* * The command string only has one argument, it should be an IP * address. we verify length and format, and then make sure there's * still room to add another nameserver. if not return error. */ if (NULL == (tok = strchr(tmp, ' '))) { /* only one argument */ if (ipstring2sockaddr(tmp, &ip) == NULL) { rc = EFAULT; } else if ( MAXNS <= dnsdomain.num_svr ) { rc = ENOSPC; } else { DNSC_DEBUG("Address to add: %s\n", dns_client_inetaddr(&ip)); for ( i = 0; i < dnsdomain.num_svr; i++ ) { if (SOCKEQUAL(&dnsdomain.nsaddr[i], &ip)) rc = EEXIST; } if (ESUCCESS == rc) { memcpy(&dnsdomain.nsaddr[dnsdomain.num_svr++], &ip, sizeof(ip)); } } } else { rc = E2BIG; } } DNSC_DEBUG("exit - error %d\n", rc); return rc;} /* end add_nameserver() *//* * delete_nameserver() * * this routine removes an IP nameserver from the existing list. * The reason it isn't a wrapper around cmd_ns_list() is * because that doesn't support error codes. * * return: ESUCCESS, or * EINVAL (parameter missing), * EFAULT (bad IP address), * E2BIG (only one parameter allowed), */intdelete_nameserver(const char *ipaddr){ char *tmp, *tok; struct sockaddr_storage ip; int rc = ESUCCESS; DNSC_DEBUG("entry - %s\n", ipaddr); /* * The incoming string is supposed to be a dotted-decimal * IPv4 address or an IPv6 address */ tmp = string_cleanup(ipaddr); if ( 0 == strlen(tmp) ) { rc = EINVAL; } else { /* * The command string only has one argument, it should be an IP * address. we verify length and format, and then yank it from the list. */ if (NULL == (tok = strchr(tmp, ' '))) { /* only one argument */ if (ipstring2sockaddr(tmp, &ip) == NULL) { rc = EFAULT; } else { ns_list_delete(&ip); } } else { rc = E2BIG; } } DNSC_DEBUG("exit - error %d\n", rc); return rc;} /* end delete_nameserver() *//* * new_searchlist() * * this routine updates the search list. * The reason it isn't a wrapper around cmd_search_list() is * because that doesn't support error codes. * * return: ESUCCESS, or * EINVAL (parameter missing), * EFAULT (bad IP address), * ENOSPC (nameserver list full), * EEXIST (nameserver already in list), * E2BIG (only one parameter allowed), */intnew_searchlist(const char *liststr){ char *tmp, *tok; char list[MAXDNSRCH][NAMEMAX+1]; int i, cnt = 0, rc = ESUCCESS; tmp = string_cleanup(liststr); if ( 0 == strlen(tmp) ) { rc = EINVAL; } else { /* * retrieve the names from the passed string and create a * search list. */ do { /* * find last input token */ if (NULL == (tok = strchr(tmp, ' '))) { /* * is this the last argument? */ if ( strlen(tmp) == 0 ) break; /* * make sure name is valid */ if ( dns_validate_hostname(tmp) < 0 ) { rc = EFAULT; break; } /* * make sure this string is not already in the list */ if ( !in_current_list(list, tmp, cnt) ) strcpy(list[cnt++], tmp); break; } *tok++ = '\0'; while ( *tok == ' ' && *tok != '\0' ) tok++; /* * make sure name is valid */ if ( dns_validate_hostname(tmp) < 0 ) { rc = EFAULT; tmp = tok; continue; } /* * make sure this string is not already in the list */ if ( !in_current_list(list, tmp, cnt) ) strcpy(list[cnt++], tmp); tmp = tok; } while ( tmp != NULL && cnt < MAXDNSRCH ); /* * if we got anything, copy it to the local configuration */ if ( cnt > 0 ) { for ( i = 0; i < cnt; i++ ) strcpy(dnsdomain.search[i], list[i]); dnsdomain.num_search = cnt; } } return rc;} /* end new_searchlist() *//* * 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 intget_nameserver6(IN int index, OUT char *nsAddrStr){ int rc = ESUCCESS; char ipstr[INET6_ADDRSTRLEN]; /* * is the requested entry in range? */ if (index < dnsdomain.num_svr) { if (dnsdomain.nsaddr[index].__ss_family != AF_INET6) return EINVAL; ip_string(&dnsdomain.nsaddr[index], ipstr, sizeof(ipstr)); sprintf(nsAddrStr, "%s", ipstr); } else { rc = EINVAL; } return rc;} /* end get_nameserver6() *//* * get_nameserver() * * this routine retrieves an IP nameserver from the existing list. * The reason it isn't a wrapper around cmd_show() is * because that doesn't support error codes. * * pab note: this routine will return only a IPv4 nameserver * * return: ESUCCESS (nameserver in string buffer), or * EINVAL (nameserver not found) */intget_nameserver(int index, char *nsaddr_str){ int rc = ESUCCESS; char ipstr[INET6_ADDRSTRLEN]; /* * is the requested entry in range? */ if (index < dnsdomain.num_svr) { if (dnsdomain.nsaddr[index].__ss_family != AF_INET) return EINVAL; ip_string(&dnsdomain.nsaddr[index], ipstr, sizeof(ipstr)); sprintf(nsaddr_str, "%s", ipstr); } else { rc = EINVAL; } return rc;} /* end get_nameserver() *//* * get_searchlist_entry() * * this routine retrieves an searchlist entry from the existing list. * The reason it isn't a wrapper around cmd_show() is * because that doesn't support error codes. * * return: ESUCCESS (searchlist entry in string buffer), or * EINVAL (searchlist entry not found) */intget_searchlist_entry(int index, char *slent_str){ int rc = ESUCCESS; /* * is the requested entry in range? */ if (index < dnsdomain.num_search) { sprintf(slent_str, "%s", dnsdomain.search[index]); } else { rc = EINVAL; } return rc;} /* end get_searchlist_entry() *//* * init_dns_resources() * * this routine will initialize the resources used by the client. it will make * the calls to set up the UDP socket and our cache. it also calls out to initialize * the resources used for DNS packet parsing and creation. * * return: 0 for success and, * -1 to indicate an error condition. */static intinit_dns_resources(DnsDomain *dm){ /* initialize the cache */ dns_cache_init(); dns_XX_cache_init(); dm->timeout = RES_TIMEOUT; dm->retry = RES_RETRY; init_pkt_resources(); return 0;} /* end init_dns_resources() *//* * cmd_XX_cache() * * this routine will handle the XX cache command. this command allow the user * to "show" what's in the cache, or "flush" elements of the cache. see the * functional spec for more details. * */static BOOLcmd_XX_cache(PTR context, const char *cmdstr){ char *tmp, *tok; UNUSED(context); tmp = string_cleanup(cmdstr); /* if the string is empty just print the help string */ if ( 0 == strlen(tmp) ) { printf("%s\n", XXCACHESTR); return FALSE; } /* * the command requires at least one argument, see what that is. if no * space then there's only one argument and that has to be "show" or the * command is invalid. if there is more than one argument, then "show" * is not valid. */ if ((tok = strchr(tmp, ' ')) == NULL) { if ( 0 == strcmp(tmp, "show") ) dns_show_resolver_XX_cache(); else printf("%s\n", XXCACHESTR); return FALSE; } *tok++ = '\0'; if ( 0 == strcmp(tmp, "show") ) { printf("%s\n", XXCACHESTR); return FALSE; } /* see if the command was "flush". if so we expect another argument. * if the argument is all the entire cache is flushed, if not the argument * is a domain name */ if ( 0 == strcmp(tmp, "flush") ) { if ((tmp = strchr(tok, ' ')) != NULL) { printf("too many arguments to flush command\n"); return FALSE; } if ( 0 == strcmp(tok, "all") ) dns_flush_resolver_XX_cache(); else { dns_flush_resolver_XX_cache_byname(tok); } } else printf("%s\n", XXCACHESTR); return FALSE;} /* end cmd_XX_cache() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -