예제 #1
0
 ResolverLibInitializer() {
   res_init();
   // We call sethostent with stayopen = 1 to keep /etc/hosts open across calls
   // to prevent mmap contention inside the kernel.  Two calls are necessary to
   // properly initialize the stayopen flag in glibc.
   sethostent(1);
   sethostent(1);
 }
예제 #2
0
파일: getent.c 프로젝트: AhmadTux/freebsd
static int
hosts(int argc, char *argv[])
{
	struct hostent	*he;
	char		addr[IN6ADDRSZ];
	int		i, rv;

	assert(argc > 1);
	assert(argv != NULL);

	sethostent(1);
	rv = RV_OK;
	if (argc == 2) {
		while ((he = gethostent()) != NULL)
			hostsprint(he);
	} else {
		for (i = 2; i < argc; i++) {
			if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
			else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, INADDRSZ, AF_INET);
			else
				he = gethostbyname(argv[i]);
			if (he != NULL)
				hostsprint(he);
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endhostent();
	return rv;
}
예제 #3
0
파일: getent.c 프로젝트: SylvestreG/bitrig
static int
hosts(int argc, char *argv[])
{
	char		addr[IN6ADDRSZ];
	int		i, rv = RV_OK;
	struct hostent	*he;

	sethostent(1);
	if (argc == 2) {
		while ((he = gethostent()) != NULL)
			hostsprint(he);
	} else {
		for (i = 2; i < argc; i++) {
			he = NULL;
			if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
			else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, INADDRSZ, AF_INET);
			if (he != NULL)
				hostsprint(he);
			else if ((rv = hostsaddrinfo(argv[i])) == RV_NOTFOUND)
				break;
		}
	}
	endhostent();
	return rv;
}
예제 #4
0
파일: host.c 프로젝트: tsiangleo/apue
int
main(void)
{
	struct hostent *ptr;
	char **p;

	sethostent(0);

	while((ptr = gethostent()) != NULL)
	{
		printf("name:%s , addrtype:%d , length:%d ,addr_list: ",
				ptr->h_name,ptr->h_addrtype,ptr->h_length);
		
		p = ptr->h_addr_list;

		while(*p)
		{
			printf("[%s] ",*p);
			p++;
		}
		printf("\n");
	}

	endhostent();
	return 0;
}
예제 #5
0
void ReadHostFile (const char *fname)
{
  if (!fname || !*fname)
     return;

  hostFname = strdup (fname);
  if (!hostFname)
     return;

  sethostent (1);
  if (!hostFile)
     return;

  while (1)
  {
    struct _hostent h;

    if (!_gethostent(&h))
       break;

    if (!tree_insert(&host_root, (void*)&h, sizeof(h), (CmpFunc)host_cmp_name))
    {
      outsnl (_LANG("Hostfile too big!\7"));
      break;
    }
  }
  atexit (endhostent);

}
예제 #6
0
WINDOW *
opennetstat()
{
	sethostent(1);
	setnetent(1);
	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
}
예제 #7
0
void
nw_sethostent(int stayopen)
{
#ifdef HAS_SETHOSTENT
        sethostent(stayopen);
#endif
}
예제 #8
0
void
setnetent(int stayopen)
{

	sethostent(stayopen);
	__setnetent(stayopen);
}
예제 #9
0
sethostent_r(int stay_open)
#endif
{
	sethostent(stay_open);
#ifdef	HOST_R_SET_RESULT
	return (HOST_R_SET_RESULT);
#endif
}
/*
** Searches the DNS mapping cache for #name#, adding a new entry if needed.
** Returns a pointer to the mapping entry, or NULL on error.
*/
static const struct hostent*
LookupByName(const char *name) {

  char **cachedName;
  char **extendedAliases;
  struct hostent *nameEntry;
  int i;
  int listLen;

  for(i = 0; i < cacheCount; i++) {
    if(strcasecmp(name, cache[i].h_name) == 0) {
      return &cache[i];
    }
    for(cachedName = cache[i].h_aliases; *cachedName != NULL; cachedName++) {
      if(strcasecmp(*cachedName, name) == 0) {
        return &cache[i];
      }
    }
  }

  sethostent(0);
  nameEntry = gethostbyname(name);
  if(nameEntry == NULL) {
    endhostent();
    return NULL;
  }
  else if(nameEntry->h_length != sizeof(struct in_addr)) {
    endhostent();
    return NULL; /* We don't (yet) handle non-in_addr addresses. */
  }

  /* We extend cached entries' h_aliases lists to include nicknames. */
  for(i = 0; i < cacheCount; i++) {
    if(strcmp(nameEntry->h_name, cache[i].h_name) == 0) {
      for(listLen = 0; cache[i].h_aliases[listLen] != NULL; listLen++) {
        ; /* Nothing more to do. */
      }
      extendedAliases =
        (char **)REALLOC(cache[i].h_aliases, sizeof(char **) * (listLen + 2));
      if(extendedAliases != NULL) {
        extendedAliases[listLen] = strdup(name);
        extendedAliases[listLen + 1] = NULL;
        cache[i].h_aliases = extendedAliases;
      }
      endhostent();
      return &cache[i];
    }
  }

  nameEntry = CacheHostent(nameEntry);
  endhostent();
  return nameEntry;

}
예제 #11
0
sethostent_r(int stay_open)
#endif
{
#ifdef HOST_R_ENT_ARGS
    UNUSED(hdptr);
#endif
    sethostent(stay_open);
#ifdef	HOST_R_SET_RESULT
    return (HOST_R_SET_RESULT);
#endif
}
예제 #12
0
파일: gethostent.c 프로젝트: jimodb/codes
main()
{
	//打开DNS服务器或者其他host文件
	sethostent(1);
	struct hostent*hptr;
	while((hptr=gethostent()))
	{
		printf("IP地址:%hhu.%hhu.%hhu.%hhu\n",hptr->h_addr[0],hptr->h_addr[1],hptr->h_addr[2],hptr->h_addr[3]);
	}
	endhostent();
}
예제 #13
0
/** Get full qualified domain name.
 * Retreive the full qualified domain name for a given hostname
 * @param name the hostname
 * @return the fqdn on success, NULL otherwise
 */
char *
get_fqdn (const char *name) /*{{{*/
{
	char		*fqdn;
	struct hostent	*hent;
	
	fqdn = NULL;
	sethostent (0);
	if ((hent = gethostbyname (name)) && hent -> h_name)
		fqdn = strdup (hent -> h_name);
	endhostent ();
	return fqdn;
}/*}}}*/
예제 #14
0
void resolver_initialize()
{
    /* initialize the lib if we havne't done so already */

    if (!_initialized)
    {
        _initialized = 1;
        thread_mutex_create (&_resolver_mutex);

        /* keep dns connects (TCP) open */
#ifdef HAVE_SETHOSTENT
        sethostent(1);
#endif
    }
}
예제 #15
0
struct hostent *
_gethostbyhtaddr(
	const char *addr,
	int len, 
	int af)
{
	register struct hostent *p;

	sethostent(0);
	while ((p = gethostent()) != NULL)
		if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
			break;
	endhostent();
	return (p);
}
예제 #16
0
파일: lookup.c 프로젝트: drurowin/netmap
string LookUp::resolv(const string& ip) 
{    
    if(!resolv_hosts) return string();
    
    static int first_run = 1;
    if(first_run) {
	first_run = 0;
	sethostent(1);
    }

    char res_ip[4];
    transform_ip(ip, res_ip);

    if(verbose_lvl) {
	cout << indent(4) << "Resolving " << ip << " : ";
	cout.flush();
    }
    
    if(cdb>d_detail) {
	dbgs << " * Resolving " << ip << " : ";
	dbgs.flush();
    }

    hostent* he = gethostbyaddr(res_ip, 4, AF_INET);

    if(!he) {
	if(verbose_lvl)
	    cout << " lookup FAILED !" << endl;
	
	if(cdb>d_detail)
	    dbgs << "FAILED!" << endl;
	return string();
    }

    string result;
    result = he->h_name;
    
    if(verbose_lvl) 
	cout << result << endl;
    
    if(cdb>d_detail)
	dbgs << result << endl;

    return result;
}
예제 #17
0
파일: netstat.c 프로젝트: UNGLinux/Obase
int
select_ns(void)
{
	static int init = 0;
	if (kd == NULL) {
		num_disp = 1;
		return (0);
	}

	if (!init) {
		sethostent(1);
		setnetent(1);
		init = 1;
	}

	num_disp = num_ns;
	return (0);
}
예제 #18
0
PAM_EXTERN
int pam_sm_authenticate (pam_handle_t *pamh, 
			 int flags,
			 int argc, 
			 const char **argv)
{
    int retval;

    if (sizeof(U32) != 4) {
        _pam_log (LOG_ALERT, "pam_rhosts module can\'t work on this hardware "
		  "(yet)");
        return PAM_AUTH_ERR;
    }
    sethostent(1);
    retval = _pam_auth_rhosts (pamh, flags, argc, argv);
    endhostent();
    return retval;
}
예제 #19
0
파일: hosts.c 프로젝트: BlueBreeze/recipes
main()
{

	//参数主要对域名查找设置有效,对本地/etc/hosts没有意义
	sethostent(1);

	struct hostent *ent;

	while(1)
	{
		ent = gethostent();
		if(!ent) break;
		printf(":%s:%hhu.%hhu.%hhu.%hhu\n",
		//printf(":%s:%u.%u.%u.%u\n",
				ent->h_name,
				ent->h_addr[0],
				ent->h_addr[1],
				ent->h_addr[2],
				ent->h_addr[3]);
	}

	endhostent();

	/////////////////////////////////
	struct utsname name;
	uname(&name);
	printf("%s\n",name.sysname);
	printf("%s\n",name.nodename);
	printf("%s\n",name.release);
	printf("%s\n",name.version);
	printf("%s\n",name.machine);
	//printf("%s\n",name.domainname);

	//////////////////////////////
	struct hostent *ipent;
	ipent=gethostbyname("www.baidu.com");	
	printf("%s:%hhu.%hhu.%hhu.%hhu\n",
			ipent->h_name,
			ipent->h_addr[0],
			ipent->h_addr[1],
			ipent->h_addr[2],
			ipent->h_addr[3]);
	
}
예제 #20
0
static struct hostent *file_find_addr(const char *addr, int len, int type,
									  struct hostent *result, char *buf,
									  int bufsize, int *errval)
{
	FILE *fp = NULL;

	pthread_mutex_lock(&host_iterate_lock);
	sethostent(0);
	while ((result = gethostent_r(result, buf, bufsize, errval)) != NULL) {
		/* Check the entry against the given address. */
		if (result->h_addrtype == type &&
			memcmp(result->h_addr, addr, len) == 0)
			break;
	}
	pthread_mutex_unlock(&host_iterate_lock);
	if (!result && errno != ERANGE)
		*errval = HOST_NOT_FOUND;
	return result;
}
예제 #21
0
파일: utils.c 프로젝트: dtaht/tc-adv
static const char *resolve_address(const void *addr, int len, int af)
{
	struct namerec *n;
	struct hostent *h_ent;
	unsigned int hash;
	static int notfirst;


	if (af == AF_INET6 && ((__u32 *)addr)[0] == 0 &&
	    ((__u32 *)addr)[1] == 0 && ((__u32 *)addr)[2] == htonl(0xffff)) {
		af = AF_INET;
		addr += 12;
		len = 4;
	}

	hash = *(__u32 *)(addr + len - 4) % NHASH;

	for (n = nht[hash]; n; n = n->next) {
		if (n->addr.family == af &&
		    n->addr.bytelen == len &&
		    memcmp(n->addr.data, addr, len) == 0)
			return n->name;
	}
	n = malloc(sizeof(*n));
	if (n == NULL)
		return NULL;
	n->addr.family = af;
	n->addr.bytelen = len;
	n->name = NULL;
	memcpy(n->addr.data, addr, len);
	n->next = nht[hash];
	nht[hash] = n;
	if (++notfirst == 1)
		sethostent(1);
	fflush(stdout);

	h_ent = gethostbyaddr(addr, len, af);
	if (h_ent != NULL)
		n->name = strdup(h_ent->h_name);

	/* Even if we fail, "negative" entry is remembered. */
	return n->name;
}
예제 #22
0
void test2()
{
	// 打开主机配置数据库文件
	sethostent(1);

	hostent* ent;
	while (1) {
		ent = gethostent();
		if (ent) {
			printf("主机名:%s\n", ent->h_name);
			printf("ip地址:%hhu.%hhu.%hhu.%hhu\n", ent->h_addr[0], ent->h_addr[1], ent->h_addr[2], ent->h_addr[3]);
		} else {
			break;
		}
	}

	// 关闭
	endhostent();
}
예제 #23
0
int main(){

	struct hostent *ent;
	/* open host config */
	sethostent(1);

	while(1){
		ent = gethostent();
		if(ent == 0){
			break;
		}
		printf("name:%s IP name:%hhu.%hhu.%hhu.%hhu alarm:%s\n",
				ent->h_name,ent->h_addr[0],
				ent->h_addr[1],ent->h_addr[2],
				ent->h_addr[3],ent->h_aliases[0]);
	}

	endhostent();
	return 0;
}
예제 #24
0
struct hostent *
_gethostbyhtname(
	const char *name,
	int af)
{
	register struct hostent *p;
	register char **cp;
	
	sethostent(0);
	while ((p = gethostent()) != NULL) {
		if (p->h_addrtype != af)
			continue;
		if (strcasecmp(p->h_name, name) == 0)
			break;
		for (cp = p->h_aliases; *cp != 0; cp++)
			if (strcasecmp(*cp, name) == 0)
				goto found;
	}
found:
	endhostent();
	return (p);
}
예제 #25
0
main(int argc, char *argv[])
{
   struct hostent *h;

/* sethostent() opens the prefix.ETC.HOSTS file, or when using a  */
/* nameserver, opens a TCP connection to the nameserver.          */

   sethostent(TRUE);

/* gethostent() reads the next sequential entry in the            */
/* prefix.ETC.HOSTS file. It returns a pointer to a "hostent"     */
/* structure.                                                     */

   while (h=gethostent())
      prthost(h);

/* endhostent() closes the prefix.ETC.HOSTS file, or the          */
/* connection to the nameserver.                                  */

   endhostent();
   exit(EXIT_SUCCESS);
}
예제 #26
0
static struct hostent *__check_hostdb( const char *name )
{
    int             i;
    struct hostent  *one;
    int             alias;

    one = NULL;
    if( name != NULL ) {
        alias = 0;
        sethostent( 1 );
        while( alias == 0 && (one = gethostent()) != NULL ) {
            if( one->h_name != NULL && strcmp( one->h_name, name ) == 0 )
                break;
            for( i = 0; one->h_aliases[i] != NULL; i++ ) {
                if( strcmp( one->h_aliases[i], name ) == 0 ) {
                    alias = 1;
                    break;
                }
            }
        }
        endhostent();
    }
    return( one );
}
/*
** Searches the DNS mapping cache for #address#, adding a new entry if needed.
** Returns a pointer to the mapping entry, or NULL on error.
*/
static const struct hostent*
LookupByAddress(IPAddress address) {

  struct in_addr addrAsInAddr;
  struct hostent *addrEntry;
  struct in_addr **cachedAddr;
  int i;

  for(i = 0; i < cacheCount; i++) {
    for(cachedAddr = (struct in_addr**)cache[i].h_addr_list;
        *cachedAddr != NULL;
        cachedAddr++) {
      if((**cachedAddr).s_addr == address) {
        return &cache[i];
      }
    }
  }

  addrAsInAddr.s_addr = address;
  sethostent(0);
  addrEntry =
    gethostbyaddr((char *)&addrAsInAddr, sizeof(addrAsInAddr), AF_INET);
  if(addrEntry == NULL) {
    endhostent();
    return NULL;
  }
  else if(addrEntry->h_length != sizeof(struct in_addr)) {
    endhostent();
    return NULL; /* We don't (yet) handle non-in_addr addresses. */
  }

  addrEntry = CacheHostent(addrEntry);
  endhostent();
  return addrEntry;

}
예제 #28
0
int main(int argc, char *argv[])
{
    char *hostname = NULL;
    struct protoent *p;
    struct protox *tp = NULL;	/* for printing cblocks & stats */
    int allprotos = 1;
    char *community = NULL;
    char *argp;
    struct snmp_session session;
    int dest_port = SNMP_PORT;
    int timeout = SNMP_DEFAULT_TIMEOUT;
    int version = SNMP_VERSION_1;
    int arg;

    init_mib();
    /*
     * Usage: snmpnetstatwalk -v 1 [-q] hostname community ...      or:
     * Usage: snmpnetstat [-v 2 ] [-q] hostname noAuth     ...
     */
    for(arg = 1; arg < argc; arg++){
	if (argv[arg][0] == '-'){
	    switch(argv[arg][1]){
              case 'V':
                fprintf(stderr,"UCD-snmp version: %s\n", VersionInfo);
                exit(0);
                break;

	      case 'h':
		usage();
		exit(0);

	      case 'd':
		snmp_set_dump_packet(1);
		break;

	      case 'q':
		snmp_set_quick_print(1);
		break;

	      case 'D':
                debug_register_tokens(&argv[arg][2]);
		snmp_set_do_debugging(1);
		break;
	      case 'p':
		if (argv[arg][2] != 0) dest_port = atoi(argv[arg]+2);
		else if (++arg == argc) {
		    usage();
		    exit(1);
		}
		else dest_port = atoi(argv[arg]);
		break;

	      case 't':
		if (argv[arg][2] != 0) timeout = atoi(argv[arg]+2);
		else if (++arg == argc) {
		    usage();
		    exit(1);
		}
		else timeout = atoi(argv[arg]);
		timeout *= 1000000;
		break;

	      case 'c':
		if (argv[arg][2] != 0) community = argv[arg]+2;
		else if (++arg == argc) {
		    usage();
		    exit(1);
		}
		else community = argv[arg];
		break;

	      case 'v':
		if (argv[arg][2] != 0) argp = argv[arg]+2;
		else if (arg == argc) {
		    usage();
		    exit(1);
		}
		else argp = argv[arg];
		if (!strcmp(argp,"1"))
		    version = SNMP_VERSION_1;
		else if (!strcmp(argp,"2c"))
		    version = SNMP_VERSION_2c;
		else {
		    fprintf(stderr, "Invalid version: %s\n", argp);
		    usage();
		    exit(1);
		}
		break;

	      case 'a':
		aflag++;
		break;

	      case 'i':
		iflag++;
		break;

	      case 'o':
		oflag++;
		break;

	      case 'n':
		nflag++;
		break;

	      case 'r':
		rflag++;
		break;

	      case 's':
		sflag++;
		break;

	      case 'P':
		if (++arg == argc) {
		    usage();
		    exit(1);
		}
		if ((tp = name2protox(argv [arg])) == NULLPROTOX) {
		  fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
			  argv [arg]);
		  exit(1);
		}
		allprotos = 0;
		tp->pr_wanted = 1;
		break;

	      case 'I':
		iflag++;
		if (*(intrface = argv[arg] + 2) == 0) {
		  if (++arg == argc) {
		      usage();
		      exit(1);
		  }
		  if ((intrface = argv[arg]) == 0)
		    break;
		}
		break;

	      default:
		printf("invalid option: -%c\n", argv[arg][1]);
		break;
	    }
	    continue;
	}
	if (hostname == NULL){
	    hostname = argv[arg];
	} else if ((version == SNMP_VERSION_1 || version == SNMP_VERSION_2c)
                   && community == NULL){
	    community = argv[arg]; 
	} else if (isdigit(argv[arg][0])) {
            interval = atoi(argv[arg]);
            if (interval <= 0){
		usage();
		exit(1);
	    }
	    iflag++;
	} else {
	    usage();
	    exit(1);
	}
    }
    
    if (!hostname ||
	((version == SNMP_VERSION_1 || version == SNMP_VERSION_2c) && !community)) {
	usage();
	exit(1);
    }

    snmp_sess_init(&session);
    session.peername = hostname;
    session.remote_port = dest_port;
    session.timeout = timeout;
    if (version == SNMP_VERSION_1 || version == SNMP_VERSION_2c){
        session.version = version;
        session.community = (u_char *)community;
        session.community_len = strlen((char *)community);
    }

    SOCK_STARTUP;

    /* open an SNMP session */
    Session = snmp_open(&session);
    if (Session == NULL){
      /* diagnose snmp_open errors with the input struct snmp_session pointer */
        snmp_sess_perror("snmpnetstat", &session);
        SOCK_CLEANUP;
	exit(1);
    }

    /*
     * Keep file descriptors open to avoid overhead
     * of open/close on each call to get* routines.
     */
    sethostent(1);
    setnetent(1);
    setprotoent(1);
    setservent(1);

    if (iflag) {
	intpr(interval);
    }
    if (oflag) {
	intpro(interval);
    }
    if (rflag) {
	if (sflag)
	    rt_stats();
	else
	    routepr();
    }
    
    if (iflag || rflag || oflag)
	;
    else {

    while ((p = getprotoent46())) {
	for (tp = protox; tp->pr_name; tp++) {
	    if (strcmp(tp->pr_name, p->p_name) == 0)
		break;
	}
	if (tp->pr_name == 0 || (tp->pr_wanted == 0 && allprotos == 0))
	    continue;
	if (sflag) {
	    if (tp->pr_stats)
		(*tp->pr_stats)();
	} else
	    if (tp->pr_cblocks)
		(*tp->pr_cblocks)(tp->pr_name);
    }
    } /* ! iflag, rflag, oflag */

    endprotoent();
    endservent();
    endnetent();
    endhostent();

    snmp_close(Session);

    SOCK_CLEANUP;
    return 0;
}
예제 #29
0
static void
find_another_host (ACE_TCHAR other_host[])
{
  static ACE_TCHAR cached_other_host[MAXHOSTNAMELEN] = {'\0'};

  if (cached_other_host[0] == '\0')
    {

      ACE_OS::strcpy (other_host,
                      ACE_DEFAULT_SERVER_HOST); // If all else fails

#if !defined (ACE_LACKS_GETHOSTENT)
      // These gethost-type things don't work everywhere.
      struct hostent *h = 0;
      ACE_utsname un;

      ACE_OS::uname (&un);

      h = ACE_OS::gethostbyname (un.nodename);

      if (h == 0)
        ACE_OS::strcpy (other_host, ACE_LOCALHOST);
      else
        // Use me if can't find another
        ACE_OS::strcpy (other_host, ACE_TEXT_CHAR_TO_TCHAR (h->h_name));

      // @@ We really need to add wrappers for these hostent methods.

      // Optimize for sequential access of DNS or hosts file.
      sethostent (1);

      int candidate_count = 0;

      // Accumulate candidates first.  There is some interaction on
      // Linux systems between <gethostent> and <gethostbyname_r>
      // (called by ACE_INET_Addr in host_is_up) This otherwise causes
      // an infinite loop on Linux --mas 03-08-2001
      while ((h = gethostent ()) != 0)
        {
          if (ACE_OS::strcmp (h->h_name,
                              ACE_TEXT_ALWAYS_CHAR (ACE_DEFAULT_SERVER_HOST)) == 0)
            continue;
          // AIX just _has_ to be different
          if (ACE_OS::strcmp (h->h_name, "loopback") == 0)
            continue;

          // If not me.
          if (ACE_OS::strcmp
                (h->h_name, ACE_TEXT_ALWAYS_CHAR (other_host)) != 0
              && ACE_OS::strcmp (h->h_name, un.nodename) != 0)
            {
               ACE_OS::strcpy (candidate[candidate_count].host_name,
                               ACE_TEXT_CHAR_TO_TCHAR (h->h_name));
               if (++candidate_count >= MAX_CANDIDATES)
                 break;
            }
        }

      // Now try to connect to candidates
      for (int i = 0; i < candidate_count; i++)
        if (host_is_up (candidate[i].host_name))
          {
            ACE_OS::strcpy (other_host, candidate[i].host_name);
            break;
          }

      sethostent (0);
      endhostent ();
#endif /* ! ACE_LACKS_GETHOSTENT */

      ACE_OS::strcpy (cached_other_host, other_host);
    }
  else
    ACE_OS::strcpy (other_host, cached_other_host);
}
예제 #30
0
파일: main.c 프로젝트: DYFeng/infinidb
int
main(int argc, char *argv[])
{
    char           *hostname = NULL;
    struct protoent *p;
    struct protox  *tp = NULL;  /* for printing cblocks & stats */
    int             allprotos = 1;
    char           *community = NULL;
    char           *argp;
    netsnmp_session session;
    int             timeout = SNMP_DEFAULT_TIMEOUT;
    int             version = SNMP_DEFAULT_VERSION;
    int             arg;

#ifndef DISABLE_MIB_LOADING
    init_mib();
#endif /* DISABLE_MIB_LOADING */
    /*
     * Usage: snmpnetstatwalk -v 1 [-q] hostname community ...      or:
     * Usage: snmpnetstat [-v 2 ] [-q] hostname noAuth     ...
     */
    while ((arg = getopt(argc, argv, "VhdqD:t:c:v:aionrsP:I:")) != EOF) {
        switch (arg) {
        case 'V':
            fprintf(stderr, "NET-SNMP version: %s\n",
                    netsnmp_get_version());
            exit(0);
            break;

        case 'h':
            usage();
            exit(0);

        case 'd':
            snmp_set_dump_packet(1);
            break;

        case 'q':
            snmp_set_quick_print(1);
            break;

        case 'D':
            debug_register_tokens(optarg);
            snmp_set_do_debugging(1);
            break;

        case 't':
            timeout = atoi(optarg);
            timeout *= 1000000;
            break;

        case 'c':
            community = optarg;
            break;

        case 'v':
            argp = optarg;
            version = -1;
#ifndef DISABLE_SNMPV1
            if (!strcasecmp(argp, "1"))
                version = SNMP_VERSION_1;
#endif
#ifndef DISABLE_SNMPV2C
            if (!strcasecmp(argp, "2c"))
                version = SNMP_VERSION_2c;
#endif
            if (version == -1) {
                fprintf(stderr, "Invalid version: %s\n", argp);
                usage();
                exit(1);
            }
            break;

        case 'a':
            aflag++;
            break;

        case 'i':
            iflag++;
            break;

        case 'o':
            oflag++;
            break;

        case 'n':
            nflag++;
            break;

        case 'r':
            rflag++;
            break;

        case 's':
            sflag++;
            break;

        case 'P':
            if ((tp = name2protox(optarg)) == NULLPROTOX) {
                fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
                        optarg);
                exit(1);
            }
            allprotos = 0;
            tp->pr_wanted = 1;
            break;

        case 'I':
            iflag++;
            intrface = optarg;
            break;

        default:
            exit(1);
            break;
        }
        continue;
    }

    init_snmp("snmpapp");
    snmp_enable_stderrlog();
    if (version == SNMP_DEFAULT_VERSION) {
        version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
		                     NETSNMP_DS_LIB_SNMPVERSION);
        if (!version) {
            switch (DEFAULT_SNMP_VERSION) {
#ifndef DISABLE_SNMPV1
            case 1:
                version = SNMP_VERSION_1;
                break;
#endif
#ifndef DISABLE_SNMPV2C
            case 2:
                version = SNMP_VERSION_2c;
                break;
#endif
            case 3:
                version = SNMP_VERSION_3;
                break;
            }
#ifndef DISABLE_SNMPV1
        } else if (version == NETSNMP_DS_SNMP_VERSION_1) {
                          /* Bogus value. version1 = 0 */
            version = SNMP_VERSION_1;
#endif
        }
    }
    if (optind < argc) {
        hostname = argv[optind++];
    }
    else {
        fprintf(stderr, "Missing host name.\n");
        exit(1);
    }
    if (community == NULL) {
	community = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
		                          NETSNMP_DS_LIB_COMMUNITY);
    }
    if (optind < argc && isdigit(argv[optind][0])) {
        interval = atoi(argv[optind++]);
        if (interval <= 0) {
            usage();
            exit(1);
        }
        iflag++;
    }
    if (optind < argc) {
        usage();
        exit(1);
    }


    snmp_sess_init(&session);
    session.peername = hostname;
    session.timeout = timeout;
#if !defined(DISABLE_SNMPV1) || !defined(DISABLE_SNMPV2C)
    if (version != SNMP_VERSION_3) {
        if (!community) {
            fprintf(stderr, "Missing community name.\n");
            exit(1);
        }
        session.version = version;
        session.community = (u_char *) community;
        session.community_len = strlen(community);
    }
#endif

    SOCK_STARTUP;

    /*
     * open an SNMP session 
     */
    Session = snmp_open(&session);
    if (Session == NULL) {
        /*
         * diagnose snmp_open errors with the input netsnmp_session pointer 
         */
        snmp_sess_perror("snmpnetstat", &session);
        SOCK_CLEANUP;
        exit(1);
    }

    /*
     * Keep file descriptors open to avoid overhead
     * of open/close on each call to get* routines.
     */
    sethostent(1);
    setnetent(1);
    setprotoent(1);
    setservent(1);

    if (iflag) {
        intpr(interval);
    }
    if (oflag) {
        intpro(interval);
    }
    if (rflag) {
        if (sflag)
            rt_stats();
        else
            routepr();
    }

    if (!(iflag || rflag || oflag)) {
        while ((p = getprotoent46())) {
            for (tp = protox; tp->pr_name; tp++) {
                if (strcmp(tp->pr_name, p->p_name) == 0)
                    break;
            }
            if (tp->pr_name == 0 || (tp->pr_wanted == 0 && allprotos == 0))
                continue;
            if (sflag) {
                if (tp->pr_stats)
                    (*tp->pr_stats) ();
            } else if (tp->pr_cblocks)
                (*tp->pr_cblocks) (tp->pr_name);
        }
    }                           /* ! iflag, rflag, oflag */

    endprotoent();
    endservent();
    endnetent();
    endhostent();

    snmp_close(Session);

    SOCK_CLEANUP;
    return 0;
}