📄 hostlib.c
字号:
/* hostLib.c - host table subroutine library *//* Copyright 1984 - 2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------02d,10may02,kbw making man page edits02c,15oct01,rae merge from truestack ver 02g, base 02b (SPR #63610)02b,15mar99,c_c Doc: updated hostGetByName manual page (SPR #5184).02a,14mar99,jdi doc: removed refs to config.h and/or configAll.h (SPR 25663).01z,20aug,97,jag added functions hostTblSearchByAddr(), hostTblSearchByName() to address (SPR #9175)01x,19may97,spm added checks for NULL pointers to user interface routines01w,20apr97,kbw fixed man page format problems, did a spell check01v,07apr97,jag added hooks for the resolver library.01w,17nov97,dgp doc: SPR 9410, hostGetByName() returns addr in network byte order01v,29aug97,dgp doc: SPR 9196 correct ERROR return for hostAdd()01u,16feb94,caf added check for NULL pointer in hostGetByName() (SPR #2920).01t,02feb93,jdi documentation cleanup for 5.1.01s,13nov92,dnw added include of semLibP.h01r,27jul92,elh Moved hostShow to netShow.c01q,18jul92,smb Changed errno.h to errnoLib.h.01p,26may92,rrr the tree shuffle -changed includes to have absolute path from h/01o,10dec91,gae more cleanup of ANSI warnings.01n,25oct91,rrr cleanup of some ansi warnings.01m,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed VOID to void -changed copyright notice01l,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by dnw.01k,12feb91,jaa documentation.01j,02oct90,hjb defined HOSTNAME_LEN. added sethostname() and gethostname().01i,15jul90,dnw changed hostNameFill() to coerce malloc() to (char*)01h,26jun90,jcf changed hostList semaphore to type mutex.01g,23may89,shl changed addr parameter in hostGetByAddr() from char* to int.01f,21apr89,shl added hostGetByAddr(). added hostDelete ().01e,18mar88,gae documentation.01d,07jul88,jcf fixed malloc to match new declaration.01c,04jun88,gae changed S_remLib_* to S_hostLib_*.01b,30may88,dnw changed to v4 names.01a,28may88,dnw extracted from remLib.*//*DESCRIPTIONThis library provides routines to store and access the network host database.The host table contains information regarding the known hosts on thelocal network. The host table (displayed with hostShow()) containsthe Internet address, the official host name, and aliases.By convention, network addresses are specified in dotted (".") decimal notation. The library inetLib contains Internet address manipulation routines. Host names and aliases may contain any printable character.Before any of the routines in this module can be used, the library must beinitialized by hostTblInit(). This is done automatically if INCLUDE_HOST_TBLis defined.INCLUDE FILES: hostLib.hSEE ALSOinetLib*/#include "vxWorks.h"#include "sys/types.h"#include "netinet/in.h"#include "stdlib.h"#include "semLib.h"#include "string.h"#include "inetLib.h"#include "lstLib.h"#include "hostLib.h"#include "errnoLib.h"#include "stdio.h"#include "private/semLibP.h"#include "memPartLib.h"#ifdef VIRTUAL_STACK#include "netinet/vsLib.h"#endif#ifndef VIRTUAL_STACK#define HOSTNAME_LEN 128/* globals */LIST hostList;SEM_ID hostListSem = NULL;BOOL hostInitFlag = FALSE;#endif /* VIRTUAL_STACK *//* Function pointers installed by resolvLib, when the resolver is included */ /* Use DNS resolver for the name query */FUNCPTR _presolvHostLibGetByName = NULL; /* Use DNS resolver for the pointer query */FUNCPTR _presolvHostLibGetByAddr = NULL; /* mutual exclusion options */int mutexOptionsHostLib = SEM_Q_FIFO | SEM_DELETE_SAFE;#ifndef VIRTUAL_STACK/* locals */LOCAL char targetName [HOSTNAME_LEN]; /* name for this target machine */#endif /* VIRTUAL_STACK *//* forward static functions */static STATUS hostNameFill (HOSTNAME *pHostName, char *newHostName);/********************************************************************************* hostTblInit - initialize the network host table** This routine initializes the host list data structure used by routines* throughout this module. It should be called before any other routines in* this module. This is done automatically if INCLUDE_HOST_TBL is defined.** RETURNS: N/A** SEE ALSO: usrConfig*/void hostTblInit (void) { if (!hostInitFlag) { hostInitFlag = TRUE; lstInit (&hostList); hostListSem = semMCreate (mutexOptionsHostLib); } }/********************************************************************************* hostAdd - add a host to the host table** This routine adds a host name to the local host table.* This must be called before sockets on the remote host are opened,* or before files on the remote host are accessed via netDrv or nfsDrv.** The host table has one entry per Internet address.* More than one name may be used for an address.* Additional host names are added as aliases.** EXAMPLE* .CS* -> hostAdd "wrs", "90.2"* -> hostShow* hostname inet address aliases* -------- ------------ -------* localhost 127.0.0.1* yuba 90.0.0.3* wrs 90.0.0.2* value = 12288 = 0x3000 = _bzero + 0x18* .CE* RETURNS:* OK, or ERROR if the host table is full, the host name/inet address pair* is already entered, the Internet address is invalid, or memory is * insufficient.** SEE ALSO: netDrv, nfsDrv*/STATUS hostAdd ( char *hostName, /* host name */ char *hostAddr /* host addr in standard Internet format */ ) { HOSTNAME *pHostNamePrev = NULL; /* pointer to previous host name entry */ FAST HOSTNAME *pHostName; /* pointer to host name entry */ FAST HOSTENTRY *pHostEntry; struct in_addr netAddr; /* network address */ if (hostName == NULL || hostAddr == NULL) { errnoSet (S_hostLib_INVALID_PARAMETER); return (ERROR); } if ((netAddr.s_addr = inet_addr (hostAddr)) == ERROR) return (ERROR); if (semTake (hostListSem, WAIT_FOREVER) == ERROR) return (ERROR); for (pHostEntry = (HOSTENTRY *)lstFirst (&hostList); pHostEntry != NULL; pHostEntry = (HOSTENTRY *)lstNext (&pHostEntry->node)) { if (pHostEntry->netAddr.s_addr == netAddr.s_addr) { /* host internet address already in table, add name as an alias */ pHostNamePrev = &pHostEntry->hostName; for (pHostName = &pHostEntry->hostName; pHostName != NULL; pHostName = pHostName->link) { /* make sure name is not already used for this address */ if (strcmp (pHostName->name, hostName) == 0) { semGive (hostListSem); errnoSet (S_hostLib_HOST_ALREADY_ENTERED); return (ERROR); } pHostNamePrev = pHostName; } if (pHostNamePrev == NULL) { /* XXX corrupted list! */ return (ERROR); } /* name not used for this address, add it as an alias */ if ((pHostNamePrev->link = (HOSTNAME *) KHEAP_ALLOC(sizeof (HOSTNAME))) == NULL) { semGive (hostListSem); return (ERROR); } bzero ((char *)pHostNamePrev->link, sizeof(HOSTNAME)); if (hostNameFill (pHostNamePrev->link, hostName) == ERROR) { semGive (hostListSem); return (ERROR); } semGive (hostListSem); return (OK); } } /* host name and internet address not in host table, add new host */ if ((pHostEntry = (HOSTENTRY *) KHEAP_ALLOC(sizeof (HOSTENTRY))) == NULL) { semGive (hostListSem); return (ERROR); } bzero ((char *)pHostEntry, sizeof(HOSTENTRY)); if ((hostNameFill (&pHostEntry->hostName, hostName)) == ERROR) { semGive (hostListSem); return (ERROR); } pHostEntry->netAddr = netAddr; lstAdd (&hostList, &pHostEntry->node); semGive (hostListSem); return (OK); }/********************************************************************************* hostDelete - delete a host from the host table** This routine deletes a host name from the local host table. If <name> is* a host name, the host entry is deleted. If <name> is a host name alias,* the alias is deleted.** RETURNS: OK, or ERROR if the parameters are invalid or the host is unknown.** ERRNO: S_hostLib_INVALID_PARAMETER, S_hostLib_UNKNOWN_HOST*/STATUS hostDelete ( char *name, /* host name or alias */ char *addr /* host addr in standard Internet format */ ) { HOSTNAME *pHostNamePrev; /* pointer to previous host name entry */ HOSTNAME *pHostNameNext; /* pointer to next host name entry */ FAST HOSTNAME *pHostName; FAST HOSTENTRY *pHostEntry; struct in_addr netAddr; if (name == NULL || addr == NULL) { errnoSet (S_hostLib_INVALID_PARAMETER); return (ERROR); } /* convert from string to int format */ if ((netAddr.s_addr = inet_addr (addr)) == ERROR) return ERROR; semTake (hostListSem, WAIT_FOREVER); /* search inet address */ for (pHostEntry = (HOSTENTRY *)lstFirst (&hostList); pHostEntry != NULL; pHostEntry = (HOSTENTRY *)lstNext (&pHostEntry->node)) { if (pHostEntry->netAddr.s_addr != netAddr.s_addr) continue; if (strcmp (pHostEntry->hostName.name, name) == 0) /* given name is exact match */ { FAST HOSTNAME * pAlias = pHostEntry->hostName.link; FAST HOSTNAME * pNext = NULL; /* free all associated alias(es) 1st if any, then free itself */ for ( ; pAlias != NULL; pAlias = pNext) { pNext = pAlias->link; KHEAP_FREE(pAlias->name); KHEAP_FREE((char *) pAlias); } lstDelete (&hostList, &pHostEntry->node); semGive (hostListSem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -