CgStringTokenizer *cg_string_tokenizer_new(char *value, char *delim)
{
	CgStringTokenizer *strToken;

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

	strToken = (CgStringTokenizer *)malloc(sizeof(CgStringTokenizer));

	if ( NULL != strToken )
	{
#if defined(NOUSE_ZERO_COPY)
		strToken->value = cg_strdup(value);
		strToken->delim = cg_strdup(delim);
#else
		strToken->value = value;
		strToken->delim = delim;
#endif
		strToken->delimCnt = cg_strlen(strToken->delim);
		strToken->nextStartPos = 0;
		strToken->lastPos = cg_strlen(value) - 1;
		strToken->currToken = NULL;
		strToken->nextToken = NULL;
		cg_string_tokenizer_nexttoken(strToken);
	}

	return strToken;

	cg_log_debug_l4("Leaving...\n");
}
Example #2
0
void cg_http_packet_read_headers(CgHttpPacket *httpPkt, CgSocket *sock, char *lineBuf, size_t lineBufSize)
{
	CgStringTokenizer *strTok;
	CgHttpHeader *header;
	ssize_t readLen;
	char *name, *value;
	
	cg_log_debug_l4("Entering...\n");

	while (1) {
		readLen = cg_socket_readline(sock, lineBuf, lineBufSize);
		if (readLen <= 2)
			break;
		name = NULL;
		value = NULL;
		strTok = cg_string_tokenizer_new(lineBuf, CG_HTTP_HEADERLINE_DELIM);
		if (cg_string_tokenizer_hasmoretoken(strTok) == TRUE)
			name = cg_string_tokenizer_nexttoken(strTok);
		if (cg_string_tokenizer_hasmoretoken(strTok) == TRUE) {
			value = cg_string_tokenizer_nextalltoken(strTok);
			cg_strrtrim(value, CG_HTTP_HEADERLINE_DELIM, cg_strlen(CG_HTTP_HEADERLINE_DELIM));
		}
		if (0 < cg_strlen(name)) {
			if (cg_strlen(value) == 0)
				value = "";
			header = cg_http_header_new();
			cg_http_header_setname(header, name);
			cg_http_header_setvalue(header, value);
			cg_http_packet_addheader(httpPkt, header);
		}
		cg_string_tokenizer_delete(strTok);
	}

	cg_log_debug_l4("Leaving...\n");
}
Example #3
0
void cg_http_packet_post(CgHttpPacket *httpPkt, CgSocket *sock)
{
	CgHttpHeader *header;
	const char *name, *value;
	char *content;
	size_t contentLen;
	
	cg_log_debug_l4("Entering...\n");

	/**** send headers ****/
	for (header = cg_http_packet_getheaders(httpPkt); header != NULL; header = cg_http_header_next(header)) {
		name = cg_http_header_getname(header);
		if (name == NULL)
			continue;
		cg_socket_write(sock, name, cg_strlen(name));
		cg_socket_write(sock, CG_HTTP_COLON, sizeof(CG_HTTP_COLON)-1);
		cg_socket_write(sock, CG_HTTP_SP, sizeof(CG_HTTP_SP)-1);
		value = cg_http_header_getvalue(header);
		if (value != NULL)
			cg_socket_write(sock, value, cg_strlen(value));
		cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1);
	}
	cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1);
	
	/**** send content ****/
	content = cg_http_packet_getcontent(httpPkt);
	contentLen = cg_http_packet_getcontentlength(httpPkt);
	if (content != NULL && 0 < contentLen)
		cg_socket_write(sock, content, contentLen);

	cg_log_debug_l4("Leaving...\n");
}
Example #4
0
size_t cg_http_packet_getheadersize(CgHttpPacket *httpPkt)
{
	CgHttpHeader *header;
	size_t headerSize;
	const char *name;
	const char *value;
	
	cg_log_debug_l4("Entering...\n");

	headerSize = 0;
	for (header = cg_http_packet_getheaders(httpPkt); header != NULL; header = cg_http_header_next(header)) {
		name = cg_http_header_getname(header);
		value = cg_http_header_getvalue(header);
		headerSize += cg_strlen(name); 
		headerSize += sizeof(CG_HTTP_COLON)-1; 
		headerSize += sizeof(CG_HTTP_SP)-1; 
		headerSize += cg_strlen(value); 
		headerSize += sizeof(CG_HTTP_CRLF)-1; 
	}
	headerSize += sizeof(CG_HTTP_CRLF)-1; 

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

	return headerSize;
}
Example #5
0
File: curi.c Project: Deanzou/DLNA
void cg_net_uri_rebuild(CgNetURI *uri)
{
	char portStr[32];
	char *path;
	
	cg_log_debug_l4("Entering...\n");

	cg_string_setvalue(uri->uri, cg_net_uri_getprotocol(uri));
	cg_string_addvalue(uri->uri, CG_NET_URI_PROTOCOL_DELIM);
	cg_string_addvalue(uri->uri, cg_net_uri_gethost(uri));
	cg_string_addvalue(uri->uri, CG_NET_URI_COLON_DELIM);
	cg_string_addvalue(uri->uri, cg_int2str(cg_net_uri_getport(uri), portStr, sizeof(portStr)));
	if (0 < cg_strlen(cg_net_uri_getpath(uri))) {
		path = cg_net_uri_getpath(uri);
		if (path[0] != '/')
			cg_string_addvalue(uri->uri, CG_NET_URI_SLASH_DELIM);
		cg_string_addvalue(uri->uri, cg_net_uri_getpath(uri));
		if (cg_strchr(cg_net_uri_getpath(uri), "?", 1) == -1 && 0 < cg_strlen(cg_net_uri_getquery(uri))) {
			cg_string_addvalue(uri->uri, CG_NET_URI_QUESTION_DELIM);
			cg_string_addvalue(uri->uri, cg_net_uri_getquery(uri));
		}
	}
	
	cg_log_debug_l4("Leaving...\n");
}
Example #6
0
/**
 * Parse the service description from the service's SCPD URL. Do not call
 * this from user applications.
 * 
 * @param service The service in question
 * @return TRUE if successful; otherwise FALSE
 */
BOOL cg_upnp_controlpoint_parsescservicescpd(CgUpnpService *service)
{
	CgNetURL *scpdURL;
	BOOL scpdParseSuccess;
	
	cg_log_debug_l4("Entering...\n");

	scpdURL = cg_upnp_service_getscpdurl(service); 

	if ( NULL == scpdURL )		
		return FALSE;
	
	cg_log_debug_s("SCPD URL: %s\n", cg_net_url_getrequest(scpdURL));
	scpdParseSuccess = cg_upnp_service_parsedescriptionurl(service, scpdURL);
	
	cg_net_url_delete(scpdURL);
	if (scpdParseSuccess == TRUE)
		return TRUE;

#if defined(CG_UPNP_USE_STDDCP)
	if (cg_upnp_service_hasstddcp(service)) {
		char *stdDCP = cg_upnp_service_getstddcp(service);
		scpdParseSuccess = cg_upnp_service_parsedescription(service, stdDCP, cg_strlen(stdDCP));
	}
#endif

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

	return scpdParseSuccess;
}
Example #7
0
/**
 * Find a device from the controlpoint by the UDN of the device.
 *
 * \param ctrlPoint Controlpoint in question
 * \param udn Type of the device
 *
 */
CgUpnpDevice *cg_upnp_controlpoint_getdevicebyudn(CgUpnpControlPoint *ctrlPoint,
						  char *udn)
{
	CgUpnpDevice *dev = NULL;
	CgUpnpDevice *childDev = NULL;
	
	cg_log_debug_l4("Entering...\n");

	if (cg_strlen(udn) <= 0 || ctrlPoint == NULL)
	{
		return NULL;
	}
	
	for (dev = cg_upnp_controlpoint_getdevices(ctrlPoint); 
	     dev != NULL;
	     dev = cg_upnp_device_next(dev))
	{
		if (cg_strcmp(cg_upnp_device_getudn(dev), udn) == 0)
		{
			return dev;
		}
		childDev = cg_upnp_device_getdevicebyudn(dev, udn);
		if (childDev != NULL)
		{
			return childDev;
		}
	}

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

	return NULL;
}
Example #8
0
File: curi.c Project: Deanzou/DLNA
char *cg_net_uri_getupnpbasepath(CgNetURI *locationURL)
{
        char *path, *c;
        int i;

        path = cg_strdup(cg_net_uri_getpath(locationURL));

	cg_log_debug_s("Mangling url string: %s\n", path);
        
	i = cg_strlen(path);

        if ( 0 >= i )
        {
                cg_log_debug("No base path, doing nothing.\n");
                return NULL;
        }

        /* Truncating out the "file name" from path */
        for ( c=( path + --i); 0<=i; c=( path + --i ))
                if ( '/' == *c )
                {
                        *( path + i + 1 ) = '\0';
			cg_log_debug_s("Truncating string from place %d\n", i);
                        break;
                }

	cg_log_debug_s("url string after mangling: %s\n", path);

        return path;
}
Example #9
0
File: curi.c Project: Deanzou/DLNA
BOOL cg_net_uri_isescapedstring(char *buf, int bufSize)
{
	int idx;

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

	if (bufSize < 1) bufSize = cg_strlen(buf) + 1;
	
	/* We assume that the URI string is escaped, if it has at least one
	   escaped character */
	
	/* First check that there is escaping character */
	idx = cg_strstr(buf, CG_NET_URI_ESCAPING_CHAR);
	if (idx < 0 || idx > bufSize - 4) return FALSE;
	
	/* Check that the next two characters are HEX */
	if (((buf[idx+1] >= '0' && buf[idx+1] <= '9') ||
	     (buf[idx+1] >= 'a' && buf[idx+1] <= 'f') ||
	     (buf[idx+1] >= 'A' && buf[idx+1] <= 'F')) &&
	    ((buf[idx+2] >= '0' && buf[idx+2] <= '9') ||
	     (buf[idx+2] >= 'a' && buf[idx+2] <= 'f') ||
	     (buf[idx+2] >= 'A' && buf[idx+2] <= 'F')))
		return TRUE;
	
	cg_log_debug_l4("Leaving...\n");

	return FALSE;
}
Example #10
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);
}
Example #11
0
char *cg_string_addvalue(CgString *str, const char *value)
{
	cg_log_debug_l5("Entering...\n");

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

	return cg_string_naddvalue(str, value, cg_strlen(value));
}
Example #12
0
BOOL cg_http_request_postresponse(CgHttpRequest *httpReq, CgHttpResponse *httpRes)
{
    CgSocket *sock;
    char httpDate[CG_HTTP_DATE_MAXLEN];
    char *version, *reasonPhrase;
    int statusCode;
    char statusCodeBuf[CG_STRING_INTEGER_BUFLEN];

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

    sock = cg_http_request_getsocket(httpReq);

    cg_log_debug_s("Got request:\n");
    cg_http_request_print(httpReq);

    cg_http_response_setdate(httpRes, cg_http_getdate(cg_getcurrentsystemtime(), httpDate, sizeof(httpDate)));

    version = cg_http_response_getversion(httpRes);
    statusCode = cg_http_response_getstatuscode(httpRes);
    reasonPhrase = cg_http_response_getreasonphrase(httpRes);

    if (version == NULL || reasonPhrase == NULL)
        return FALSE;

    cg_int2str(statusCode, statusCodeBuf, sizeof(statusCodeBuf));

    /**** send first line ****/
    cg_socket_write(sock, version, cg_strlen(version));
    cg_socket_write(sock, CG_HTTP_SP, sizeof(CG_HTTP_SP)-1);
    cg_socket_write(sock, statusCodeBuf, cg_strlen(statusCodeBuf));
    cg_socket_write(sock, CG_HTTP_SP, sizeof(CG_HTTP_SP)-1);
    cg_socket_write(sock, reasonPhrase, cg_strlen(reasonPhrase));
    cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1);

    cg_log_debug_s("Posting response:\n");
    cg_http_response_print(httpRes);

    /**** send header and content ****/
    cg_http_packet_post((CgHttpPacket *)httpRes, sock);

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

    return TRUE;
}
Example #13
0
void cg_string_setvalue(CgString *str, const char *value)
{
	cg_log_debug_l5("Entering...\n");

	if (NULL != str) {
		if (value != NULL)
			cg_string_setnvalue(str, value, cg_strlen(value));
	}
	
	cg_log_debug_l5("Leaving...\n");
}
Example #14
0
CgDictionaryElement *cg_dictionary_getelement(CgDictionary *dir, char *key)
{
	CgDictionaryElement *dirElem;
	char *dirElemKey;

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

	if (NULL == dir || cg_strlen(key) <= 0 )
		return NULL;
	
	for (dirElem = cg_dictionary_gets(dir); dirElem != NULL; dirElem = cg_dictionary_element_next(dirElem)) {
			dirElemKey = cg_dictionary_element_getkey(dirElem);
			if (cg_strlen(dirElemKey) <= 0)
				continue;
			if (cg_streq(dirElemKey, key) == TRUE)
				return dirElem;
	}

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

	return NULL;
}
Example #15
0
void cg_http_packet_sethost(CgHttpPacket *httpPkt, const char *addr, int port)
{
	char *host;
	size_t hostMaxLen;

	cg_log_debug_l4("Entering...\n");
	
	if (addr == NULL)
		return;

	hostMaxLen = cg_strlen(addr) + CG_NET_IPV6_ADDRSTRING_MAXSIZE + CG_STRING_INTEGER_BUFLEN;
	host = malloc(sizeof(char) * hostMaxLen);

	if (host == NULL)
		/* Memory allocation failure */
		return;

#if defined(HAVE_SNPRINTF)
	if (0 < port && port != CG_HTTP_DEFAULT_PORT) {
		if (cg_net_isipv6address(addr) == TRUE)
			snprintf(host, hostMaxLen, "[%s]:%d", addr, port);
		else
			snprintf(host, hostMaxLen, "%s:%d", addr, port);
	}
	else {
		if (cg_net_isipv6address(addr) == TRUE)
			snprintf(host, hostMaxLen, "[%s]", addr);
		else
			snprintf(host, hostMaxLen, "%s", addr);
	}
#else	
	if (0 < port && port != CG_HTTP_DEFAULT_PORT) {
		if (cg_net_isipv6address(addr) == TRUE)
			sprintf(host, "[%s]:%d", addr, port);
		else
			sprintf(host, "%s:%d", addr, port);
	}
	else {
		if (cg_net_isipv6address(addr) == TRUE)
			sprintf(host, "[%s]", addr);
		else
			sprintf(host, "%s", addr);
	}
#endif

	cg_http_packet_setheadervalue(httpPkt, CG_HTTP_HOST, host);

	free(host);

	cg_log_debug_l4("Leaving...\n");
}
char *cg_string_tokenizer_nextalltoken(CgStringTokenizer *strToken)
{
	int nextTokenLen;

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

	nextTokenLen = cg_strlen(strToken->nextToken);
	strToken->nextToken[nextTokenLen] = strToken->repToken;
	strToken->currToken = strToken->nextToken;
	strToken->nextToken = NULL;
	strToken->hasNextTokens = FALSE;
	return strToken->currToken;

	cg_log_debug_l4("Leaving...\n");
}
Example #17
0
BOOL cg_http_request_postchunkedsize(CgHttpRequest *httpReq, int dataLen)
{
    CgSocket *sock;
    char chunkedChar[CG_STRING_LONG_BUFLEN+2];

    sock = cg_http_request_getsocket(httpReq);
#if defined(HAVE_SNPRINTF)
    snprintf(chunkedChar, sizeof(chunkedChar), "%x%s", dataLen, CG_HTTP_CRLF);
#else
    sprintf(chunkedChar, "%x%s", dataLen, CG_HTTP_CRLF);
#endif
    cg_socket_write(sock, chunkedChar, cg_strlen(chunkedChar));

    return TRUE;
}
CgStringTokenizer *cg_string_tokenizer_new(const char *value, const char *delim)
{
	CgStringTokenizer *strToken;

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

	strToken = (CgStringTokenizer *)malloc(sizeof(CgStringTokenizer));

	if ( NULL != strToken )
	{
		strToken->value = cg_strdup(value);
		strToken->delim = cg_strdup(delim);
		strToken->delimCnt = cg_strlen(strToken->delim);
		strToken->nextStartPos = 0;
		strToken->lastPos = cg_strlen(value) - 1;
		strToken->currToken = NULL;
		strToken->nextToken = NULL;
		cg_string_tokenizer_nexttoken(strToken);
	}
  
	cg_log_debug_l4("Leaving...\n");
  
	return strToken;
}
void cg_soap_request_setcontent(CgSoapRequest *soapReq, CgXmlNode *node)
{
    CgHttpRequest *httpReq;

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

    httpReq = cg_soap_request_gethttprequest(soapReq);

    /**** content type ****/
    cg_http_request_setcontenttype(httpReq, CG_XML_CONTENT_TYPE);

    /**** content ****/
    cg_http_request_appendncontent(httpReq, CG_SOAP_VERSION_HEADER,
                    cg_strlen(CG_SOAP_VERSION_HEADER));
    cg_http_request_appendncontent(httpReq, CG_XML_CONTENT_LF,
                    cg_strlen(CG_XML_CONTENT_LF));
    cg_xml_node_tostring(node, TRUE, httpReq->content);

    /**** content length ****/
    cg_http_request_setcontentlength(httpReq,
                     cg_string_length(httpReq->content));

    cg_log_debug_l4("Leaving...\n");
}
Example #20
0
File: curi.c Project: Deanzou/DLNA
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);
}
Example #21
0
/**
 * Find a device from the controlpoint by the type of the device.
 * This function searches for devices, whose *type part* (i.e. not including
 * the version) of the device type string matches the given string.
 * For example: "urn:schemas-upnp-org:device:FooDevice". If you need
 * to know the version of a device, use \ref cg_upnp_devicetype_getversion
 *
 * \param ctrlPoint Controlpoint in question
 * \param type Type of the device
 *
 */
CgUpnpDevice *cg_upnp_controlpoint_getdevicebytype(CgUpnpControlPoint *ctrlPoint,
						   char *type)
{
	CgUpnpDevice *dev = NULL;
	CgUpnpDevice *childDev = NULL;
	const char* typeString = NULL;
	char* part = NULL;
	
	cg_log_debug_l4("Entering...\n");

	if (cg_strlen(type) <= 0 || ctrlPoint == NULL)
	{
		return NULL;
	}

	for (dev = cg_upnp_controlpoint_getdevices(ctrlPoint);
	     dev != NULL;
	     dev = cg_upnp_device_next(dev))
	{
		typeString = cg_upnp_device_getdevicetype(dev);
		if (typeString != NULL)
		{
			part = cg_upnp_devicetype_getschematype(typeString);
			if (cg_strcmp(part, type) == 0)
			{
				free(part);
				
				return dev;
			}
			else
			{
				free(part);
			}
		}
				
		childDev = cg_upnp_device_getdevicebytype(dev, type);
		if (childDev != NULL)
		{
			return childDev;
		}
	}
	
	cg_log_debug_l4("Leaving...\n");

	return NULL;
}
Example #22
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;
}
Example #23
0
char *cg_upnp_getservername(char *buf, int bufSize)
{
	int nameLen;

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

	cg_http_getservername(buf, bufSize);
	nameLen = cg_strlen(buf);
	bufSize -= nameLen;
	if (bufSize <= 0)
		return buf;
#if defined(HAVE_SNPRINTF)
	snprintf((buf+nameLen), bufSize, " %s/%s UPnP/%s DLNADOC/%s", CG_CLINK_NAME, CG_CLINK_VER, CG_UPNP_VER, CG_DLNA_VER);
#else
	sprintf((buf+nameLen), " %s/%s UPnP/%s DLNADOC/%s", CG_CLINK_NAME, CG_CLINK_VER, CG_UPNP_VER, CG_DLNA_VER);
#endif

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

	return buf;
}
int cg_net_getipv6scopeid(const char *addr)
{
    size_t addrLen;
    ssize_t perIdx;
    char scopeIDBuf[8+1];

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

    if (cg_net_isipv6address(addr) == FALSE)
        return 0;
    addrLen = cg_strlen(addr);
    perIdx = cg_strchr(addr, "%", 1);
    if (perIdx < 0)
        return 0;
    cg_strncpy(scopeIDBuf, (addr+perIdx+1), sizeof(scopeIDBuf)-1);
    scopeIDBuf[sizeof(scopeIDBuf)-1] = '\0';

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

    return atoi(scopeIDBuf);
}
Example #25
0
static CgUpnpDevice *cg_upnp_controlpoint_createdevicefromssdkpacket(CgUpnpSSDPPacket *ssdpPkt)
{
	const char *location;
	CgNetURL *url;
	CgUpnpDevice *dev;
	BOOL parseSuccess;
	
	cg_log_debug_l4("Entering...\n");

	location = cg_upnp_ssdp_packet_getlocation(ssdpPkt);
	if (cg_strlen(location) <= 0)
		return NULL;
			
	dev = cg_upnp_device_new();
	
	url = cg_net_url_new();
	cg_net_url_set(url, location);
	parseSuccess =  cg_upnp_device_parsedescriptionurl(dev, url);
	cg_net_url_delete(url);
	
	if (parseSuccess == FALSE) {
		cg_upnp_device_delete(dev);
		return NULL;
	}

	cg_upnp_device_setssdppacket(dev, ssdpPkt);

#ifndef CG_OPTIMIZED_CP_MODE
	if (cg_upnp_controlpoint_parseservicesfordevice(dev, ssdpPkt) == FALSE)
	{
		cg_upnp_device_delete(dev);
		return NULL;
	}
#endif
	
	cg_log_debug_l4("Leaving...\n");

	return dev;
}
Example #26
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;
}
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 #28
0
File: curi.c Project: Deanzou/DLNA
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;
}
Example #29
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;	
}