int
route_add(route_t *route, const struct route_entry *entry)
{
	MIB_IPFORWARDROW ipfrow;
	struct addr net;

	memset(&ipfrow, 0, sizeof(ipfrow));

	if (GetBestInterface(entry->route_gw.addr_ip,
	    &ipfrow.dwForwardIfIndex) != NO_ERROR)
		return (-1);

	if (addr_net(&entry->route_dst, &net) < 0 ||
	    net.addr_type != ADDR_TYPE_IP)
		return (-1);
	
	ipfrow.dwForwardDest = net.addr_ip;
	addr_btom(entry->route_dst.addr_bits,
	    &ipfrow.dwForwardMask, IP_ADDR_LEN);
	ipfrow.dwForwardNextHop = entry->route_gw.addr_ip;
	ipfrow.dwForwardType = 4;	/* XXX - next hop != final dest */
	ipfrow.dwForwardProto = 3;	/* XXX - MIB_PROTO_NETMGMT */
	
	if (CreateIpForwardEntry(&ipfrow) != NO_ERROR)
		return (-1);
	
	return (0);
}
/////////////////////////////////////////////////////////////////////////////////
// Initializes m_localIP variable, for future access to GetLocalIP()
/////////////////////////////////////////////////////////////////////////////////
void MyUPnP::InitLocalIP()
{
#ifndef _DEBUG
	try
#endif
	{
		DWORD best_if_index;
		GetBestInterface(inet_addr("223.255.255.255"), &best_if_index);

		PMIB_IPADDRTABLE ip_addr_table;
		char buffer[1024];
		ip_addr_table = (PMIB_IPADDRTABLE)buffer;
		DWORD size = sizeof(buffer);
		GetIpAddrTable(ip_addr_table, &size, 0);
		DWORD local_ip = 0;
		for (DWORD i=0; i<ip_addr_table->dwNumEntries; i++) {
			if (ip_addr_table->table[i].dwIndex == best_if_index) {
				local_ip = ip_addr_table->table[i].dwAddr;
				break;
			}
		}

		if (local_ip) {
			struct in_addr addr;
			addr.S_un.S_addr = local_ip;
			m_slocalIP = inet_ntoa(addr);
			m_uLocalIP = local_ip;
		} else {

		char szHost[256];
		if (gethostname(szHost, sizeof szHost) == 0){
			hostent* pHostEnt = gethostbyname(szHost);
			if (pHostEnt != NULL && pHostEnt->h_length == 4 && pHostEnt->h_addr_list[0] != NULL){
				UPNPNAT_MAPPING mapping;
				struct in_addr addr;

				memcpy(&addr, pHostEnt->h_addr_list[0], sizeof(struct in_addr));
				m_slocalIP = inet_ntoa(addr);
				m_uLocalIP = addr.S_un.S_addr;
			}
			else{
				m_slocalIP = _T("");
				m_uLocalIP = 0;
			}
		}
		else{
			m_slocalIP = _T("");
			m_uLocalIP = 0;
		}
		}
	}
#ifndef _DEBUG
	catch(...){
		m_slocalIP = _T("");
		m_uLocalIP = 0;
	}
#endif
}
  /*!
   * @if jp
   * @brief 宛先アドレスから利用されるエンドポイントアドレスを得る
   * @else
   * @brief Getting network interface name from destination address
   * @endif
   */
  bool dest_to_endpoint(std::string dest_addr, std::string& endpoint)
  {
    Winsock winsock;
    {
      struct hostent* hp;
      hp = ::gethostbyname(dest_addr.c_str());
      if (hp == 0) { return false; }

      int i(0);
      while (hp->h_addr_list[i] != 0)
        {
          if(hp->h_addrtype == AF_INET)
            {
              struct sockaddr_in addr;
              memset((char*)&addr, 0, sizeof(addr));
              memcpy((char*)&addr.sin_addr, hp->h_addr_list[i], hp->h_length);
              dest_addr = inet_ntoa(addr.sin_addr);
              break;
            }
          ++i;
        }
    }
    
    UINT ipaddress(inet_addr(dest_addr.c_str()));
    if (ipaddress == INADDR_NONE) { return false; }
    
    DWORD bestifindex;
    if (NO_ERROR != GetBestInterface(ipaddress, &bestifindex)) { return false; }
        
    PMIB_IPADDRTABLE ipaddr_table;
    ipaddr_table = (MIB_IPADDRTABLE *) MALLOC(sizeof (MIB_IPADDRTABLE));
    if (ipaddr_table == 0) { return false; }

    // Make an initial call to GetIpAddrTable to get the
    // necessary size into the size variable
    DWORD size(0);
    if (GetIpAddrTable(ipaddr_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER)
      {
        FREE(ipaddr_table);
        ipaddr_table = (MIB_IPADDRTABLE *) MALLOC(size);
      }
    if (ipaddr_table == 0) { return false; }
    if (GetIpAddrTable(ipaddr_table, &size, 0) != NO_ERROR) { return false; }
    
    for (int i(0); i < (int) ipaddr_table->dwNumEntries; ++i)
      {
        if (bestifindex == ipaddr_table->table[i].dwIndex)
          {
            IN_ADDR inipaddr;
            inipaddr.S_un.S_addr = (u_long) ipaddr_table->table[i].dwAddr;
            endpoint = inet_ntoa(inipaddr);
            return true;
          }
      }
    return false;
  }
void vmsTrafficUsageModeMgr::CheckIfBestInterfaceChanged ()
{
	vmsIpHelper iph;
	if (!iph.GetAdaptersInfo ())
		return;

	DWORD dwIfIndex;
	if (NO_ERROR != GetBestInterface (inet_addr ("8.8.8.8"), &dwIfIndex))
		return;

	PIP_ADAPTER_INFO pAdapter = iph.FindAdapterByIfIndex (dwIfIndex);
	if (!pAdapter)
		return;

	if (pAdapter->IpAddressList.IpAddress.String == m_strCurrentIP)
		return;

	if (!(m_dwState & TUMM_SPEEDS_INFO_CAN_BE_INCORRECT))
		SaveCurrentAdapterInfo ();

	std::string strAdapterAddress;
	for (UINT i = 0; i < pAdapter->AddressLength; i++)
	{
		char sz [10];
		itoa (pAdapter->Address [i], sz, 16);
		if (sz [1] == 0)
			strAdapterAddress += '0';
		strAdapterAddress += sz;
	}

	m_strCurrentAdapterAddress = strAdapterAddress;
	m_strCurrentIP = pAdapter->IpAddressList.IpAddress.String;

	vmsAUTOLOCKSECTION (m_csMisc);
	m_dwState |= TUMM_SPEEDS_INFO_CAN_BE_INCORRECT;
	vmsAUTOLOCKSECTION_UNLOCK (m_csMisc);

	vmsAUTOLOCKSECTION (m_csManageForSpeed);
	
	for (size_t i = 0; i < m_vpManageForSpeed.size (); i++)
	{
		ManageForSpeedItemsList *pList = m_vpManageForSpeed [i];

		for (size_t j = 0; j < pList->vItems.size (); j++)
			pList->vItems [j].state |= ManageForSpeedItem::MSIS_MAY_USE_NOT_CURRENT_ADAPTER;
	}
	vmsAUTOLOCKSECTION_UNLOCK (m_csManageForSpeed);

	ResetAllSpeedsInfo ();
}
Exemple #5
0
/**
 * Retrieve the MAC address of the system
 */
unsigned pixie_get_mac_address(unsigned char macaddr[6])
{
	memset(macaddr, 0, 6);
#ifdef WIN32
	{
		DWORD dwStatus;
		IP_ADAPTER_INFO *p;
		IP_ADAPTER_INFO AdapterInfo[16];
		DWORD dwBufLen = sizeof(AdapterInfo);
		DWORD interface_index = -1;

		GetBestInterface(0x01010101, &interface_index);
		
		dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
		if (dwStatus != ERROR_SUCCESS)
			  return 1;

		for (p=AdapterInfo; p; p = p->Next) {

			if (p->Index == interface_index || interface_index == -1) {
				memcpy(macaddr, p->Address, 6);
				return 0;
			}
			/*(
			printf("[%02x:%02x:%02x:%02x:%02x:%02x]\n",
			mac_address[0], mac_address[1], mac_address[2], 
			mac_address[3], mac_address[4], mac_address[5]
			);
			printf("    %s\n", p->AdapterName);
			printf("    %s\n", p->Description);
			printf("    IP: ");
			for (a = &p->IpAddressList; a; a = a->Next) {
				printf("%s ", a->IpAddress.String);
			}
			printf("\n");
			*/
		}
	}
#else
	return -1;
#endif
	return -1;
}
// ----------------------------------------------------------------------------
// SnoopAutoDetectAdapter
// ----------------------------------------------------------------------------
int SnoopAutoDetectAdapter::detect(QString& host)
{
  IP ip;
  DWORD  bestIfIndex;
  ip = htonl(VNet::resolve(host));
  DWORD res = GetBestInterface(ip, &bestIfIndex);
  if (res != NO_ERROR)
  {
    LOG_FATAL("GetBestInterface return %d", res);
    return snoop::INVALID_ADAPTER_INDEX;
  }

  foreach (const SnoopInterface& intf, SnoopInterfaces::instance())
  {
    if (intf.adapterInfo != NULL && intf.adapterInfo->Index == bestIfIndex)
      return intf.index;
  }

  LOG_FATAL("can not find appropriate adapter for %s", qPrintable(host));
  return snoop::INVALID_ADAPTER_INDEX;;
}
UINT MeasureNet::GetBestInterfaceOrByName(const WCHAR* iface)
{
	if (c_Table == nullptr) return 0;

	if (_wcsicmp(iface, L"BEST") == 0)
	{
		DWORD dwBestIndex;
		if (NO_ERROR == GetBestInterface(INADDR_ANY, &dwBestIndex))
		{
			MIB_IF_ROW2* table = (MIB_IF_ROW2*)c_Table->Table;
			for (size_t i = 0; i < c_NumOfTables; ++i)
			{
				if (table[i].InterfaceIndex == (NET_IFINDEX)dwBestIndex)
				{
					if (GetRainmeter().GetDebug())
					{
						LogDebugF(this, L"Using network interface: Number=(%i), Name=\"%s\"", i + 1, table[i].Description);
					}

					return (UINT)(i + 1);
				}
			}
		}
	}
	else
	{
		MIB_IF_ROW2* table = (MIB_IF_ROW2*)c_Table->Table;
		for (size_t i = 0; i < c_NumOfTables; ++i)
		{
			if (_wcsicmp(iface, table[i].Description) == 0)
			{
				return (UINT)(i + 1);
			}
		}
	}

	LogErrorF(this, L"Cannot find interface: \"%s\"", iface);
	return 0;
}
Exemple #8
0
int
intf_get_dst(intf_t *intf, struct intf_entry *entry, struct addr *dst)
{
	MIB_IFROW ifrow;
	
	if (dst->addr_type != ADDR_TYPE_IP) {
		errno = EINVAL;
		return (-1);
	}
	if (GetBestInterface(dst->addr_ip, &ifrow.dwIndex) != NO_ERROR)
		return (-1);

	if (GetIfEntry(&ifrow) != NO_ERROR)
		return (-1);
	
	if (_refresh_tables(intf) < 0)
		return (-1);
	
	_ifrow_to_entry(intf, &ifrow, entry);
	
	return (0);
}
Exemple #9
0
UINT MeasureNet::GetBestInterfaceOrByName(const WCHAR* iface)
{
	if (c_Table == nullptr) return 0;

	if (_wcsicmp(iface, L"BEST") == 0)
	{
		DWORD dwBestIndex;
		if (NO_ERROR == GetBestInterface(INADDR_ANY, &dwBestIndex))
		{
			if (c_GetIfTable2)
			{
				MIB_IF_ROW2* table = (MIB_IF_ROW2*)((MIB_IF_TABLE2*)c_Table)->Table;
				for (size_t i = 0; i < c_NumOfTables; ++i)
				{
					if (table[i].InterfaceIndex == (NET_IFINDEX)dwBestIndex)
					{
						if (GetRainmeter().GetDebug())
						{
							LogDebugF(this, L"Using network interface: Number=(%i), Name=\"%s\"", i + 1, table[i].Description);
						}

						return (i + 1);
					}
				}
			}
			else
			{
				MIB_IFROW* table = (MIB_IFROW*)((MIB_IFTABLE*)c_Table)->table;
				for (size_t i = 0; i < c_NumOfTables; ++i)
				{
					if (table[i].dwIndex == (NET_IFINDEX)dwBestIndex)
					{
						if (GetRainmeter().GetDebug())
						{
							LogDebugF(this, L"Using network interface: Number=(%i), Name=\"%.*S\"", (int)i + 1, table[i].dwDescrLen, (char*)table[i].bDescr);
						}

						return (i + 1);
					}
				}
			}
		}
	}
	else
	{
		if (c_GetIfTable2)
		{
			MIB_IF_ROW2* table = (MIB_IF_ROW2*)((MIB_IF_TABLE2*)c_Table)->Table;
			for (size_t i = 0; i < c_NumOfTables; ++i)
			{
				if (_wcsicmp(iface, table[i].Description) == 0)
				{
					return (i + 1);
				}
			}
		}
		else
		{
			MIB_IFROW* table = (MIB_IFROW*)((MIB_IFTABLE*)c_Table)->table;
			for (size_t i = 0; i < c_NumOfTables; ++i)
			{
				if (_wcsicmp(iface, StringUtil::Widen((char*)table[i].bDescr).c_str()) == 0)
				{
					return (i + 1);
				}
			}
		}
	}

	LogErrorF(this, L"Cannot find interface: \"%s\"", iface);
	return 0;
}
Exemple #10
0
int
eXosip_guess_ip_for_via (int family, char *address, int size)
{
	/* w2000 and W95/98 */
	unsigned long  best_interface_index;
	DWORD hr;

	/* NT4 (sp4 only?) */
	PMIB_IPFORWARDTABLE ipfwdt;
	DWORD siz_ipfwd_table = 0;
	unsigned int ipf_cnt;

	address[0] = '\0';
	best_interface_index = -1;
	/* w2000 and W95/98 only */
	hr = GetBestInterface(inet_addr("217.12.3.11"),&best_interface_index);
	if (hr)
	{
		LPVOID lpMsgBuf;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			hr,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPSTR) &lpMsgBuf, 0, NULL);

		OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL,
					 "GetBestInterface: %s\r\n", lpMsgBuf));
		best_interface_index = -1;
	}

	if (best_interface_index != -1)
	{ /* probably W2000 or W95/W98 */
		char *servername;
		char *serverip;
		char *netmask;
		OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL,
					 "Default Interface found %i\r\n", best_interface_index));

		if (0 == ppl_dns_get_local_fqdn(&servername, &serverip, &netmask,
						best_interface_index))
		{
			osip_strncpy(address, serverip, size);
			osip_free(servername);
			osip_free(serverip);
			osip_free(netmask);
			return 0;
		}
		return -1;
	}


	if (!GetIpForwardTable(NULL, &siz_ipfwd_table, FALSE) == ERROR_INSUFFICIENT_BUFFER
		|| !(ipfwdt = (PMIB_IPFORWARDTABLE) alloca (siz_ipfwd_table)))
	{
		OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL,
			"Allocation error\r\n"));
		return -1;
	}


	/* NT4 (sp4 support only?) */
	if (!GetIpForwardTable(ipfwdt, &siz_ipfwd_table, FALSE))
	{
		for (ipf_cnt = 0; ipf_cnt < ipfwdt->dwNumEntries; ++ipf_cnt) 
		{
			if (ipfwdt->table[ipf_cnt].dwForwardDest == 0)
			{ /* default gateway found */
				char *servername;
				char *serverip;
				char *netmask;
				OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL,
					"Default Interface found %i\r\n", ipfwdt->table[ipf_cnt].dwForwardIfIndex));

				if (0 ==  ppl_dns_get_local_fqdn(&servername,
								 &serverip,
								 &netmask,
								 ipfwdt->table[ipf_cnt].dwForwardIfIndex))
				{
					osip_strncpy(address, serverip, size);
					osip_free(servername);
					osip_free(serverip);
					osip_free(netmask);
					return 0;
				}
				return -1;
			}
		}

	}
	/* no default gateway interface found */
	return -1;
}
Exemple #11
0
/**
 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on error
 */
int
EnumNICs3 (struct EnumNICs3_results **results, int *results_count)
{
  DWORD dwRetVal = 0;
  int count = 0;
  ULONG flags = /*GAA_FLAG_INCLUDE_PREFIX |*/ GAA_FLAG_SKIP_ANYCAST |
      GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
  struct sockaddr_in6 examplecom6;
  IPAddr examplecom;
  DWORD best_interface = 0;
  DWORD best_interface6 = 0;

  int use_enum2 = 0;
  INTERFACE_INFO *interfaces4 = NULL;
  int interfaces4_len = 0;
  SOCKET_ADDRESS_LIST *interfaces6 = NULL;

  unsigned long outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
  IP_ADAPTER_ADDRESSES *pCurrentAddress = NULL;
  IP_ADAPTER_ADDRESSES *pAddresses = (IP_ADAPTER_ADDRESSES *) GNUNET_malloc (outBufLen);

  if (GetAdaptersAddresses (AF_UNSPEC, flags, NULL, pAddresses, &outBufLen)
      == ERROR_BUFFER_OVERFLOW)
  {
    GNUNET_free (pAddresses);
    pAddresses = (IP_ADAPTER_ADDRESSES *) GNUNET_malloc (outBufLen);
  }

  dwRetVal = GetAdaptersAddresses (AF_UNSPEC, flags, NULL, pAddresses, &outBufLen);

  if (dwRetVal != NO_ERROR)
  {
    GNUNET_free (pAddresses);
    return GNUNET_SYSERR;
  }

  if (pAddresses->Length < sizeof (IP_ADAPTER_ADDRESSES_VISTA))
  {
    use_enum2 = 1;

    /* Enumerate NICs using WSAIoctl() */
    if (GNUNET_OK != EnumNICs2 (&interfaces4, &interfaces4_len, &interfaces6))
    {
      GNUNET_free (pAddresses);
      return GNUNET_SYSERR;
    }
  }

  examplecom = inet_addr("192.0.34.166"); /* www.example.com */
  if (GetBestInterface (examplecom, &best_interface) != NO_ERROR)
    best_interface = 0;

  if (GNGetBestInterfaceEx != NULL)
  {
    examplecom6.sin6_family = AF_INET6;
    examplecom6.sin6_port = 0;
    examplecom6.sin6_flowinfo = 0;
    examplecom6.sin6_scope_id = 0;
    inet_pton (AF_INET6, "2001:500:88:200:0:0:0:10",
        (struct sockaddr *) &examplecom6.sin6_addr);
    dwRetVal = GNGetBestInterfaceEx ((struct sockaddr *) &examplecom6,
        &best_interface6);
    if (dwRetVal != NO_ERROR)
      best_interface6 = 0;
  }

  /* Give IPv6 a priority */
  if (best_interface6 != 0)
    best_interface = best_interface6;

  count = 0;
  for (pCurrentAddress = pAddresses;
      pCurrentAddress != NULL; pCurrentAddress = pCurrentAddress->Next)
  {
    if (pCurrentAddress->OperStatus == IfOperStatusUp)
    {
      IP_ADAPTER_UNICAST_ADDRESS *unicast = NULL;
      for (unicast = pCurrentAddress->FirstUnicastAddress; unicast != NULL;
          unicast = unicast->Next)
      {
        if ((unicast->Address.lpSockaddr->sa_family == AF_INET ||
            unicast->Address.lpSockaddr->sa_family == AF_INET6) &&
            (unicast->DadState == IpDadStateDeprecated ||
            unicast->DadState == IpDadStatePreferred))
          count += 1;
      }
    }
  }

  if (count == 0)
  {
    *results = NULL;
    *results_count = 0;
    GNUNET_free (pAddresses);
    GNUNET_free_non_null (interfaces4);
    GNUNET_free_non_null (interfaces6);
    return GNUNET_OK;
  }

  *results = (struct EnumNICs3_results *) GNUNET_malloc (
      sizeof (struct EnumNICs3_results) * count);
  *results_count = count;

  count = 0;
  for (pCurrentAddress = pAddresses;
      pCurrentAddress != NULL; pCurrentAddress = pCurrentAddress->Next)
  {
    struct EnumNICs3_results *r;
    IP_ADAPTER_UNICAST_ADDRESS *unicast = NULL;
    if (pCurrentAddress->OperStatus != IfOperStatusUp)
      continue;
    for (unicast = pCurrentAddress->FirstUnicastAddress; unicast != NULL;
        unicast = unicast->Next)
    {
      int i, j;
      int mask_length = -1;
      char dst[INET6_ADDRSTRLEN + 1];

      if ((unicast->Address.lpSockaddr->sa_family != AF_INET &&
          unicast->Address.lpSockaddr->sa_family != AF_INET6) ||
          (unicast->DadState != IpDadStateDeprecated &&
          unicast->DadState != IpDadStatePreferred))
        continue;

      r = &(*results)[count];
      r->flags = 0;
      if (pCurrentAddress->IfIndex > 0 &&
          pCurrentAddress->IfIndex == best_interface &&
          unicast->Address.lpSockaddr->sa_family == AF_INET)
        r->is_default = 1;
      else if (pCurrentAddress->Ipv6IfIndex > 0 &&
          pCurrentAddress->Ipv6IfIndex == best_interface6 &&
          unicast->Address.lpSockaddr->sa_family == AF_INET6)
        r->is_default = 1;
      else
        r->is_default = 0;

      /* Don't choose default interface twice */
      if (r->is_default)
        best_interface = best_interface6 = 0;

      if (!use_enum2)
      {
        memcpy (&r->address, unicast->Address.lpSockaddr,
            unicast->Address.iSockaddrLength);
        memset (&r->mask, 0, sizeof (struct sockaddr));
        mask_length = ((IP_ADAPTER_UNICAST_ADDRESS_VISTA *) unicast)->
              OnLinkPrefixLength;
        /* OnLinkPrefixLength is the number of leading 1s in the mask.
         * OnLinkPrefixLength is available on Vista and later (hence use_enum2).
         */
        if (unicast->Address.lpSockaddr->sa_family == AF_INET)
        {
          struct sockaddr_in *m = (struct sockaddr_in *) &r->mask;
          for (i = 0; i < mask_length; i++)
              ((unsigned char *) &m->sin_addr)[i / 8] |= 0x80 >> (i % 8);
        }
        else if (unicast->Address.lpSockaddr->sa_family == AF_INET6)
        {
          struct sockaddr_in6 *m = (struct sockaddr_in6 *) &r->mask;
          struct sockaddr_in6 *b = (struct sockaddr_in6 *) &r->broadcast;
          for (i = 0; i < mask_length; i++)
            ((unsigned char *) &m->sin6_addr)[i / 8] |= 0x80 >> (i % 8);
          memcpy (&r->broadcast, &r->address, unicast->Address.iSockaddrLength);
          for (i = mask_length; i < 128; i++)
            ((unsigned char *) &b->sin6_addr)[i / 8] |= 0x80 >> (i % 8);
        }
        r->flags |= ENUMNICS3_MASK_OK;
      }
      else
      {
Exemple #12
0
bool NetworkObserver::canConnect()
{
#if defined(WIN32)
/* w2000 and W95/98 */
	unsigned long  best_interface_index;
	DWORD hr;

	/* NT4 (sp4 only?) */
	PMIB_IPFORWARDTABLE ipfwdt;
	DWORD siz_ipfwd_table = 0;
	unsigned int ipf_cnt;

	best_interface_index = -1;
	/* w2000 and W95/98 only */
	hr = GetBestInterface(inet_addr("216.151.151.59"),&best_interface_index); // VOXOX -ASV- 07-09-2009: we ping voxox.com to know if voxox has internet
	if (hr)
	{
		LPVOID lpMsgBuf;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			hr,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPSTR) &lpMsgBuf, 0, NULL);
		best_interface_index = -1;
	}

	if (best_interface_index != -1)
	{ /* probably W2000 or W95/W98 */
		char *servername;
		char *serverip;
		char *netmask;

		if (ppl_dns_get_local_fqdn(&servername, &serverip, &netmask,
						best_interface_index))
		{
			/*if(servername)
				delete servername;
			if(serverip)
				delete serverip;
			if(netmask)
				delete netmask;*/
			
			return true;
		}
		return false;
	}


	if (!GetIpForwardTable(NULL, &siz_ipfwd_table, FALSE) == ERROR_INSUFFICIENT_BUFFER
		|| !(ipfwdt = (PMIB_IPFORWARDTABLE) alloca (siz_ipfwd_table)))
	{
		return false;
	}


	/* NT4 (sp4 support only?) */
	if (!GetIpForwardTable(ipfwdt, &siz_ipfwd_table, FALSE))
	{
		for (ipf_cnt = 0; ipf_cnt < ipfwdt->dwNumEntries; ++ipf_cnt) 
		{
			if (ipfwdt->table[ipf_cnt].dwForwardDest == 0)
			{ /* default gateway found */
				char *servername;
				char *serverip;
				char *netmask;

				if (ppl_dns_get_local_fqdn(&servername,
								 &serverip,
								 &netmask,
								 ipfwdt->table[ipf_cnt].dwForwardIfIndex))
				{
					delete servername;
					delete serverip;
					delete netmask;
					return true;
				}
				return false;
			}
		}

	}
	/* no default gateway interface found */
	return true;

#else

	#if defined(OS_MACOSX)
		socklen_t len;
	#else
		unsigned int len;
	#endif
	int sock_rt, on=1;
	struct sockaddr_in iface_out;

	struct sockaddr_in remote;

	memset(&remote, 0, sizeof(struct sockaddr_in));

	remote.sin_family = AF_INET;
	remote.sin_addr.s_addr = inet_addr("216.151.151.59");// VOXOX -ASV- 07-09-2009: we ping voxox.com to know if voxox has internet
	remote.sin_port = htons(80);

	memset(&iface_out, 0, sizeof(iface_out));
	sock_rt = socket(AF_INET, SOCK_DGRAM, 0 );

	if (setsockopt(sock_rt, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) 
	{
		close(sock_rt);
		return false;
	}

	if (::connect(sock_rt, (struct sockaddr*)&remote, sizeof(struct sockaddr_in)) == -1 ) 
	{
		close(sock_rt);
		return false;
	}

	len = sizeof(iface_out);
	if (getsockname(sock_rt, (struct sockaddr *)&iface_out, &len) == -1 ) 
	{
		close(sock_rt);
		return false;
	}

	close(sock_rt);
	if (iface_out.sin_addr.s_addr == 0)
	{ /* what is this case?? */
		return false;
	}

	return true;

#endif
}
Exemple #13
0
IPCCommandResult NetIPTop::HandleGetInterfaceOptRequest(const IOCtlVRequest& request)
{
  const u32 param = Memory::Read_U32(request.in_vectors[0].address);
  const u32 param2 = Memory::Read_U32(request.in_vectors[0].address + 4);
  const u32 param3 = Memory::Read_U32(request.io_vectors[0].address);
  const u32 param4 = Memory::Read_U32(request.io_vectors[1].address);
  u32 param5 = 0;

  if (request.io_vectors[0].size >= 8)
  {
    param5 = Memory::Read_U32(request.io_vectors[0].address + 4);
  }

  INFO_LOG(IOS_NET, "IOCTLV_SO_GETINTERFACEOPT(%08X, %08X, %X, %X, %X) "
                    "BufferIn: (%08x, %i), BufferIn2: (%08x, %i) ",
           param, param2, param3, param4, param5, request.in_vectors[0].address,
           request.in_vectors[0].size,
           request.in_vectors.size() > 1 ? request.in_vectors[1].address : 0,
           request.in_vectors.size() > 1 ? request.in_vectors[1].size : 0);

  switch (param2)
  {
  case 0xb003:  // dns server table
  {
    u32 address = 0;
#ifdef _WIN32
    if (!Core::WantsDeterminism())
    {
      PIP_ADAPTER_ADDRESSES AdapterAddresses = nullptr;
      ULONG OutBufferLength = 0;
      ULONG RetVal = 0, i;
      for (i = 0; i < 5; ++i)
      {
        RetVal = GetAdaptersAddresses(AF_INET, 0, nullptr, AdapterAddresses, &OutBufferLength);

        if (RetVal != ERROR_BUFFER_OVERFLOW)
        {
          break;
        }

        if (AdapterAddresses != nullptr)
        {
          FREE(AdapterAddresses);
        }

        AdapterAddresses = (PIP_ADAPTER_ADDRESSES)MALLOC(OutBufferLength);
        if (AdapterAddresses == nullptr)
        {
          RetVal = GetLastError();
          break;
        }
      }
      if (RetVal == NO_ERROR)
      {
        unsigned long dwBestIfIndex = 0;
        IPAddr dwDestAddr = (IPAddr)0x08080808;
        // If successful, output some information from the data we received
        PIP_ADAPTER_ADDRESSES AdapterList = AdapterAddresses;
        if (GetBestInterface(dwDestAddr, &dwBestIfIndex) == NO_ERROR)
        {
          while (AdapterList)
          {
            if (AdapterList->IfIndex == dwBestIfIndex && AdapterList->FirstDnsServerAddress &&
                AdapterList->OperStatus == IfOperStatusUp)
            {
              INFO_LOG(IOS_NET, "Name of valid interface: %S", AdapterList->FriendlyName);
              INFO_LOG(
                  IOS_NET, "DNS: %u.%u.%u.%u",
                  (unsigned char)AdapterList->FirstDnsServerAddress->Address.lpSockaddr->sa_data[2],
                  (unsigned char)AdapterList->FirstDnsServerAddress->Address.lpSockaddr->sa_data[3],
                  (unsigned char)AdapterList->FirstDnsServerAddress->Address.lpSockaddr->sa_data[4],
                  (unsigned char)
                      AdapterList->FirstDnsServerAddress->Address.lpSockaddr->sa_data[5]);
              address = Common::swap32(
                  *(u32*)(&AdapterList->FirstDnsServerAddress->Address.lpSockaddr->sa_data[2]));
              break;
            }
            AdapterList = AdapterList->Next;
          }
        }
      }
      if (AdapterAddresses != nullptr)
      {
        FREE(AdapterAddresses);
      }
    }
#endif
    if (address == 0)
      address = 0x08080808;

    Memory::Write_U32(address, request.io_vectors[0].address);
    Memory::Write_U32(0x08080404, request.io_vectors[0].address + 4);
    break;
  }
  case 0x1003:  // error
    Memory::Write_U32(0, request.io_vectors[0].address);
    break;

  case 0x1004:  // mac address
    u8 address[Common::MAC_ADDRESS_SIZE];
    IOS::Net::GetMACAddress(address);
    Memory::CopyToEmu(request.io_vectors[0].address, address, sizeof(address));
    break;

  case 0x1005:  // link state
    Memory::Write_U32(1, request.io_vectors[0].address);
    break;

  case 0x3001:  // hardcoded value
    Memory::Write_U32(0x10, request.io_vectors[0].address);
    break;

  case 0x4002:  // ip addr numberHandle
    Memory::Write_U32(1, request.io_vectors[0].address);
    break;

  case 0x4003:  // ip addr table
    Memory::Write_U32(0xC, request.io_vectors[1].address);
    Memory::Write_U32(inet_addr(10, 0, 1, 30), request.io_vectors[0].address);
    Memory::Write_U32(inet_addr(255, 255, 255, 0), request.io_vectors[0].address + 4);
    Memory::Write_U32(inet_addr(10, 0, 255, 255), request.io_vectors[0].address + 8);
    break;

  case 0x4005:  // hardcoded value
    Memory::Write_U32(0x20, request.io_vectors[0].address);
    break;

  case 0x6003:  // hardcoded value
    Memory::Write_U32(0x80, request.io_vectors[0].address);
    break;

  case 0x600a:  // hardcoded value
    Memory::Write_U32(0x80, request.io_vectors[0].address);
    break;

  case 0x600c:  // hardcoded value
    Memory::Write_U32(0x80, request.io_vectors[0].address);
    break;

  case 0xb002:  // hardcoded value
    Memory::Write_U32(2, request.io_vectors[0].address);
    break;

  default:
    ERROR_LOG(IOS_NET, "Unknown param2: %08X", param2);
    break;
  }

  return GetDefaultReply(0);
}