Ejemplo n.º 1
0
/* display info about network interfaces */
void display_ip_address()
{

  //unsigned char      *u;
  int                sockfd, size  = 1;
  struct ifreq       *ifr;
  struct ifconf      ifc;
  struct sockaddr_in sa;
  int                i = 0;

  if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
          fprintf(stderr, "Cannot open socket.\n");
    exit(EXIT_FAILURE);
  }

  ifc.ifc_len = IFRSIZE;
  ifc.ifc_req = NULL;

  do {
    ++size;
    /* realloc buffer size until no overflow occurs  */
    if (NULL == (ifc.ifc_req = realloc(ifc.ifc_req, IFRSIZE))) {
      fprintf(stderr, "Out of memory.\n");
      exit(EXIT_FAILURE);
    }
    ifc.ifc_len = IFRSIZE;
    if (ioctl(sockfd, SIOCGIFCONF, &ifc)) {
      perror("ioctl SIOCFIFCONF");
      exit(EXIT_FAILURE);
    }
  } while  (IFRSIZE <= ifc.ifc_len);


  ifr = ifc.ifc_req;
  for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr) {

    if (ifr->ifr_addr.sa_data == (ifr+1)->ifr_addr.sa_data) {
      continue;  /* duplicate, skip it */
    }

    if (ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
      continue;  /* failed to get flags, skip it */
    }

    if ( !(strncmp("127.0.0.1", inet_ntoa(inaddrr(ifr_addr.sa_data)), 9) == 0))
    {
      strcpy(ms_server_ip_addr[i], inet_ntoa(inaddrr(ifr_addr.sa_data)));
      strcpy(ms_server_if_name[i], ifr->ifr_name);
      //printf("\t\t%d. %s: %s\n", i+1, ifr->ifr_name, ms_server_ip_addr[i]);
      i++;
    }


  }

  close(sockfd);
  return ;
}
Ejemplo n.º 2
0
static int get_addr(int sock, char * ifname) {

  struct ifreq *ifr;
  struct ifreq ifrr;
  struct sockaddr_in sa;
  unsigned char      *u;
  int i;

  ifr = &ifrr;
  ifrr.ifr_addr.sa_family = AF_INET;
  strncpy(ifrr.ifr_name, ifname, sizeof(ifrr.ifr_name));

  if (ioctl(sock, SIOCGIFADDR, ifr) < 0) {
    printf("No %s interface.\n", ifname);
    return -1;
  }

  ip_address=inaddrr(ifr_addr.sa_data).s_addr;

  if (ioctl(sock, SIOCGIFHWADDR, ifr) < 0) {
    printf("No %s interface.\n", ifname);
    return -1;
  }

  u = (unsigned char *) &ifr->ifr_addr.sa_data;

  for(i=0;i<6;i++)
      hw_address[i]=u[i];


  return 0;
}
Ejemplo n.º 3
0
list<string> getLocalIP()
{
//	unsigned char      *u;
	int                sockfd, size  = 1;
	struct ifreq       *ifr;
	struct ifconf      ifc;
	struct sockaddr_in sa;
	list<string> ret;

	if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
		fprintf(stderr, "Cannot open socket.\n");
//		exit(EXIT_FAILURE);
		return ret;
	}

	ifc.ifc_len = IFRSIZE;
	ifc.ifc_req = NULL;

	do {
		++size;
		/* realloc buffer size until no overflow occurs  */
		if (NULL == (ifc.ifc_req = static_cast<struct ifreq*>(realloc(ifc.ifc_req, IFRSIZE)))) {
			fprintf(stderr, "Out of memory.\n");
//			exit(EXIT_FAILURE);
			return ret;
		}

		ifc.ifc_len = IFRSIZE;
		if (ioctl(sockfd, SIOCGIFCONF, &ifc)) {
			perror("ioctl SIOCFIFCONF");
//			exit(EXIT_FAILURE);
			return ret;
		}
	} while  (IFRSIZE <= ifc.ifc_len);


	ifr = ifc.ifc_req;
	for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr) {

		if (ifr->ifr_addr.sa_data == (ifr+1)->ifr_addr.sa_data) {
			continue;  /* duplicate, skip it */
		}

		if (ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
			continue;  /* failed to get flags, skip it */
		}

		//    printf("Interface:  %s\n", ifr->ifr_name);

		if (strcmp(ifr->ifr_name, "lo" ) )
		{
			//cout << "IP Address: " << inet_ntoa(inaddrr(ifr_addr.sa_data)) << endl;
			ret.push_back(inet_ntoa(inaddrr(ifr_addr.sa_data)));
		}

	}

	close(sockfd);
	return ret;
}
Ejemplo n.º 4
0
char *bb_getMyIP()
{

	#ifdef WIN32

		struct sockaddr_in ipAdress;
	
		char buffer[80];
		gethostname(buffer,sizeof(buffer));
		hostent *phe = gethostbyname(buffer);
		memcpy(&ipAdress.sin_addr, phe->h_addr_list[0], sizeof(struct in_addr));
		//printf("%s\n",buffer);
		return inet_ntoa(ipAdress.sin_addr);

	#else

		unsigned char      *u;
		int                sockfd, size  = 1;
		struct ifreq       *ifr;
		struct ifconf      ifc;
		struct sockaddr_in sa;

		if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)))
		{
			perror("Socket Creation");
			return 0;
		}
		
		ifc.ifc_len = IFRSIZE;
		ifc.ifc_req = NULL;
		
		do
		{
			++size;
			/* realloc buffer size until no overflow occurs  */
			if (NULL == (ifc.ifc_req = (ifreq*)realloc(ifc.ifc_req, IFRSIZE)))
			{
				perror("Realloc buffer size");
				return 0;
			}
	
			ifc.ifc_len = IFRSIZE;
			if (ioctl(sockfd, SIOCGIFCONF, &ifc))
			{
				perror("ioctl SIOCFIFCONF");
				return 0;
			}
		} while  (IFRSIZE <= ifc.ifc_len);

		
		ifr = ifc.ifc_req;
		int ii = 0;
		for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr)
		{
		
			if (ifr->ifr_addr.sa_data == (ifr+1)->ifr_addr.sa_data)
			{
				continue;  /* duplicate, skip it */
			}
		
			if (ioctl(sockfd, SIOCGIFFLAGS, ifr))
			{
				continue;  /* failed to get flags, skip it */
			}

			ii++;
			
			if(ii==2)
			{
				CloseSocket(sockfd);
				return inet_ntoa(inaddrr(ifr_addr.sa_data));
			}

		}

		return 0;


	#endif

}
Ejemplo n.º 5
0
//*****************************************************************************
//
NETADDRESS_s NETWORK_GetLocalAddress( void )
{
	char				szBuffer[512];
	struct sockaddr_in	SocketAddress;
	NETADDRESS_s		Address;
	int					iNameLength;

#ifndef __WINE__
	gethostname( szBuffer, 512 );
#endif
	szBuffer[512-1] = 0;

	// Convert the host name to our local 
	bool stringToAddress = NETWORK_StringToAddress( szBuffer, &Address );

	iNameLength = sizeof( SocketAddress );
#ifndef	WIN32
	if ( getsockname ( g_NetworkSocket, (struct sockaddr *)&SocketAddress, (socklen_t *)&iNameLength) == -1 )
#else
	if ( getsockname ( g_NetworkSocket, (struct sockaddr *)&SocketAddress, &iNameLength ) == -1 )
#endif
	{
		Printf( "NETWORK_GetLocalAddress: Error getting socket name: %s", strerror( errno ));
	}

#ifdef unix
	// [BB] The "gethostname -> gethostbyname" trick didn't reveal the local IP.
	// Now we need to resort to something more complicated.
	if ( stringToAddress == false );
	{
#ifndef __FreeBSD__
		unsigned char      *u;
		int                size  = 1;
		struct ifreq       *ifr;
		struct ifconf      ifc;
		struct sockaddr_in sa;
		
		ifc.ifc_len = IFRSIZE;
		ifc.ifc_req = NULL;
		
		do {
			++size;
			/* realloc buffer size until no overflow occurs  */
			if (NULL == (ifc.ifc_req = (ifreq*)realloc(ifc.ifc_req, IFRSIZE)))
			{
				fprintf(stderr, "Out of memory.\n");
				exit(EXIT_FAILURE);
			}
			ifc.ifc_len = IFRSIZE;
			if (ioctl(g_NetworkSocket, SIOCGIFCONF, &ifc))
			{
				perror("ioctl SIOCFIFCONF");
				exit(EXIT_FAILURE);
			}
		} while  (IFRSIZE <= ifc.ifc_len);
		
		ifr = ifc.ifc_req;
		for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr)
		{
		
			if (ifr->ifr_addr.sa_data == (ifr+1)->ifr_addr.sa_data)
			{
				continue;  /* duplicate, skip it */
			}
		
			if (ioctl(g_NetworkSocket, SIOCGIFFLAGS, ifr))
			{
				continue;  /* failed to get flags, skip it */
			}
		
			Printf("Found interface %s", ifr->ifr_name);
			Printf(" with IP address: %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
			*(int *)&Address.abIP = *(int *)&inaddrr(ifr_addr.sa_data);
			if ( Address.abIP[0] != 127 )
			{
				Printf ( "Using IP address of interface %s as local address.\n", ifr->ifr_name );
				break;
			}
		}
		if ( ifc.ifc_req != NULL )
			free ( ifc.ifc_req );
#else
		struct ifreq       *ifr;
		struct ifconf      ifc;
		bzero(&ifc, sizeof(ifc));
		unsigned int n = 1;
		struct ifreq *lifr;
		ifr = (ifreq*)calloc( ifc.ifc_len, sizeof(*ifr) );
		do
		{
			n *= 2;
			ifr = (ifreq*)realloc( ifr, PAGE_SIZE * n );
			bzero( ifr, PAGE_SIZE * n );
			ifc.ifc_req = ifr;
			ifc.ifc_len = n * PAGE_SIZE;
		} while( ( ioctl( g_NetworkSocket, SIOCGIFCONF, &ifc ) == -1 ) || ( ifc.ifc_len >= ( (n-1) * PAGE_SIZE)) );
		
		lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
		
		while (ifr < lifr)
		{
			struct sockaddr *sa = &ifr->ifr_ifru.ifru_addr;
			if( AF_INET == sa->sa_family )
			{
				struct sockaddr_in dummysa;
				in_addr inAddr = *(struct in_addr *) &ifr->ifr_addr.sa_data[sizeof dummysa.sin_port];
	
				Printf("Found interface %s", ifr->ifr_name);
				Printf(" with IP address: %s\n", inet_ntoa(inAddr));
				*(int *)&Address.abIP = *(int *)&inAddr;
				if ( Address.abIP[0] != 127 )
				{
					Printf ( "Using IP address of interface %s as local address.\n", ifr->ifr_name );
					break;
				}
			 }
	 	ifr = (struct ifreq *)(((char *)ifr) + _SIZEOF_ADDR_IFREQ(*ifr));
 		}
#endif
	}
#endif

	Address.usPort = SocketAddress.sin_port;
	return ( Address );
}
Ejemplo n.º 6
0
vp_com_config_t* wifi_config(void)
{
	static vp_com_wifi_config_t config =
	{
		{ 0 },
		WIFI_MOBILE_IP,
		WIFI_NETMASK,
		WIFI_BROADCAST,
		WIFI_GATEWAY,
		WIFI_SERVER,
		WIFI_INFRASTRUCTURE,
		WIFI_SECURE,
		WIFI_PASSKEY,
		{ 0 },
	};

	unsigned char *u;
	int sockfd, size = 1;
	struct ifreq *ifr;
	struct ifconf ifc;
	struct sockaddr_in sa;

	if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
		fprintf(stderr, "Cannot open socket.\n");
		exit(EXIT_FAILURE);
	}

	ifc.ifc_len = IFRSIZE;
	ifc.ifc_req = NULL;

	do {
		++size;
		/* realloc buffer size until no overflow occurs */
		if (NULL == (ifc.ifc_req = vp_os_realloc(ifc.ifc_req, IFRSIZE))) {
			fprintf(stderr, "Out of memory.\n");
			exit(EXIT_FAILURE);
		}
		ifc.ifc_len = IFRSIZE;
		if (ioctl(sockfd, SIOCGIFCONF, &ifc)) {
			perror("ioctl SIOCFIFCONF");
			exit(EXIT_FAILURE);
		}
	} while (IFRSIZE <= ifc.ifc_len);

	ifr = ifc.ifc_req;
	for (; (char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr) {

		if (ifr->ifr_addr.sa_data == (ifr + 1)->ifr_addr.sa_data) {
			continue; /* duplicate, skip it */
		}

		if (ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
			continue; /* failed to get flags, skip it */
		}

		struct in_addr addr;
		addr = inaddrr(ifr_addr.sa_data);

		if ( (ntohl(addr.s_addr) & 0xFFFFFF00) == WIFI_BASE_ADDR )
		{
			inet_ntop(AF_INET, &addr, config.localHost, VP_COM_NAME_MAXSIZE);

			//INTERFACE
			memcpy(config.itfName, ifr->ifr_name, strlen(ifr->ifr_name));

			//IP ADDRESS
			addr.s_addr = htonl ( ntohl(addr.s_addr) - ( ( ( ntohl(addr.s_addr) & 0xFF ) - 1 ) % 5 ) );
			memcpy(config.server, inet_ntoa(addr), strlen(inet_ntoa(addr)));

			//NET MASK
			if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) && strcmp(
					"255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {

				addr = inaddrr(ifr_addr.sa_data);
				inet_ntop(AF_INET, &addr, config.netmask, VP_COM_NAME_MAXSIZE);
			}

			//BROADCAST ADDRESS
			if (ifr->ifr_flags & IFF_BROADCAST) {
				if (0 == ioctl(sockfd, SIOCGIFBRDADDR, ifr) && strcmp("0.0.0.0",
						inet_ntoa(inaddrr(ifr_addr.sa_data)))) {

					addr = inaddrr(ifr_addr.sa_data);
					inet_ntop(AF_INET, &addr, config.broadcast, VP_COM_NAME_MAXSIZE);
				}
			}

			break;
		}
	}

	close(sockfd);
	return (vp_com_config_t*)&config;
}