Exemplo n.º 1
0
unsigned long deviceIPAddress(const char *itfName, char *hw_address)
{
	unsigned long theAddr = LOCALHOST;
	
	InitAddresses();
	GetIPAddresses();
	GetHWAddresses();
	
	int i = 0;
	while(i++ < MAXADDRS)
	{
		if (ip_addrs[i] == 0) 
			break;
		
		if (ip_addrs[i] == LOCALHOST) 
			continue;
		
		if ((if_names[i] != NULL) && strcmp(if_names[i], itfName) == 0)
		{
			theAddr = ip_addrs[i];
			if(hw_address != NULL)
				strcpy(hw_address, hw_addrs[i]);
			break;
		}
	}
	
	FreeAddresses();
	//this will get you the right IP from your device in format like 198.111.222.444. If you use the for loop above you will se that ip_names array will also contain localhost IP 127.0.0.1 that's why I don't use it. Eventualy this was code from mac that's why it uses arrays for ip_names as macs can have multiple IPs
	return theAddr;
}
Exemplo n.º 2
0
void FreeAddresses() {
  int i;
  for (i=0; i<MAXADDRS; ++i) {
    if (if_names[i] != 0) free(if_names[i]);
    if (ip_names[i] != 0) free(ip_names[i]);
    if (hw_addrs[i] != 0) free(hw_addrs[i]);
    ip_addrs[i] = 0;
  }
  InitAddresses();
}