[Freeswitch-svn] [commit] r7165 - in freeswitch/trunk/src: . include include/private mod/applications/mod_commands mod/applications/mod_dptools mod/applications/mod_enum mod/applications/mod_esf mod/applications/mod_rss mod/applications/mod_voicemail mod/endpoints/mod_iax mod/endpoints/mod_sofia mod/event_handlers/mod_cdr_csv mod/event_handlers/mod_event_socket mod/languages/mod_spidermonkey mod/loggers/mod_logfile mod/say/mod_say_de mod/say/mod_say_en mod/say/mod_say_es mod/say/mod_say_fr mod/say/mod_say_it mod/say/mod_say_nl

Freeswitch SVN anthm at freeswitch.org
Thu Jan 10 19:43:49 EST 2008


Author: anthm
Date: Thu Jan 10 19:43:49 2008
New Revision: 7165

Modified:
   freeswitch/trunk/src/include/private/switch_core_pvt.h
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/include/switch_types.h
   freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
   freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c
   freeswitch/trunk/src/mod/applications/mod_enum/mod_enum.c
   freeswitch/trunk/src/mod/applications/mod_esf/mod_esf.c
   freeswitch/trunk/src/mod/applications/mod_rss/mod_rss.c
   freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c
   freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c
   freeswitch/trunk/src/mod/event_handlers/mod_cdr_csv/mod_cdr_csv.c
   freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
   freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
   freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c
   freeswitch/trunk/src/mod/say/mod_say_de/mod_say_de.c
   freeswitch/trunk/src/mod/say/mod_say_en/mod_say_en.c
   freeswitch/trunk/src/mod/say/mod_say_es/mod_say_es.c
   freeswitch/trunk/src/mod/say/mod_say_fr/mod_say_fr.c
   freeswitch/trunk/src/mod/say/mod_say_it/mod_say_it.c
   freeswitch/trunk/src/mod/say/mod_say_nl/mod_say_nl.c
   freeswitch/trunk/src/switch_channel.c
   freeswitch/trunk/src/switch_core.c
   freeswitch/trunk/src/switch_core_io.c
   freeswitch/trunk/src/switch_core_port_allocator.c
   freeswitch/trunk/src/switch_ivr.c
   freeswitch/trunk/src/switch_ivr_async.c
   freeswitch/trunk/src/switch_ivr_originate.c
   freeswitch/trunk/src/switch_ivr_play_say.c
   freeswitch/trunk/src/switch_rtp.c
   freeswitch/trunk/src/switch_scheduler.c
   freeswitch/trunk/src/switch_stun.c
   freeswitch/trunk/src/switch_time.c
   freeswitch/trunk/src/switch_utils.c

Log:
try to use clock_gettime to make sure we don't care if the system time changes

Modified: freeswitch/trunk/src/include/private/switch_core_pvt.h
==============================================================================
--- freeswitch/trunk/src/include/private/switch_core_pvt.h	(original)
+++ freeswitch/trunk/src/include/private/switch_core_pvt.h	Thu Jan 10 19:43:49 2008
@@ -158,6 +158,8 @@
 
 struct switch_runtime {
 	switch_time_t initiated;
+	switch_time_t reference;
+	int64_t offset;
 	switch_hash_t *global_vars;
 	switch_hash_t *mime_types;
 	switch_memory_pool_t *memory_pool;

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Thu Jan 10 19:43:49 2008
@@ -1536,6 +1536,8 @@
 SWITCH_DECLARE(void) switch_core_memory_reclaim_logger(void);
 SWITCH_DECLARE(void) switch_core_memory_reclaim_all(void);
 SWITCH_DECLARE(void) switch_core_setrlimits(void);
+SWITCH_DECLARE(void) switch_time_sync(void);
+SWITCH_DECLARE(time_t) switch_timestamp(time_t *t);
 
 ///\}
 

Modified: freeswitch/trunk/src/include/switch_types.h
==============================================================================
--- freeswitch/trunk/src/include/switch_types.h	(original)
+++ freeswitch/trunk/src/include/switch_types.h	Thu Jan 10 19:43:49 2008
@@ -1065,7 +1065,8 @@
 	SCSC_SPS,
 	SCSC_LAST_SPS,
 	SCSC_RECLAIM,
-	SCSC_MAX_SESSIONS
+	SCSC_MAX_SESSIONS,
+	SCSC_SYNC_CLOCK
 } switch_session_ctl_t;
 
 typedef struct apr_pool_t switch_memory_pool_t;

Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	Thu Jan 10 19:43:49 2008
@@ -600,11 +600,16 @@
 			}
 			switch_core_session_ctl(SCSC_SPS, &arg);
 			stream->write_function(stream, "+OK sessions per second: %d\n", arg);
+		} else if (!strcasecmp(argv[0], "sync_clock")) {
+			arg = 0;
+			switch_core_session_ctl(SCSC_SYNC_CLOCK, &arg);
+			stream->write_function(stream, "+OK clock syncronized\n");
 		} else {
 			stream->write_function(stream, "-ERR INVALID COMMAND\nUSAGE: fsctl %s", CTL_SYNTAX);
 			goto end;
-		}
-
+		} 
+		
+		
 		stream->write_function(stream, "+OK\n");
 	  end:
 		free(mydata);
@@ -830,13 +835,13 @@
 		uint32_t mto;
 		if (*argv[4] == '+') {
 			if ((mto = atoi(argv[4]+1)) > 0) {
-				to = time(NULL) + mto;
+				to = switch_timestamp(NULL) + mto;
 			} else {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID Timeout!\n");
 				goto done;
 			}
 		} else {
-			if ((to = atoi(argv[4])) < time(NULL)) {
+			if ((to = atoi(argv[4])) < switch_timestamp(NULL)) {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID Timeout!\n");
 				to = 0;
 				goto done;
@@ -930,7 +935,7 @@
 		time_t when;
 
 		if (*argv[0] == '+') {
-			when = time(NULL) + atol(argv[0] + 1);
+			when = switch_timestamp(NULL) + atol(argv[0] + 1);
 		} else {
 			when = atol(argv[0]);
 		}
@@ -972,7 +977,7 @@
 		switch_call_cause_t cause = SWITCH_CAUSE_ALLOTTED_TIMEOUT;
 
 		if (*argv[0] == '+') {
-			when = time(NULL) + atol(argv[0] + 1);
+			when = switch_timestamp(NULL) + atol(argv[0] + 1);
 		} else {
 			when = atol(argv[0]);
 		}
@@ -1091,7 +1096,7 @@
 		time_t when;
 
 		if (*argv[0] == '+') {
-			when = time(NULL) + atol(argv[0] + 1);
+			when = switch_timestamp(NULL) + atol(argv[0] + 1);
 		} else {
 			when = atol(argv[0]);
 		}
@@ -1603,7 +1608,7 @@
 			*dcmd++ = '\0';
 
 			if (*tm == '+') {
-				when = time(NULL) + atol(tm + 1);
+				when = switch_timestamp(NULL) + atol(tm + 1);
 			} else {
 				when = atol(tm);
 			}

Modified: freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c	Thu Jan 10 19:43:49 2008
@@ -220,7 +220,7 @@
 			time_t when;
 
 			if (*argv[0] == '+') {
-				when = time(NULL) + atol(argv[0] + 1);
+				when = switch_timestamp(NULL) + atol(argv[0] + 1);
 			} else {
 				when = atol(argv[0]);
 			}
@@ -245,7 +245,7 @@
 			switch_bool_t bleg = SWITCH_FALSE;
 
 			if (*argv[0] == '+') {
-				when = time(NULL) + atol(argv[0] + 1);
+				when = switch_timestamp(NULL) + atol(argv[0] + 1);
 			} else {
 				when = atol(argv[0]);
 			}
@@ -277,7 +277,7 @@
 			switch_media_flag_t flags = SMF_NONE;
 
 			if (*argv[0] == '+') {
-				when = time(NULL) + atol(argv[0] + 1);
+				when = switch_timestamp(NULL) + atol(argv[0] + 1);
 			} else {
 				when = atol(argv[0]);
 			}
@@ -749,7 +749,7 @@
 		channel = switch_core_session_get_channel(session);
 		assert(channel != NULL);
 
-		switch_time_exp_lt(&tm, switch_time_now());
+		switch_time_exp_lt(&tm, switch_timestamp_now());
 		switch_strftime(date, &retsize, sizeof(date), argv[1], &tm);
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SET [%s]=[%s]\n", argv[0], date);
 		switch_channel_set_variable(channel, argv[0], date);
@@ -761,7 +761,7 @@
 	switch_time_t out;
 
 	if (switch_strlen_zero(cmd)) {
-		out = switch_time_now();
+		out = switch_timestamp_now();
 	} else {
 		out = switch_str_time(cmd);
 	}
@@ -782,7 +782,7 @@
 		thetime = switch_time_make(atoi(cmd),0);
 		cmd = p+1;
 	} else {
-		thetime = switch_time_now();
+		thetime = switch_timestamp_now();
 	}
 	switch_time_exp_lt(&tm, thetime);
 	switch_strftime(date, &retsize, sizeof(date), switch_strlen_zero(cmd) ? "%Y-%m-%d %T" : cmd, &tm);
@@ -969,12 +969,12 @@
 		uint32_t mto;
 		if (*argv[3] == '+') {
 			if ((mto = atol(argv[3]+1)) > 0) {
-				to = time(NULL) + mto;
+				to = switch_timestamp(NULL) + mto;
 			} else {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID Timeout!\n");
 			}
 		} else {
-			if ((to = atol(argv[3])) < time(NULL)) {
+			if ((to = atol(argv[3])) < switch_timestamp(NULL)) {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID Timeout!\n");
 				to = 0;
 			}

Modified: freeswitch/trunk/src/mod/applications/mod_enum/mod_enum.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_enum/mod_enum.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_enum/mod_enum.c	Thu Jan 10 19:43:49 2008
@@ -510,7 +510,7 @@
 		tv.tv_sec = i;
 		tv.tv_usec = 0;
 		i = select((int) (fd + 1), &fds, 0, 0, &tv);
-		now = time(NULL);
+		now = switch_timestamp(NULL);
 		if (i > 0) {
 			dns_ioevent(nctx, now);
 		}

Modified: freeswitch/trunk/src/mod/applications/mod_esf/mod_esf.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_esf/mod_esf.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_esf/mod_esf.c	Thu Jan 10 19:43:49 2008
@@ -182,7 +182,7 @@
 		}
 	}
 
-	control_packet.unique_id = htonl((u_long)time(NULL));
+	control_packet.unique_id = htonl((u_long)switch_timestamp(NULL));
 	control_packet.command = htonl(LS_START_BCAST);
 	control_packet.ip = inet_addr(mcast_ip);
 	control_packet.port = htonl(mcast_port);
@@ -211,7 +211,7 @@
 
 
 
-	control_packet.unique_id = htonl((u_long)time(NULL));
+	control_packet.unique_id = htonl((u_long)switch_timestamp(NULL));
 	control_packet.command = htonl(LS_STOP_BCAST);
 	bytes = 8;
 	switch_socket_sendto(socket, control_packet_addr, 0, (void *)&control_packet, &bytes);

Modified: freeswitch/trunk/src/mod/applications/mod_rss/mod_rss.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_rss/mod_rss.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_rss/mod_rss.c	Thu Jan 10 19:43:49 2008
@@ -484,7 +484,7 @@
 			switch_size_t retsize;
 			char dtmf[5] = "";
 
-			switch_time_exp_lt(&tm, switch_time_now());
+			switch_time_exp_lt(&tm, switch_timestamp_now());
 			switch_strftime(date, &retsize, sizeof(date), "%I:%M %p", &tm);
 
 

Modified: freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c	Thu Jan 10 19:43:49 2008
@@ -1380,7 +1380,7 @@
 			}
 		}
 		switch_snprintf(sql, sizeof(sql), "update voicemail_data set read_epoch=%ld where user='%s' and domain='%s' and flags='save'", 
-			(long)time(NULL), myid, domain_name);
+			(long)switch_timestamp(NULL), myid, domain_name);
 		vm_execute_sql(profile, sql, profile->mutex);
 		switch_snprintf(sql, sizeof(sql), "select file_path from voicemail_data where user='%s' and domain='%s' and flags='delete'", myid, domain_name);
 		vm_execute_sql_callback(profile, profile->mutex, sql, unlink_callback, NULL);
@@ -1891,7 +1891,7 @@
 		int total_new_urgent_messages = 0;
 		int total_saved_urgent_messages = 0;
 
-		usql = switch_mprintf("insert into voicemail_data values(%ld,0,'%q','%q','%q','%q','%q','%q','%q','%u','','%q')", (long)time(NULL),
+		usql = switch_mprintf("insert into voicemail_data values(%ld,0,'%q','%q','%q','%q','%q','%q','%q','%u','','%q')", (long)switch_timestamp(NULL),
 			id, domain_name, uuid, caller_profile->caller_id_name, caller_profile->caller_id_number, 
 			myfolder, file_path, message_len, read_flags);
 		vm_execute_sql(profile, usql, profile->mutex);
@@ -1936,7 +1936,7 @@
 		message_count(profile, id, domain_name, myfolder, &total_new_messages, &total_saved_messages,
 			&total_new_urgent_messages, &total_saved_urgent_messages);
 
-		switch_time_exp_lt(&tm, switch_time_now());
+		switch_time_exp_lt(&tm, switch_timestamp_now());
 		switch_strftime(date, &retsize, sizeof(date), profile->date_fmt, &tm);
 
 		switch_channel_set_variable(channel, "voicemail_current_folder", myfolder);
@@ -2255,7 +2255,7 @@
 	struct holder holder;
 
 	sql = switch_mprintf("update voicemail_data set read_epoch=%ld where user='%s' and domain='%s' and file_path like '%%%s'", 
-		(long)time(NULL), user, domain, file);
+		(long)switch_timestamp(NULL), user, domain, file);
 
 	vm_execute_sql(profile, sql, profile->mutex);
 	free(sql);

Modified: freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c	Thu Jan 10 19:43:49 2008
@@ -637,7 +637,7 @@
 {
 	switch_channel_t *channel = NULL;
 	private_t *tech_pvt = NULL;
-	switch_time_t started = switch_time_now();
+	switch_time_t started = switch_timestamp_now();
 	unsigned int elapsed;
 	switch_byte_t *data;
 
@@ -680,7 +680,7 @@
 		}
 
 		if (timeout > -1) {
-			elapsed = (unsigned int) ((switch_time_now() - started) / 1000);
+			elapsed = (unsigned int) ((switch_timestamp_now() - started) / 1000);
 			if (elapsed >= (unsigned int) timeout) {
 				return SWITCH_STATUS_SUCCESS;
 			}
@@ -724,7 +724,6 @@
 		switch_swap_linear(frame->data, (int) frame->datalen / 2);
 	}
 #endif
-	//printf("Send %ld %d\n", time(NULL), (int) frame->datalen);
 	iax_send_voice(tech_pvt->iax_session, tech_pvt->codec, frame->data, (int) frame->datalen, tech_pvt->write_codec.implementation->samples_per_frame);
 
 	return SWITCH_STATUS_SUCCESS;

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	Thu Jan 10 19:43:49 2008
@@ -1242,7 +1242,7 @@
 					switch_assert(gp->state < REG_STATE_LAST);
 					stream->write_function(stream, "%25s\t%s\t  %32s\t%s", gp->name, "gateway", gp->register_to, sofia_state_names[gp->state]);
 					if (gp->state == REG_STATE_FAILED || gp->state == REG_STATE_TRYING) {
-						stream->write_function(stream, " (retry: %ds)", gp->retry - time(NULL));
+						stream->write_function(stream, " (retry: %ds)", gp->retry - switch_timestamp(NULL));
 					}
 					stream->write_function(stream, "\n");
 				}
@@ -1348,7 +1348,7 @@
 
 	if (!strcasecmp(argv[1], "stop") || !strcasecmp(argv[1], "restart")) {
 		int rsec = 3;
-		int diff = (int) (time(NULL) - profile->started);
+		int diff = (int) (switch_timestamp(NULL) - profile->started);
 		int remain = rsec - diff;
 		if (diff < rsec) {
 			stream->write_function(stream, "Profile %s must be up for at least %d seconds to stop/restart.\nPlease wait %d second%s\n", 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	Thu Jan 10 19:43:49 2008
@@ -345,7 +345,7 @@
 		char *rpid = switch_event_get_header(event, "orig-rpid");
 		char *call_id = switch_event_get_header(event, "orig-call-id");
 		char *user_agent = switch_event_get_header(event, "user-agent");
-		long expires = (long) time(NULL) + atol(exp_str);
+		long expires = (long) switch_timestamp(NULL) + atol(exp_str);
 		char *profile_name = switch_event_get_header(event, "orig-profile-name");
 		sofia_profile_t *profile = NULL;
 
@@ -512,19 +512,19 @@
 
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting thread for %s\n", profile->name);
 
-	profile->started = time(NULL);
+	profile->started = switch_timestamp(NULL);
 	switch_yield(1000000);
 
 	sofia_set_pflag_locked(profile, PFLAG_RUNNING);
 
 	while (mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) {
 		if (++ireg_loops >= IREG_SECONDS) {
-			sofia_reg_check_expire(profile, time(NULL));
+			sofia_reg_check_expire(profile, switch_timestamp(NULL));
 			ireg_loops = 0;
 		}
 
 		if (++gateway_loops >= GATEWAY_SECONDS) {
-			sofia_reg_check_gateway(profile, time(NULL));
+			sofia_reg_check_gateway(profile, switch_timestamp(NULL));
 			gateway_loops = 0;
 		}
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	Thu Jan 10 19:43:49 2008
@@ -72,7 +72,7 @@
 	}
 
 	if (!tech_pvt->owner_id) {
-		tech_pvt->owner_id = (uint32_t) time(NULL) - port;
+		tech_pvt->owner_id = (uint32_t) switch_timestamp(NULL) - port;
 	}
 
 	if (!tech_pvt->session_id) {
@@ -1468,7 +1468,8 @@
 					}
 				
 					if (match) {
-						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Our existing codec is still good, let's keep it\n");
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Our existing codec [%s] is still good, let's keep it\n", 
+										  tech_pvt->rm_encoding);
 						goto end;
 					}
 				}

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	Thu Jan 10 19:43:49 2008
@@ -608,13 +608,13 @@
 	char *prpid;
 	int done = 0;
 	const char *ct;
-	time_t exptime = time(NULL) + 3600; 
+	time_t exptime = switch_timestamp(NULL) + 3600; 
 	char exp[80] = "";   
 	
 	if (expires) { 
 		long tmp = atol(expires); 
 		if (tmp > 0) { 
-			exptime = tmp - time(NULL); 
+			exptime = tmp - switch_timestamp(NULL); 
 		}
 	}
 
@@ -855,7 +855,7 @@
 	}
 
 	id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
-	expire_sec = (int)(expire_sec - time(NULL));
+	expire_sec = (int)(expire_sec - switch_timestamp(NULL));
 	if (expire_sec < 0) {
 		expire_sec = 3600;
 	}
@@ -1047,7 +1047,7 @@
 		full_via = sip_header_as_string(profile->home, (void *) sip->sip_via);
 
 		exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
-		exp = (long) time(NULL) + exp_raw;
+		exp = (long) switch_timestamp(NULL) + exp_raw;
 
 		switch_mutex_lock(profile->ireg_mutex);
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	Thu Jan 10 19:43:49 2008
@@ -302,7 +302,7 @@
 
 	switch_mutex_lock(profile->ireg_mutex);
 	sql = switch_mprintf("insert into sip_authentication (nonce, expires) values('%q', %ld)",
-						 uuid_str, time(NULL) + profile->nonce_ttl);
+						 uuid_str, switch_timestamp(NULL) + profile->nonce_ttl);
 	switch_assert(sql != NULL);
 	sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
 	switch_safe_free(sql);
@@ -495,7 +495,7 @@
 		
 		sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','%q', '%q', %ld, '%q')", call_id,
 							 to_user, to_host, contact_str, cd ? "Registered(NATHACK)" : "Registered", 
-							 rpid, (long) time(NULL) + (long) exptime * 2, agent);
+							 rpid, (long) switch_timestamp(NULL) + (long) exptime * 2, agent);
 
 		
 		if (sql) {

Modified: freeswitch/trunk/src/mod/event_handlers/mod_cdr_csv/mod_cdr_csv.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_cdr_csv/mod_cdr_csv.c	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_cdr_csv/mod_cdr_csv.c	Thu Jan 10 19:43:49 2008
@@ -87,7 +87,7 @@
 	close(fd->fd);
 
 	if (globals.rotate) {
-		switch_time_exp_lt(&tm, switch_time_now());
+		switch_time_exp_lt(&tm, switch_timestamp_now());
 		switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);
 	
 		len = strlen(fd->path) + strlen(date) + 2;

Modified: freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c	Thu Jan 10 19:43:49 2008
@@ -354,7 +354,7 @@
 	uint32_t max_len = sizeof(mbuf);
 	switch_channel_t *channel = NULL;
 	*event = NULL;
-	start = time(NULL);
+	start = switch_timestamp(NULL);
 	ptr = mbuf;
 
 
@@ -436,7 +436,7 @@
 		}
 
 		if (timeout) {
-			elapsed = (uint32_t) (time(NULL) - start);
+			elapsed = (uint32_t) (switch_timestamp(NULL) - start);
 			if (elapsed >= timeout) {
 				switch_clear_flag_locked(listener, LFLAG_RUNNING);
 				return SWITCH_STATUS_FALSE;

Modified: freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c	(original)
+++ freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c	Thu Jan 10 19:43:49 2008
@@ -2077,14 +2077,14 @@
 	channel = switch_core_session_get_channel(jss->session);
 	switch_assert(channel != NULL);
 
-	started = switch_time_now();
+	started = switch_timestamp_now();
 
 	if (argc > 0) {
 		JS_ValueToInt32(cx, argv[0], &timeout);
 	}
 
 	for (;;) {
-		if (((elapsed = (unsigned int) ((switch_time_now() - started) / 1000)) > (switch_time_t) timeout)
+		if (((elapsed = (unsigned int) ((switch_timestamp_now() - started) / 1000)) > (switch_time_t) timeout)
 			|| switch_channel_get_state(channel) >= CS_HANGUP) {
 			*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
 			break;
@@ -2116,14 +2116,14 @@
 	channel = switch_core_session_get_channel(jss->session);
 	switch_assert(channel != NULL);
 
-	started = switch_time_now();
+	started = switch_timestamp_now();
 
 	if (argc > 0) {
 		JS_ValueToInt32(cx, argv[0], &timeout);
 	}
 
 	for (;;) {
-		if (((elapsed = (unsigned int) ((switch_time_now() - started) / 1000)) > (switch_time_t) timeout)
+		if (((elapsed = (unsigned int) ((switch_timestamp_now() - started) / 1000)) > (switch_time_t) timeout)
 			|| switch_channel_get_state(channel) >= CS_HANGUP) {
 			*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
 			break;

Modified: freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c
==============================================================================
--- freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c	(original)
+++ freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c	Thu Jan 10 19:43:49 2008
@@ -123,7 +123,7 @@
 
 	switch_mutex_lock(globals.mutex);
 
-	switch_time_exp_lt(&tm, switch_time_now());
+	switch_time_exp_lt(&tm, switch_timestamp_now());
 	switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);
 
 	profile->log_size = 0;

Modified: freeswitch/trunk/src/mod/say/mod_say_de/mod_say_de.c
==============================================================================
--- freeswitch/trunk/src/mod/say/mod_say_de/mod_say_de.c	(original)
+++ freeswitch/trunk/src/mod/say/mod_say_de/mod_say_de.c	Thu Jan 10 19:43:49 2008
@@ -303,7 +303,7 @@
 			}
 		} else {
 			if ((seconds = atoi(tosay)) <= 0) {
-				seconds = (int64_t) time(NULL);
+				seconds = (int64_t) switch_timestamp(NULL);
 			}
 
 			if (seconds >= 60) {

Modified: freeswitch/trunk/src/mod/say/mod_say_en/mod_say_en.c
==============================================================================
--- freeswitch/trunk/src/mod/say/mod_say_en/mod_say_en.c	(original)
+++ freeswitch/trunk/src/mod/say/mod_say_en/mod_say_en.c	Thu Jan 10 19:43:49 2008
@@ -303,7 +303,7 @@
 			}
 		} else {
 			if ((seconds = atoi(tosay)) <= 0) {
-				seconds = (int64_t) time(NULL);
+				seconds = (int64_t) switch_timestamp(NULL);
 			}
 
 			if (seconds >= 60) {

Modified: freeswitch/trunk/src/mod/say/mod_say_es/mod_say_es.c
==============================================================================
--- freeswitch/trunk/src/mod/say/mod_say_es/mod_say_es.c	(original)
+++ freeswitch/trunk/src/mod/say/mod_say_es/mod_say_es.c	Thu Jan 10 19:43:49 2008
@@ -303,7 +303,7 @@
 			}
 		} else {
 			if ((seconds = atoi(tosay)) <= 0) {
-				seconds = (int64_t) time(NULL);
+				seconds = (int64_t) switch_timestamp(NULL);
 			}
 
 			if (seconds >= 60) {

Modified: freeswitch/trunk/src/mod/say/mod_say_fr/mod_say_fr.c
==============================================================================
--- freeswitch/trunk/src/mod/say/mod_say_fr/mod_say_fr.c	(original)
+++ freeswitch/trunk/src/mod/say/mod_say_fr/mod_say_fr.c	Thu Jan 10 19:43:49 2008
@@ -303,7 +303,7 @@
 			}
 		} else {
 			if ((seconds = atoi(tosay)) <= 0) {
-				seconds = (int64_t) time(NULL);
+				seconds = (int64_t) switch_timestamp(NULL);
 			}
 
 			if (seconds >= 60) {

Modified: freeswitch/trunk/src/mod/say/mod_say_it/mod_say_it.c
==============================================================================
--- freeswitch/trunk/src/mod/say/mod_say_it/mod_say_it.c	(original)
+++ freeswitch/trunk/src/mod/say/mod_say_it/mod_say_it.c	Thu Jan 10 19:43:49 2008
@@ -303,7 +303,7 @@
 			}
 		} else {
 			if ((seconds = atoi(tosay)) <= 0) {
-				seconds = (int64_t) time(NULL);
+				seconds = (int64_t) switch_timestamp(NULL);
 			}
 
 			if (seconds >= 60) {

Modified: freeswitch/trunk/src/mod/say/mod_say_nl/mod_say_nl.c
==============================================================================
--- freeswitch/trunk/src/mod/say/mod_say_nl/mod_say_nl.c	(original)
+++ freeswitch/trunk/src/mod/say/mod_say_nl/mod_say_nl.c	Thu Jan 10 19:43:49 2008
@@ -303,7 +303,7 @@
 			}
 		} else {
 			if ((seconds = atoi(tosay)) <= 0) {
-				seconds = (int64_t) time(NULL);
+				seconds = (int64_t) switch_timestamp(NULL);
 			}
 
 			if (seconds >= 60) {

Modified: freeswitch/trunk/src/switch_channel.c
==============================================================================
--- freeswitch/trunk/src/switch_channel.c	(original)
+++ freeswitch/trunk/src/switch_channel.c	Thu Jan 10 19:43:49 2008
@@ -1048,7 +1048,7 @@
 	}
 
 	caller_profile->times = (switch_channel_timetable_t *) switch_core_session_alloc(channel->session, sizeof(*caller_profile->times));
-	caller_profile->times->profile_created = switch_time_now();
+	caller_profile->times->profile_created = switch_timestamp_now();
 
 	if (channel->caller_profile && channel->caller_profile->times) {
 		channel->caller_profile->times->transferred = caller_profile->times->profile_created;
@@ -1056,7 +1056,7 @@
 		caller_profile->times->created = channel->caller_profile->times->created;
 		caller_profile->times->hungup = channel->caller_profile->times->hungup;
 	} else {
-		caller_profile->times->created = switch_time_now();
+		caller_profile->times->created = switch_timestamp_now();
 	}
 
 	caller_profile->next = channel->caller_profile;
@@ -1246,7 +1246,7 @@
 
 	if (channel->caller_profile && channel->caller_profile->times && !channel->caller_profile->times->hungup) {
 		switch_mutex_lock(channel->profile_mutex);
-		channel->caller_profile->times->hungup = switch_time_now();
+		channel->caller_profile->times->hungup = switch_timestamp_now();
 		switch_mutex_unlock(channel->profile_mutex);
 	}
 
@@ -1403,7 +1403,7 @@
 
 	if (channel->caller_profile && channel->caller_profile->times) {
 		switch_mutex_lock(channel->profile_mutex);
-		channel->caller_profile->times->answered = switch_time_now();
+		channel->caller_profile->times->answered = switch_timestamp_now();
 		switch_mutex_unlock(channel->profile_mutex);
 	}
 

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Thu Jan 10 19:43:49 2008
@@ -80,13 +80,9 @@
 	send_heartbeat();
 
 	/* reschedule this task */
-	task->runtime = time(NULL) + 20;
+	task->runtime = switch_timestamp(NULL) + 20;
 }
 
-SWITCH_DECLARE(switch_time_t) switch_timestamp_now(void)
-{
-	return runtime.timestamp ? runtime.timestamp : switch_time_now();
-}
 
 SWITCH_DECLARE(switch_status_t) switch_core_set_console(const char *console)
 {
@@ -832,7 +828,7 @@
 	switch_scheduler_task_thread_start();
 	runtime.initiated = switch_time_now();
 
-	switch_scheduler_add_task(time(NULL), heartbeat_callback, "heartbeat", "core", 0, NULL, SSHF_NONE | SSHF_NO_DEL);
+	switch_scheduler_add_task(switch_timestamp(NULL), heartbeat_callback, "heartbeat", "core", 0, NULL, SSHF_NONE | SSHF_NO_DEL);
 
 
 	switch_uuid_get(&uuid);
@@ -966,7 +962,7 @@
 
 SWITCH_DECLARE(switch_time_t) switch_core_uptime(void)
 {
-	return switch_time_now() - runtime.initiated;
+	return switch_timestamp_now() - runtime.initiated;
 }
 
 SWITCH_DECLARE(int32_t) switch_core_session_ctl(switch_session_ctl_t cmd, int32_t * val)
@@ -976,6 +972,10 @@
 	}
 
 	switch (cmd) {
+	case SCSC_SYNC_CLOCK:
+		switch_time_sync();
+		*val = 0;
+		break;
 	case SCSC_PAUSE_INBOUND:
 		if (*val) {
 			switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);

Modified: freeswitch/trunk/src/switch_core_io.c
==============================================================================
--- freeswitch/trunk/src/switch_core_io.c	(original)
+++ freeswitch/trunk/src/switch_core_io.c	Thu Jan 10 19:43:49 2008
@@ -270,7 +270,7 @@
 					switch_mutex_lock(bp->read_mutex);
 					switch_buffer_write(bp->raw_read_buffer, read_frame->data, read_frame->datalen);
 					if (bp->callback) {
-						if (bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ) == SWITCH_FALSE || (bp->stop_time && bp->stop_time <= time(NULL))) {
+						if (bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ) == SWITCH_FALSE || (bp->stop_time && bp->stop_time <= switch_timestamp(NULL))) {
 							ok = SWITCH_FALSE;
 						}
 					}
@@ -404,7 +404,7 @@
 				if (bp->ready && switch_test_flag(bp, SMBF_READ_PING)) {
 					switch_mutex_lock(bp->read_mutex);
 					if (bp->callback) {
-						if (bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ_PING) == SWITCH_FALSE || (bp->stop_time && bp->stop_time <= time(NULL))) {
+						if (bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ_PING) == SWITCH_FALSE || (bp->stop_time && bp->stop_time <= switch_timestamp(NULL))) {
 							ok = SWITCH_FALSE;
 						}
 					}
@@ -616,7 +616,7 @@
 					}
 				}
 
-				if (bp->stop_time && bp->stop_time <= time(NULL)) {
+				if (bp->stop_time && bp->stop_time <= switch_timestamp(NULL)) {
 					ok = SWITCH_FALSE;
 				}
 

Modified: freeswitch/trunk/src/switch_core_port_allocator.c
==============================================================================
--- freeswitch/trunk/src/switch_core_port_allocator.c	(original)
+++ freeswitch/trunk/src/switch_core_port_allocator.c	Thu Jan 10 19:43:49 2008
@@ -118,7 +118,7 @@
 	int odd = switch_test_flag(alloc, SPF_ODD);
 
 	switch_mutex_lock(alloc->mutex);
-	srand(getpid() + (unsigned)time(NULL));
+	srand(getpid() + (unsigned)switch_timestamp(NULL));
 	
 	while(alloc->track_used < alloc->track_len) {
 		double r;

Modified: freeswitch/trunk/src/switch_ivr.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr.c	(original)
+++ freeswitch/trunk/src/switch_ivr.c	Thu Jan 10 19:43:49 2008
@@ -40,17 +40,17 @@
 {
 	switch_channel_t *channel;
 	switch_status_t status = SWITCH_STATUS_SUCCESS;
-	switch_time_t start, now, done = switch_time_now() + (ms * 1000);
+	switch_time_t start, now, done = switch_timestamp_now() + (ms * 1000);
 	switch_frame_t *read_frame;
 	int32_t left, elapsed;
 
 	channel = switch_core_session_get_channel(session);
 	switch_assert(channel != NULL);
 
-	start = switch_time_now();
+	start = switch_timestamp_now();
 
 	for (;;) {
-		now = switch_time_now();
+		now = switch_timestamp_now();
 		elapsed = (int32_t) ((now - start) / 1000);
 		left = ms - elapsed;
 
@@ -548,7 +548,7 @@
 	}
 
 	if (timeout) {
-		started = switch_time_now();
+		started = switch_timestamp_now();
 	}
 
 	while (switch_channel_ready(channel)) {
@@ -557,7 +557,7 @@
 		switch_dtmf_t dtmf = {0};
 
 		if (timeout) {
-			elapsed = (uint32_t) ((switch_time_now() - started) / 1000);
+			elapsed = (uint32_t) ((switch_timestamp_now() - started) / 1000);
 			if (elapsed >= timeout) {
 				break;
 			}
@@ -1191,12 +1191,12 @@
 					stream->digits = tmp;
 					*(stream->digits + (len++)) = digit;
 					*(stream->digits + len) = '\0';
-					stream->last_digit_time = switch_time_now() / 1000;
+					stream->last_digit_time = switch_timestamp_now() / 1000;
 				}
 			}
 		}
 		// don't allow collected digit string testing if there are varying sized keys until timeout
-		if (parser->maxlen - parser->minlen > 0 && (switch_time_now() / 1000) - stream->last_digit_time < parser->digit_timeout_ms) {
+		if (parser->maxlen - parser->minlen > 0 && (switch_timestamp_now() / 1000) - stream->last_digit_time < parser->digit_timeout_ms) {
 			len = 0;
 		}
 		// if we have digits to test

Modified: freeswitch/trunk/src/switch_ivr_async.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_async.c	(original)
+++ freeswitch/trunk/src/switch_ivr_async.c	Thu Jan 10 19:43:49 2008
@@ -307,7 +307,7 @@
 	switch_channel_pre_answer(channel);
 
 	if (limit) {
-		to = time(NULL) + limit;
+		to = switch_timestamp(NULL) + limit;
 	}
 
 	if (flags && strchr(flags, 'm')) {
@@ -763,7 +763,7 @@
 	}
 
 	if (limit) {
-		to = time(NULL) + limit;
+		to = switch_timestamp(NULL) + limit;
 	}
 	
 	if ((status = switch_core_media_bug_add(session, record_callback, fh, to, flags, &bug)) != SWITCH_STATUS_SUCCESS) {

Modified: freeswitch/trunk/src/switch_ivr_originate.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_originate.c	(original)
+++ freeswitch/trunk/src/switch_ivr_originate.c	Thu Jan 10 19:43:49 2008
@@ -899,7 +899,7 @@
 
 			}			
 
-			time(&start);
+			switch_timestamp(&start);
 
 			for (;;) {
 				uint32_t valid_channels = 0;
@@ -922,7 +922,7 @@
 						goto notready;
 					}
 
-					if ((time(NULL) - start) > (time_t) timelimit_sec) {
+					if ((switch_timestamp(NULL) - start) > (time_t) timelimit_sec) {
 						to++;
 						idx = IDX_CANCEL;
 						goto notready;
@@ -1036,7 +1036,7 @@
 				}
 
 				// When the AND operator is being used, and fail_on_single_reject is set, a hangup indicates that the call should fail.
-				if ((to = (uint8_t) ((time(NULL) - start) >= (time_t) timelimit_sec))
+				if ((to = (uint8_t) ((switch_timestamp(NULL) - start) >= (time_t) timelimit_sec))
 					|| (fail_on_single_reject && hups)) {
 					idx = IDX_CANCEL;
 					goto notready;

Modified: freeswitch/trunk/src/switch_ivr_play_say.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_play_say.c	(original)
+++ freeswitch/trunk/src/switch_ivr_play_say.c	Thu Jan 10 19:43:49 2008
@@ -434,7 +434,7 @@
 	}
 
 	if (limit) {
-		start = time(NULL);
+		start = switch_timestamp(NULL);
 	}
 
 	if (fh->thresh) {
@@ -464,7 +464,7 @@
 			switch_ivr_parse_all_events(session);
 		}
 
-		if (start && (time(NULL) - start) > limit) {
+		if (start && (switch_timestamp(NULL) - start) > limit) {
 			break;
 		}
 

Modified: freeswitch/trunk/src/switch_rtp.c
==============================================================================
--- freeswitch/trunk/src/switch_rtp.c	(original)
+++ freeswitch/trunk/src/switch_rtp.c	Thu Jan 10 19:43:49 2008
@@ -1063,7 +1063,7 @@
 			uint16_t in_digit_seq = ntohs((uint16_t) rtp_session->recv_msg.header.seq);
 
 			/* SHEESH.... Curse you RFC2833 inventors!!!! */
-			if ((time(NULL) - rtp_session->dtmf_data.last_digit_time) > 2) {
+			if ((switch_timestamp(NULL) - rtp_session->dtmf_data.last_digit_time) > 2) {
 				rtp_session->dtmf_data.last_digit = 0;
 				rtp_session->dtmf_data.dc = 0;
 				rtp_session->dtmf_data.in_digit_seq = 0;
@@ -1073,7 +1073,7 @@
 				if (duration && end) {
 					if (key != rtp_session->dtmf_data.last_digit) {
 						switch_dtmf_t dtmf = { key, duration };
-						time(&rtp_session->dtmf_data.last_digit_time);
+						switch_timestamp(&rtp_session->dtmf_data.last_digit_time);
 						switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
 						switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_BREAK);
 					}
@@ -1347,7 +1347,7 @@
 		uint32_t rate = 0;
 		uint32_t codec_flags = 0;
 		uint32_t len = sizeof(decoded);
-		time_t now = time(NULL);
+		time_t now = switch_timestamp(NULL);
 		send = 0;
 
 
@@ -1523,7 +1523,7 @@
 	rtp_session->vad_data.cng_freq = 50;
 	rtp_session->vad_data.ts = 1;
 	rtp_session->vad_data.start = 0;
-	rtp_session->vad_data.next_scan = time(NULL);
+	rtp_session->vad_data.next_scan = switch_timestamp(NULL);
 	rtp_session->vad_data.scan_freq = 0;
 	switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_VAD);
 	switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_CNG);

Modified: freeswitch/trunk/src/switch_scheduler.c
==============================================================================
--- freeswitch/trunk/src/switch_scheduler.c	(original)
+++ freeswitch/trunk/src/switch_scheduler.c	Thu Jan 10 19:43:49 2008
@@ -77,7 +77,7 @@
 		if (done) {
 			tp->destroyed = 1;
 		} else {
-			int64_t now = time(NULL);
+			int64_t now = switch_timestamp(NULL);
 			if (now >= tp->task.runtime && !tp->in_thread) {
 				int32_t diff = (int32_t)(now - tp->task.runtime);
 				if (diff > 1) {
@@ -160,7 +160,7 @@
 	switch_zmalloc(container, sizeof(*container));
 	switch_assert(func);
 	container->func = func;
-	container->task.created = time(NULL);
+	container->task.created = switch_timestamp(NULL);
 	container->task.runtime = task_runtime;
 	container->task.group = strdup(group ? group : "none");
 	container->task.cmd_id = cmd_id;

Modified: freeswitch/trunk/src/switch_stun.c
==============================================================================
--- freeswitch/trunk/src/switch_stun.c	(original)
+++ freeswitch/trunk/src/switch_stun.c	Thu Jan 10 19:43:49 2008
@@ -314,7 +314,7 @@
 	switch_stun_packet_attribute_add_username(packet, username, 32);
 	bytes = switch_stun_packet_length(packet);
 	switch_socket_sendto(sock, remote_addr, 0, (void *) packet, &bytes);
-	started = switch_time_now();
+	started = switch_timestamp_now();
 
 	*ip = NULL;
 	*port = 0;
@@ -326,7 +326,7 @@
 			break;
 		}
 
-		if ((elapsed = (unsigned int) ((switch_time_now() - started) / 1000)) > 5000) {
+		if ((elapsed = (unsigned int) ((switch_timestamp_now() - started) / 1000)) > 5000) {
 			*err = "Timeout";
 			switch_socket_shutdown(sock, SWITCH_SHUTDOWN_READWRITE);
 			switch_socket_close(sock);

Modified: freeswitch/trunk/src/switch_time.c
==============================================================================
--- freeswitch/trunk/src/switch_time.c	(original)
+++ freeswitch/trunk/src/switch_time.c	Thu Jan 10 19:43:49 2008
@@ -59,6 +59,9 @@
 SWITCH_MODULE_DEFINITION(softtimer, softtimer_load, softtimer_shutdown, softtimer_runtime);
 
 #define MAX_ELEMENTS 1000
+#define IDLE_SPEED 100
+#define STEP_MS 1
+#define STEP_MIC 1000
 
 struct timer_private {
 	switch_size_t reference;
@@ -77,7 +80,45 @@
 
 static timer_matrix_t TIMER_MATRIX[MAX_ELEMENTS + 1];
 
-#define IDLE_SPEED 100
+
+SWITCH_DECLARE(switch_time_t) switch_timestamp_now(void)
+{
+	return runtime.timestamp ? runtime.timestamp : switch_time_now();
+}
+
+
+SWITCH_DECLARE(time_t) switch_timestamp(time_t *t)
+{
+	time_t now = switch_timestamp_now() / APR_USEC_PER_SEC;
+	if (t) {
+		*t = now;
+	}
+	return now;
+}
+
+static switch_time_t time_now(int64_t offset)
+{
+	switch_time_t now;
+
+#if defined(HAVE_CLOCK_GETTIME)
+	struct timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    now = ts.tv_sec * APR_USEC_PER_SEC + (ts.tv_nsec/1000) + offset;
+
+#else
+	now = switch_time_now();
+#endif
+
+	return now;
+}
+
+SWITCH_DECLARE(void) switch_time_sync(void)
+{
+	runtime.reference = switch_time_now();
+	runtime.offset = runtime.reference - time_now(0);
+	runtime.reference = time_now(runtime.offset);
+}
+
 
 
 SWITCH_DECLARE(void) switch_sleep(switch_interval_time_t t)
@@ -225,15 +266,14 @@
 }
 
 
-#define STEP_MS 1
-#define STEP_MIC 1000
-
 SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime)
 {
-	switch_time_t reference = switch_time_now();
+	switch_time_t too_late = STEP_MIC * 128;
 	uint32_t current_ms = 0;
 	uint32_t x, tick = 0;
-	switch_time_t ts = 0;
+	switch_time_t ts = 0, last = 0;
+	
+	switch_time_sync();
 
 	memset(&globals, 0, sizeof(globals));
 	switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool);
@@ -243,15 +283,35 @@
 	runtime.sps = runtime.sps_total;
 	switch_mutex_unlock(runtime.throttle_mutex);
 
+
 	while (globals.RUNNING == 1) {
-		reference += STEP_MIC;
-		while ((ts = switch_time_now()) < reference) {
+		runtime.reference += STEP_MIC;
+		while ((ts = time_now(runtime.offset)) < runtime.reference) {
+			if (ts < last) {
+				int64_t diff = (int64_t)(ts - last);
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Reverse Clock Skew Detected!\n");
+				runtime.reference = switch_time_now();
+				current_ms = 0;
+				tick = 0;
+				runtime.initiated += diff;
+			}
 			switch_yield(STEP_MIC);
+			last = ts;
 		}
+
+		if (ts > (runtime.reference + too_late)) {
+			switch_time_t diff = ts - runtime.reference - STEP_MIC;
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Forward Clock Skew Detected!\n");
+			runtime.reference = switch_time_now();
+			current_ms = 0;
+			tick = 0;
+			runtime.initiated += diff;
+		}
+		
 		runtime.timestamp = ts;
 		current_ms += STEP_MS;
 		tick += STEP_MS;
-
+		
 		if (tick >= 1000) {
 			if (runtime.sps <= 0) {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Over Session Rate of %d!\n", runtime.sps_total);

Modified: freeswitch/trunk/src/switch_utils.c
==============================================================================
--- freeswitch/trunk/src/switch_utils.c	(original)
+++ freeswitch/trunk/src/switch_utils.c	Thu Jan 10 19:43:49 2008
@@ -230,7 +230,7 @@
 	unsigned char in[B64BUFFLEN];
 	unsigned char out[B64BUFFLEN + 512];
 
-    switch_snprintf(filename, 80, "%smail.%d%04x", SWITCH_GLOBAL_dirs.temp_dir, (int)time(NULL), rand() & 0xffff);
+    switch_snprintf(filename, 80, "%smail.%d%04x", SWITCH_GLOBAL_dirs.temp_dir, (int)switch_timestamp(NULL), rand() & 0xffff);
     
     if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644))) {
         if (file) {
@@ -691,7 +691,7 @@
 	switch_time_t ret = 0;
 	char *pattern = "^(\\d+)-(\\d+)-(\\d+)\\s*(\\d*):{0,1}(\\d*):{0,1}(\\d*)";
 
-	switch_time_exp_lt(&tm, switch_time_now());
+	switch_time_exp_lt(&tm, switch_timestamp_now());
 	tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
 
 	if ((proceed = switch_regex_perform(in, pattern, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {



More information about the Freeswitch-svn mailing list