BOOL upnp_clock_actionreceived(mUpnpAction *action) { mUpnpTime currTime; const char *actionName; mUpnpArgument *currTimeArg; char sysTimeStr[SYSTEM_TIME_BUF_LEN]; mUpnpArgument *newTimeArg, *resultArg; currTime = mupnp_getcurrentsystemtime(); actionName = mupnp_action_getname(action); if (strcmp("GetTime", actionName) == 0) { GetSystemTimeString(currTime, sysTimeStr); currTimeArg = mupnp_action_getargumentbyname(action, "CurrentTime"); mupnp_argument_setvalue(currTimeArg, sysTimeStr); return TRUE; } if (strcmp(actionName, "SetTime") == 0) { newTimeArg = mupnp_action_getargumentbyname(action, "NewTime"); resultArg = mupnp_action_getargumentbyname(action, "Result"); mupnp_argument_setvalue(resultArg, "Not implemented"); return TRUE; } return FALSE; }
/** * Renew a subscription. Essentially sets subscription time (duration) * to zero and resets notify count (== event key). * * @param sub The event subscriber */ void mupnp_subscriber_renew(mUpnpSubscriber* sub) { mupnp_log_debug_l4("Entering...\n"); //Theo Beisch use clinkc time mupnp_subscriber_setsubscriptiontime(sub, mupnp_getcurrentsystemtime()); mupnp_log_debug_l4("Leaving...\n"); }
const char *mupnp_createuuid(char *uuidBuf, size_t uuidBufSize) { #if defined(HAVE_LIBUUID) uuid_t uuid; char uuidStr[MUPNP_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 mupnp_log_debug_l4("Entering...\n"); #if defined(HAVE_LIBUUID) 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", MUPNP_UUID_NAME, szUuid); RpcStringFree(&szUuid); #else /**** Thanks for Makela Aapo (10/30/05) ****/ time1 = mupnp_getcurrentsystemtime(); time2 = (time_t)((double)mupnp_getcurrentsystemtime(NULL) * ((double)rand() / (double)RAND_MAX)); snprintf(uuidBuf, uuidBufSize, "%s:%04x-%04x-%04x-%04x", MUPNP_UUID_NAME, (int)(time1 & 0xFFFF), (int)(((time1 >> 31) | 0xA000) & 0xFFFF), (int)(time2 & 0xFFFF), (int)(((time2 >> 31) | 0xE000) & 0xFFFF)); #endif mupnp_log_debug_l4("Leaving...\n"); return uuidBuf; }
int mupnp_createbootid() { mUpnpTime currentTime; mupnp_log_debug_l4("Entering...\n"); currentTime = mupnp_getcurrentsystemtime(); mupnp_log_debug_l4("Leaving...\n"); return (int)(currentTime % INT_MAX); }
BOOL upnp_clock_queryreceived(mUpnpStateVariable *statVar) { const char *varName; mUpnpTime currTime; char sysTimeStr[SYSTEM_TIME_BUF_LEN]; varName = mupnp_statevariable_getname(statVar); if (strcmp("Time", varName) == 0) { currTime = mupnp_getcurrentsystemtime(); GetSystemTimeString(currTime, sysTimeStr); mupnp_statevariable_setvalue(statVar, sysTimeStr); return TRUE; } return FALSE; }
/** * Check, whether a subscriber's event subscription has been expired * * @param sub The subscriber * @return true if the subscription has been expired; otherwise false */ bool mupnp_subscriber_isexpired(mUpnpSubscriber* sub) { mUpnpTime currTime; mUpnpTime timeout; mUpnpTime expiredTime; timeout = mupnp_subscriber_gettimeout(sub); if (timeout == MUPNP_SUBSCRIPTION_INFINITE_VALUE) return false; //Theo Beisch - use clinkc function currTime = mupnp_getcurrentsystemtime(); //returns time in s expiredTime = mupnp_subscriber_getsubscriptiontime(sub) + (timeout); //tb: removed( *1000); if (expiredTime < currTime) return true; return false; mupnp_log_debug_l4("Leaving...\n"); }
void upnp_clock_device_update(mUpnpDevice *clockDev) { mUpnpTime currTime; mUpnpService *timeService; mUpnpStateVariable *timeState; char sysTimeStr[SYSTEM_TIME_BUF_LEN]; timeService = mupnp_device_getservicebyexacttype(clockDev, "urn:schemas-upnp-org:service:timer:1"); if (timeService == NULL) return; timeState = mupnp_service_getstatevariablebyname(timeService, "Time"); if (timeState == NULL) return; currTime = mupnp_getcurrentsystemtime(); GetSystemTimeString(currTime, sysTimeStr); mupnp_statevariable_setvalue(timeState, sysTimeStr); printf("%s\n", sysTimeStr); }
void mupnp_ssdp_packet_copy(mUpnpSSDPPacket *dstSsdpPkt, mUpnpSSDPPacket *srcSsdpPkt) { mUpnpHttpHeader *srcHeader; mUpnpHttpHeader *destHeader; mupnp_log_debug_l4("Entering...\n"); mupnp_socket_datagram_packet_copy(dstSsdpPkt->dgmPkt, srcSsdpPkt->dgmPkt); /**** copy headers ****/ mupnp_ssdp_packet_clear(dstSsdpPkt); for (srcHeader = mupnp_ssdp_packet_getheaders(srcSsdpPkt); srcHeader != NULL; srcHeader = mupnp_http_header_next(srcHeader)) { destHeader = mupnp_http_header_new(); mupnp_http_header_setname(destHeader, mupnp_http_header_getname(srcHeader)); mupnp_http_header_setvalue(destHeader, mupnp_http_header_getvalue(srcHeader)); mupnp_ssdp_packet_addheader(dstSsdpPkt, destHeader); } /* Set timestamp */ mupnp_ssdp_packet_settimestamp(dstSsdpPkt, mupnp_getcurrentsystemtime()); mupnp_log_debug_l4("Leaving...\n"); }
mUpnpSSDPPacket *mupnp_ssdp_packet_new() { mUpnpSSDPPacket *ssdpPkt; mupnp_log_debug_l4("Entering...\n"); ssdpPkt = (mUpnpSSDPPacket *)malloc(sizeof(mUpnpSSDPPacket)); if ( NULL != ssdpPkt ) { ssdpPkt->dgmPkt = mupnp_socket_datagram_packet_new(); ssdpPkt->headerList = mupnp_http_headerlist_new(); ssdpPkt->initialized = 0; mupnp_ssdp_packet_setuserdata(ssdpPkt, NULL); mupnp_ssdp_packet_settimestamp(ssdpPkt, mupnp_getcurrentsystemtime()); ssdpPkt->timestamps = (mUpnpTime *)malloc(MUPNP_SSDP_FILTER_TABLE_SIZE * sizeof(mUpnpTime)); } return ssdpPkt; mupnp_log_debug_l4("Leaving...\n"); }
void upnp_clock_device_httprequestrecieved(mUpnpHttpRequest *httpReq) { mUpnpTime currTime; mUpnpDevice *dev; char *uri; char content[2048]; char sysTimeStr[SYSTEM_TIME_BUF_LEN]; char serverName[MUPNP_SEVERNAME_MAXLEN]; mUpnpHttpResponse *httpRes; BOOL postRet; dev = (mUpnpDevice *)mupnp_http_request_getuserdata(httpReq); uri = mupnp_http_request_geturi(httpReq); if (strcmp(uri, "/presentation") != 0) { mupnp_device_httprequestrecieved(httpReq); return; } currTime = mupnp_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), mupnp_getservername(serverName, sizeof(serverName))); httpRes = mupnp_http_response_new(); mupnp_http_response_setstatuscode(httpRes, CG_HTTP_STATUS_OK); mupnp_http_response_setcontent(httpRes, content); mupnp_http_response_setcontenttype(httpRes, "text/html"); mupnp_http_response_setcontentlength(httpRes, strlen(content)); postRet = mupnp_http_request_postresponse(httpReq, httpRes); mupnp_http_response_delete(httpRes); }
static int filter_duplicate_m_search(mUpnpSSDPPacket *ssdpPkt) { mUpnpTime *timestamps = ssdpPkt->timestamps; size_t s_length; int loc; const char *st; char *id_string, *r_address, port[6]; mUpnpTime curr_time; mupnp_log_debug_l4("Entering...\n"); /* Initializing hash table to zero */ if (!ssdpPkt->initialized) { ssdpPkt->initialized = 1; memset(timestamps, '\0', MUPNP_SSDP_FILTER_TABLE_SIZE * sizeof( mUpnpTime )); } r_address = mupnp_string_getvalue(ssdpPkt->dgmPkt->remoteAddress); st = mupnp_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 ) { mupnp_log_debug_s("Memory allocation problem!\n"); return FALSE; } memset(id_string, '\0', s_length + 1); mupnp_strcat(id_string, r_address ); mupnp_strcat(id_string, port); mupnp_strcat(id_string, st ); loc = simple_string_hash(id_string, MUPNP_SSDP_FILTER_TABLE_SIZE); mupnp_log_debug("Calculated hash: %d\n", loc); free(id_string); curr_time = mupnp_getcurrentsystemtime(); if ( 0 == timestamps[loc] ) { timestamps[loc] = curr_time; mupnp_log_debug("First packet... Updating hash table.\n"); return FALSE; } else if ( ( curr_time - timestamps[loc] ) < MUPNP_DEVICE_M_SEARCH_FILTER_INTERVAL ) { mupnp_log_debug("Filtering packet!\n"); timestamps[loc] = curr_time; return TRUE; } else { timestamps[loc] = curr_time; mupnp_log_debug("Old timestamp found, just updating it.\n"); return FALSE; } mupnp_log_debug_l4("Leaving...\n"); }
BOOL mupnp_http_persistentconnection_put(char *host, int port, void *data) { mUpnpHttpPersistentConnection *new_node = NULL, *node = NULL; mupnp_log_debug_l4("Entering...\n"); /* If we dont have cache, then just exit */ if (cache == NULL) { mupnp_log_debug("(put) No cache! Persistent connections not initialized?\n"); return FALSE; } /* Check if we already have this one cached */ for (node = (mUpnpHttpPersistentConnection*)mupnp_list_gets((mUpnpList*)cache); node != NULL; node = (mUpnpHttpPersistentConnection*)mupnp_list_next((mUpnpList*)node)) { if (mupnp_strcmp(mupnp_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 = mupnp_getcurrentsystemtime(); return TRUE; } mupnp_log_debug_s("Found cached persistent connection for %s:%d\n", mupnp_string_getvalue(node->host), node->port); new_node = node; mupnp_list_remove((mUpnpList*)new_node); break; } } /* We didn't find it */ if (new_node == NULL) { /* Check if we have already too many cached things */ if (mupnp_list_size((mUpnpList*)cache) >= CG_HTTP_PERSISTENT_CACHE_SIZE) { /* Take last node (not refreshed for a long time) */ new_node = (mUpnpHttpPersistentConnection *)mupnp_list_next((mUpnpList *)cache); mupnp_list_remove((mUpnpList*)new_node); mupnp_http_persistentconnection_delete(new_node); new_node = NULL; mupnp_log_debug_s("Max persistent HTTP connection cache reached.\n"); } if (new_node == NULL) { if (data == NULL) return TRUE; new_node = mupnp_http_persistentconnection_new(); if (new_node == NULL) return FALSE; mupnp_log_debug_s("Adding persistent HTTP Connection %s:%d to cache\n", host, port); mupnp_log_debug_s("Persistent connections: %d\n", mupnp_list_size((mUpnpList*)cache)); } } if (data != NULL) { /* Set appropriate values for the node */ mupnp_string_setvalue(new_node->host, host); new_node->port = port; new_node->cacheData = data; new_node->timestamp = mupnp_getcurrentsystemtime(); mupnp_list_add((mUpnpList*)cache, (mUpnpList*)new_node); } else { /* remove and delete node */ mupnp_http_persistentconnection_delete(new_node); } return TRUE; mupnp_log_debug_l4("Leaving...\n"); }
void *mupnp_http_persistentconnection_get(char *host, int port) { mUpnpHttpPersistentConnection *node; mUpnpTime sys_time = mupnp_getcurrentsystemtime(); BOOL iterate; mupnp_log_debug_l4("Entering...\n"); /* If we dont have cache, then just exit */ if (cache == NULL) { mupnp_log_debug("(get) No cache! Persistent connections not initialized?\n"); return NULL; } /* Clear all expired nodes */ do { iterate = FALSE; for (node = (mUpnpHttpPersistentConnection*)mupnp_list_gets((mUpnpList*)cache); node != NULL; node = (mUpnpHttpPersistentConnection*)mupnp_list_next((mUpnpList*)node)) { if (sys_time > node->timestamp + CG_HTTP_PERSISTENT_TIMEOUT_PERIOD) { mupnp_log_debug_s("Timeout for persistent HTTP Connection to %s:%d " "(timestamp: %d)\n", mupnp_string_getvalue(node->host), node->port, node->timestamp); mupnp_list_remove((mUpnpList*)node); mupnp_http_persistentconnection_delete(node); iterate = TRUE; break; } } } while (iterate); /* Get persistent node */ for (node = (mUpnpHttpPersistentConnection*)mupnp_list_gets((mUpnpList*)cache); node != NULL; node = (mUpnpHttpPersistentConnection*)mupnp_list_next((mUpnpList*)node)) { if (mupnp_strcmp(mupnp_string_getvalue(node->host), host) == 0 && node->port == port) { /* Node was required, remove and add again to refresh cache */ mupnp_list_remove((mUpnpList*)node); mupnp_list_add((mUpnpList*)cache, (mUpnpList*)node); node->timestamp = mupnp_getcurrentsystemtime(); mupnp_log_debug_s("Persistent HTTP Connection cache HIT for %s:%d\n", host, port); return node->cacheData; } } mupnp_log_debug_s("Persistent HTTP Connection cache MISS for %s:%d\n", host, port); return NULL; mupnp_log_debug_l4("Leaving...\n"); }