Ejemplo n.º 1
0
char* mupnp_net_selectaddr(struct sockaddr* remoteaddr)
{
  mUpnpNetworkInterfaceList* netIfList;
  mUpnpNetworkInterface* netIf;
  mUpnpNetworkInterface* selectNetIf;
  char* selectNetIfAddr;
  u_long laddr, lmask, raddr;
  struct addrinfo hints;
  struct addrinfo* netIfAddrInfo;
  struct addrinfo* netMaskAddrInfo;

  netIfList = mupnp_net_interfacelist_new();
  if (!netIfList)
    return mupnp_strdup("127.0.0.1");

  if (mupnp_net_gethostinterfaces(netIfList) <= 0) {
    mupnp_net_interfacelist_delete(netIfList);
    return mupnp_strdup("127.0.0.1");
  }

  raddr = ntohl(((struct sockaddr_in*)remoteaddr)->sin_addr.s_addr);

  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;

  selectNetIf = NULL;
  if (1 <= mupnp_net_gethostinterfaces(netIfList)) {
    for (netIf = mupnp_net_interfacelist_gets(netIfList); netIf; netIf = mupnp_net_interface_next(netIf)) {
      if (getaddrinfo(mupnp_net_interface_getaddress(netIf), NULL, &hints, &netIfAddrInfo) != 0)
        continue;
      if (getaddrinfo(mupnp_net_interface_getnetmask(netIf), NULL, &hints, &netMaskAddrInfo) != 0) {
        freeaddrinfo(netIfAddrInfo);
        continue;
      }
      laddr = ntohl(((struct sockaddr_in*)netIfAddrInfo->ai_addr)->sin_addr.s_addr);
      lmask = ntohl(((struct sockaddr_in*)netMaskAddrInfo->ai_addr)->sin_addr.s_addr);
      if ((laddr & lmask) == (raddr & lmask))
        selectNetIf = netIf;
      freeaddrinfo(netIfAddrInfo);
      freeaddrinfo(netMaskAddrInfo);
      if (selectNetIf)
        break;
    }
  }

  if (!selectNetIf)
    selectNetIf = mupnp_net_interfacelist_gets(netIfList);

  selectNetIfAddr = mupnp_strdup(mupnp_net_interface_getaddress(selectNetIf));

  mupnp_net_interfacelist_delete(netIfList);

  return selectNetIfAddr;
}
Ejemplo n.º 2
0
bool mupnp_controlpoint_ipchanged(mUpnpControlPoint *ctrlPoint)
{
	mUpnpNetworkInterfaceList *current, *added, *removed;
	mUpnpNetworkInterface *netIf;
	mUpnpDevice *dev, *tmp;
	mUpnpSSDPPacket *ssdpPkt;
	char *address;
	
	mupnp_log_debug_l4("Entering...\n");

	current = mupnp_net_interfacelist_new();
	added = mupnp_net_interfacelist_new();
	removed = mupnp_net_interfacelist_new();

	if (current == NULL || added == NULL || removed == NULL)
	{
		if (current != NULL) mupnp_net_interfacelist_delete(current);
		if (added != NULL) mupnp_net_interfacelist_delete(added);
		if (removed != NULL) mupnp_net_interfacelist_delete(removed);
		return false;
	}
	
	/* Get Interface changes */
	mupnp_net_gethostinterfaces(current);
	mupnp_net_interfacelist_getchanges(ctrlPoint->ifCache, current, 
					added, removed);
	
	/* Remove all devices registered through old interface */
	for (netIf = mupnp_net_interfacelist_gets(removed);
	     netIf != NULL; netIf = mupnp_net_interface_next(netIf))
	{
		mupnp_controlpoint_lock(ctrlPoint);
		tmp = mupnp_controlpoint_getdevices(ctrlPoint);
		while (tmp != NULL)
		{
			dev = tmp; tmp = mupnp_device_next(dev);
			ssdpPkt = mupnp_device_getssdppacket(dev);
			address = mupnp_ssdp_packet_getlocaladdress(ssdpPkt);
			
			if (address != NULL && 
			    mupnp_strcmp(address, mupnp_net_interface_getaddress(netIf)) == 0)
			{
				/* This device has been received from the 
				   removed interface, so it does not exist */
				mupnp_controlpoint_unlock(ctrlPoint);
				mupnp_controlpoint_removedevicebyssdppacket(ctrlPoint, 
									      ssdpPkt);
				mupnp_controlpoint_lock(ctrlPoint);
				address = NULL; dev = NULL; ssdpPkt = NULL;
			}
		}
		mupnp_controlpoint_unlock(ctrlPoint);
	}

	/* Launch new M-SEARCH */
	mupnp_controlpoint_search(ctrlPoint, MUPNP_ST_ROOT_DEVICE);
	
	/**** Cache current interfaces ****/
	mupnp_net_gethostinterfaces(ctrlPoint->ifCache);
	
	mupnp_net_interfacelist_delete(current);
	mupnp_net_interfacelist_delete(added);
	mupnp_net_interfacelist_delete(removed);

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

	return true;
}