Exemplo n.º 1
0
_WCRTLINK struct protoent *getprotobyname(const char *name)
{
    struct protoent *ret;
    int i;

    if( name == NULL ) {
        _RWD_errno = EINVAL;
        return( NULL );
    }

    setprotoent( 1 );

    ret = getprotoent();
    while( ret != NULL ) {
        if( ret->p_name != NULL && strcmp(name, ret->p_name) == 0 )
            goto protobyname_cleanup;

        for( i = 0; ret->p_aliases != NULL && ret->p_aliases[i] != NULL; i++ ) {
            if( strcmp( name, ret->p_aliases[i] ) == 0 ) {
                goto protobyname_cleanup;
            }
        }

        ret = getprotoent();
    }

protobyname_cleanup:

    endprotoent();

    return( ret );
}
Exemplo n.º 2
0
/*
 * protocols
 */
static int
protocols(int argc, char *argv[])
{
	struct protoent	*pe;
	unsigned long	id;
	int		i, rv;

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

#define PROTOCOLSPRINT	printfmtstrings(pe->p_aliases, "  ", " ", \
			    "%-16s  %5d", pe->p_name, pe->p_proto)

	setprotoent(1);
	rv = RV_OK;
	if (argc == 2) {
		while ((pe = getprotoent()) != NULL)
			PROTOCOLSPRINT;
	} else {
		for (i = 2; i < argc; i++) {
			if (parsenum(argv[i], &id))
				pe = getprotobynumber((int)id);
			else
				pe = getprotobyname(argv[i]);
			if (pe != NULL)
				PROTOCOLSPRINT;
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endprotoent();
	return rv;
}
Exemplo n.º 3
0
/*
 * Find the protox corresponding to name.
 */
static struct protox *
name2protox(const char *name)
{
	struct protox *tp;
	char **alias;			/* alias from p->aliases */
	struct protoent *p;

	/*
	 * Try to find the name in the list of "well-known" names. If that
	 * fails, check if name is an alias for an Internet protocol.
	 */
	if ((tp = knownname(name)) != NULL)
		return (tp);

	setprotoent(1);			/* make protocol lookup cheaper */
	while ((p = getprotoent()) != NULL) {
		/* assert: name not same as p->name */
		for (alias = p->p_aliases; *alias; alias++)
			if (strcmp(name, *alias) == 0) {
				endprotoent();
				return (knownname(p->p_name));
			}
	}
	endprotoent();
	return (NULL);
}
Exemplo n.º 4
0
int normalize_LTX_manager_plugin_init(requiem_plugin_entry_t *pe, void *root_opt)
{
        requiem_option_t *opt;
        requiem_plugin_instance_t *pi;
        static manager_decode_plugin_t normalize;

#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        setprotoent(1);
#endif

        memset(&normalize, 0, sizeof(normalize));

        requiem_plugin_set_name(&normalize, "Normalize");
        manager_decode_plugin_set_running_func(&normalize, normalize_run);
        requiem_plugin_entry_set_plugin(pe, (void *) &normalize);

        requiem_option_add(root_opt, &opt, REQUIEM_OPTION_TYPE_CFG,
                           0, "normalize", "Option for the normalize plugin", REQUIEM_OPTION_ARGUMENT_NONE, NULL, NULL);

        requiem_option_add(opt, NULL, REQUIEM_OPTION_TYPE_CFG,
                           '6', "ipv6-only", "Map IPv4 addresses to IPv6",
                           REQUIEM_OPTION_ARGUMENT_NONE, normalize_to_ipv6_cb, NULL);

        requiem_option_add(opt, NULL, REQUIEM_OPTION_TYPE_CFG,
                           '4', "keep-ipv4-mapped-ipv6",
                           "Do not normalize IPv4 mapped IPv6 address to IPv4",
                           REQUIEM_OPTION_ARGUMENT_NONE, normalize_keep_ipv6, NULL);

        return requiem_plugin_new_instance(&pi, (void *) &normalize, NULL, NULL);
}
Exemplo n.º 5
0
static int
protocols(int argc, char *argv[])
{
	struct protoent	*pe;
	int		i, rv = RV_OK;

	setprotoent(1);
	if (argc == 2) {
		while ((pe = getprotoent()) != NULL)
			PROTOCOLSPRINT;
	} else {
		for (i = 2; i < argc; i++) {
			const char	*err;
			long long id = strtonum(argv[i], 0, UINT_MAX, &err);

			if (!err)
				pe = getprotobynumber((int)id);
			else
				pe = getprotobyname(argv[i]);
			if (pe != NULL)
				PROTOCOLSPRINT;
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endprotoent();
	return rv;
}
Exemplo n.º 6
0
setprotoent_r(int stay_open)
#endif
{
	setprotoent(stay_open);
#ifdef PROTO_R_SET_RESULT
	return (PROTO_R_SET_RESULT);
#endif
}
Exemplo n.º 7
0
setprotoent_r(int stay_open)
#endif
{
#ifdef PROTO_R_ENT_UNUSED
        PROTO_R_ENT_UNUSED;
#endif
	setprotoent(stay_open);
#ifdef PROTO_R_SET_RESULT
	return (PROTO_R_SET_RESULT);
#endif
}
Exemplo n.º 8
0
/*
 * getinetproto --
 *	print name of protocol number
 */
void
getinetproto(int number)
{
    static int isopen;
    struct protoent *pe;

    if (!isopen)
        setprotoent(++isopen);
    if ((pe = getprotobynumber(number)) != NULL)
        printf(" %s", pe->p_name);
    else
        printf(" %d", number);
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
    struct protoent *protoent_p = NULL;

    setprotoent(0);
    while ((protoent_p = getprotoent()) != NULL) {
        printf("%-20s\t", protoent_p->p_name);
        printf("%d\t\n", protoent_p->p_proto);
    }
    endprotoent();

    return 0;
}
Exemplo n.º 10
0
/*
 * setup parse
 */
void
zonec_setup_parser(namedb_type* db)
{
	region_type* rr_region = region_create(xalloc, free);
	parser = zparser_create(db->region, rr_region, db);
	assert(parser);
	/* Unique pointers used to mark errors.	 */
	error_dname = (dname_type *) region_alloc(db->region, 1);
	error_domain = (domain_type *) region_alloc(db->region, 1);
	/* Open the network database */
	setprotoent(1);
	setservent(1);
}
Exemplo n.º 11
0
int ex_byname(void)
{
    struct protoent *pp;

    setprotoent(FLAGS_stayopen); 

    if ( (pp = getprotobyname("tcp")) != NULL ) {
        dump_prot(pp); 
    }
    if ( (pp = getprotobyname("TCP")) != NULL ) {
        dump_prot(pp); 
    }
    return 0; 
}
Exemplo n.º 12
0
/*
 * call-seq:
 *    Proto.getprotobynumber(num)
 *
 * Given a protocol number, returns the corresponding string, or nil if not
 * found.
 *
 * Examples:
 *
 *    Net::Proto.getprotobynumber(6)   # => "tcp"
 *    Net::Proto.getprotobynumber(999) # => nil
 */
static VALUE np_getprotobynumber(VALUE klass, VALUE v_proto_num){
   struct protoent p;
   char buffer[BUF_SIZE];
   VALUE v_proto_name = Qnil;

   setprotoent(0);
   
   if(getprotobynumber_r(NUM2INT(v_proto_num),&p,buffer,BUF_SIZE) != NULL)
      v_proto_name = rb_str_new2(p.p_name);

   endprotoent();

   return v_proto_name;
}
Exemplo n.º 13
0
/* The getprotoent() function reads the next line from the file
   `/etc/protocols' and returns a structure protoent containing the broken
   out fields from the line. The `/etc/protocols' file is opened if 
   necessary.  */
struct protoent *
getprotoent (void)
{
  char line[MAX_NAMLEN], *end, *p;
  int done = 0, i, n, a;
  struct protoent *e = NULL;

  /* Ensure a open file.  */
  if (scm_i_protoent.fd == NULL || feof (scm_i_protoent.fd))
    {
      setprotoent (1);
      if (scm_i_protoent.fd == NULL)
	return NULL;
    }

  while (!done)
    {
      /* Get new line.  */
      if (fgets (line, MAX_NAMLEN, scm_i_protoent.fd) != NULL)
	{
	  end = scm_i_socket_uncomment (line);

	  /* Scan the line.  */
	  if ((i = sscanf (line, "%s %d%n", 
			   scm_i_protoent.name,
			   &scm_i_protoent.proto, &n)) != 2)
	    continue;

	  /* Scan the remaining aliases.  */
	  p = line + n;
	  for (a = 0; a < MAX_ALIASES && p < end && i != -1 && n > 1; 
	       a++, p += n)
	    i = sscanf (p, "%s%n", scm_i_protoent.alias[a], &n);

	  /* Prepare the return value.  */
	  e = &scm_i_protoent.ent;
	  e->p_name = scm_i_protoent.name;
	  e->p_proto = scm_i_protoent.proto;
	  e->p_aliases = scm_i_protoent.aliases;
	  scm_i_protoent.aliases[a] = NULL;
	  while (a--)
	    scm_i_protoent.aliases[a] = scm_i_protoent.alias[a];
	  done = 1;
	}
      else
	break;
    }
  return done ? e : NULL;
}
Exemplo n.º 14
0
static int
protoent_fill_test_data(struct protoent_test_data *td)
{
    struct protoent *pe;

    setprotoent(1);
    while ((pe = getprotoent()) != NULL) {
        if (protoent_test_correctness(pe, NULL) == 0)
            TEST_DATA_APPEND(protoent, td, pe);
        else
            return (-1);
    }
    endprotoent();

    return (0);
}
Exemplo n.º 15
0
/*
 * call-seq:
 *    Proto.getprotobyname(name)
 *
 * Given a protocol string, returns the corresponding number, or nil if not
 * found.
 *
 * Examples:
 *
 *    Net::Proto.getprotobyname("tcp")   # => 6
 *    Net::Proto.getprotobyname("bogus") # => nil
*/
static VALUE np_getprotobyname(VALUE klass, VALUE v_proto_name){
   struct protoent p;
   char buffer[BUF_SIZE];
   VALUE v_proto_num = Qnil;
   
   SafeStringValue(v_proto_name);

   setprotoent(0);
   
   if(getprotobyname_r(StringValuePtr(v_proto_name),&p,buffer,BUF_SIZE) != NULL)
      v_proto_num = INT2FIX(p.p_proto);

   endprotoent();

   return v_proto_num;
}
Exemplo n.º 16
0
int getprotobynumber_r (int proto_num,
			struct protoent *result_buf,
			char *buf, size_t buflen,
			struct protoent **result)
{
    int ret;

    __UCLIBC_MUTEX_LOCK(mylock);
    setprotoent(proto_stayopen);
    while (!(ret=getprotoent_r(result_buf, buf, buflen, result)))
	if (result_buf->p_proto == proto_num)
	    break;
    if (!proto_stayopen)
	endprotoent();
    __UCLIBC_MUTEX_UNLOCK(mylock);
    return *result?0:ret;
}
Exemplo n.º 17
0
struct protoent * getprotobynumber(int proto) {
	struct protoent *pe;

	setprotoent(1);

	while ((pe = getprotoent()) != NULL) {
		/* same proto? */
		if (pe->p_proto == proto) {
			break;
		}
		/* nope. try again */
	}

	endprotoent();

	return pe;
}
Exemplo n.º 18
0
struct protoent *
getprotobynumber(
	int proto )
{
	register struct protoent *p;

	setprotoent(_proto_stayopen);
	while ( (p = getprotoent()) )
		if (p->p_proto == proto)
			break;
	if (!_proto_stayopen)
		endprotoent();

	if ( !p )
		p = getprotobynumber_static(proto);
	return (p);
}
Exemplo n.º 19
0
/* Drop root privileges and chroot if necessary */
static void
droproot(const char *username, const char *chroot_dir)
{
	struct passwd *pw = NULL;

	if (chroot_dir && !username) {
		fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
		exit(1);
	}
	
	pw = getpwnam(username);
	if (pw) {
		if (initgroups(pw->pw_name, pw->pw_gid) != 0) {
			fprintf(stderr, "tcpdump: Couldn't initgroups to "
			    "'%.32s' gid=%lu: %s\n", pw->pw_name,
			    (unsigned long)pw->pw_gid,
			    pcap_strerror(errno));
			exit(1);
		}
		if (chroot_dir) {
			setprotoent(1);
			res_init();
			if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
				fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
				    chroot_dir, pcap_strerror(errno));
				exit(1);
			}
		}
		if (setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
			fprintf(stderr, "tcpdump: Couldn't change to "
			    "'%.32s' uid=%lu gid=%lu: %s\n",
			    username, 
			    (unsigned long)pw->pw_uid,
			    (unsigned long)pw->pw_gid,
			    pcap_strerror(errno));
			exit(1);
		}
	}
	else {
		fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
		    username);
		exit(1);
	}
}
Exemplo n.º 20
0
int ex_set(void)
{
    struct protoent *pp;

    errno = 0;

    if ( (pp = getprotoent())!=NULL ) dump_prot(pp); 
    if ( (pp = getprotoent())!=NULL ) dump_prot(pp); 

    setprotoent(FLAGS_stayopen); 

    if ( (pp = getprotoent())!=NULL ) dump_prot(pp); 
    if ( (pp = getprotoent())!=NULL ) dump_prot(pp); 

    endprotoent(); 

    return 0; 

}
Exemplo n.º 21
0
void ReadProtoFile (const char *fname)
{
  static int been_here = 0;

  if (!fname || !*fname)
     return;

  if (been_here)  /* loading multiple protocol files */
  {
    free (protoFname);
    fclose (protoFile);
    protoFile = NULL;
  }
  been_here = 1;

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

  setprotoent (1);
  if (!protoFile)
     return;

  while (1)
  {
    struct _protoent *p, *p2 = (struct _protoent*) getprotoent();

    if (!p2)
       break;

    p = malloc (sizeof(*p));
    if (!p)
    {
      outsnl ("Protocol-file too big!\7");
      break;
    }
    *p = *p2;
    p->next = _proto0;
    _proto0 = p;
  }
  rewind (protoFile);
  atexit (endprotoent);
}
Exemplo n.º 22
0
struct protoent *getprotobyname_r(const char *name, struct protoent *result,
								  char *buf, int bufsize)
{
	char **alias;

	pthread_mutex_lock(&proto_iterate_lock);
	setprotoent(0);
	while ((result = getprotoent_r(result, buf, bufsize)) != NULL) {
		/* Check the entry's name and aliases against the given name. */
		if (strcmp(result->p_name, name) == 0)
			break;
		for (alias = result->p_aliases; *alias != 0; alias++) {
			if (strcmp(*alias, name) == 0)
				break;
		}
	}
	pthread_mutex_unlock(&proto_iterate_lock);
	return result;
}
Exemplo n.º 23
0
struct protoent *
getprotobyname(const char *name)
{
register struct protoent *p;
register char **cp;

	setprotoent(_proto_stayopen);
	while (p = getprotoent()) {
		if (strcmp(p->p_name, name) == 0)
			break;
		for (cp = p->p_aliases; *cp != 0; cp++)
			if (strcmp(*cp, name) == 0)
				goto found;
	}
found:
	if (!_proto_stayopen)
		endprotoent();
	return (p);
}
Exemplo n.º 24
0
static void
test_protocols (void)
{
  struct protoent *prptr;

  prptr = getprotobyname ("IP");
  output_protoent ("getprotobyname (\"IP\")", prptr);

  prptr = getprotobynumber (1);
  output_protoent ("getprotobynumber (1)", prptr);

  setprotoent (0);
  do
    {
      prptr = getprotoent ();
      output_protoent ("getprotoent ()", prptr);
    }
  while (prptr != NULL);
  endprotoent ();
}
Exemplo n.º 25
0
		void SocketProtocol::loadProtocols(){
			read_m.lock();
			if(__all_p.size() == 0){
				setprotoent(0);
				struct protoent *pro;
				while((pro = getprotoent()) != 0){
					SocketProtocol sp(pro->p_proto, pro->p_name);
					for(int i = 0; ; i++){
						if(pro->p_aliases[i] != 0){
							sp.addAlias(pro->p_aliases[i]);
						}
						else{
							break;
						}
					}
					__all_p.push_back(sp);
				}
			}
			read_m.unlock();
		}
Exemplo n.º 26
0
static int read_services(void)
{
    struct servent *se;
    struct protoent *pe;
    struct service *item;

    setservent(1);
    while ((se = getservent())) {
	/* Allocate a service entry. */
	item = (struct service *) xmalloc(sizeof(struct service));
	item->name = strdup(se->s_name);
	item->number = se->s_port;

	/* Fill it in. */
	if (!strcmp(se->s_proto, "tcp")) {
	    add2list(&tcp_name, item);
	} else if (!strcmp(se->s_proto, "udp")) {
	    add2list(&udp_name, item);
	} else if (!strcmp(se->s_proto, "sctp")) {
	    add2list(&sctp_name, item);
	} else if (!strcmp(se->s_proto, "raw")) {
	    add2list(&raw_name, item);
	} else { /* ddp, dccp */
	    free(item->name);
	    free(item);
	}
    }
    endservent();
    setprotoent(1);
    while ((pe = getprotoent())) {
	/* Allocate a service entry. */
	item = (struct service *) xmalloc(sizeof(struct service));
	item->name = strdup(pe->p_name);
	item->number = htons(pe->p_proto);
	add2list(&raw_name, item);
    }
    endprotoent();
    return (0);
}
Exemplo n.º 27
0
/*
 * call-seq:
 *    Proto.getprotoent
 *    Proto.getprotoent{ |struct| ... }
 *
 * In block form, yields each entry from /etc/protocols as a struct of type
 * Proto::ProtoStruct.  In non-block form, returns an array of
 * Proto::ProtoStruct objects.
	
 * The fields are 'name' (a String), 'aliases' (an Array of String's,
 * though often only one element), and 'proto' (a Fixnum).
 *
 * Example:
 *
 *   Net::Proto.getprotoent.each{ |prot|
 *      p prot.name
 *      p prot.aliases
 *      p prot.proto
 *   }
*/
static VALUE np_getprotoent(){
   struct protoent p;
   char buffer[BUF_SIZE];
   VALUE v_alias_array = Qnil;
   VALUE v_array = Qnil;
   VALUE v_struct = Qnil;
   
   if(!rb_block_given_p())
   	v_array = rb_ary_new();

   setprotoent(0);

   while(getprotoent_r(&p, buffer, BUF_SIZE)){
      v_alias_array = rb_ary_new();
      
      while(*p.p_aliases){
         rb_ary_push(v_alias_array ,rb_str_new2(*p.p_aliases));
         (void)p.p_aliases++;
      }
      
      v_struct = rb_struct_new(sProto,
      	rb_str_new2(p.p_name),
      	v_alias_array,
      	INT2FIX(p.p_proto)
      );

      OBJ_FREEZE(v_struct); /* This is read-only data */

      if(rb_block_given_p())
         rb_yield(v_struct);
      else
         rb_ary_push(v_array, v_struct);
   }

   endprotoent();

   return v_array; /* nil unless a block is given */
}
Exemplo n.º 28
0
int getprotobyname_r(const char *name,
		    struct protoent *result_buf,
		    char *buf, size_t buflen,
		    struct protoent **result)
{
    register char **cp;
    int ret;

    __UCLIBC_MUTEX_LOCK(mylock);
    setprotoent(proto_stayopen);
    while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) {
	if (strcmp(result_buf->p_name, name) == 0)
	    break;
	for (cp = result_buf->p_aliases; *cp != 0; cp++)
	    if (strcmp(*cp, name) == 0)
		goto found;
    }
found:
    if (!proto_stayopen)
	endprotoent();
    __UCLIBC_MUTEX_UNLOCK(mylock);
    return *result?0:ret;
}
Exemplo n.º 29
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;
}
Exemplo n.º 30
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;
    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;
}