/* resolvLib.c - DNS resolver library */
/* Copyright 1984 - 2001 Wind River Systems, Inc */
#include <copyright_wrs.h>
/*
modification history
-------------------------
01k,15oct01,rae merge from truestack ver 01l, base 01j (SPRs 67238, 28659)
01j,06oct98,jmp moved doc to resolvLibDoc.c.
01i,14dec97,jdi doc: cleanup.
01h,19aug97,jag fixed man page problems in resolvInit() SPR#9173, fixed
SPR#9174, SPR#9175. Deleted getHostInfo(), getbyaddrWrapper()
01g,04aug97,kbw fixed man page problems found in beta review
01f,19may97,spm added checks for NULL pointers to user interface (SPR #8603)
01e,30apr97,kbw fiddled man page text
01d,01apr97,kbw fixed man page text, changed parameter name to "length"
in resolvDNExpand
01c,01apr97,jag removed unused variable resolvDefaultDomainName. Added
routines: resolvHostLibGetByAddr(), resolvHostLibGetByName().
Added necessary man pages.
01b,05feb97,jag added debug function pointer in resolvInit. MAX_DOMAIN_NAME
01a,12aug96,rjc written
*/
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DOCUMENTATION of this library is located in resolvLibDoc.c
If you modify any documented routine, please update resolvLibDoc.c.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
/* includes */
#include <vxWorks.h>
#include <resolvLib.h>
#include <semLib.h>
#include <hostLib.h>
#include <stdlib.h>
#include <sockLib.h>
#include <netinet/in.h>
#include <inetLib.h>
#include <stdio.h>
#include <ctype.h>
#include <errnoLib.h>
#include <string.h>
/* defines */
#define LOCAL_ENTRIES_TTL 60 /* Local entries have a ttl for 60 seconds */
#define ALIGNMENT 4 /* for word alignment */
#define MAX_DOMAIN_NAME 260 /* Max domain name including EOS marker */
#define MAX_HOSTLIB_BUF 512 /* hostLib interface functions buffer */
/* macro to align ptr to buffer and decrement buffer size accordingly */
#define ALIGN_BUF_PTR(pBuf, bufLen, alignment) \
while ((UINT) (pBuf) % (alignment) != 0) \
{ \
++ (pBuf); \
-- (bufLen); \
}
/* typedefs */
/* Globals */
/*
* Resolver state default settings. Structure moved here from res_init.c
*/
struct __res_state _res =
{
RES_TIMEOUT, /* retransmition time interval */
4, /* number of times to retransmit */
RES_DEFAULT, /* options flags */
1, /* number of name servers */
};
/* This variable point to the optional debug routine */
FUNCPTR pdnsDebugFunc;
/* locals */
LOCAL RESOLV_PARAMS_S resolvParams; /* resolver settings */
LOCAL STATUS resolvHostLibGetByAddr (int addr, char * pHostName);
LOCAL int resolvHostLibGetByName (char * pHostName);
/* externs */
extern struct hostent * _gethostbyname ();
extern struct hostent * _gethostbyaddr ();
/* Ptrs defined in hostLib.c. These ptrs are set by the resolver library */
extern FUNCPTR _presolvHostLibGetByName;
extern FUNCPTR _presolvHostLibGetByAddr;
/*******************************************************************************
*
* hostEntFormat - Formats a char array as a structure of type hostent
*
* This routine formarts a character array pointed to by <ppBuf> of the length
* specified by <pBuflen>. It creates a hostent structure in the buffer along
* with the `h_aliases' and `h_addr_list' vectors big enough for <numAliases>
* aliases and <numAddrs> addresses respectively. On a successful return
* <ppBuf> and <pBuflen> specify the remaining unused part of the buffer.
*
* NOMANUAL
*
* RETURNS: A pointer to a hostent structure on success, or NULL if the
* input buffer is too small.
*/
LOCAL struct hostent * hostEntFormat
(
char ** ppBuf, /* Pointer to character buffer pointer */
int * pBufLen, /* Pointer to the buffer length */
int numAliases, /* Max # of host aliases expected */
int numAddrs /* Max # of host addresses expected */
)
{
char * pTmp;
struct hostent * pHostEnt;
pTmp = *ppBuf;
if (((numAliases + 1 + numAddrs + 1) * sizeof (char *) +
sizeof (struct hostent) + 3 + 3) > *pBufLen)
{
return (NULL);
}
ALIGN_BUF_PTR (pTmp, *pBufLen, ALIGNMENT);
pHostEnt = (struct hostent *) pTmp;
pTmp += sizeof (struct hostent);
*pBufLen -= sizeof (struct hostent);
ALIGN_BUF_PTR (pTmp, *pBufLen, ALIGNMENT);
pHostEnt->h_aliases = (char **) pTmp;
pTmp += (numAliases + 1)* sizeof (char **);
*pBufLen -= (numAliases + 1) * sizeof (char **);
pHostEnt->h_addr_list = (char **) pTmp;
pTmp += (numAddrs + 1) * sizeof (char **);
*pBufLen -= (numAddrs + 1) * sizeof (char **);
*ppBuf = pTmp;
return (pHostEnt);
}
/*******************************************************************************
*
* resolvInit - initialize the resolver library
*
* This function initializes the resolver. <pNameServer> is a single IP address
* for a name server in dotted decimal notation. <pDefaultDomainName> is the
* default domain name to be appended to names without a dot. The function
* pointer <pdnsDebugRtn> is set to the resolver debug function. Additional
* name servers can be configured using the function resolvParamsSet().
*
* RETURNS: OK or ERROR.
*
* SEE ALSO:
* resolvGetHostByName(), resolvGetHostByAddr(), resolvDNExpand(),
* resolvDNComp(), resolvSend(), resolvParamsSet(), resolvParamsGet(),
* resolvQuery()
*/
STATUS resolvInit
(
char * pNameServer, /* pointer to Name server IP address */
char * pDefaultDomainName, /* default domain name */
FUNCPTR pdnsDebugRtn /* function ptr to debug routine */
)
{
int nserv = 0; /* number of nameserver records read from file */
struct in_addr ipAddr;
/* Initialize global pointer to the DNS debug routine */
printf("## %s: nameServer=%s, default domain name=%s!\n", __FUNCTION__,
pNameServer, pDefaultDomainName);
pdnsDebugFunc = (FUNCPTR) NULL;
if (pdnsDebugRtn != NULL)
pdnsDebugFunc = pdnsDebugRtn;
_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
_res.nsaddr.sin_family = AF_INET;
_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
_res.ndots = 1;
_res.pfcode = 0;
strncpy (_res.lookups, "b", sizeof _res.lookups);
if (inet_aton (pNameServer, &ipAddr) == OK)
{
_res.nsaddr_list [nserv].sin_addr = ipAddr;
_res.nsaddr_list [nserv].sin_family = AF_INET;
_res.nsaddr_list [nserv].sin_port = htons(NAMESERVER_PORT);
nserv++;
}
else
{
/* Illegal IP address for DNS server */
return (ERROR);
}
_res.nscount = nserv; /* Number of name servers */
_res.dnsrch [0] = _res.defdname;
_res.options |= RES_INIT | RES_DEFNAMES | RES_DEBUG;
(void) strcpy (_res.defdname, pDefaultDomainName);
/* The resolver is initialized to query only the DNS server */
resolvParams.queryOrder = QUERY_DNS_ONLY;
/* Install pointers used by hostLib to access the resolver library */
_presolvHostLibGetByName = resolvHostLibGetByName;
_presolvHostLibGetByAddr = resolvHostLibGetByAddr;
return (OK);
}
/*******************************************************************************
*
* resolvHostGetByName - queries the static host table for the host name
*
* Retrieve host information installed by hostAdd() and install it in the
* `hostent' structure referenced in <pHostEnt>. This routine uses the
* buffer referenced in <pBuf> to store hostname and network address
* information.
*
* NOMANUAL
*
* RETURNS: A pointer to hostent, or NULL if the entry was not found.
*/
LOCAL struct hostent * resolvHostGetByName
(
char * pHostName, /* pointer to the name of the host */
DNS(vxWorks内核)
4星 · 超过85%的资源 需积分: 18 27 浏览量
2009-12-21
00:19:10
上传
评论
收藏 38KB RAR 举报
xiaogang_121
- 粉丝: 2
- 资源: 3
- 1
- 2
前往页