コード例 #1
0
ファイル: wifi.c プロジェクト: ericyao2013/ARdroneUAV
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;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: tocchan/guildhall_sd6
//-------------------------------------------------------------------------------------------------------
static SOCKET BindAddress( char const *ip, char const *port, int family = AF_UNSPEC, int type = SOCK_STREAM )
{
   SOCKET host_sock = INVALID_SOCKET;

   addrinfo *addr = AllocAddressesForHost( ip, port, family, type, true ); 
   ForEachAddress( addr, TryToBind, &host_sock );
   FreeAddresses(addr);

   return host_sock;
}
コード例 #3
0
ファイル: main.cpp プロジェクト: tocchan/guildhall_sd6
//-------------------------------------------------------------------------------------------------------
static void NetworkClient( char const *target, char const *port, char const *msg )
{
   char const *host_name = AllocLocalHostName();
   SOCKET sock = BindAddress(host_name, gClientPort, AF_INET, SOCK_DGRAM);
   FreeLocalHostName(host_name);

   if (sock == INVALID_SOCKET) {
      printf( "Could not bind adddress.\n" );
      return;
   }
   
   SpamHelper helper;
   helper.sock = sock;
   helper.msg = msg;

   addrinfo *spam = AllocAddressesForHost( target, port, AF_UNSPEC, SOCK_DGRAM, false );
   ForEachAddress( spam, SpamMessage, &helper ); 
   FreeAddresses( spam );
   
   closesocket( sock );
}
コード例 #4
0
ファイル: IPAddress.c プロジェクト: KingsleyYau/DrPalmEBaby
int GetIPAddresses()
{
	int					nextAddr = 0;
    int                 len = 0, flags = 0;
    char                buffer[BUFFERSIZE] = {'\0'}, *ptr = NULL, lastname[IFNAMSIZ] = {'\0'}, *cptr = NULL, temp[80] = {'\0'};
    struct ifconf       ifc;
    struct ifreq        *ifr, ifrcopy;
    struct sockaddr_in  *sin;    
    int					sockfd;
    
    FreeAddresses();
    
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        return nextAddr;
    }
    
    ifc.ifc_len = BUFFERSIZE;
    ifc.ifc_buf = buffer;
    
    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
    {
        return nextAddr;
    }
    
    lastname[0] = 0;
    
    for (ptr = buffer; ptr < buffer + ifc.ifc_len; ) {
        ifr = (struct ifreq *)ptr;
        len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
        ptr += sizeof(ifr->ifr_name) + len;    // for next one in buffer
        
        if (ifr->ifr_addr.sa_family != AF_INET) {
            continue;    // ignore if not desired address family
        }
        
        if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) {
            *cptr = 0;        // replace colon will null
        }
        
        if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
            continue;    /* already processed this interface */
        }
        
        memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
        
        ifrcopy = *ifr;
        ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
        flags = ifrcopy.ifr_flags;
        if ((flags & IFF_UP) == 0) {
            continue;    // ignore if interface not up
        }
        
        if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);
        if (if_names[nextAddr] == NULL) {
            return nextAddr;
        }
        strcpy(if_names[nextAddr], ifr->ifr_name);
        
        sin = (struct sockaddr_in *)&ifr->ifr_addr;
        strcpy(temp, inet_ntoa(sin->sin_addr));
        
        ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);
        if (ip_names[nextAddr] == NULL) {
            return nextAddr;
        }
        strcpy(ip_names[nextAddr], temp);
        
        ++nextAddr;
    }
    
    close(sockfd);
	return nextAddr;
}