char *cg_upnp_event_subscription_createsid(char *buf, int bufSize)
{
#if !defined(ITRON)
	time_t seed1;
	time_t seed2; 
#else
	long seed1;
	long seed2; 
#endif

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

#if !defined(ITRON)
	seed1 = cg_getcurrentsystemtime();
	seed2 = (time_t)((double)cg_getcurrentsystemtime() * ((double)rand() / (double)RAND_MAX));
#else
	seed1 = rand();
	seed2 = rand();
#endif

#if defined(HAVE_SNPRINTF)
	snprintf(buf, bufSize,
#else
	sprintf(buf,
#endif
		 "%04x-%04x-%04x-%04x", 
		(int)(seed1 & 0xFFFF), 
		(int)(((seed1 >> 31) | 0xA000) & 0xFFFF),
		(int)(seed2 & 0xFFFF), 
		(int)(((seed2 >> 31) | 0xE000) & 0xFFFF));
	return buf;

	cg_log_debug_l4("Leaving...\n");
}
Example #2
0
BOOL upnp_clock_actionreceived(CgUpnpAction *action)
{
    CgTime currTime;
    char *actionName;
    CgUpnpArgument *currTimeArg;
    char sysTimeStr[SYSTEM_TIME_BUF_LEN];
    CgUpnpArgument *newTimeArg, *resultArg;

    currTime = cg_getcurrentsystemtime();

    actionName = cg_upnp_action_getname(action);
    if (strcmp("GetTime", actionName) == 0) {
        GetSystemTimeString(currTime, sysTimeStr);
        currTimeArg = cg_upnp_action_getargumentbyname(action, "CurrentTime");
        cg_upnp_argument_setvalue(currTimeArg, sysTimeStr);
        return TRUE;
    }
    if (strcmp(actionName, "SetTime") == 0) {
        newTimeArg = cg_upnp_action_getargumentbyname(action, "NewTime");
        resultArg = cg_upnp_action_getargumentbyname(action, "Result");
        cg_upnp_argument_setvalue(resultArg, "Not implemented");
        return TRUE;
    }

    return FALSE;
}
Example #3
0
char *cg_upnp_createuuid(char *uuidBuf, int uuidBufSize)
{
#if defined(HAVE_LIBUUID) || defined(TARGET_OS_IPHONE)
	uuid_t uuid;
	char uuidStr[CG_UPNP_UUID_MAX_LEN];
#elif defined(WIN32)
	#pragma comment(lib, "Rpcrt4.lib")
	UUID uuid;
	unsigned char* szUuid = NULL;
#else
	time_t time1;
	time_t time2;
#endif

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

#if defined(HAVE_LIBUUID) || defined(TARGET_OS_IPHONE)
    uuid_generate(uuid);
	uuid_unparse_lower(uuid, uuidStr);
	snprintf(uuidBuf,uuidBufSize, "uuid:%s",uuidStr);
#elif defined(WIN32)
	UuidCreate(&uuid);
	UuidToString(&uuid, &szUuid);
	sprintf(uuidBuf, "%s:%s",
		CG_UPNP_UUID_NAME,
		szUuid);
	RpcStringFree(&szUuid);
#else
	/**** Thanks for Makela Aapo (10/30/05) ****/
	time1 = cg_getcurrentsystemtime();
	time2 = (time_t)((double)cg_getcurrentsystemtime(NULL) * ((double)rand() / (double)RAND_MAX));
	snprintf(uuidBuf, uuidBufSize, "%s:%04x-%04x-%04x-%04x",
		CG_UPNP_UUID_NAME,
		(int)(time1 & 0xFFFF),
		(int)(((time1 >> 31) | 0xA000) & 0xFFFF),
		(int)(time2 & 0xFFFF),
		(int)(((time2 >> 31) | 0xE000) & 0xFFFF));
#endif

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

	return uuidBuf;
}
Example #4
0
BOOL upnp_clock_queryreceived(CgUpnpStateVariable *statVar)
{
    char *varName;
    CgTime currTime;
    char sysTimeStr[SYSTEM_TIME_BUF_LEN];

    varName = cg_upnp_statevariable_getname(statVar);
    if (strcmp("Time", varName) == 0) {
        currTime = cg_getcurrentsystemtime();
        GetSystemTimeString(currTime, sysTimeStr);
        cg_upnp_statevariable_setvalue(statVar, sysTimeStr);
        return TRUE;
    }

    return FALSE;
}
Example #5
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 #6
0
void upnp_clock_device_update(CgUpnpDevice *clockDev)
{
    CgTime currTime;
    CgUpnpService *timeService;
    CgUpnpStateVariable *timeState;
    char sysTimeStr[SYSTEM_TIME_BUF_LEN];

    timeService = cg_upnp_device_getservicebyexacttype(clockDev, "urn:schemas-upnp-org:service:timer:1");
    if (timeService == NULL)
        return;

    timeState = cg_upnp_service_getstatevariablebyname(timeService, "Time");
    if (timeState == NULL)
        return;

    currTime = cg_getcurrentsystemtime();
    GetSystemTimeString(currTime, sysTimeStr);
    cg_upnp_statevariable_setvalue(timeState, sysTimeStr);

    printf("%s\n", sysTimeStr);
}
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");
}
Example #8
0
void upnp_clock_device_httprequestrecieved(CgHttpRequest *httpReq)
{
    CgTime currTime;
    CgUpnpDevice *dev;
    char *uri;
    char content[2048];
    char sysTimeStr[SYSTEM_TIME_BUF_LEN];
    char serverName[CG_UPNP_SEVERNAME_MAXLEN];
    CgHttpResponse *httpRes;
    BOOL postRet;

    dev = (CgUpnpDevice *)cg_http_request_getuserdata(httpReq);

    uri = cg_http_request_geturi(httpReq);
    if (strcmp(uri, "/presentation") != 0) {
        cg_upnp_device_httprequestrecieved(httpReq);
        return;
    }

    currTime = cg_getcurrentsystemtime();

#if defined(HAVE_SNPRINTF)
    snprintf(content, sizeof(content),
#else
    sprintf(content,
#endif
             "<HTML>"
             "<HEAD>"
             "<TITLE>UPnP Clock Sample</TITLE>"
             "</HEAD>"
             "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=/presentation\">"
             "<BODY><CENTER>"
             "<H1>UPnP Clock Sample</H1>"
             "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
             "<TR>"
             "<TD style=\"width: 50px; height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "<TD style=\"background-color: rgb(176, 176, 176);\"></TD>"
             "<TD style=\"width: 50px; height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "</TR>"
             "<TR>"
             "<TD style=\"height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "<TD style=\"height: 50px; background-color: rgb(221, 236, 245);\" align=\"center\"><H1>"
             "%s"
             "</H1></TD>"
             "<TD style=\"height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "</TR>"
             "<TR>"
             "<TD style=\"height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "<TD style=\"height: 50px; background-color: rgb(221, 236, 245);\" align=\"center\"><H3>"
             "Server : %s"
             "</H3></TD>"
             "<TD style=\"height: 30px; background-color: rgb(176, 176, 176);\"></TD>"
             "</TR>"
             "<TR>"
             "<TD style=\"width: 30px; height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "<TD style=\"background-color: rgb(176, 176, 176);\"></TD>"
             "<TD style=\"width: 30px; height: 50px; background-color: rgb(176, 176, 176);\"></TD>"
             "</TR>"
             "</TABLE>"
             "<CENTER></BODY>"
             "</HTML>",
             GetSystemTimeString(currTime, sysTimeStr),
             cg_upnp_getservername(serverName, sizeof(serverName)));

    httpRes = cg_http_response_new();
    cg_http_response_setstatuscode(httpRes, CG_HTTP_STATUS_OK);
    cg_http_response_setcontent(httpRes, content);
    cg_http_response_setcontenttype(httpRes, "text/html");
    cg_http_response_setcontentlength(httpRes, strlen(content));
    postRet = cg_http_request_postresponse(httpReq, httpRes);
    cg_http_response_delete(httpRes);
}
BOOL cg_http_persistentconnection_put(char *host, int port, void *data)
{
       CgHttpPersistentConnection *new_node = NULL, *node = NULL;

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

       /* If we dont have cache, then just exit */
       if (cache == NULL) {
	       cg_log_debug("(put) No cache! Persistent connections not initialized?\n");
	       return FALSE;
       }

       /* Check if we already have this one cached */
       for (node = (CgHttpPersistentConnection*)cg_list_gets((CgList*)cache);
            node != NULL;
            node = (CgHttpPersistentConnection*)cg_list_next((CgList*)node))
       {
               if (cg_strcmp(cg_string_getvalue(node->host), host) == 0 &&
                   node->port == port)
               {
                       /* If also data is the same, then update just
                          timestamp */
                       if (node->cacheData == data)
                       {
                               node->timestamp = cg_getcurrentsystemtime();
                               return TRUE;
                       }

		      cg_log_debug_s("Found cached persistent connection for %s:%d\n",
			      cg_string_getvalue(node->host), node->port);
                       new_node = node;
                       cg_list_remove((CgList*)new_node);
                       break;
               }
       }

       /* We didn't find it */
       if (new_node == NULL)
       {
               /* Check if we have already too many cached things */
               if (cg_list_size((CgList*)cache) >= CG_HTTP_PERSISTENT_CACHE_SIZE)
               {
                       /* Take last node (not refreshed for a long time) */
                       new_node = (CgHttpPersistentConnection *)cg_list_next((CgList *)cache);
                       cg_list_remove((CgList*)new_node);
                       cg_http_persistentconnection_delete(new_node);
                       new_node = NULL;

		      cg_log_debug_s("Max persistent HTTP connection cache reached.\n");
               }

               if (new_node == NULL)
               {
                       if (data == NULL) return TRUE;

                       new_node = cg_http_persistentconnection_new();
                       if (new_node == NULL) return FALSE;

		      cg_log_debug_s("Adding persistent HTTP Connection %s:%d to cache\n",
			       host, port);
		      cg_log_debug_s("Persistent connections: %d\n", cg_list_size((CgList*)cache));
               }
       }

       if (data != NULL)
       {
               /* Set appropriate values for the node */
               cg_string_setvalue(new_node->host, host);
               new_node->port = port;
               new_node->cacheData = data;
               new_node->timestamp = cg_getcurrentsystemtime();

               cg_list_add((CgList*)cache, (CgList*)new_node);
       } else {
               /* remove and delete node */
               cg_http_persistentconnection_delete(new_node);
       }

       return TRUE;

	cg_log_debug_l4("Leaving...\n");
}
void *cg_http_persistentconnection_get(char *host, int port)
{
       CgHttpPersistentConnection *node;
       CgSysTime sys_time = cg_getcurrentsystemtime();
       BOOL iterate;

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

       /* If we dont have cache, then just exit */
       if (cache == NULL) { 
	       cg_log_debug("(get) No cache! Persistent connections not initialized?\n");
	       return NULL;
	}

       /* Clear all expired nodes */
       do {
               iterate = FALSE;
               for (node = (CgHttpPersistentConnection*)cg_list_gets((CgList*)cache);
                    node != NULL;
                    node = (CgHttpPersistentConnection*)cg_list_next((CgList*)node))
               {
                       if (sys_time > node->timestamp + CG_HTTP_PERSISTENT_TIMEOUT_PERIOD)
                       {
			      cg_log_debug_s("Timeout for persistent HTTP Connection to %s:%d "
				       "(timestamp: %d)\n",
				      cg_string_getvalue(node->host), node->port,
				      node->timestamp);
                               cg_list_remove((CgList*)node);
                               cg_http_persistentconnection_delete(node);
                               iterate = TRUE;
                               break;
                       }
               }
       } while (iterate);

       /* Get persistent node */
       for (node = (CgHttpPersistentConnection*)cg_list_gets((CgList*)cache);
            node != NULL;
            node = (CgHttpPersistentConnection*)cg_list_next((CgList*)node))
       {
               if (cg_strcmp(cg_string_getvalue(node->host), host) == 0 &&
                   node->port == port)
               {
                       /* Node was required, remove and add again to refresh
                          cache */
                       cg_list_remove((CgList*)node);
                       cg_list_add((CgList*)cache, (CgList*)node);

                       node->timestamp = cg_getcurrentsystemtime();

		      cg_log_debug_s("Persistent HTTP Connection cache HIT for %s:%d\n",
			       host, port);

                       return node->cacheData;
               }
       }

      cg_log_debug_s("Persistent HTTP Connection cache MISS for %s:%d\n",
	      host, port);

       return NULL;

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