Exemplo n.º 1
0
static void
test_network (void)
{
  struct netent *nptr;
  u_int32_t ip;

  /*
     This test needs the following line in /etc/networks:
     loopback        127.0.0.0
  */
  nptr = getnetbyname ("loopback");
  output_netent ("getnetbyname (\"loopback\")",nptr);

  nptr = getnetbyname ("LoopBACK");
  output_netent ("getnetbyname (\"LoopBACK\")",nptr);

  ip = inet_network ("127.0.0.0");
  nptr = getnetbyaddr (ip, AF_INET);
  output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);

  setnetent (0);
  do
    {
      nptr = getnetent ();
      output_netent ("getnetent ()", nptr);
    }
  while (nptr != NULL);
  endnetent ();
}
Exemplo n.º 2
0
static int
networks(int argc, char *argv[])
{
	int		i, rv = RV_OK;
	struct netent	*ne;
	in_addr_t	net;

	setnetent(1);
	if (argc == 2) {
		while ((ne = getnetent()) != NULL)
			networksprint(ne);
	} else {
		for (i = 2; i < argc; i++) {
			net = inet_network(argv[i]);
			if (net != INADDR_NONE)
				ne = getnetbyaddr(net, AF_INET);
			else
				ne = getnetbyname(argv[i]);
			if (ne != NULL)
				networksprint(ne);
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endnetent();
	return rv;
}
Exemplo n.º 3
0
void parse_end(void)
{
    endprotoent() ;
    endpwent() ;
    endgrent() ;
    endnetent() ;
    endhostent() ;
}
Exemplo n.º 4
0
endnetent_r()
#endif
{
#ifdef NET_R_ENT_ARGS
	UNUSED(ndptr);
#endif
	endnetent();
	NET_R_END_RESULT(NET_R_OK);
}
Exemplo n.º 5
0
struct netent *getnetbyaddr (uint32_t net, int type)
{
	register struct netent *p;

	setnetent(_net_stayopen);
	while ((p = getnetent()))
		if (p->n_addrtype == type && p->n_net == net)
			break;
	if (!_net_stayopen)
		endnetent();
	return (p);
}
Exemplo n.º 6
0
struct netent *
getnetbyaddr_p(unsigned long net, int type, struct net_data *net_data) {
	struct irs_nw *nw;
	u_char addr[4];
	int bits;

	if (!net_data || !(nw = net_data->nw))
		return (NULL);
	if (net_data->nw_stayopen && net_data->nw_last)
		if (type == net_data->nw_last->n_addrtype &&
		    net == net_data->nw_last->n_net)
			return (net_data->nw_last);

	/* cannonize net(host order) */
	if (net < 256UL) {
		net <<= 24;
		bits = 8;
	} else if (net < 65536UL) {
		net <<= 16;
		bits = 16;
	} else if (net < 16777216UL) {
		net <<= 8;
		bits = 24;
	} else
		bits = 32;

	/* convert to net order */
	addr[0] = (0xFF000000 & net) >> 24;
	addr[1] = (0x00FF0000 & net) >> 16;
	addr[2] = (0x0000FF00 & net) >> 8;
	addr[3] = (0x000000FF & net);

	/* reduce bits to as close to natural number as possible */
	if ((bits == 32) && (addr[0] < 224) && (addr[3] == 0)) {
		if ((addr[0] < 192) && (addr[2] == 0)) {
			if ((addr[0] < 128) && (addr[1] == 0))
				bits = 8;
			else
				bits = 16;
		} else {
			bits = 24;
		}
	}

	net_data->nww_last = (*nw->byaddr)(nw, addr, bits, AF_INET);
	net_data->nw_last = nw_to_net(net_data->nww_last, net_data);
	if (!net_data->nw_stayopen)
		endnetent();
	return (net_data->nw_last);
}
Exemplo n.º 7
0
/*
 * Return the next (non-commented) line from the network-file
 * Format is:
 *   name [=] net [alias..] {\n | # ..}
 *
 * e.g.
 *   loopback     127
 *   arpanet      10   arpa
 */
struct netent * getnetent (void)
{
  static struct netent n;
  char  *name, *net, *alias;
  char   buf[100];

  if (!netdb_init())
     return (NULL);

  do
  {
    if (!fgets(buf,sizeof(buf)-1,networkFile))
       return (NULL);
  }
  while (buf[0] == '#' || buf[0] == ';' || buf[0] == '\n');

  if (networkClose)
     endnetent();

  name = strtok (buf, " \t");
  net  = strtok (NULL,"= \t\n");

  n.n_net  = inet_network (net);
  n.n_name = strdup (name);
  if (!n.n_name)
     return (NULL);

  n.n_addrtype = AF_INET;
  n.n_aliases  = NULL;
  alias        = strtok (NULL," \t\n");

  if (alias && *alias != '#' && *alias != ';')
  {
    char **alist = calloc ((1+MAX_NETW_ALIASES) * sizeof(char*), 1);
    int  i = 0;
    do
    {
      if (*alias == '#' || *alias == ';')
         break;
      if (!alist || (i == MAX_NETW_ALIASES) ||
          (alist[i++] = strdup(alias)) == NULL)
         break;
      alias = strtok (NULL," \t\n");
    }
    while (alias);
    n.n_aliases = alist;
  }
  return (&n);
}
Exemplo n.º 8
0
/*
 * Return the next (non-commented) line from the network-file
 * Format is:
 *   name [=] net [alias..] {\n | # ..}
 *
 * e.g.
 *   loopback     127
 *   arpanet      10   arpa
 */
struct netent * W32_CALL getnetent (void)
{
  struct _netent n;
  char  *name, *net, *alias;
  char   buf [2*MAX_NAMELEN], *tok;
  int    i;

  if (!netdb_init())
     return (NULL);

  while (1)
  {
    if (!fgets(buf,sizeof(buf),networkFile))
       return (NULL);

    tok = strltrim (buf);
    if (*tok == '#' || *tok == ';' || *tok == '\n')
       continue;

    name = strtok (tok, " \t");
    net  = strtok (NULL, "= \t\n");
    if (name && net)
       break;
  }

  if (networkClose)
     endnetent();

  memset (&n, 0, sizeof(n));
  n.n_net  = inet_network (net);
  n.n_name = name;
  alias    = strtok (NULL, " \t\n");

  for (i = 0; alias && i < MAX_NETENT_ALIASES; i++)
  {
    static char aliases [MAX_NETENT_ALIASES][MAX_NAMELEN];

    if (*alias == '#' || *alias == ';')
       break;

    n.n_aliases[i] = StrLcpy (aliases[i], alias, sizeof(aliases[i]));
    alias = strtok (NULL, " \t\n");
  }
  return fill_netent (&n);
}
Exemplo n.º 9
0
struct netent *
getnetbyname(const char *name)
{
	register struct netent *p;
	register char **cp;

	setnetent(_net_stayopen);
	while (p = getnetent()) {
		if (strcmp(p->n_name, name) == 0)
			break;
		for (cp = p->n_aliases; *cp != 0; cp++)
			if (strcmp(*cp, name) == 0)
				goto found;
	}
found:
	if (!_net_stayopen)
		endnetent();
	return (p);
}
Exemplo n.º 10
0
struct netent *
getnetbyname_p(const char *name, struct net_data *net_data) {
	struct irs_nw *nw;
	struct netent *np;
	char **nap;

	if (!net_data || !(nw = net_data->nw))
		return (NULL);
	if (net_data->nw_stayopen && net_data->nw_last) {
		if (!strcmp(net_data->nw_last->n_name, name))
			return (net_data->nw_last);
		for (nap = net_data->nw_last->n_aliases; nap && *nap; nap++)
			if (!strcmp(name, *nap))
				return (net_data->nw_last);
	}
	if ((np = fakeaddr(name, AF_INET, net_data)) != NULL)
		return (np);
	net_data->nww_last = (*nw->byname)(nw, name, AF_INET);
	net_data->nw_last = nw_to_net(net_data->nww_last, net_data);
	if (!net_data->nw_stayopen)
		endnetent();
	return (net_data->nw_last);
}
Exemplo n.º 11
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.º 12
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;
}
Exemplo n.º 13
0
void
nw_endnetent()
{
        endnetent();
}
Exemplo n.º 14
0
_WCRTLINK struct netent *getnetent( void )
{
    static struct netent    ret;
    char                    *alias;

    /* For loading a line from the services file */
    static char             *buf = NULL;
    static size_t           buflen = 0;
    char                    *buf_ptr;

    /* Translating the port string */
    char *address;


    /* For line parsing */
    int i;

    if( __netFile == NULL )
        setnetent( 0 );

    /* First pass */
    if( buf == NULL ) {
        buflen = 64;
        buf = (char *)malloc( buflen );
        if( buf ) {
            *buf = '\0';
        } else {
            buflen = 0;
        }
    }

    if( __getconfigline( &buf, &buflen, __netFile ) < 0 )
        return( NULL );

    if( __netClose )
        endnetent();

    /* Name */
    ret.n_name = strtok_r( buf, " \t", &buf_ptr );

    /* OW only supports AF_INET for now */
    ret.n_addrtype  = AF_INET;

    /* Address */
    address = strtok_r( NULL, " \t", &buf_ptr );
    ret.n_net = inet_addr(address);

    /* Load in any aliases */
    if(ret.n_aliases == NULL)
        ret.n_aliases = (char **)malloc( (1 + MAX_NET_ALIASES)*sizeof( char * ) );

    /* Explcitly NULL the entries in a nice, readable manner */
    for( i = 0; i < (1 + MAX_NET_ALIASES) && ret.n_aliases != NULL; i++ )
        ret.n_aliases[i] = NULL;

    alias = strtok_r( NULL, " \t", &buf_ptr );

    if( alias && *alias != '#' && *alias != ';' && ret.n_aliases != NULL ) {
        i = -1;
        do {
            i++;
            if( *alias == '#' || *alias == ';' )
                break;

            ret.n_aliases[i] = strtok_r( NULL, " \t", &buf_ptr );

            if( ret.n_aliases[i] == NULL ) {
                break;
            } else if( ret.n_aliases[i][0] == '\0' ) {
                ret.n_aliases[i] = NULL;
                break;
            }

        } while( i < MAX_NET_ALIASES );

    }

    return( &ret );
}
Exemplo n.º 15
0
struct netent * getnetent(void) {
	char netent_buff[NETENT_BUFF_SZ];
	struct netent *ne, *result;
	char *tmp, *name, *net_str;
	struct in_addr net;

	/* prepare file (if needed) */
	if (networks == NULL) {
		setnetent(0);
	}

	result = NULL;

	/* read hostent from file */
	while (1) {
		/* Format: <name> <net> [alias, ...] */

		/* get line */
		tmp = fgets(&netent_buff[0], sizeof netent_buff, networks);
		if (tmp == NULL) {
			break;
		}

		/* skip comments */
		if (*tmp == '#') {
			continue;
		}

		/* create netent structure */
		ne = netent_create();
		if (ne == NULL) {
			break;
		}

		/* get main name */
		skip_spaces(tmp);
		name = tmp;
		skip_word(tmp);
		*tmp++ = '\0';
		if (*name == '\0') {
			continue; /* unspecified official name */
		}
		if (netent_set_name(ne, name) != 0) {
			break; /* error occurred */
		}

		/* get net */
		skip_spaces(tmp);
		net_str = tmp;
		skip_word(tmp);
		if (*tmp == '\0') {
			continue; /* invalid format */
		}
		*tmp++ = '\0';
		if (!inet_aton(net_str, &net)) {
			continue; /* bad ip */
		}
		if (netent_set_addr_info(ne, AF_INET) != 0) {
			break; /* error occurred */
		}
		if (netent_set_net(ne, ntohl(net.s_addr)) != 0) {
			break; /* error occurred */
		}

		/* get alternate name */
		while (1) {
			skip_spaces(tmp);
			name = tmp;
			skip_word(tmp);
			*tmp++ = '\0';
			if (*name == '\0') {
				break;
			}
			if (netent_add_alias(ne, name) != 0) {
				break;
			}
		}

		/* save result */
		result = ne;

		/* exit */
		break;
	}

	/* close file (if needed) */
	if (is_disposable) {
		endnetent();
	}

	return result;
}