Ejemplo n.º 1
0
int mupnp_net_gethostinterfaces(mUpnpNetworkInterfaceList* netIfList)
{
  mupnp_log_debug_l4("Entering...\n");

  mUpnpNetworkInterface* netIf;
  struct in_addr inAddr;
  char ipaddr[MUPNP_NET_IPV6_ADDRSTRING_MAXSIZE];
  int kaRet;

  mupnp_socket_startup();

  inAddr.s_addr = 0;
  kaRet = ka_tfGetIpAddress(kaInterfaceHandle, &(inAddr.s_addr), 0);
  if (kaRet != 0)
    return 0;

  ka_tfInetToAscii((unsigned long)inAddr.s_addr, ipaddr);

  netIf = mupnp_net_interface_new();
  mupnp_net_interface_setname(netIf, MUPNP_NET_DEFAULT_IFNAME);
  mupnp_net_interface_setaddress(netIf, ipaddr);
  mupnp_net_interfacelist_add(netIfList, netIf);

  return mupnp_net_interfacelist_size(netIfList);

  mupnp_log_debug_l4("Leaving...\n");
}
Ejemplo n.º 2
0
int mupnp_net_gethostinterfaces(mUpnpNetworkInterfaceList* netIfList)
{
  mupnp_log_debug_l4("Entering...\n");

  mUpnpNetworkInterface* netIf;
  struct hostent hostEnt;
  B buf[HBUFLEN];
  ERR err;
  char* ifname = MUPNP_NET_DEFAULT_IFNAME;
  char ifaddr[32];

  mupnp_net_interfacelist_clear(netIfList);

  err = so_gethostbyname("localhost", &hostEnt, buf);
  if (err != 0)
    return 0;

  inet_ntop(hostEnt.h_addrtype, hostEnt.h_addr, ifaddr, sizeof(ifname));

  netIf = mupnp_net_interface_new();
  mupnp_net_interface_setname(netIf, ifname);
  mupnp_net_interface_setaddress(netIf, ifaddr);
  mupnp_net_interfacelist_add(netIfList, netIf);

  mupnp_log_debug_l4("Leaving...\n");

  return mupnp_net_interfacelist_size(netIfList);
}
Ejemplo n.º 3
0
int mupnp_net_gethostinterfaces(mUpnpNetworkInterfaceList* netIfList)
{
  mupnp_log_debug_l4("Entering...\n");

  mUpnpNetworkInterface* netIf;
  FILE* fd;
  int s;
  char buffer[256 + 1];
  char ifaddr[20 + 1];
  char* ifname;
  char* sep;

  mupnp_net_interfacelist_clear(netIfList);

  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0)
    return 0;
  fd = fopen(PATH_PROC_NET_DEV, "r");
  fgets(buffer, sizeof(buffer) - 1, fd);
  fgets(buffer, sizeof(buffer) - 1, fd);
  while (!feof(fd)) {
    ifname = buffer;
    sep;
    if (fgets(buffer, sizeof(buffer) - 1, fd) == NULL)
      break;
    sep = strrchr(buffer, ':');
    if (sep)
      *sep = 0;
    while (*ifname == ' ')
      ifname++;
    struct ifreq req;
    strcpy(req.ifr_name, ifname);
    if (ioctl(s, SIOCGIFFLAGS, &req) < 0)
      continue;
    if (!(req.ifr_flags & IFF_UP))
      continue;
    if (req.ifr_flags & IFF_LOOPBACK)
      continue;
    if (ioctl(s, SIOCGIFADDR, &req) < 0)
      continue;
    strncpy(ifaddr, inet_ntoa(((struct sockaddr_in*)&req.ifr_addr)->sin_addr), sizeof(ifaddr) - 1);
    netIf = mupnp_net_interface_new();
    mupnp_net_interface_setname(netIf, ifname);
    mupnp_net_interface_setaddress(netIf, ifaddr);
    mupnp_net_interfacelist_add(netIfList, netIf);
    mupnp_log_debug("Interface name: %s, address: %s\n", ifname, ifaddr);
    //cout << ifname << ", " << ifaddr << endl;
  }
  fclose(fd);
  close(s);
  return mupnp_net_interfacelist_size(netIfList);

  mupnp_log_debug_l4("Leaving...\n");
}
Ejemplo n.º 4
0
mUpnpNetworkInterface *mupnp_net_interface_getany()
{
	mUpnpNetworkInterface *netIf;

	mupnp_log_debug_l4("Entering...\n");

	netIf = mupnp_net_interface_new();
	/* It would be more approriate to use INADDR_ANY, but this will do */
	mupnp_net_interface_setname(netIf, "INADDR_ANY");
	mupnp_net_interface_setaddress(netIf, "0.0.0.0");
	
	mupnp_log_debug_l4("Leaving...\n");

	return netIf;
}
Ejemplo n.º 5
0
int mupnp_net_gethostinterfaces(mUpnpNetworkInterfaceList* netIfList)
{
  mUpnpNetworkInterface* netIf;

  mupnp_log_debug_l4("Entering...\n");

  if (IsInterfaceAddressInitialized == false)
    return 0;

  netIf = mupnp_net_interface_new();
  mupnp_net_interface_setname(netIf, "");
  mupnp_net_interface_setaddress(netIf, InterfaceAddress);
  mupnp_net_interfacelist_add(netIfList, netIf);

  mupnp_log_debug_l4("Leaving...\n");

  return mupnp_net_interfacelist_size(netIfList);
}
Ejemplo n.º 6
0
int mupnp_net_gethostinterfaces(mUpnpNetworkInterfaceList* netIfList)
{
  mUpnpNetworkInterface* netIf;
  struct ifaddrs* ifaddr;
  char addr[NI_MAXHOST + 1];
  char netmask[NI_MAXHOST + 1];
  char* ifname;
  struct ifaddrs* i;
#if defined(HAVE_SOCKADDR_DL)
  struct sockaddr_dl* dladdr;
#elif defined(HAVE_SIOCGIFHWADDR)
  int sock;
  struct ifreq ifr;
#endif

  mupnp_log_debug_l4("Entering...\n");

  mupnp_net_interfacelist_clear(netIfList);

  if (getifaddrs(&ifaddr) != 0) {
    mupnp_log_debug("No addresses for interfaces!\n");
    return 0;
  }

  for (i = ifaddr; i != NULL; i = i->ifa_next) {

    // Thanks for Ricardo Rivldo (04/10/12)
    //  - for some reason, vmware and virtualbox \"virtual\" interfaces does not return ifa_addr
    if (i->ifa_addr == NULL || i->ifa_netmask == NULL)
      continue;

    // Thanks for Tobias.Gansen (01/15/06)
    if (i->ifa_addr->sa_family != AF_INET)
      continue;
    if (!(i->ifa_flags & IFF_UP))
      continue;
    if (i->ifa_flags & IFF_LOOPBACK)
      continue;

    if (getnameinfo(i->ifa_addr, sizeof(struct sockaddr), addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) != 0)
      continue;

    if (getnameinfo(i->ifa_netmask, sizeof(struct sockaddr), netmask, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) != 0)
      continue;

    ifname = i->ifa_name;
    mupnp_log_debug("Interface name: %s, address: %s\n", ifname, addr);
    netIf = mupnp_net_interface_new();
    mupnp_net_interface_setname(netIf, ifname);
    mupnp_net_interface_setaddress(netIf, addr);
    mupnp_net_interface_setnetmask(netIf, netmask);
#if defined(HAVE_SOCKADDR_DL)
    dladdr = (struct sockaddr_dl*)(i->ifa_addr);
    mupnp_net_interface_setmacaddress(netIf, LLADDR(dladdr));
#elif defined(HAVE_SIOCGIFHWADDR)
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
    ifr.ifr_addr.sa_family = AF_INET;
    ioctl(sock, SIOCGIFHWADDR, &ifr);
    mupnp_net_interface_setmacaddress(netIf, ifr.ifr_hwaddr.sa_data);
    close(sock);
#endif
    mupnp_net_interfacelist_add(netIfList, netIf);
  }
  freeifaddrs(ifaddr);

  mupnp_log_debug_l4("Leaving...\n");

  return mupnp_net_interfacelist_size(netIfList);
}