📄 dns_client_lib.c
字号:
/* ############################################################################ (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_lib.c * * description: this file contains the API to access the dns client * process. note that the relavate RFCs 1034 and 1035 * suggest retruning the try again error for most types * of errors and the these routines follow that suggestion. */#include "config.h"#include "atypes.h"#include "kernel.h"#include "messages.h"#include <stdio.h>#include "dns_resolv.h"#include "errno.h"#include <stdlib.h>#include "dns_client.h"/* * get_hostbyname() * * this routine constructs and sends a DNS_GET_ADDRBYNAME message to the * dns_client. we wait for a return message so the routine blocks until * the dns_client completes. * * return: ESUCCESS, or * error value from returned message. */intget_hostbyname(const char *hostname, BOOL use_cache, struct dns_hostent *hptr){ ATMOS_MESSAGE msg; MSG_D_DNS_GET_ADDRBYNAME(dmsg, &msg); if ( get_u_hostbyname(hostname, use_cache, hptr, &msg) == ESUCCESS ) { awaitspecificmessage(&msg); return dmsg->error; } else return ETRYAGAIN;} /* end get_hostbyname() *//* * get_hostbyaddr() * * this routine will construct and send the DNS_GET_HOSTBYADDR message and * wait for a reply. * * return: ESUCCESS, or * error value returned by message. */intget_hostbyaddr(U32 ipaddr, BOOL use_cache, struct dns_hostent *hptr){ ATMOS_MESSAGE msg; MSG_D_DNS_GET_HOSTBYADDR(dmsg, &msg); if ( get_u_hostbyaddr(ipaddr, use_cache, hptr, &msg) == ESUCCESS ) { awaitspecificmessage(&msg); return dmsg->error; } else return ETRYAGAIN;} /* end get_hostbyaddr() *//* * get_u_hostbyname() * * this routine will construct the DNS_GET_ADDRBYNAME message and send it using * sendmessage(). we first retrieve the dns_client queue handle using findqueue(). * NOTE: we return the standard ETRYAGAIN if not able to retrieve the queue handle. * this routine is non-blocking and expects the caller to handle the reply message. * * return: ESUCCESS or, * an error return from sendmessage(). */intget_u_hostbyname(const char *hostname,BOOL use_cache,struct dns_hostent *hptr,ATMOS_MESSAGE *msg){ ATMOS_MQID qid; MSG_D_DNS_GET_ADDRBYNAME(dmsg, msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->hostname = (char *)hostname; dmsg->use_cache = use_cache; dmsg->hptr = hptr; dmsg->error = ESUCCESS; dmsg->qcnt = 0; return sendmessage(msg, MSG_N_DNS_GET_ADDRBYNAME, qid);} /* end get_u_hostbyname() *//* * get_u_hostbyaddr() * * this routine will construct the DNS_GET_HOSTBYADDR message and send it. it does * this by getting the dns_client queue id using findqueue(). the routine does not * wait for the reply from the dns_client, the caller is expected to do so. * * return: ESUCCESS, or * an error return from sendmessage(). */intget_u_hostbyaddr(U32 ipaddr, BOOL use_cache, struct dns_hostent *hptr, ATMOS_MESSAGE *msg){ ATMOS_MQID qid; MSG_D_DNS_GET_HOSTBYADDR(dmsg, msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->ipaddr = ipaddr; dmsg->use_cache = use_cache; dmsg->hptr = hptr; dmsg->error = ESUCCESS; dmsg->qcnt = 0; return sendmessage(msg, MSG_N_DNS_GET_HOSTBYADDR, qid);} /* end get_u_hostbyaddr() *//* * dns_client_flush_cache() * * this routine is used to flush the resolver cache. it constructs the appropriate * message and waits for its return. */intdns_client_flush_cache(){ ATMOS_MQID qid; ATMOS_MESSAGE msg; int rc; MSG_D_DNS_FLUSH_CACHE(dmsg, &msg); dmsg->error = ESUCCESS; if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } if ((rc = sendmessage(&msg, MSG_N_DNS_FLUSH_CACHE, qid)) != ESUCCESS) { printf("%C error attempting to send flush cache message\n"); return rc; } awaitspecificmessage(&msg); return dmsg->error;} /* end dns_client_flush_cache() *//* * dns_client_flush_cachebyname() * * this routine works just as the flush cache routine but allows a user to * specify a cache entry by the host name. the hostname must match exactly, * it must be a complete name with the trailing '.' or it will not match * an entry in the cache. it is not considered an error if the hostname is * not found in the cache so no error would be reported. */intdns_client_flush_cachebyname(const char *hostname){ ATMOS_MQID qid; ATMOS_MESSAGE msg; int rc; MSG_D_DNS_FLUSH_CACHE_NAME(dmsg, &msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->error = ESUCCESS; dmsg->hostname = (char *)hostname; if ((rc = sendmessage(&msg, MSG_N_DNS_FLUSH_CACHE_NAME, qid)) != ESUCCESS) { printf("%C error attempting to send flush cache message\n"); return rc; } awaitspecificmessage(&msg); return dmsg->error;} /* end dns_client_flush_cachebyname() *//* * dns_client_flush_cachebyip() * * this routine also works as the flush cache routine but allows a user to * specify a cache entry by one of its IP addresses. */intdns_client_flush_cachebyip(U32 ipaddr){ ATMOS_MQID qid; ATMOS_MESSAGE msg; int rc; MSG_D_DNS_FLUSH_CACHE_IP(dmsg, &msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->error = ESUCCESS; dmsg->ipaddr = ipaddr; if ((rc = sendmessage(&msg, MSG_N_DNS_FLUSH_CACHE_IP, qid)) != ESUCCESS) { printf("%C error attempting to send flush cache message\n"); return rc; } awaitspecificmessage(&msg); return dmsg->error;} /* end dns_client_flush_cachebyip() *//* * Following methods added for MAPI support *//* * dns_client_add_nameserver() * * this routine adds an IP nameserver to the existing list. * It is an API analog to the 'nameserver' console command. * * configuration must be saved to make this data persistent. * * return: ESUCCESS, or * ESRCH (msg queue not found), * EINVAL (parameter missing), * EFAULT (bad IP address), * ENOSPC (nameserver list full), * EEXIST (nameserver already in list), * E2BIG (only one parameter allowed), */intdns_client_add_nameserver(const char *ipaddr){ ATMOS_MQID qid; ATMOS_MESSAGE msg; int rc; MSG_D_DNS_ADD_NAMESERVER(dmsg, &msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->error = ESUCCESS; dmsg->ipaddr = (char *)ipaddr; if ((rc = sendmessage(&msg, MSG_N_DNS_ADD_NAMESERVER, qid)) != ESUCCESS) { printf("%C error attempting to send add nameserver message\n"); return rc; } awaitspecificmessage(&msg); return dmsg->error;} /* end dns_client_add_nameserver() *//* * dns_client_delete_nameserver() * * this routine removes an IP nameserver from the existing list. * It is an API analog to the 'nameserver delete' console command. * * configuration must be saved to make this data persistent. * * return: ESUCCESS, or * ESRCH (msg queue not found), * EINVAL (parameter missing), * EFAULT (bad IP address), * E2BIG (only one parameter allowed), */intdns_client_delete_nameserver(const char *ipaddr){ ATMOS_MQID qid; ATMOS_MESSAGE msg; int rc; MSG_D_DNS_DELETE_NAMESERVER(dmsg, &msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->error = ESUCCESS; dmsg->ipaddr = (char *)ipaddr; if ((rc = sendmessage(&msg, MSG_N_DNS_DELETE_NAMESERVER, qid)) != ESUCCESS) { printf("%C error attempting to send delete nameserver message\n"); return rc; } awaitspecificmessage(&msg); return dmsg->error;} /* end dns_client_delete_nameserver() *//* * dns_client_new_searchlist() * * this routine updates the search list. * It is an API analog to the 'search' console command. * * configuration must be saved to make this data persistent. * * return: ESUCCESS, or * ESRCH (msg queue not found), * EINVAL (parameter missing), * EFAULT (bad IP address), * ENOSPC (nameserver list full), * EEXIST (nameserver already in list), * E2BIG (only one parameter allowed), */intdns_client_new_searchlist(const char *liststr){ ATMOS_MQID qid; ATMOS_MESSAGE msg; int rc; MSG_D_DNS_NEW_SEARCHLIST(dmsg, &msg); if ((qid = findqueue(DNSCLIENT_QUEUE_NAME)) == 0) { printf("%C could not acquire the %s queue handle\n", DNSCLIENT_QUEUE_NAME); return ESRCH; } dmsg->error = ESUCCESS; dmsg->liststr = (char *)liststr; if ((rc = sendmessage(&msg, MSG_N_DNS_NEW_SEARCHLIST, qid)) != ESUCCESS) { printf("%C error attempting to send new searchlist message\n"); return rc; } awaitspecificmessage(&msg); return dmsg->error;} /* end dns_client_new_searchlist() *//* * dns_client_get_nameserver() * * this routine retrieves an IP nameserver from the existing list. * It is an API analog to the 'show' console command. * * the caller must provide a buffer with enough space to * store a dotted-decimal IP address string (16). * * pab note: this routine will only return an IPv4 nameserver * * return: ESUCCESS (nameserver in string buffer), or * EINVAL (nameserver not found) */intdns_client_get_nameserver(int index, char *nsaddr_str)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -