📄 dns_client_lib6.c
字号:
/* * DNS Resolver * IPv6 Resolver API * * Author: Pavel A. Bolokhov <Pavel.Bolokhov@oktet.ru> * * @(#) dns_client_lib6.c,v 1.3 2005/12/19 13:45:25 sachinj Exp * * External API: * dns_getipnodebyname() * dns_getipnodebyaddr() * dns_client_get_nameserver6() * dns_client_flush_cachebyname6() * dns_client_flush_cachebyaddr() * dns_alloc_struct_hostent() * dns_alloc_hostname() * dns_alloc_aliases() * dns_alloc_addrlist() * dns_dns_hostent_to_hostent() */#include "config.h"#include "atypes.h"#include "kernel.h"#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include "dns_resolv.h"#include "errno.h"#include "messages.h"#include "dns_resolv.h"#include "dns_client.h"#include "dns_client_util6.h"#include "dns_client_resource.h"/* dns_getipnodebyname -- * This routine constructs and sends either a DNS_GET_NODEBYNAME or * DNS_GET_ADDRBYNAME message to the dns_client. If 'msg' is NULL * then this is a blocking call, otherwise non-blocking. * a blocking call for IPv4 addresses calls get_hostby() functions, * whereas blocking call for IPv6 addresses calls the same * function with 'msg' set to some non-nil value. * a non-blocking call for IPv4 addresses invokes get_u_hostbyname(), * whereas a non-blocking call for IPv6 addresses directly sends a * message to dns_client. * * Note, non-blocking call for IPv4 address resolution assumes 'hptr' * field set in the 'msg' message. * * PARAMETERS * hostname - domain name to resolve * family - protocol family (AF_INET or AF_INET6) * p_err - error values * msg - pointer to ATMOS_MESSAGE structure or NULL, see above * * RETURNS * For blocking calls: * On success, pointer to dynamically allocated hostent structure * which must be released later by user via call of * 'dns_freehostent()'. * On failure, returns NULL, and 'err' set to one of error values * described below. * For non-blocking calls: * always NULL. on success 'err' is set to ESUCCESS, otherwise * err is one of the described below error values. * * Error values: * EINVAL bad address family * ENOMEM not enough memory * EHOSTNOTFOUND specified host is unknown * ENOADDRESS name is valid but does not have an IP address * ENORECOVERY non-recoverable name server error occured * ETRYAGAIN temporary error occured - try again later * ENONSADDRESS no nameserver address provided, configuration error * ENOSEARCHLIST no search list provided * (when using relative hostname) */struct hostent *dns_getipnodebyname(IN const char *hostname, IN int family, IN int *p_err, IN ATMOS_MESSAGE *p_msg){ DNSC_DEBUG("entry\n"); if (p_msg == NULL) /* blocking call */ { DNSC_DEBUG("blocking call\n"); switch (family) { case AF_INET: { struct dns_hostent dns_hent; struct hostent *p_hent; DNSC_DEBUG("AF_INET query\n"); memset(&dns_hent, 0, sizeof(dns_hent)); dns_hent.h_addr_list = calloc(MAXIPENTRYS, sizeof(U32)); dns_hent.h_numaddrs = MAXIPENTRYS; *p_err = get_hostbyname(hostname, TRUE, &dns_hent); if (*p_err != ESUCCESS) { DNSC_DEBUG("exit blocking - error %d\n", *p_err); return NULL; } /* Convert 'dns_hostent' to 'hostent' */ p_hent = dns_dns_hostent_to_hostent(&dns_hent); if (p_hent == NULL) { *p_err = ENOMEM; DNSC_DEBUG("conversation failed - no memory\n"); } DNSC_DEBUG("exit blocking - error %d\n", *p_err); return p_hent; } /* case AF_INET */ case AF_INET6: { struct hostent *p_hent; ATMOS_MESSAGE local_msg; MSG_D_DNS_GET_NODEBYNAME(p_dns_msg, &local_msg); DNSC_DEBUG("AF_INET6 query\n"); /* Non-blocking call must always return NULL */ p_hent = dns_getipnodebyname(hostname, family, p_err, &local_msg); if (p_hent == NULL) { if (*p_err == ESUCCESS) { awaitspecificmessage(&local_msg); *p_err = p_dns_msg->error; DNSC_DEBUG("exit blocking - error %d\n", *p_err); return p_dns_msg->hent; } else { DNSC_DEBUG("exit blocking - failure %d\n", *p_err); return NULL; } } else { ASSERT(FALSE); } break; } /* case AF_INET6 */ default: break; } /* switch (family) */ } /* blocking call */ else { /* non-blocking call */ DNSC_DEBUG("non-blocking call\n"); switch (family) { case AF_INET: { MSG_D_DNS_GET_ADDRBYNAME(p_dns_msg, p_msg); DNSC_DEBUG("AF_INET query\n"); *p_err = get_u_hostbyname(hostname, TRUE, p_dns_msg->hptr, p_msg); DNSC_DEBUG("exit non-blocking - error %d\n", *p_err); return NULL; } /* case AF_INET */ case AF_INET6: { ATMOS_MQID qid; MSG_D_DNS_GET_NODEBYNAME(p_dns_msg, p_msg); DNSC_DEBUG("AF_INET6 query\n"); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { DNSC_TRACE("could not acquire the %s queue " "handle\n", DNSCLIENT_QUEUE_NAME); *p_err = ESRCH; DNSC_DEBUG("exit non-blocking - ESRCH\n"); return NULL; } DNSC_DEBUG("Send message MSG_N_DNS_GET_NODEBYNAME\n"); p_dns_msg->qcnt = 0; p_dns_msg->hostname = (char *)hostname; p_dns_msg->error = ESUCCESS; p_dns_msg->hent = NULL; *p_err = sendmessage(p_msg, MSG_N_DNS_GET_NODEBYNAME, qid); DNSC_DEBUG("exit non-blocking - error %d\n", *p_err); return NULL; } /* case AF_INET6 */ default: break; } /* switch (family) */ } /* non-blocking call */ /* invalid family supplied */ *p_err = EINVAL; DNSC_DEBUG("exit - EINVAL\n"); return NULL;} /* dns_getipnodebyname *//* dns_getipnodebyaddr -- * This routine constructs and sends either a DNS_GET_HOSTBYADDR6 or * message to the dns_client. If 'msg' is NULL then * this is a blocking call, otherwise non-blocking. * * PARAMETERS * addr - address to resolve * p_err - location of error value * msg - pointer to ATMOS_MESSAGE structure or NULL, see above * * RETURNS * For blocking calls: * On success, pointer to dynamically allocated hostent structure * which must be released later by user via call of * 'dns_freehostent()'. * On failure, returns NULL, and '*p_err' set to one of error * values described below. * For non-blocking calls: * always NULL. on success '*p_err' is set to ESUCCESS, otherwise * err is one of the described below error values. * * Error values: * EINVAL bad address family * ENOMEM not enough memory * EHOSTNOTFOUND specified host is unknown * ENOADDRESS name is valid but does not have an IP address * ENORECOVERY non-recoverable name server error occured * ETRYAGAIN temporary error occured - try again later * ENONSADDRESS no nameserver address provided, configuration error * ENOSEARCHLIST no search list provided * (when using relative hostname) */struct hostent *dns_getipnodebyaddr(IN const struct sockaddr *addr, OUT int *p_err, IN ATMOS_MESSAGE *p_msg){ DNSC_DEBUG("entry\n"); if (p_msg == NULL) /* blocking call */ { switch (addr->sa_family) { case AF_INET: { struct dns_hostent dns_hent; struct hostent *p_hent; DNSC_DEBUG("AF_INET query\n"); memset(&dns_hent, 0, sizeof(dns_hent)); dns_hent.h_addr_list = calloc(MAXIPENTRYS, sizeof(U32)); dns_hent.h_numaddrs = MAXIPENTRYS; *p_err = get_hostbyaddr( ((struct sockaddr_in *)addr)->sin_addr.s_addr, TRUE, &dns_hent); if (*p_err != ESUCCESS) { DNSC_DEBUG("exit blocking - get_hostbyaddr failed\n"); return NULL; } /* Convert 'dns_hostent' to 'hostent' */ p_hent = dns_dns_hostent_to_hostent(&dns_hent); if (p_hent == NULL) { *p_err = ENOMEM; DNSC_DEBUG("conversation failed - no memory\n"); } DNSC_DEBUG("exit blocking - error %d\n", *p_err); return p_hent; } /* case AF_INET */ case AF_INET6: { struct hostent *p_hent; ATMOS_MESSAGE local_msg; MSG_D_DNS_GET_HOSTBYADDR6(p_dns_msg, &local_msg); DNSC_DEBUG("AF_INET6 query\n"); p_hent = dns_getipnodebyaddr(addr, p_err, &local_msg); if (p_hent == NULL) { if (*p_err == ESUCCESS) { awaitspecificmessage(&local_msg); *p_err = p_dns_msg->error; DNSC_DEBUG("exit blocking - error %d\n", *p_err); return p_dns_msg->hent; } else { DNSC_DEBUG("exit blocking - failure %d\n", *p_err); return NULL; } } else { ASSERT(FALSE); } break; } /* case AF_INET6 */ default: break; } /* switch (family) */ } /* blocking call */ else { /* non-blocking call */ switch (addr->sa_family) { case AF_INET: { MSG_D_DNS_GET_HOSTBYADDR(p_dns_msg, p_msg); DNSC_DEBUG("AF_INET query\n"); *p_err = get_u_hostbyaddr( ((struct sockaddr_in *)addr)->sin_addr.s_addr, TRUE, p_dns_msg->hptr, p_msg); DNSC_DEBUG("exit non-blocking - error %d\n", *p_err); return NULL; } /* case AF_INET */ case AF_INET6: { ATMOS_MQID qid; MSG_D_DNS_GET_HOSTBYADDR6(p_dns_msg, p_msg); DNSC_DEBUG("AF_INET6 query\n"); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { DNSC_TRACE("could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); *p_err = ESRCH; DNSC_DEBUG("exit non-blocking - ESRCH\n"); return NULL; } DNSC_DEBUG("Send message MSG_N_DNS_GET_HOSTBYADDR6\n"); p_dns_msg->qcnt = 0; p_dns_msg->addr = &((struct sockaddr_in6 *)addr)->sin6_addr; p_dns_msg->error = ESUCCESS; p_dns_msg->hent = NULL; *p_err = sendmessage(p_msg, MSG_N_DNS_GET_HOSTBYADDR6, qid); DNSC_DEBUG("exit non-blocking - error %d\n", *p_err); return NULL; } /* case AF_INET6 */ default: break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -