Exemple #1
0
char *cg_string_replace(CgString *str, char *fromStr[], char *toStr[], size_t fromStrCnt)
{
	char *orgValue = NULL;
	size_t orgValueLen = 0;
	int n = 0;
	int copyPos = 0;
	size_t *fromStrLen = NULL;
	CgString *repValue = NULL;
	BOOL isReplaced = FALSE;

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

	if (NULL == str )
		return NULL;
	
	repValue = cg_string_new();
	
	fromStrLen = (size_t *)malloc(sizeof(size_t) * fromStrCnt);

	if ( NULL == fromStrLen )
	{
        cg_string_delete(repValue);
		cg_log_debug_s("Memory allocation failure!\n");
		return NULL;
	}
	
	for (n=0; n<fromStrCnt; n++)
		fromStrLen[n] = cg_strlen(fromStr[n]);
	
	orgValue = cg_string_getvalue(str);
	orgValueLen = cg_string_length(str);
	
	copyPos = 0;
	while (copyPos<orgValueLen) {
		isReplaced = FALSE;
		for (n=0; n<fromStrCnt; n++) {
			if (strncmp(fromStr[n], orgValue + copyPos,  fromStrLen[n]) == 0) {
				cg_string_addvalue(repValue, toStr[n]);
				copyPos += fromStrLen[n];
				isReplaced = TRUE;
				continue;
			}
		}
		if (isReplaced == TRUE)
			continue;
		cg_string_naddvalue(repValue, orgValue + copyPos, 1);
		copyPos++;
	}
	
	free(fromStrLen);

	cg_string_setvalue(str, cg_string_getvalue(repValue));	

	cg_string_delete(repValue);
		
	cg_log_debug_l5("Leaving...\n");

	return cg_string_getvalue(str);
}
BOOL cg_upnp_ssdpresponse_server_post(CgUpnpSSDPResponseServer *server, CgUpnpSSDPRequest *ssdpReq)
{
	CgUpnpHttpUSocket *httpuSock;
	char *ifAddr;
	const char *ssdpAddr;
	CgString *ssdpMsg;
	size_t sentLen = 0;
	
	cg_log_debug_l4("Entering...\n");

	httpuSock = cg_upnp_ssdpresponse_server_getsocket(server);
	
	ifAddr = cg_socket_getaddress(httpuSock);
	ssdpAddr = cg_upnp_ssdp_gethostaddress(ifAddr);
	cg_upnp_ssdprequest_sethost(ssdpReq, ssdpAddr, CG_UPNP_SSDP_PORT);
		
	ssdpMsg = cg_string_new();
	cg_upnp_ssdprequest_tostring(ssdpReq, ssdpMsg);

	sentLen = cg_socket_sendto(httpuSock, ssdpAddr, CG_UPNP_SSDP_PORT, cg_string_getvalue(ssdpMsg), cg_string_length(ssdpMsg));
	cg_string_delete(ssdpMsg);
	
	cg_log_debug_l4("Leaving...\n");

	return (sentLen > 0);
}
Exemple #3
0
void  cg_xml_node_print(CgXmlNode *node)
{
	CgString *str = cg_string_new();
	cg_xml_node_tostring(node, TRUE, str);
	printf("%s", cg_string_getvalue(str));
	cg_string_delete(str);
}
char *cg_upnp_ssdpresponse_tostring(CgUpnpSSDPResponse *ssdpRes, CgString *ssdpMsg)
{
    CgHttpHeader *header;
    char statusCodeBuf[CG_STRING_INTEGER_BUFLEN];
    const char *name;
    const char *value;

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

    cg_string_addvalue(ssdpMsg, cg_http_response_getversion(ssdpRes));
    cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
    cg_string_addvalue(ssdpMsg, cg_int2str(cg_http_response_getstatuscode(ssdpRes), statusCodeBuf, sizeof(statusCodeBuf)));
    cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
    cg_string_addvalue(ssdpMsg, cg_http_response_getreasonphrase(ssdpRes));
    cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);

    for (header = cg_http_packet_getheaders((CgHttpPacket *)ssdpRes); header != NULL; header = cg_http_header_next(header)) {
        name = cg_http_header_getname(header);
        value = cg_http_header_getvalue(header);
        cg_string_addvalue(ssdpMsg, name);
        cg_string_addvalue(ssdpMsg, CG_HTTP_COLON);
        cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
        cg_string_addvalue(ssdpMsg, value);
        cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
    }
    cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);

    return cg_string_getvalue(ssdpMsg);

    cg_log_debug_l4("Leaving...\n");
}
char *cg_upnp_ssdprequest_tostring(CgUpnpSSDPRequest *ssdpReq, CgString *ssdpMsg)
{
	CgHttpHeader *header;
	const char *name;
	const char *value;
	
	cg_log_debug_l4("Entering...\n");

	cg_string_addvalue(ssdpMsg, cg_http_request_getmethod(ssdpReq));
	cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
	cg_string_addvalue(ssdpMsg, cg_http_request_geturi(ssdpReq));
	cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
	cg_string_addvalue(ssdpMsg, cg_http_request_getversion(ssdpReq));
	cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
	
	for (header = cg_http_packet_getheaders((CgHttpPacket *)ssdpReq); header != NULL; header = cg_http_header_next(header)) {
		name = cg_http_header_getname(header);
		value = cg_http_header_getvalue(header);
		cg_string_addvalue(ssdpMsg, name);
		cg_string_addvalue(ssdpMsg, CG_HTTP_COLON);
		cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
		cg_string_addvalue(ssdpMsg, value);
		cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
	}
	cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
	
	return cg_string_getvalue(ssdpMsg);

	cg_log_debug_l4("Leaving...\n");
}
BOOL cg_bittorrent_metainfo_save(CgBittorrentMetainfo *cbm, char *fileName)
{
	FILE *fp;
	CgString *str;

	if (!cbm)
		return FALSE;

	str = cg_string_new();
	if (!str)
		return FALSE;

	if (!cg_bittorrent_metainfo_tostring(cbm, str)) {
		cg_string_delete(str);
		return FALSE;
	}

	fp = fopen(fileName, "wb");
	if (fp) {
		fwrite(cg_string_getvalue(str), sizeof(char), cg_string_length(str), fp);
		fclose(fp);
	}

	cg_string_delete(str);

	return (fp ? TRUE : FALSE);
}
Exemple #7
0
char *cg_string_naddvalue(CgString *str, const char *value, size_t valueLen)
{
	char *newValue = NULL;
	size_t newMemSize = 0;

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

	if (NULL == str)
		return NULL;

	if (value == NULL || valueLen <= 0)
	{
		/* Empty string, nothing to add */
		return cg_string_getvalue(str);
	}

	/* Check, if we need to allocate memory for the new data */
	newMemSize = str->valueSize + valueLen + 1;
	if (newMemSize > str->memSize || str->value == NULL)
	{
		/* realloc also some extra in order to avoid multiple reallocs */
		newMemSize += CG_STRING_REALLOC_EXTRA;
		newValue = realloc(str->value, newMemSize * sizeof(char));

		if (newValue == NULL)
		{
			/* Memory allocation failed, bail out */
			return NULL;
		}

		str->memSize = newMemSize;
		str->value = newValue;
	}

	/* memcpy works better with non-zero-terminated data
	   than strncpy */
	memcpy(str->value + str->valueSize, value, valueLen);

	str->valueSize += valueLen;
	
	/* In case this is a string, append a termination character */
	str->value[str->valueSize] = '\0';

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

	return cg_string_getvalue(str);
}
Exemple #8
0
char *cg_http_request_geturi(CgHttpRequest *httpReq)
{
    cg_log_debug_l4("Entering...\n");

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

    return cg_string_getvalue(httpReq->uri);
}
char *cg_net_interface_getnetmask(CgNetworkInterface *netIf)
{
	cg_log_debug_l4("Entering...\n");

	return cg_string_getvalue(netIf->netmask);

	cg_log_debug_l4("Leaving...\n");
}
Exemple #10
0
char *cg_net_interface_getaddress(CgNetworkInterface *netIf)
{
	cg_log_debug_l4("Entering...\n");

	return cg_string_getvalue(netIf->ipaddr);

	cg_log_debug_l4("Leaving...\n");
}
const char *cg_http_header_getname(CgHttpHeader *header)
{
    cg_log_debug_l4("Entering...\n");

    return cg_string_getvalue(header->name);

    cg_log_debug_l4("Leaving...\n");
}
void cg_upnpav_resource_updateattributes(CgUpnpAvResource *res)
{
    CgUpnpAvResourceData *nodeData;
    CgString *resAttr;
    char *mimeType;
    char *dlnaAttr;

    nodeData = (CgUpnpAvResourceData *)cg_xml_node_getuserdata(res);
    mimeType = (0 < cg_string_length(nodeData->mimeType)) ? cg_string_getvalue(nodeData->mimeType) : "*/*";
    dlnaAttr = (0 < cg_string_length(nodeData->dlnaAttr)) ? cg_string_getvalue(nodeData->dlnaAttr) : "*";

    resAttr = cg_string_new();
    cg_string_addvalue(resAttr, "http-get:*:");
    cg_string_addvalue(resAttr, mimeType);
    cg_string_addvalue(resAttr, ":");
    cg_string_addvalue(resAttr, dlnaAttr);
    cg_xml_node_setattribute(res, CG_UPNPAV_RESOURCE_PROTOCOLINFO, cg_string_getvalue(resAttr));
    cg_string_delete(resAttr);
}
Exemple #13
0
char *cg_string_addrepvalue(CgString *str, const char *value, size_t repeatCnt)
{
	int n;
	cg_log_debug_l5("Entering...\n");

	for (n = 0; n < repeatCnt; n++)
		cg_string_addvalue(str, value);

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

	return cg_string_getvalue(str);
}
Exemple #14
0
char *cg_string_naddrepvalue(CgString *str, char *value, int valueLen, int repeatCnt)
{
	int n;

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

	for (n = 0; n < repeatCnt; n++)
		cg_string_naddvalue(str, value, valueLen);

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

	return cg_string_getvalue(str);
}
Exemple #15
0
CgDictionary *cg_net_uri_getquerydictionary(CgNetURI *uri)
{
	char *query;
	int queryOffset;
	int eqIdx, ampIdx;
	CgString *paramName;
	CgString *paramValue;

	if (NULL == uri->queryDictionary)
		uri->queryDictionary = cg_dictionary_new();

	paramName = cg_string_new();
	paramValue = cg_string_new();

	query = cg_net_uri_getquery(uri);
	queryOffset = 0;

	eqIdx = cg_strstr(query, "=");
	while (0 < eqIdx) {
		ampIdx = cg_strstr(query + queryOffset, "&");
		if (ampIdx <= 0) {
			ampIdx = cg_strstr(query + queryOffset, "#");
			if (ampIdx <= 0)
				ampIdx = cg_strlen(query + queryOffset);
		}
		if (ampIdx <= eqIdx)
			break;
		cg_string_setnvalue(paramName, query + queryOffset, eqIdx);
		cg_string_setnvalue(paramValue, query + queryOffset + eqIdx + 1, (ampIdx - eqIdx -1));
		cg_dictionary_setvalue(uri->queryDictionary, cg_string_getvalue(paramName), cg_string_getvalue(paramValue));
		queryOffset += ampIdx + 1;
		eqIdx = cg_strstr(query + queryOffset, "=");
	}

	cg_string_delete(paramName);
	cg_string_delete(paramValue);

	return uri->queryDictionary;
}
Exemple #16
0
static char *cg_xml_node_attribute_tostring(CgXmlNode *node, CgString *str)
{
	CgXmlAttribute *attr;
	char *name;
	char *value;
	CgString *valueStr;
	
	cg_log_debug_l4("Entering...\n");

	valueStr = cg_string_new();
	if (valueStr == NULL) return NULL;

	for (attr = cg_xml_node_getattributes(node); attr != NULL; attr = cg_xml_attribute_next(attr)) {
		name = cg_xml_attribute_getname(attr);
		value = cg_xml_attribute_getvalue(attr);
		
		cg_string_setvalue(valueStr, value);
		cg_xml_escapechars(valueStr);

		/* All the following functions return NULL only when memory 
		   allocation fails, so we can check them all */
		if (!cg_string_naddvalue(str, " ", 1) || 
		    !cg_string_addvalue(str, name) ||
		    !cg_string_naddvalue(str, "=\"", 2) ||
		    !cg_string_addvalue(str, cg_string_getvalue(valueStr)) ||
		    !cg_string_naddvalue(str, "\"", 1))
		{
			/* Memory allocation failed */
			cg_string_delete(valueStr);
			return NULL;
		}
	}
	cg_string_delete(valueStr);
	
	cg_log_debug_l4("Leaving...\n");

	return cg_string_getvalue(str);
}
Exemple #17
0
char *cg_net_uri_escapestring(char *buf, int bufSize, CgString *retBuf)
{
#if defined(CG_HTTP_CURL)
	char *tmp;
#else
	int n;
	unsigned char c;
	char hexChar[4];
#endif

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

	if (!retBuf)
		return NULL;

#if defined(CG_HTTP_CURL)
	tmp = (bufSize < 1)?curl_escape(buf, 0):curl_escape(buf, bufSize);
	if (tmp == NULL)
	{
		cg_log_debug_s("Memory allocation problem!\n");
		return NULL;
	}
	cg_string_addvalue(retBuf, tmp);
	curl_free(tmp);
#else
	if (bufSize < 1)
		bufSize = cg_strlen(buf) + 1;

	for (n=0; n<bufSize; n++) {
		c = (unsigned char)buf[n];
		if (!cg_net_uri_isalphanumchar(c)) {
#if defined(HAVE_SNPRINTF)
			snprintf(hexChar, sizeof(hexChar), "%%%02X", c);
#else
			sprintf(hexChar, "%%%02X", c);
#endif
			cg_string_naddvalue(retBuf, hexChar, 3);
		}
		else
			cg_string_naddvalue(retBuf, buf+n, 1);
	}

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

	return cg_string_getvalue(retBuf);
}
const char *cg_upnp_event_subscription_totimeoutheaderstring(CgTime time, CgString *buf)
{
    char timeBuf[CG_STRING_LONG_BUFLEN];

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

    if (time != CG_UPNP_SUBSCRIPTION_INFINITE_VALUE) {
        cg_string_setvalue(buf, CG_UPNP_SUBSCRIPTION_TIMEOUT_HEADER);
        cg_string_addvalue(buf, cg_long2str(time, timeBuf, sizeof(timeBuf)));
    }
    else
        cg_string_setvalue(buf, CG_UPNP_SUBSCRIPTION_INFINITE_STRING);
    return cg_string_getvalue(buf);

    cg_log_debug_l4("Leaving...\n");
}
Exemple #19
0
BOOL cg_upnp_ssdp_socket_postresponse(CgUpnpSSDPSocket *ssdpSock, CgUpnpSSDPResponse *ssdpRes, const char *host, int port)
{
	CgString *ssdpMsg;
  size_t ssdpMsgLen;
	BOOL postSuccess;

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

	ssdpMsg = cg_string_new();
	cg_upnp_ssdpresponse_tostring(ssdpRes, ssdpMsg);
  ssdpMsgLen = cg_string_length(ssdpMsg);
	postSuccess = (cg_socket_sendto(ssdpSock, host, port, cg_string_getvalue(ssdpMsg), ssdpMsgLen) == ssdpMsgLen) ? TRUE : FALSE;
	cg_string_delete(ssdpMsg);

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

	return postSuccess;
}
Exemple #20
0
char *cg_net_uri_getrequest(CgNetURI *uri)
{
	cg_log_debug_l4("Entering...\n");

	if (cg_net_uri_hasquery(uri) == FALSE)
	{
		return cg_net_uri_getpath(uri);
	}
	
	if (uri->request == NULL) uri->request = cg_string_new();

	cg_string_setvalue(uri->request, cg_net_uri_getpath(uri));
	cg_string_addvalue(uri->request, CG_NET_URI_QUESTION_DELIM);
	cg_string_addvalue(uri->request, cg_net_uri_getquery(uri));
	
	cg_log_debug_l4("Leaving...\n");

	return cg_string_getvalue(uri->request);
}
Exemple #21
0
static BOOL cg_upnp_ssdp_socket_notify(CgUpnpSSDPSocket *ssdpSock, CgUpnpSSDPRequest *ssdpReq, const char *ssdpAddr)
{
	CgString *ssdpMsg;
	size_t sentLen;

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

	cg_upnp_ssdprequest_setmethod(ssdpReq, CG_HTTP_NOTIFY);
	
	ssdpMsg = cg_string_new();
	cg_upnp_ssdprequest_tostring(ssdpReq, ssdpMsg);

	sentLen = cg_socket_sendto(ssdpSock, ssdpAddr, CG_UPNP_SSDP_PORT, cg_string_getvalue(ssdpMsg), cg_string_length(ssdpMsg));
	cg_string_delete(ssdpMsg);
	
	cg_log_debug_l4("Leaving...\n");

	return (0 < sentLen) ? TRUE : FALSE;
}
void cg_upnp_event_subscription_request_setsid(CgUpnpSubscriptionRequest *subReq, const char *sid)
{
    CgString *headerSID;
    ssize_t uuidIdx;

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

    headerSID = cg_string_new();

    uuidIdx = cg_strstr(sid, CG_UPNP_ST_UUID_DEVICE);
    if (uuidIdx < 0)
        cg_string_addvalue(headerSID, CG_UPNP_ST_UUID_DEVICE ":");
    cg_string_addvalue(headerSID, sid);

    cg_http_packet_setheadervalue(((CgHttpPacket*)subReq), CG_HTTP_SID, cg_string_getvalue(headerSID));

    cg_string_delete(headerSID);

    cg_log_debug_l4("Leaving...\n");
}
Exemple #23
0
BOOL cg_upnpav_dms_conmgr_actionreceived(CgUpnpAction* action)
{
  CgUpnpAvServer* dms;
  CgUpnpDevice* dev;
  char* actionName;
  CgUpnpArgument* arg;
  CgString* protocolInfos;
  CgUpnpAvProtocolInfo* protocolInfo;

  actionName = (char*)cg_upnp_action_getname(action);
  if (cg_strlen(actionName) <= 0)
    return FALSE;

  dev = (CgUpnpDevice*)cg_upnp_service_getdevice(cg_upnp_action_getservice(action));
  if (!dev)
    return FALSE;

  dms = (CgUpnpAvServer*)cg_upnp_device_getuserdata(dev);
  if (!dms)
    return FALSE;

  /* GetProtocolInfo*/
  if (cg_streq(actionName, CG_UPNPAV_DMS_CONNECTIONMANAGER_GET_PROTOCOL_INFO)) {
    arg = cg_upnp_action_getargumentbyname(action, CG_UPNPAV_DMS_CONNECTIONMANAGER_SOURCE);
    if (!arg)
      return FALSE;
    protocolInfos = cg_string_new();
    for (protocolInfo = cg_upnpav_dms_getprotocolinfos(dms); protocolInfo; protocolInfo = cg_upnpav_protocolinfo_next(protocolInfo)) {
      if (0 < cg_string_length(protocolInfos))
        cg_string_addvalue(protocolInfos, ",");
      cg_string_addvalue(protocolInfos, cg_upnpav_protocolinfo_getstring(protocolInfo));
    }
    cg_upnp_argument_setvalue(arg, cg_string_getvalue(protocolInfos));
    cg_string_delete(protocolInfos);
    return TRUE;
  }

  return FALSE;
}
Exemple #24
0
void cg_upnpav_dmr_addprotocolinfo(CgUpnpAvRenderer* dmr, CgUpnpAvProtocolInfo* info)
{
  CgString* protocolInfos;
  CgUpnpAvProtocolInfo* protocolInfo;
  CgUpnpService* service;
  CgUpnpStateVariable* stateVar;

  cg_upnpav_protocolinfolist_add(dmr->protocolInfoList, info);

  protocolInfos = cg_string_new();
  for (protocolInfo = cg_upnpav_dmr_getprotocolinfos(dmr); protocolInfo; protocolInfo = cg_upnpav_protocolinfo_next(protocolInfo)) {
    if (0 < cg_string_length(protocolInfos))
      cg_string_addvalue(protocolInfos, ",");
    cg_string_addvalue(protocolInfos, cg_upnpav_protocolinfo_getstring(protocolInfo));
  }

  service = cg_upnp_device_getservicebyexacttype(dmr->dev, CG_UPNPAV_DMR_CONNECTIONMANAGER_SERVICE_TYPE);
  stateVar = cg_upnp_service_getstatevariablebyname(service, CG_UPNPAV_DMR_CONNECTIONMANAGER_SINKPROTOCOLINFO);
  cg_upnp_statevariable_setvalue(stateVar, cg_string_getvalue(protocolInfos));

  cg_string_delete(protocolInfos);
}
void cg_upnp_device_ssdpmessagereceived(CgUpnpDevice *dev, CgUpnpSSDPPacket *ssdpPkt, int filter)
{
    BOOL isRootDev;
    char *ssdpST;
    char *devUDN, *devType;
    char ssdpMsg[CG_UPNP_SSDP_HEADER_LINE_MAXSIZE];
    char deviceUSN[CG_UPNP_SSDP_HEADER_LINE_MAXSIZE];
#if defined WINCE
    size_t n;
#else
    int n;
#endif
    CgUpnpService *service;
    CgUpnpDevice *childDev;
    char *ssdpMXString;
    int ssdpMX;

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

    ssdpMXString = cg_http_headerlist_getvalue(ssdpPkt->headerList, CG_HTTP_MX);
    ssdpST = cg_upnp_ssdp_packet_getst(ssdpPkt);

    /* Check if this ssdp packet has already been checked + filtered */
    if (filter)
    {

        /****************************************
         * Request line
         * Check the request line for errors, this is not ideal as it currently only
         * checks for the presence of the strings and not the order.
         ***************************************/
        /**** check for M-SEARCH and return if not found ****/
        if (cg_strstr(cg_string_getvalue(ssdpPkt->dgmPkt->data), CG_HTTP_MSEARCH) < 0)
            return;
        /**** check for * and return if not found ****/
        if (cg_strstr(cg_string_getvalue(ssdpPkt->dgmPkt->data), "*") < 0)
            return;
        /**** check HTTP version and return if not found ****/
        if (cg_strstr(cg_string_getvalue(ssdpPkt->dgmPkt->data),  CG_HTTP_VER11) < 0)
            return;

        /****************************************
         * check HOST header, should always be 239.255.255.250:1900, return if incorrect
         ***************************************/
        if (cg_strcmp(cg_upnp_ssdp_packet_gethost(ssdpPkt), CG_UPNP_SSDP_MULTICAST_ADDRESS) != 0)
            return;

        /****************************************
         * check MAN header, return if incorrect
         ***************************************/
        if (cg_upnp_ssdp_packet_isdiscover(ssdpPkt) == FALSE)
            return;

        /****************************************
         * check MX header, return if incorrect
         ***************************************/
        if (ssdpMXString == NULL || cg_strlen(ssdpMXString)==0)
            /* return if the MX value does not exist or is empty */
            return;
        /* check if MX value is not an integer */
        for (n=0; n<strlen(ssdpMXString); n++) {
            if (isdigit(ssdpMXString[n]) == 0)
                /* MX value contains a non-digit so is invalid */
                return;
        }

        /****************************************
         * check ST header and if empty return
         ***************************************/
        if (cg_strlen(ssdpST) <= 0)
            return;

        /* Check if we have received this search recently
         * and ignore duplicates. */
        if ( filter_duplicate_m_search(ssdpPkt) )
            return;

        ssdpMX = cg_upnp_ssdp_packet_getmx(ssdpPkt);
        cg_log_debug("Sleeping for a while... (MX:%d)\n", ssdpMX);
        cg_waitrandom((ssdpMX*1000)/4);
    }

    isRootDev = cg_upnp_device_isrootdevice(dev);

    if (cg_upnp_st_isalldevice(ssdpST) == TRUE) {
        /* for root device only */
        if (isRootDev == TRUE) {
            cg_upnp_device_getnotifydevicent(dev, ssdpMsg, sizeof(ssdpMsg));
            cg_upnp_device_getnotifydeviceusn(dev, deviceUSN, sizeof(deviceUSN));
            cg_upnp_device_postsearchresponse(dev, ssdpPkt, ssdpMsg, deviceUSN);
        }
        /* for all devices send */
        /* device type : device version */
        cg_upnp_device_getnotifydevicetypent(dev, ssdpMsg, sizeof(ssdpMsg));
        cg_upnp_device_getnotifydevicetypeusn(dev, deviceUSN, sizeof(deviceUSN));
        cg_upnp_device_postsearchresponse(dev, ssdpPkt, ssdpMsg, deviceUSN);
        /* device UUID */
        cg_upnp_device_postsearchresponse(dev, ssdpPkt, cg_upnp_device_getudn(dev), cg_upnp_device_getudn(dev));
    }
    else if (cg_upnp_st_isrootdevice(ssdpST)  == TRUE) {
        if (isRootDev == TRUE) {
            cg_upnp_device_getnotifydeviceusn(dev, deviceUSN, sizeof(deviceUSN));
            cg_upnp_device_postsearchresponse(dev, ssdpPkt, CG_UPNP_ST_ROOT_DEVICE, deviceUSN);
        }
    }
    else if (cg_upnp_st_isuuiddevice(ssdpST)  == TRUE) {
        devUDN = cg_upnp_device_getudn(dev);
        if (cg_streq(ssdpST, devUDN) == TRUE)
            cg_upnp_device_postsearchresponse(dev, ssdpPkt, devUDN, devUDN);
    }
    else if (cg_upnp_st_isurn(ssdpST)  == TRUE) {
        devType = cg_upnp_device_getdevicetype(dev);
        if (cg_streq(ssdpST, devType) == TRUE) {
            cg_upnp_device_getnotifydevicetypeusn(dev, deviceUSN, sizeof(deviceUSN));
            cg_upnp_device_postsearchresponse(dev, ssdpPkt, devType, deviceUSN);
        }
    }

    for (service=cg_upnp_device_getservices(dev); service != NULL; service = cg_upnp_service_next(service))
        cg_upnp_service_ssdpmessagereceived(service, ssdpPkt);

    for (childDev = cg_upnp_device_getdevices(dev); childDev != NULL; childDev = cg_upnp_device_next(childDev))
        cg_upnp_device_ssdpmessagereceived(childDev, ssdpPkt, FALSE);


    cg_log_debug_l4("Leaving...\n");
}
static int filter_duplicate_m_search(CgUpnpSSDPPacket *ssdpPkt)
{
    CgSysTime *timestamps = ssdpPkt->timestamps;
    int loc, s_length;
    char *id_string, *r_address, *st, port[6];
    CgSysTime curr_time;

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

    /* Initializing hash table to zero */
    if (!ssdpPkt->initialized) {
        ssdpPkt->initialized = 1;
        memset(timestamps, '\0', CG_UPNP_SSDP_FILTER_TABLE_SIZE * sizeof( CgSysTime ));
    }

    r_address = cg_string_getvalue(ssdpPkt->dgmPkt->remoteAddress);
    st = cg_upnp_ssdp_packet_getst(ssdpPkt);
    sprintf(port, "%d", ssdpPkt->dgmPkt->remotePort);

    /* Catenating remote address string with ssdp ST header field. */
    s_length = strlen( r_address ) + strlen( st ) + strlen( port );
    id_string = (char *)malloc( s_length + 1 );

    if ( NULL == id_string )
    {
        cg_log_debug_s("Memory allocation problem!\n");
        return FALSE;
    }

    memset(id_string, '\0', s_length + 1);

    cg_strcat(id_string, r_address );
    cg_strcat(id_string, port);
    cg_strcat(id_string, st );

    loc = simple_string_hash(id_string, CG_UPNP_SSDP_FILTER_TABLE_SIZE);

    cg_log_debug("Calculated hash: %d\n", loc);

    free(id_string);

    curr_time = cg_getcurrentsystemtime();

    if ( 0 == timestamps[loc] ) {
        timestamps[loc] = curr_time;
        cg_log_debug("First packet... Updating hash table.\n");
        return FALSE;
    }
    else if ( ( curr_time - timestamps[loc] ) < CG_UPNP_DEVICE_M_SEARCH_FILTER_INTERVAL ) {
        cg_log_debug("Filtering packet!\n");
        timestamps[loc] = curr_time;
        return TRUE;
    }
    else {
        timestamps[loc] = curr_time;
        cg_log_debug("Old timestamp found, just updating it.\n");
        return FALSE;
    }

    cg_log_debug_l4("Leaving...\n");
}
Exemple #27
0
static char *cg_xml_node_tostring_indent(CgXmlNode *node, int indentLevel, BOOL withChildNode, CgString *str)
{
	char *name;
	char *value;
	CgString *valueStr;
	CgXmlNode *childNode;
	
	cg_log_debug_l4("Entering...\n");

	name = cg_xml_node_getname(node);
	value = cg_xml_node_getvalue(node);

	if (cg_xml_node_haschildnodes(node) == FALSE || withChildNode == FALSE) {
		cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel);
		if (!cg_string_naddvalue(str, "<", 1) ||
		    !cg_string_addvalue(str, name) ||
		    !cg_xml_node_attribute_tostring(node, str))
			/* Memory allocation failed */
			return NULL;
		
		valueStr = cg_string_new();
		if (!valueStr)
			/* Memory allocation failed */
			return NULL;
		
		cg_string_setvalue(valueStr, value);
		cg_xml_escapechars(valueStr);

		if (!cg_string_naddvalue(str, ">", 1) ||
		    !cg_string_addvalue(str, cg_string_getvalue(valueStr)) ||
		    !cg_string_naddvalue(str, "</", 2) ||
		    !cg_string_addvalue(str, name) ||
		    !cg_string_naddvalue(str, ">", 1) ||
		    !cg_string_addvalue(str, "\n"))
		{
			/* Memory allocation failed */
			cg_string_delete(valueStr);
			return NULL;
		}

		cg_string_delete(valueStr);
		
		return cg_string_getvalue(str);
	}

	cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel);
	if (!cg_string_naddvalue(str, "<", 1) ||
	    !cg_string_addvalue(str, name) ||
	    !cg_xml_node_attribute_tostring(node, str) ||
	    !cg_string_naddvalue(str, ">", 1) ||
	    !cg_string_addvalue(str, "\n"))
		/* Memory allocation failed */
		return NULL;

	for (childNode = cg_xml_node_getchildnodes(node); childNode != NULL; childNode = cg_xml_node_next(childNode))
		if (!cg_xml_node_tostring_indent(childNode, indentLevel+1, TRUE, str))
			/* Memory allocation failed */
			return NULL;

	cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel);
	if (!cg_string_naddvalue(str, "</", 2) ||
	    !cg_string_addvalue(str, name) ||
	    !cg_string_naddvalue(str, ">", 1) ||
	    !cg_string_addvalue(str, "\n"))
		/* Memory allocation failed */
		return NULL;

	cg_log_debug_l4("Leaving...\n");
	
	return cg_string_getvalue(str);
}
Exemple #28
0
void cg_net_uri_setvalue(CgNetURI *uri, char *value)
{
	char *protocol;
	int uriLen;
	int currIdx;
	int protoIdx;
	int atIdx;
	int colonIdx;
	int shashIdx;
	char *host;
	int eblacketIdx;
	CgString *hostStr;
	CgString *portStr;
	int hostLen;
	int sharpIdx;
	int questionIdx;
	int queryLen;
	
	cg_log_debug_l4("Entering...\n");

	uriLen = cg_strlen(value);
	cg_net_uri_clear(uri);
	cg_net_uri_seturi(uri, value);
		
	currIdx = 0;
	
	/*** Protocol ****/
	protoIdx = cg_strstr(value, CG_NET_URI_PROTOCOL_DELIM);
	if (0 < protoIdx) {
		cg_string_setnvalue(uri->protocol, value,  protoIdx);
		currIdx += protoIdx + cg_strlen(CG_NET_URI_PROTOCOL_DELIM);
	}

	/*** User (Password) ****/
	atIdx = cg_strstr(value+currIdx, CG_NET_URI_USER_DELIM);
	if (0 < atIdx) {
		colonIdx = cg_strstr(value+currIdx, CG_NET_URI_COLON_DELIM);
		/**** Thanks for Theo Beisch (2005/08/25) ****/
		if (0 < colonIdx && colonIdx<atIdx) {
			cg_string_setnvalue(uri->user, value+currIdx,  colonIdx);
			cg_string_setnvalue(uri->password, value+currIdx+colonIdx+1,  atIdx-(colonIdx+1));
		}
		else 
			cg_string_setnvalue(uri->user, value+currIdx,  atIdx - currIdx);
		currIdx += atIdx + 1;
	}

	/*** Host (Port) ****/
	shashIdx = cg_strstr(value+currIdx, CG_NET_URI_SLASH_DELIM);
	if (0 < shashIdx)
		cg_string_setnvalue(uri->host, value+currIdx,  shashIdx);
	else if (cg_net_uri_isabsolute(uri) == TRUE)
		cg_string_setnvalue(uri->host, value+currIdx, cg_strlen(value) - currIdx);
	host = cg_net_uri_gethost(uri);
	colonIdx = cg_strrchr(host, CG_NET_URI_COLON_DELIM, 1);
	eblacketIdx = cg_strrchr(host, CG_NET_URI_EBLACET_DELIM, 1);
	if (0 < colonIdx && eblacketIdx < colonIdx) {
		hostStr = cg_string_new();
		cg_string_setvalue(hostStr, host);
		hostLen = cg_string_length(hostStr);
		/**** host ****/
		cg_string_setnvalue(uri->host, cg_string_getvalue(hostStr),  colonIdx);
		host = cg_net_uri_gethost(uri);
		if (0 < hostLen) {
			if (host[0] == '[' && host[hostLen-1] == ']')
				cg_string_setnvalue(uri->host, cg_string_getvalue(hostStr)+1,  colonIdx-2);
		}
		/**** port ****/
		portStr = cg_string_new();
		cg_string_setnvalue(portStr, cg_string_getvalue(hostStr)+colonIdx+1,  hostLen- colonIdx-1);
		uri->port = atoi(cg_string_getvalue(portStr));
		cg_string_delete(portStr);
		cg_string_delete(hostStr);
	}
	else {
		uri->port = CG_NET_URI_KNKOWN_PORT;
		protocol = cg_net_uri_getprotocol(uri);
		if (cg_strcmp(protocol, CG_NET_URI_PROTOCOL_HTTP) == 0)
			uri->port = CG_NET_URI_DEFAULT_HTTP_PORT;
		if (cg_strcmp(protocol, CG_NET_URI_PROTOCOL_FTP) == 0)
			uri->port = CG_NET_URI_DEFAULT_FTP_PORT;
	}
	
	if (shashIdx > 0) currIdx += shashIdx;
	
	/*
		Handle relative URL
	*/
	if (cg_net_uri_isabsolute(uri) == FALSE)
	{
		cg_string_addvalue(uri->path, value);
		
	} else {
		/* First set path simply to the rest of URI */
		cg_string_setnvalue(uri->path, value+currIdx,  uriLen-currIdx);
	}
		
	/**** Path (Query/Fragment) ****/
	sharpIdx = cg_strstr(value+currIdx, CG_NET_URI_SHARP_DELIM);
	if (0 < sharpIdx) {
		cg_string_setnvalue(uri->path, value+currIdx,  sharpIdx);
		cg_string_setnvalue(uri->fragment, value+currIdx+sharpIdx+1,  uriLen-(currIdx+sharpIdx+1));
	}
	questionIdx = cg_strstr(value+currIdx, CG_NET_URI_QUESTION_DELIM);
	if (0 < questionIdx) {
		cg_string_setnvalue(uri->path, value+currIdx,  questionIdx);
		queryLen = uriLen-(currIdx+questionIdx+1);
		if (0 < sharpIdx)
			queryLen -= uriLen - (currIdx+sharpIdx);
		cg_string_setnvalue(uri->query, value+currIdx+questionIdx+1,  queryLen);
	}

	cg_log_debug_l4("Leaving...\n");
}
Exemple #29
0
char *cg_net_uri_getvalue(CgNetURI *uri)
{
	cg_net_uri_rebuild(uri);
	return cg_string_getvalue(uri->uri);
}
Exemple #30
0
char *cg_net_uri_unescapestring(char *buf, int bufSize, CgString *retBuf)
{
#if defined(CG_HTTP_CURL)
	char *tmp;
#else
	int n;
	char hexStr[3];
	long hex;
	unsigned char c;
#endif
	int idx = 0;
#if defined(CG_USE_NET_URI_ESCAPESTRING_SKIP)
	int tmpIdx = 0;
#endif
	
	cg_log_debug_l4("Entering...\n");

	if (!retBuf)
		return NULL;

	/* Check if URI is already escaped */
	if (cg_net_uri_isescapedstring(buf + idx, bufSize) == TRUE) 
		return buf;
	
	/* We can safely assume that the non-path part is already escaped */
#if defined(CG_USE_NET_URI_ESCAPESTRING_SKIP)
	idx = cg_strstr(buf, CG_NET_URI_PROTOCOL_DELIM);
	if (idx > 0)
	{
		idx = idx + cg_strlen(CG_NET_URI_PROTOCOL_DELIM);
		tmpIdx = cg_strstr(buf + idx, CG_NET_URI_SLASH_DELIM);
		if (tmpIdx > 0)
			idx += tmpIdx + cg_strlen(CG_NET_URI_SLASH_DELIM);
	} else {
		idx = 0;
	}
#endif

	if (bufSize < 1)
		bufSize = cg_strlen(buf) + 1;	
	
#if defined(CG_HTTP_CURL)
	tmp = curl_unescape(buf + idx, 0);
	if (tmp == NULL)
		return NULL;
	cg_string_addvalue(retBuf, tmp);
	cg_log_debug_s("%s ==> %s\n", buf + idx, tmp);
	curl_free(tmp);
#else
	for (n=0; n<bufSize;) {
		c = (unsigned char)buf[n];
		if (buf[n] == '%' && cg_net_uri_isalphanumchar(buf[n+1]) && cg_net_uri_isalphanumchar(buf[n+2])) {
			hexStr[0] = buf[n+1];
			hexStr[1] = buf[n+2];
			hexStr[2] = '\0';
			hex = strtol(hexStr, NULL, 16);
			c = (unsigned char)hex;
			n += 3;
		}
		else
			n++;
		cg_string_naddvalue(retBuf, (char *)&c, 1);
	}
#endif
	
	cg_log_debug_l4("Leaving...\n");

	return cg_string_getvalue(retBuf);
}