Ejemplo n.º 1
0
void if_prop::print_if_info() const{
     HC_LOG_TRACE("");

     if (!is_getaddrs_valid()) {
          HC_LOG_ERROR("data invalid");
          return;
     }

     const if_prop_map* prop = get_if_props();
     if(prop == NULL){
          HC_LOG_ERROR("data struct not found");
          return;
     }

     const struct ifaddrs* if_p;
     const list<const struct ifaddrs*>* if_p_list;

     cout << "##-- IPv4 [count:" << prop->size() << "]--##" << endl;
     for(if_prop_map::const_iterator iter = prop->begin(); iter != prop->end(); iter++){
          if_p = get_ip4_if(iter->first);
          if(if_p == NULL){
               HC_LOG_ERROR("interface name not found: " << iter->first);
               continue;
          }

          print_if_addr(if_p);
     }

     cout << "##-- IPv6 [count:" << prop->size() << "]--##" << endl;
     for(if_prop_map::const_iterator iter = prop->begin(); iter != prop->end(); iter++){
          if_p_list = get_ip6_if(iter->first);

          if(if_p_list == NULL){
               HC_LOG_ERROR("interface name not found: " << iter->first);
               continue;
          }

          for(list<const struct ifaddrs*>::const_iterator itera = if_p_list->begin(); itera != if_p_list->end(); itera++){
               print_if_addr(*itera);
          }
     }
}
Ejemplo n.º 2
0
int print_interface_info(char *if_name){
	int sockfd;
	/*create sockfd for interface test.*/
	if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ){
		err_sys("socket error");
	}

	printf("%s:\n", if_name);
	print_if_addr(sockfd, if_name);
	print_if_broadaddr(sockfd, if_name);
	print_if_mask(sockfd, if_name);
	print_if_mac(sockfd, if_name);
	print_if_mtu(sockfd, if_name);
	close(sockfd);
	return 0;
}
Ejemplo n.º 3
0
int print_all_interface(){
	struct ifconf ifc;
	struct ifreq *ifr_p;
	int sockfd, len, old_len = 0, i, icount;
	char *buf;

	if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ){
		err_sys("socket error");
	}

	len = 10 * sizeof(struct ifreq);
	for(;;){
		if( (buf = malloc(len)) == NULL ){
			err_sys("malloc error");
		}
		ifc.ifc_len = len;
		ifc.ifc_buf = buf;
		if(ioctl(sockfd, SIOCGIFCONF, &ifc) < 0){
			err_sys("ioctl SIOCGIFCONF error");
		}
		if(ifc.ifc_len == old_len)
			break;
		old_len = ifc.ifc_len;
		len += 10 * sizeof(struct ifreq);
		free(buf);
	}
	icount = ifc.ifc_len/sizeof(struct ifreq);
	printf("we have %d interface\n", icount);

	for(i = 0; i < icount; i++){
		ifr_p = &ifc.ifc_req[i];
		printf("\ninterface [%s]:\n", ifr_p->ifr_name);

		print_if_addr(sockfd, ifr_p->ifr_name);
		print_if_broadaddr(sockfd, ifr_p->ifr_name);
		print_if_mask(sockfd, ifr_p->ifr_name);
		print_if_mac(sockfd, ifr_p->ifr_name);
		print_if_mtu(sockfd, ifr_p->ifr_name);
	}
	close(sockfd);
	return 0;
}