コード例 #1
0
ファイル: interface_function.c プロジェクト: Coramo/mupnp
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;
}
コード例 #2
0
ファイル: cmediaserver_device.c プロジェクト: jink2005/mupnpc
bool mupnp_upnpav_dms_updatenetworkinterfaces(mUpnpAvServer *dms)
{
	mupnp_upnpav_dms_lock(dms);
	mupnp_net_interfacelist_clear(dms->networkInterfaceList);
	mupnp_net_gethostinterfaces(dms->networkInterfaceList);
	mupnp_upnpav_dms_unlock(dms);
	return (0 < mupnp_net_interfacelist_size(dms->networkInterfaceList)) ? true : false;
}
コード例 #3
0
ファイル: controlpoint.c プロジェクト: jink2005/mupnpc
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;
}
コード例 #4
0
ファイル: controlpoint.c プロジェクト: jink2005/mupnpc
/**
 * Activate the control point. Starts listening for SSDP messages etc.
 * You must call this function before you can actually use a control point.
 *
 * @param ctrlPoint The control point to start
 *
 * @return true if successful; otherwise false
 *
 */
bool mupnp_controlpoint_start(mUpnpControlPoint *ctrlPoint)
{
	mUpnpHttpServerList *httpServerList;
	MUPNP_HTTP_LISTENER httpListener;
	int httpEventPort;
	mUpnpSSDPServerList *ssdpServerList;
	mUpnpSSDPResponseServerList *ssdpResServerList;
	int ssdpResPort;
	int ssdpMaxResPort;
	
	mupnp_log_debug_l4("Entering...\n");

	mupnp_controlpoint_stop(ctrlPoint);

	/* Expiration handling */
	mupnp_thread_start(ctrlPoint->expThread);
	
	/**** Cache current interfaces ****/
	mupnp_net_gethostinterfaces(ctrlPoint->ifCache);
	
	/**** HTTP Server ****/
	httpEventPort = mupnp_controlpoint_geteventport(ctrlPoint);
	httpServerList = mupnp_controlpoint_gethttpserverlist(ctrlPoint);
	/* Opening HTTP server may fail, so try many ports */
	while(mupnp_http_serverlist_open(httpServerList, httpEventPort) == false) {
		mupnp_controlpoint_seteventport(ctrlPoint, httpEventPort + 1);
		httpEventPort = mupnp_controlpoint_geteventport(ctrlPoint);
	}
	mupnp_http_serverlist_setuserdata(httpServerList, ctrlPoint);
	httpListener = mupnp_controlpoint_gethttplistener(ctrlPoint);
	if (httpListener == NULL)
		httpListener = mupnp_controlpoint_httprequestreceived;
	mupnp_http_serverlist_setlistener(httpServerList, httpListener);
	mupnp_http_serverlist_start(httpServerList);

	/**** SSDP Server ****/
	ssdpServerList = mupnp_controlpoint_getssdpserverlist(ctrlPoint);
	if (mupnp_ssdp_serverlist_open(ssdpServerList) == false)
		return false;
	mupnp_ssdp_serverlist_setlistener(ssdpServerList, mupnp_controlpoint_ssdplistner);
	mupnp_ssdp_serverlist_setuserdata(ssdpServerList, ctrlPoint);
	if (mupnp_ssdp_serverlist_start(ssdpServerList) == false)
		return false;

	/**** SSDP Response Server ****/
	ssdpResPort = mupnp_controlpoint_getssdpresponseport(ctrlPoint);
	/* below is the max SSDP Response port number to assign (rosfran.borges) */
	ssdpMaxResPort = ssdpResPort + MUPNP_CONTROLPOINT_SSDP_RESPONSE_PORT_MAX_TRIES_INDEX;
	ssdpResServerList = mupnp_controlpoint_getssdpresponseserverlist(ctrlPoint);
	/* Opening SSDP response server may fail, so try many ports */
	while(mupnp_ssdpresponse_serverlist_open(ssdpResServerList, ssdpResPort) == false &&
		(ssdpResPort < ssdpMaxResPort) ) {
		mupnp_controlpoint_setssdpresponseport(ctrlPoint, ssdpResPort + 1);
		ssdpResPort = mupnp_controlpoint_getssdpresponseport(ctrlPoint);
	}
	mupnp_ssdpresponse_serverlist_setlistener(ssdpResServerList, mupnp_controlpoint_ssdpresponselistner);
	mupnp_ssdpresponse_serverlist_setuserdata(ssdpResServerList, ctrlPoint);
	if (mupnp_ssdpresponse_serverlist_start(ssdpResServerList) == false)
		return false;

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

	return true;
}