Exemplo n.º 1
0
int if_global_nameindex(lua_State *L)
{
  struct if_nameindex *ni, *oni;
  int i = 1;
  oni = ni = if_nameindex();
  lua_newtable(L);
  while (ni && ni->if_index && *(ni->if_name))
    {
      /* at result[i], we store.. */
      lua_pushnumber(L, i);

      /* new table with two items - index, name*/
      lua_newtable(L);
      lua_pushstring(L, "index");
      lua_pushnumber(L, ni->if_index);
      lua_settable(L, -3);

      lua_pushstring(L, "name");
      lua_pushstring(L, ni->if_name);
      lua_settable(L, -3);

      /* Then, actually store it */
      lua_settable(L, -3);

      i++;
      ni++;
    }
  if_freenameindex(oni);
  return 1;
}
Exemplo n.º 2
0
InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void)
{
  DWORD numInterfaces;
  InterfaceIndexTable *ret;
  int fd = socket(PF_INET, SOCK_DGRAM, 0);

  if (fd != -1) {
    struct if_nameindex *indexes = if_nameindex();

    if (indexes) {
      struct if_nameindex *p;
      DWORD size = sizeof(InterfaceIndexTable);

      for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
        if (!isLoopbackInterface(fd, p->if_name))
          numInterfaces++;
      if (numInterfaces > 1)
        size += (numInterfaces - 1) * sizeof(DWORD);
      ret = HeapAlloc(GetProcessHeap(), 0, size);
      if (ret) {
        ret->numIndexes = 0;
        for (p = indexes; p && p->if_name; p++)
          if (!isLoopbackInterface(fd, p->if_name))
            ret->indexes[ret->numIndexes++] = p->if_index;
      }
      if_freenameindex(indexes);
    }
    else
      ret = NULL;
    close(fd);
  }
  else
    ret = NULL;
  return ret;
}
Exemplo n.º 3
0
Arquivo: net.c Projeto: HupuInc/zabbix
int	NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	struct if_nameindex	*ni;
	struct zbx_json		j;
	int			i;

	if (NULL == (ni = if_nameindex()))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);

	zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);

	for (i = 0; 0 != ni[i].if_index; i++)
	{
		zbx_json_addobject(&j, NULL);
		zbx_json_addstring(&j, "{#IFNAME}", ni[i].if_name, ZBX_JSON_TYPE_STRING);
		zbx_json_close(&j);
	}

	if_freenameindex(ni);

	zbx_json_close(&j);

	SET_STR_RESULT(result, strdup(j.buffer));

	zbx_json_free(&j);

	return SYSINFO_RET_OK;
}
Exemplo n.º 4
0
int checkinterface(netdata* data)
{
	int interfacefound = FALSE;
	unsigned int i;
	struct if_nameindex *ifs;

#ifdef DEBUG
    fprintf( stderr, "Checking the interface '%s' now ...\n", data->ifdata.if_name );
#endif

    
	if ((ifs = if_nameindex()) == NULL)
		return FALSE;

	for (i = 0; ifs[i].if_index; i++)
	{
		if (strcmp(ifs[i].if_name, data->ifdata.if_name) == 0)
		{
			interfacefound = TRUE;
			break;
		}
	}
    
    /* free the nameindex structure */
    if_freenameindex(ifs);
    
    /* check if the /proc/net/dev exists */
    if (access(PATH_NET_DEV, R_OK) != 0)
    {
        data->errorcode = PROC_DEVICE_NOT_FOUND;
        return FALSE;
    }

	return interfacefound;
}
Exemplo n.º 5
0
Arquivo: complete.c Projeto: danrl/nsh
unsigned char
complete_ifname(char *word, int list, EditLine *el)
{
	StringList *words;
	size_t wordlen;
	unsigned char rv;   

	words = sl_init();
	wordlen = strlen(word);

	struct if_nameindex *ifn_list, *ifnp;

	if ((ifn_list = if_nameindex()) == NULL)
		return 0;

	for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
                if (wordlen > strlen(ifnp->if_name))
                        continue;
                if (strncmp(word, ifnp->if_name, wordlen) == 0)
                        sl_add(words, ifnp->if_name);
        }

        rv = complete_ambiguous(word, list, words, el);
	if_freenameindex(ifn_list);
        sl_free(words, 0);
        return (rv);
}
Exemplo n.º 6
0
int	NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	int			ret = SYSINFO_RET_FAIL, i;
	struct zbx_json		j;
	struct if_nameindex	*interfaces;

	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);

	zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);

	if (NULL != (interfaces = if_nameindex()))
	{
		i = 0;

		while (0 != interfaces[i].if_index)
		{
			zbx_json_addobject(&j, NULL);
			zbx_json_addstring(&j, "{#IFNAME}", interfaces[i].if_name, ZBX_JSON_TYPE_STRING);
			zbx_json_close(&j);
			i++;
		}

		ret = SYSINFO_RET_OK;
	}

	zbx_json_close(&j);

	SET_STR_RESULT(result, strdup(j.buffer));

	zbx_json_free(&j);

	return ret;
}
Exemplo n.º 7
0
/**
 * net_get_netdev_from_hwaddress - given a hwaddress return the ethX
 * @hwaddress: hw address no larger than ISCSI_HWADDRESS_BUF_SIZE
 * @netdev: buffer of IFNAMSIZ size that will hold the ethX
 *
 * Does not support interfaces like a bond or alias because
 * multiple interfaces will have the same hwaddress.
 */
int net_get_netdev_from_hwaddress(char *hwaddress, char *netdev)
{
	struct if_nameindex *ifni;
	struct ifreq if_hwaddr;
	int found = 0, sockfd, i = 0;
	unsigned char *hwaddr;
	char tmp_hwaddress[ISCSI_HWADDRESS_BUF_SIZE];

	ifni = if_nameindex();
	if (ifni == NULL) {
		log_error("Could not match hwaddress %s to netdev. "
			  "getifaddrs failed %d", hwaddress, errno);
		return errno;
	}

	/* Open a basic socket. */
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		log_error("Could not open socket for ioctl.");
		goto free_ifni;
	}

	for (i = 0; ifni[i].if_index && ifni[i].if_name; i++) {
		struct if_nameindex *n = &ifni[i];

		strlcpy(if_hwaddr.ifr_name, n->if_name, IFNAMSIZ);
		if (ioctl(sockfd, SIOCGIFHWADDR, &if_hwaddr) < 0) {
			log_error("Could not match %s to netdevice.",
				  hwaddress);
			continue;
		}

		/* check for ARPHRD_ETHER (ethernet) */
		if (if_hwaddr.ifr_hwaddr.sa_family != 1)
			continue;
		hwaddr = (unsigned char *)if_hwaddr.ifr_hwaddr.sa_data;

		memset(tmp_hwaddress, 0, ISCSI_HWADDRESS_BUF_SIZE);
		/* TODO should look and covert so we do not need tmp buf */
		sprintf(tmp_hwaddress, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
			hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3],
			hwaddr[4], hwaddr[5]);
		log_debug(4, "Found hardware address %s", tmp_hwaddress);
		if (!strcasecmp(tmp_hwaddress, hwaddress)) {
			log_debug(4, "Matches %s to %s", hwaddress,
				  n->if_name);
			memset(netdev, 0, IFNAMSIZ); 
			strlcpy(netdev, n->if_name, IFNAMSIZ);
			found = 1;
			break;
		}
	}

	close(sockfd);
free_ifni:
	if_freenameindex(ifni);
	if (!found)
		return ENODEV;
	return 0;
}
Exemplo n.º 8
0
void tcp_get_mac_address(rdpTcp * tcp)
{
#ifdef LINUX
	uint8* mac;
	struct ifreq if_req;
	struct if_nameindex* ni;

	ni = if_nameindex();
	mac = tcp->mac_address;

	while (ni->if_name != NULL)
	{
		if (strcmp(ni->if_name, "lo") != 0)
			break;

		ni++;
	}

	strncpy(if_req.ifr_name, ni->if_name, IF_NAMESIZE);

	if (ioctl(tcp->sockfd, SIOCGIFHWADDR, &if_req) != 0)
	{
		freerdp_log(tcp->instance, "failed to obtain MAC address\n");
		return;
	}

	memmove((void*) mac, (void*) &if_req.ifr_ifru.ifru_hwaddr.sa_data[0], 6);
#endif

	/* freerdp_log(tcp->instance, "MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); */
}
Exemplo n.º 9
0
InterfaceIndexTable *getInterfaceIndexTable(void)
{
  DWORD numInterfaces;
  InterfaceIndexTable *ret;
  struct if_nameindex *indexes = if_nameindex();

  if (indexes) {
    struct if_nameindex *p;
    DWORD size = sizeof(InterfaceIndexTable);

    for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
      numInterfaces++;
    if (numInterfaces > 1)
      size += (numInterfaces - 1) * sizeof(DWORD);
    ret = HeapAlloc(GetProcessHeap(), 0, size);
    if (ret) {
      ret->numIndexes = 0;
      for (p = indexes; p && p->if_name; p++)
        ret->indexes[ret->numIndexes++] = p->if_index;
    }
    if_freenameindex(indexes);
  }
  else
    ret = NULL;
  return ret;
}
Exemplo n.º 10
0
struct if_nameindex* If_nameindex(void)
{
    struct if_nameindex* ifptr;

    if ((ifptr = if_nameindex()) == NULL) err_quit("if_nameindex error");
    return (ifptr);
}
Exemplo n.º 11
0
extern char* get_ip()
{

int fd;
struct if_nameindex *curif, *ifs;
struct ifreq req;
char ip[16];
if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1) {
ifs = if_nameindex();
if(ifs) {
for(curif = ifs; curif && curif->if_name; curif++) {
strncpy(req.ifr_name, curif->if_name, IFNAMSIZ);
req.ifr_name[IFNAMSIZ] = 0;
if (ioctl(fd, SIOCGIFADDR, &req) >= 0 && !strcmp(curif->if_name,"eth2")){
sprintf(ip,"%s",inet_ntoa(((struct sockaddr_in*) &req.ifr_addr)->sin_addr));
}
}
if_freenameindex(ifs);
if(close(fd)!=0)
perror("close");
} else
perror("if_nameindex");
} else
perror("socket");
return ip;
}
Exemplo n.º 12
0
Arquivo: join.c Projeto: udhos/nexthop
static int name_to_index(const char *ifname)
{
  struct if_nameindex *ini;
  int ifindex = -1;
  int i;

  if (!ifname)
    return -1;

  ini = if_nameindex();
  if (!ini) {
    int err = errno;
    fprintf(stderr,
	    "%s: interface=%s: failure solving index: errno=%d: %s\n",
	    prog_name, ifname, err, strerror(err));
    errno = err;
    return -1;
  }

  for (i = 0; ini[i].if_index; ++i) {
    if (!strcmp(ini[i].if_name, ifname)) {
      ifindex = ini[i].if_index;
      break;
    }
  }

  if_freenameindex(ini);

  return ifindex;
}
Exemplo n.º 13
0
int	NET_IF_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	struct if_nameindex	*ni;
	struct zbx_json		j;
	int			i;

	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);

	zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);

	for (ni = if_nameindex(), i = 0; 0 != ni[i].if_index; i++)
	{
		zbx_json_addobject(&j, NULL);
		zbx_json_addstring(&j, "{#IFNAME}", ni[i].if_name, ZBX_JSON_TYPE_STRING);
		zbx_json_close(&j);
	}

	if_freenameindex(ni);

	zbx_json_close(&j);

	SET_STR_RESULT(result, strdup(j.buffer));

	zbx_json_free(&j);

	return SYSINFO_RET_OK;
}
Exemplo n.º 14
0
int main( void )
{
    struct if_nameindex *nis;
    char ifname[ IFNAMSIZ ];
    int i;

    nis = if_nameindex();

    if( nis )
    {
        printf("----- List of interfaces -----\n");
        for( i = 0; nis[ i ].if_index != 0; i++ )
            printf("Index %d = [%s]\n", nis[ i ].if_index, nis[ i ].if_name );

        printf("----- End of list -----\n");
        if_freenameindex( nis );

    }
    else
        perror("if_nameindex()");

    i = if_nametoindex("lan0");
    printf("Index of lan0 = %d\n", i );
    printf("name of index %d = [%s]\n", i, if_indextoname( i, ifname ));

    i = if_nametoindex("lo");
    printf("Index of lo = %d\n", i );
    printf("name of index %d = [%s]\n", i, if_indextoname( i, ifname ));

    return 0;
}
Exemplo n.º 15
0
/*
 * read the config file
 */
int
qcmd_config(void)
{
	FILE	*fp;
	int	rval;

	if (if_namelist != NULL)
		if_freenameindex(if_namelist);
	if_namelist = if_nameindex();
	curifname[0] = '\0';

	LOG(LOG_INFO, 0, "ALTQ config file is %s", altqconfigfile);

	fp = fopen(altqconfigfile, "r");
	if (fp == NULL) {
		LOG(LOG_ERR, errno, "can't open %s", altqconfigfile, 0);
		return (QOPERR_INVAL);
	}
	line_no = 0;
	rval = 1;
	while (rval)
		rval = do_command(fp);

	if (!feof(fp)) {
		LOG(LOG_ERR, 0, "Error in %s, line %d.  config failed.",
		    altqconfigfile, line_no);
		(void) qcmd_destroyall();
		rval = QOPERR_INVAL;
	} else
		rval = 0;

	(void)fclose(fp);
	line_no = 0;
	return (rval);
}
Exemplo n.º 16
0
/*
 * see if ("option routers %s;\n",dst) is preset in any possible dhclient
 * lease file
 */
int dhclient_isenabled(char *dst)
{
	int gatewayfound = 0;
	struct stat enst;
	struct if_nameindex *ifn_list, *ifnp;
	char ortext[128];
	char leasefile[sizeof(LEASEPREFIX)+IFNAMSIZ+1];

	if ((ifn_list = if_nameindex()) == NULL) {
		printf("%% dhclient_isenabled: if_nameindex failed\n");
		return 0;
	}

	snprintf(ortext, sizeof(ortext), "  option routers %s;\n", dst);

	for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
		snprintf(leasefile, sizeof(leasefile), "%s.%s", LEASEPREFIX,
		    ifnp->if_name);
		if (stat(leasefile, &enst) == 0 && S_ISREG(enst.st_mode))
			if(scantext(leasefile, ortext)) {
				gatewayfound = 1;
				break;
			}
	}

	if_freenameindex(ifn_list);

	return (gatewayfound);
}
Exemplo n.º 17
0
struct sockaddr_in AddressForInterfaceIndex( int interfaceIndex ) {
	struct sockaddr_in addr;

	memset( &addr, 0, sizeof( addr ) );
	addr.sin_len = sizeof( addr );
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;

	if ( interfaceIndex == 0 ) {
		return addr;
	}
		
	struct if_nameindex *ifnames = if_nameindex();
	if ( !ifnames ) {
		perror( "if_ameindex()" );
		return addr;
	}
	for ( int i = 0 ; ifnames[i].if_index != 0 ; i++ ) {
		if ( ifnames[i].if_index == interfaceIndex ) {
			addr = AddressForInterfaceName( ifnames[i].if_name );
			if_freenameindex( ifnames );
			return addr;
		}
	}	
	printf( "AddressForInterfaceIndex( %i ): Couldn't find interface\n", interfaceIndex );
	if_freenameindex( ifnames );
	return addr;
}
Exemplo n.º 18
0
std::vector<std::string> get_mac_addresses() {
    int mib[6];

    std::vector<std::string> result;

    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;

    auto indices = if_nameindex();

    std::unique_ptr<char> buf;
    size_t buf_size = 0;

    for (auto i = indices; !(i->if_index == 0 && i->if_name == nullptr); ++i) {
        mib[5] = static_cast<int>(i->if_index);

        size_t len;
        if (sysctl(mib, 6, nullptr, &len, nullptr, 0) < 0) {
            perror("sysctl 1 error");
            exit(3);
        }

        if (buf_size < len) {
            buf.reset(new char[len]);
            buf_size = len;
        }

        if (sysctl(mib, 6, buf.get(), &len, nullptr, 0) < 0) {
            perror("sysctl 2 error");
            exit(5);
        }

        auto ifm = reinterpret_cast<if_msghdr*>(buf.get());
        auto sdl = reinterpret_cast<sockaddr_dl*>(ifm + 1);
        auto ptr = reinterpret_cast<unsigned char*>(LLADDR(sdl));

        auto uctoi = [](unsigned char c) -> unsigned {
            return static_cast<unsigned char>(c);
        };

        std::ostringstream oss;
        oss << std::hex;
        oss.fill('0');
        oss.width(2);
        oss << uctoi(*ptr++);
        for (auto j = 0; j < 5; ++j) {
            oss << ":";
            oss.width(2);
            oss << uctoi(*ptr++);
        }
        auto addr = oss.str();
        if (addr != "00:00:00:00:00:00") result.push_back(std::move(addr));
    }
    if_freenameindex(indices);
    return result;
}
Exemplo n.º 19
0
TInt CTestIfioctls::TestIfNameIndex()
	{
	TInt ret = KErrNone;
	struct if_nameindex *ifname;
	ifname = if_nameindex();
	if_freenameindex(ifname);
	return ret;
	}	
Exemplo n.º 20
0
/* Initiale the variables above.  */
static void
setup_interface (void)
{
  struct if_nameindex *list = if_nameindex ();
  if (list != NULL && list[0].if_index != 0 && list[0].if_name[0] != '\0')
    {
      interface_name = list[0].if_name;
      interface_index = list[0].if_index;
    }
}
Exemplo n.º 21
0
void testValues() {
    f = 2;
    struct if_nameindex * ni = if_nameindex();
    if (ni == NULL) {return;}

    if_freenameindex(ni);
    
    //@ assert f == 2;
    //@ assert vacuous: \false;
}
Exemplo n.º 22
0
ointerfaces::ointerfaces ()

{

#if defined (WINPCAP) 

	char buffer [PCAP_ERRBUF_SIZE];
	pcap_if_t * devices = (pcap_if_t *) (0);
	pcap_if_t * device;
	this->mindex = 0;
	this->mcount = 0;
	if (pcap_findalldevs (& devices, buffer) != -1)
	{
		for (device = devices; device; device = device->next)
		{
			this->mcount++;
		}
		this->mtable = new ointerface * [this->mcount];
		for (device = devices; device; device = device->next)
		{
			ointerface * ifo = new ointerface (device->name);
			ifo->Description (device->description);
			this->mtable [this->mindex++] = ifo;
		}
		pcap_freealldevs (devices);
	}

#else

	struct if_nameindex * ifs = if_nameindex ();
	struct if_nameindex * ifp;
	this->mindex = 0;
	this->mcount = 0;
	for (ifp = ifs; ifp->if_index; ++ ifp)
	{
		this->mcount++;
	}
	this->mtable = new ointerface * [this->mcount];
	for (ifp = ifs; ifp->if_index; ++ ifp)
	{
		ointerface * ifo = new ointerface (ifp->if_name);
		ifo->Description (ifp->if_name);
		this->mtable [this->mindex++] = ifo;
	}
	if_freenameindex (ifs);

#endif

	if (! this->mcount)
	{
		this->mtable = new ointerface * [1];
	}
	this->mindex = 0;
	return;
}
Exemplo n.º 23
0
int main(int argc, char *argv[])
{
	struct if_nameindex *table, *iface;

	table = if_nameindex();
	for (iface = table; iface->if_index; ++iface) {
		printf("%i: %s\n", iface->if_index, iface->if_name);
	}
	if_freenameindex(table);
	return 0;
}
Exemplo n.º 24
0
static int do_test(void)
{
  printf("Test 1\n");

  struct if_nameindex *if_ni, *i;
  int n, cnt = 0;

  if_ni = if_nameindex();
  if (if_ni == NULL) {
    perrno_and(return 1, "if_nameindex");
  }
Exemplo n.º 25
0
/*
 *  First, we find all shaper devices and down them. Then we
 *  down all real interfaces. This is because the comment in the
 *  shaper driver says "if you down the shaper device before the
 *  attached inerface your computer will follow".
 */
int ifdown(void)
{
	struct if_nameindex *ifa, *ifp;
	struct ifreq ifr;
	int fd, shaper;

	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		fprintf(stderr, "ifdown: ");
		perror("socket");
		goto error;
	}

	if ((ifa = if_nameindex()) == NULL) {
		fprintf(stderr, "ifdown: ");
		perror("if_nameindex");
		goto error;
	}

	for (shaper = 1; shaper >= 0; shaper--) {
		for (ifp = ifa; ifp->if_index; ifp++) {

			if ((strncmp(ifp->if_name, "shaper", 6) == 0)
			    != shaper) continue;
			if (strcmp(ifp->if_name, "lo") == 0)
				continue;
			if (strchr(ifp->if_name, ':') != NULL)
				continue;

			strncpy(ifr.ifr_name, ifp->if_name, IFNAMSIZ);
			if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
				fprintf(stderr, "ifdown: shutdown ");
				perror(ifp->if_name);
				goto error;
			}
			ifr.ifr_flags &= ~(IFF_UP);
			if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
				fprintf(stderr, "ifdown: shutdown ");
				perror(ifp->if_name);
				goto error;
			}

		}
	}

	close(fd);
	return 0;

error:
	close(fd);
	return -1;
}
Exemplo n.º 26
0
int main(int argc, char **argv) {
	int sockfd = 0;
	int iftype = 0;
	struct if_nameindex *pInterfaces;
	struct if_nameindex *pFreeInterfacesMem;
	ur::WiredInterface *ifWiredHead = NULL;
	std::string ssidname;
	std::string wirelessaddress("notset");

	pInterfaces = pFreeInterfacesMem = if_nameindex();

	while ((pInterfaces != NULL) && (pInterfaces->if_name != NULL)) {
// (DEBUG)		std::cout << "Interface[" << pInterfaces->if_index << "]: " << pInterfaces->if_name << std::endl;
		if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
			continue;

		/* For now we only need to find wireless interfaces.  Eventually
		 * we will enumerate both wired and wireless, so I will just loop through all
		 * interfaces for now and call a 'no_op()' function for the wired interface */
		get_interfaces(sockfd, pInterfaces->if_name, &iftype, &ssidname);
		if(iftype == IFT_WIRED) {
			get_wired_interface(sockfd, pInterfaces->if_name, ifWiredHead);
		}
		if(iftype == IFT_WIRELESS) {
			get_wireless_interface(sockfd, pInterfaces->if_name, &wirelessaddress);
		}
		close(sockfd);
		++pInterfaces;
	}

	// (DEBUG) See if it worked
	std::cout << "SSID: " << ssidname << std::endl;
	std::cout <<  "ADDR: " << wirelessaddress << std::endl;


	/* Open the serial comm port for the LCD display */

	/* Print the SSID and the IPv4 Address */

	/* cleanup and go away */
		if (pFreeInterfacesMem != NULL)
		if_freenameindex(pFreeInterfacesMem);
	while (ifWiredHead != NULL) {
		ur::WiredInterface *pWiredInterface;
		pWiredInterface = ifWiredHead->GetNext();
		delete ifWiredHead;
		ifWiredHead = pWiredInterface;
	}
	return EXEC_SUCCESS;
}
Exemplo n.º 27
0
static unsigned int
if_maxindex()
{
	struct if_nameindex *p, *p0;
	unsigned int max = 0;

	p0 = if_nameindex();
	for (p = p0; p && p->if_index && p->if_name; p++) {
		if (max < p->if_index)
			max = p->if_index;
	}
	if_freenameindex(p0);
	return max;
}
Exemplo n.º 28
0
DWORD getNumInterfaces(void)
{
  DWORD numInterfaces;
  struct if_nameindex *indexes = if_nameindex();

  if (indexes) {
    struct if_nameindex *p;

    for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
      numInterfaces++;
    if_freenameindex(indexes);
  }
  else
    numInterfaces = 0;
  return numInterfaces;
}
Exemplo n.º 29
0
string GetMacAddress() {
  int fd; // Socket descriptor
  struct ifreq sIfReq; // Interface request
  struct if_nameindex *pIfList; // Ptr to interface name index
  struct if_nameindex *pListSave; // Ptr to interface name index
  //// Initialize this function
  pIfList = (struct if_nameindex *)NULL;
  pListSave = (struct if_nameindex *)NULL;
  // Create a socket that we can use for all of our ioctls
  fd = socket( PF_INET, SOCK_STREAM, 0 );
  if ( fd < 0 ) {
    // Socket creation failed, this is a fatal error
    LOG(WARNING) << "Create socket fail";
    return "";
  }
  // Obtain a list of dynamically allocated structures
  pIfList = pListSave = if_nameindex();
  //
  // Walk thru the array returned and query for each interface's
  // address
  //
  for (pIfList; *(char *)pIfList != 0; pIfList++ ) {
    strncpy( sIfReq.ifr_name, pIfList->if_name, IF_NAMESIZE );
    // Get the MAC address for this interface
    if (ioctl(fd, SIOCGIFHWADDR, &sIfReq) != 0) {
      // We failed to get the MAC address for the interface
      LOG(WARNING) << pIfList->if_name << " Ioctl fail:" << strerror(errno);
      continue;
    }
    uint8 *mac_addr = reinterpret_cast<uint8*>(sIfReq.ifr_ifru.ifru_hwaddr.sa_data);
    if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
        mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
      VLOG(2) << pIfList->if_name << " haven't mac address";
      continue;
    }
    string str_mac_address = StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x",
                  mac_addr[0], mac_addr[1], mac_addr[2],
                  mac_addr[3], mac_addr[4], mac_addr[5]);
    VLOG(2) << pIfList->if_name << " mac address : " << str_mac_address;
    if_freenameindex(pListSave);
    close(fd);
    return str_mac_address;
  }
  if_freenameindex(pListSave);
  close(fd);
  return "";
}
Exemplo n.º 30
0
char* getIP(char* interface) {
	char* ip; // Chaîne de retour
	ip = (char*)malloc(16 * sizeof(char));
	int descSock;
	struct ifreq devea;
	struct sockaddr_in *sa;
	//récupération d'un pointeur sur toutes les cartes disponible;
	struct if_nameindex *nameindex = if_nameindex();
	if(nameindex == NULL){
		perror("if_nameindex ");
		exit(errno);
	}
	int i = 0;
	while(1){
		if(nameindex[i].if_name == NULL) break;//il n'y a plus de cates on sort
		//Copie du nom dans la structure ifreq et au passage on prépare l'index pour la prochaine carte
		strcpy(devea.ifr_name, nameindex[i++].if_name);
		//Affichage de celui-ci
		if (strcmp(devea.ifr_name, interface) == 0) {
			//on ouvre un socket pour chacune des cartes que l'on closera après
			descSock = socket(AF_INET, SOCK_DGRAM, 0);
			if (descSock < 0){
				perror("socket ");
				//On relache le pointeur dynamiquement alloue par if_nameindex
				if_freenameindex(nameindex);
				exit(errno);
			}
			//Récupération de l'adresse ip
			if (ioctl(descSock,SIOCGIFADDR, &devea) < 0) {
				printf("ERROR MAYBE NOT STARTED ?\n");
				exit(1);
			} else {
				//Récupère la structure sockaddr_in contenue dans ifreq
				sa = (struct sockaddr_in *)&devea.ifr_ifru.ifru_addr;
				//Convertion en ascii et copie dans la chaîne de retour
				strcpy(ip, inet_ntoa(sa->sin_addr));
			}
			//Ferme le socket pour passer au prochain
			close(descSock);
		}
	}
	//Relache le pointeur dynamiquement alloue par if_nameindex
	if_freenameindex(nameindex);
	return ip;
	
}