Example #1
0
static BOOL cg_socket_getavailablelocaladdress(T_IPV4EP *localAddr)
{
	ER ret;
	char *ifAddr;
	int localPort;

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

	CgNetworkInterfaceList *netIfList;
	CgNetworkInterface *netIf;
	int netIfCnt;
	netIfList = cg_net_interfacelist_new();
	netIfCnt = cg_net_gethostinterfaces(netIfList);
	if (netIfCnt <= 0) {
		cg_net_interfacelist_delete(netIfList);
		return FALSE;
	}
	netIf = cg_net_interfacelist_gets(netIfList);
	ifAddr = cg_net_interface_getaddress(netIf);
	localPort = cg_socket_getavailableport();
	localAddr->ipaddr = ascii_to_ipaddr(ifAddr);
	localAddr->portno = htons(localPort);
	cg_net_interfacelist_delete(netIfList);

	return FALSE;
}
Example #2
0
/**
 * Create a new control point. Does not start any threads.
 *
 * @return A newly-created CgUpnpControlPoint
 */
CgUpnpControlPoint *cg_upnp_controlpoint_new()
{
	CgUpnpControlPoint *ctrlPoint;

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

	ctrlPoint = (CgUpnpControlPoint *)malloc(sizeof(CgUpnpControlPoint));

	if ( NULL != ctrlPoint )
	{
#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS	
		cg_http_persistentconnection_init();
#endif
		ctrlPoint->mutex = cg_mutex_new();
		ctrlPoint->deviceRootNodeList = cg_xml_nodelist_new();
		ctrlPoint->deviceList = cg_upnp_devicelist_new();
		ctrlPoint->ssdpServerList = cg_upnp_ssdp_serverlist_new();
		ctrlPoint->ssdpResServerList = cg_upnp_ssdpresponse_serverlist_new();
		ctrlPoint->httpServerList = cg_http_serverlist_new();
		ctrlPoint->httpEventURI = cg_string_new();
		ctrlPoint->eventListeners = cg_upnp_eventlistenerlist_new();

		/* Expiration handling */
		ctrlPoint->expThread = cg_thread_new();
		cg_thread_setaction(ctrlPoint->expThread, cg_upnp_controlpoint_expirationhandler);
		cg_thread_setuserdata(ctrlPoint->expThread, ctrlPoint);
		ctrlPoint->expMutex = cg_mutex_new();
		ctrlPoint->expCond = cg_cond_new();
		
		ctrlPoint->ifCache = cg_net_interfacelist_new();
		
		cg_upnp_controlpoint_setssdplistener(ctrlPoint, NULL);
		cg_upnp_controlpoint_setssdpresponselistener(ctrlPoint, NULL);
		cg_upnp_controlpoint_sethttplistener(ctrlPoint, NULL);
		cg_upnp_controlpoint_setdevicelistener(ctrlPoint, NULL);
		
		cg_upnp_controlpoint_setssdpresponseport(ctrlPoint, CG_UPNP_CONTROLPOINT_SSDP_RESPONSE_DEFAULT_PORT);
		cg_upnp_controlpoint_setssdpsearchmx(ctrlPoint, CG_UPNP_CONTROLPOINT_SSDP_DEFAULT_SEARCH_MX);
		cg_upnp_controlpoint_seteventport(ctrlPoint, CG_UPNP_CONTROLPOINT_HTTP_EVENT_DEFAULT_PORT);
		cg_upnp_controlpoint_seteventsuburi(ctrlPoint, CG_UPNP_CONTROLPOINT_HTTP_EVENTSUB_URI);

		cg_upnp_controlpoint_setuserdata(ctrlPoint, NULL);
	}

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

	return ctrlPoint;
}
Example #3
0
BOOL cg_bittorrent_client_createpeerid(CgBittorrentClient *cbc, CgByte *peerId)
{
	CgNetworkInterfaceList *netIfList;
	CgNetworkInterface *netIf;
	char hostName[CG_HTTP_SEVERNAME_MAXLEN];
	CgByte hostNameHash[CG_SHA1_HASH_SIZE];
	CgSHA1Context sha;
	int err;

	memset(peerId, 0, CG_BITTORRENT_CLIENT_PEERID_SIZE);

	/* Client ID/Vertion */
	peerId[0] = '-';
	memcpy(peerId + 1, CG_BITTORRENT_CLIENT_ID, 2);
	memcpy(peerId + 3, CG_BITTORRENT_CLIENT_VER, 4);

	/* Host Name */
	cg_http_getservername(hostName, CG_HTTP_SEVERNAME_MAXLEN);
	err = cg_sha1_reset(&sha);
	if (!err) {
		err = cg_sha1_input(&sha, hostName, cg_strlen(hostName));
		if (!err)
			err = cg_sha1_result(&sha, hostNameHash);
	}
	if (err)
		return FALSE;
	memcpy(peerId + 7, hostNameHash, 7);
	
	/* Mac Address */
	netIfList = cg_net_interfacelist_new();
	if (!netIfList)
		return FALSE;
	cg_net_gethostinterfaces(netIfList);
	netIf =cg_net_interfacelist_gets(netIfList);
	if (!netIf) {
		cg_net_interfacelist_delete(netIfList);
		return FALSE;
	}
	cg_net_interface_getmacaddress(netIf, (peerId + 14));
	cg_net_interfacelist_delete(netIfList);

	return FALSE;
}
BOOL cg_http_serverlist_open(CgHttpServerList *httpServerList, int port)
{
	CgNetworkInterfaceList *netIfList;
	CgNetworkInterface *netIf;
	CgHttpServer *httpServer;
	char *bindAddr;
	BOOL result =FALSE;
	
	cg_log_debug_l4("Entering...\n");

	netIfList = cg_net_interfacelist_new();
#ifndef CG_NET_USE_ANYADDR
	cg_net_gethostinterfaces(netIfList);
#else
	netIf = cg_net_interface_getany();
	cg_net_interfacelist_add(netIfList, netIf);
#endif
	for (netIf = cg_net_interfacelist_gets(netIfList); netIf; netIf = cg_net_interface_next(netIf)) {
		bindAddr = cg_net_interface_getaddress(netIf);
		if (cg_strlen(bindAddr) <= 0)
			continue;
		httpServer = cg_http_server_new();

		if (cg_http_server_open(httpServer, port, bindAddr) == FALSE) {
			/* Theo Beisch - why break off, 
			   we may be running ok on at least some IF??? 
			   (at least WINCE does...) */
			continue;
		}
		cg_http_serverlist_add(httpServerList, httpServer);
		result = TRUE; //at least one IF found ok
	}

	
	if (result==FALSE) cg_http_serverlist_clear(httpServerList);
	cg_net_interfacelist_delete(netIfList);

	return result;	
}
Example #5
0
BOOL cg_socket_setmulticastinterface(CgSocket *sock, char *ifaddr)
{
	struct sockaddr_in sockaddr;
	BOOL sockAddrSuccess;
	int optSuccess;
	CgNetworkInterfaceList *netIfList;
	CgNetworkInterface *netIf;
	int netIfCnt;

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

	netIfList = NULL;
	if (cg_strlen(ifaddr) <= 0) {
		netIfList = cg_net_interfacelist_new();
		netIfCnt = cg_net_gethostinterfaces(netIfList);
		if (netIfCnt <= 0) {
			cg_net_interfacelist_delete(netIfList);
			return FALSE;
		}
		netIf = cg_net_interfacelist_gets(netIfList);
		ifaddr = cg_net_interface_getaddress(netIf);
	}

	sockAddrSuccess = cg_socket_tosockaddrin(ifaddr, 0, &sockaddr, TRUE);
	if (netIfList != NULL)
		cg_net_interfacelist_delete(netIfList);
	if (sockAddrSuccess == FALSE)
		return FALSE;

	optSuccess = ka_setsockopt(sock->id, IP_PROTOIP, IPO_MULTICAST_IF, (const char *)&sockaddr.sin_addr, sizeof(sockaddr.sin_addr));
	if (optSuccess != 0)
		return FALSE;

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

	return TRUE;
}
BOOL cg_upnp_ssdpresponse_serverlist_open(CgUpnpSSDPResponseServerList *ssdpServerList, int bindPort)
{
	CgNetworkInterfaceList *netIfList;
	CgNetworkInterface *netIf;
	CgUpnpSSDPResponseServer *ssdpServer;
	char *bindAddr;
	BOOL result = FALSE;	

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

	netIfList = cg_net_interfacelist_new();
#ifndef CG_NET_USE_ANYADDR
	cg_net_gethostinterfaces(netIfList);
#else
	netIf = cg_net_interface_getany();
	cg_net_interfacelist_add(netIfList, netIf);
#endif
	for (netIf = cg_net_interfacelist_gets(netIfList); netIf; netIf = cg_net_interface_next(netIf)) {
		bindAddr = cg_net_interface_getaddress(netIf);
		if (cg_strlen(bindAddr) <= 0)
			continue;
		ssdpServer = cg_upnp_ssdpresponse_server_new();
		if (cg_upnp_ssdpresponse_server_open(ssdpServer, bindPort, bindAddr) == FALSE) {
			cg_upnp_ssdpresponse_server_delete(ssdpServer);
			continue;
		}
		cg_upnp_ssdpresponse_serverlist_add(ssdpServerList, ssdpServer);
		result = TRUE;
	}

	if (result == FALSE) cg_upnp_ssdpresponse_serverlist_clear(ssdpServerList);

	cg_net_interfacelist_delete(netIfList);

	return result;	
}
CgUpnpAvServer *cg_upnpav_dms_new()
{
    CgUpnpAvServer *dms;

    dms = (CgUpnpAvServer *)malloc(sizeof(CgUpnpAvServer));

    dms->dev = cg_upnp_device_new();
    if (!dms->dev) {
        free(dms);
        return NULL;
    }

    if (cg_upnp_device_parsedescription(dms->dev, CG_UPNPAV_DMS_DEVICE_DESCRIPTION, cg_strlen(CG_UPNPAV_DMS_DEVICE_DESCRIPTION)) == FALSE) {
        cg_upnp_device_delete(dms->dev);
        free(dms);
        return NULL;
    }

    if (cg_upnpav_dms_conmgr_init(dms) == FALSE) {
        cg_upnp_device_delete(dms->dev);
        free(dms);
        return NULL;
    }

    if (cg_upnpav_dms_condir_init(dms) == FALSE) {
        cg_upnp_device_delete(dms->dev);
        free(dms);
        return NULL;
    }

    if (cg_upnpav_dms_medrec_init(dms) == FALSE) {
        cg_upnp_device_delete(dms->dev);
        free(dms);
        return NULL;
    }

    dms->rootContent = cg_upnpav_content_new();
    cg_upnpav_content_settype(dms->rootContent, CG_UPNPAV_CONTENT_CONTAINER);
    cg_upnpav_content_settitle(dms->rootContent, CG_UPNPAV_ROOT_CONTENT_TITLE);
    cg_upnpav_content_setid(dms->rootContent, CG_UPNPAV_ROOT_CONTENT_ID);
    cg_upnpav_content_setparentid(dms->rootContent, CG_UPNPAV_ROOT_CONTENT_PARENTID);

    if (!dms->rootContent) {
        cg_upnpav_dms_delete(dms);
        return NULL;
    }

    dms->mutex = cg_mutex_new();
    if (!dms->mutex) {
        cg_upnpav_dms_delete(dms);
        return NULL;
    }

    dms->networkInterfaceList = cg_net_interfacelist_new();
    if (!dms->networkInterfaceList) {
        cg_upnpav_dms_delete(dms);
        return NULL;
    }

    cg_upnp_device_setactionlistener(dms->dev, cg_upnpav_dms_actionreceived);
    cg_upnp_device_setquerylistener(dms->dev, cg_upnpav_dms_queryreceived);
    cg_upnp_device_sethttplistener(dms->dev, cg_upnpav_dms_device_httprequestrecieved);

    cg_upnpav_dms_setactionlistener(dms, NULL);
    cg_upnpav_dms_setquerylistener(dms, NULL);
    cg_upnpav_dms_sethttplistener(dms, NULL);

    cg_upnp_device_setuserdata(dms->dev, dms);
    cg_upnp_device_updateudn(dms->dev);

    dms->protocolInfoList = cg_upnpav_protocolinfolist_new();

    return dms;
}
Example #8
0
BOOL cg_upnp_controlpoint_ipchanged(CgUpnpControlPoint *ctrlPoint)
{
	CgNetworkInterfaceList *current, *added, *removed;
	CgNetworkInterface *netIf;
	CgUpnpDevice *dev, *tmp;
	CgUpnpSSDPPacket *ssdpPkt;
	char *address;
	
	cg_log_debug_l4("Entering...\n");

	current = cg_net_interfacelist_new();
	added = cg_net_interfacelist_new();
	removed = cg_net_interfacelist_new();

	if (current == NULL || added == NULL || removed == NULL)
	{
		if (current != NULL) cg_net_interfacelist_delete(current);
		if (added != NULL) cg_net_interfacelist_delete(added);
		if (removed != NULL) cg_net_interfacelist_delete(removed);
		return FALSE;
	}
	
	/* Get Interface changes */
	cg_net_gethostinterfaces(current);
	cg_net_interfacelist_getchanges(ctrlPoint->ifCache, current, 
					added, removed);
	
	/* Remove all devices registered through old interface */
	for (netIf = cg_net_interfacelist_gets(removed);
	     netIf != NULL; netIf = cg_net_interface_next(netIf))
	{
		cg_upnp_controlpoint_lock(ctrlPoint);
		tmp = cg_upnp_controlpoint_getdevices(ctrlPoint);
		while (tmp != NULL)
		{
			dev = tmp; tmp = cg_upnp_device_next(dev);
			ssdpPkt = cg_upnp_device_getssdppacket(dev);
			address = cg_upnp_ssdp_packet_getlocaladdress(ssdpPkt);
			
			if (address != NULL && 
			    cg_strcmp(address, cg_net_interface_getaddress(netIf)) == 0)
			{
				/* This device has been received from the 
				   removed interface, so it does not exist */
				cg_upnp_controlpoint_unlock(ctrlPoint);
				cg_upnp_controlpoint_removedevicebyssdppacket(ctrlPoint, 
									      ssdpPkt);
				cg_upnp_controlpoint_lock(ctrlPoint);
				address = NULL; dev = NULL; ssdpPkt = NULL;
			}
		}
		cg_upnp_controlpoint_unlock(ctrlPoint);
	}

	/* Launch new M-SEARCH */
	cg_upnp_controlpoint_search(ctrlPoint, CG_UPNP_ST_ROOT_DEVICE);
	
	/**** Cache current interfaces ****/
	cg_net_gethostinterfaces(ctrlPoint->ifCache);
	
	cg_net_interfacelist_delete(current);
	cg_net_interfacelist_delete(added);
	cg_net_interfacelist_delete(removed);

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

	return TRUE;
}