]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
try to use clock_gettime to make sure we don't care if the system time changes
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 11 Jan 2008 00:43:49 +0000 (00:43 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 11 Jan 2008 00:43:49 +0000 (00:43 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7165 d0543943-73ff-0310-b7d9-9358b9ac24b2

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

index 1f99c1eed190077a7f6ee651221b3f8a8b0bbbec..d50f8962061690b9cd68f3c4be0655bec1289ac1 100644 (file)
@@ -158,6 +158,8 @@ struct switch_media_bug {
 
 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;
index 226b3409a26eb8fbeb0021f0273e05fb65c8d5ab..4f6ad612bd15b68e8c27a51fe9c178687955c5df 100644 (file)
@@ -1536,6 +1536,8 @@ SWITCH_DECLARE(void) switch_core_memory_reclaim_events(void);
 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);
 
 ///\}
 
index 1641d09d899a1d4ac19ca1e3c6e3dd936ddb3323..d5fd14d66624c629609e15e66611603deb994ef3 100644 (file)
@@ -1065,7 +1065,8 @@ typedef enum {
        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;
index 536319c225da59037ddc3424eebbf653bd5329b6..1b1a2ce3370842ee40141d0ad103337b2e80c729 100644 (file)
@@ -600,11 +600,16 @@ SWITCH_STANDARD_API(ctl_function)
                        }
                        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 @@ SWITCH_STANDARD_API(tone_detect_session_function)
                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 @@ SWITCH_STANDARD_API(sched_transfer_function)
                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_STANDARD_API(sched_hangup_function)
                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 @@ SWITCH_STANDARD_API(sched_broadcast_function)
                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 @@ SWITCH_STANDARD_API(sched_api_function)
                        *dcmd++ = '\0';
 
                        if (*tm == '+') {
-                               when = time(NULL) + atol(tm + 1);
+                               when = switch_timestamp(NULL) + atol(tm + 1);
                        } else {
                                when = atol(tm);
                        }
index 4ac4777742cc96813854392b18b98c562232993e..63408d29f37ae81f3892fa10d44911c13a70a52c 100644 (file)
@@ -220,7 +220,7 @@ SWITCH_STANDARD_APP(sched_transfer_function)
                        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_STANDARD_APP(sched_hangup_function)
                        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_STANDARD_APP(sched_broadcast_function)
                        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 @@ SWITCH_STANDARD_APP(strftime_function)
                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_STANDARD_API(strepoch_api_function)
        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 @@ SWITCH_STANDARD_API(strftime_api_function)
                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 @@ SWITCH_STANDARD_APP(tone_detect_session_function)
                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;
                        }
index 492e3103cf69abd77a4da9022a28c204f8f517a1..6ac8094ea1f6e8a23a9bed8314fe322e09ba4da8 100644 (file)
@@ -510,7 +510,7 @@ static switch_status_t enum_lookup(char *root, char *in, enum_record_t ** result
                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);
                }
index 7a835d843ea518591d9b93402cd7903eae9e9a7a..d385796d8ae53756fd53854c24c712c12e58445e 100644 (file)
@@ -182,7 +182,7 @@ SWITCH_STANDARD_APP(bcast_function)
                }
        }
 
-       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 @@ SWITCH_STANDARD_APP(bcast_function)
 
 
 
-       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);
index 93b46d401dbd248a76eeb407f615cc994b948cb3..d4fc57bdee8b022ba6ea17c4a28a6e4f8c910cb0 100644 (file)
@@ -484,7 +484,7 @@ SWITCH_STANDARD_APP(rss_function)
                        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);
 
 
index d7d6c170fc2a656df7621e32c51e0fc52ede83e6..26dbd70414ed958b555105307aaf2da6124a0eba 100644 (file)
@@ -1380,7 +1380,7 @@ default:
                        }
                }
                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 @@ greet:
                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 @@ end:
                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 @@ static void do_play(vm_profile_t *profile, char *user, char *domain, char *file,
        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);
index 0d99ff91761f1ec779704e362c8f7bf4f2bed2ec..a21f10328a986ba5952074efe1872f3209367027 100644 (file)
@@ -637,7 +637,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
 {
        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 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
                }
 
                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 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc
                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;
index 44f251afcb55e0f6ed5ed76f40e405b1fc96d3ba..980194686382702cfb86898403ea6a247691b06f 100644 (file)
@@ -1242,7 +1242,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
                                        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 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
 
        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", 
index 1a8faad22706ce15c3b0dee28f8d29927c836740..e681848e52c3134c50cdc610a8d62afe2b2b4383 100644 (file)
@@ -345,7 +345,7 @@ void event_handler(switch_event_t *event)
                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 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
 
        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;
                }
 
index 21f98ea439c95b2603271b1661611db8bd5b4a7f..2453625e2be38d29f970100360954b9b70a30a1b 100644 (file)
@@ -72,7 +72,7 @@ void sofia_glue_set_local_sdp(private_object_t *tech_pvt, const char *ip, uint32
        }
 
        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 @@ uint8_t sofia_glue_negotiate_sdp(switch_core_session_t *session, sdp_session_t *
                                        }
                                
                                        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;
                                        }
                                }
index d616beb26304586154dfe943c45597dd92bdb325..e579355d281543e27d505b4db906a82bbda878d6 100644 (file)
@@ -608,13 +608,13 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
        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 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
        }
 
        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 @@ void sofia_presence_handle_sip_i_subscribe(int status,
                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);
 
index 102e4aa5f49a2419a39631872c1f4c0d8714b7c4..c4970b1e152de1debdbffa1efcb368ee11da8067 100644 (file)
@@ -302,7 +302,7 @@ void sofia_reg_auth_challange(nua_t *nua, sofia_profile_t *profile, nua_handle_t
 
        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 @@ uint8_t sofia_reg_handle_register(nua_t * nua, sofia_profile_t *profile, nua_han
                
                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) {
index 45d8ceea42e7da1ba3c6236f95dcbb7b62c86739..3c921604645450b41ffe90d2f27c6e32b273f85e 100644 (file)
@@ -87,7 +87,7 @@ static void do_rotate(cdr_fd_t *fd)
        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;
index 3a01798c900f37778921d9d304378e35eef5918c..58be810188dfb902aafea80726e520888143a26f 100644 (file)
@@ -354,7 +354,7 @@ static switch_status_t read_packet(listener_t * listener, switch_event_t **event
        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 @@ static switch_status_t read_packet(listener_t * listener, switch_event_t **event
                }
 
                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;
index aa39da22e73dd470d39297751e264a877a4e8d84..5744e91787ece804b0030dbef055839e05549e6a 100644 (file)
@@ -2077,14 +2077,14 @@ static JSBool session_wait_for_media(JSContext * cx, JSObject * obj, uintN argc,
        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 @@ static JSBool session_wait_for_answer(JSContext * cx, JSObject * obj, uintN argc
        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;
index 2d612baba1a1f1cbcd07a314bfe3b8e425909f35..b75c927cf763873e8318f0d06027ac0ea3ed8b9a 100644 (file)
@@ -123,7 +123,7 @@ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile)
 
        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;
index d42db3cb23a5a886f1df3851c13b3d5bd00dbe31..4f1deebebbf924ab41eb57ed85c06a966f666ea0 100644 (file)
@@ -303,7 +303,7 @@ static switch_status_t de_say_time(switch_core_session_t *session, char *tosay,
                        }
                } else {
                        if ((seconds = atoi(tosay)) <= 0) {
-                               seconds = (int64_t) time(NULL);
+                               seconds = (int64_t) switch_timestamp(NULL);
                        }
 
                        if (seconds >= 60) {
index 7e87ae51511629abb28a169d5e027d3d425a9236..146eb2542cc703207072adda41e24543e4f3d0e2 100644 (file)
@@ -303,7 +303,7 @@ static switch_status_t en_say_time(switch_core_session_t *session, char *tosay,
                        }
                } else {
                        if ((seconds = atoi(tosay)) <= 0) {
-                               seconds = (int64_t) time(NULL);
+                               seconds = (int64_t) switch_timestamp(NULL);
                        }
 
                        if (seconds >= 60) {
index d75aa4d386ae89a5ef4a25066be348efa030d26d..18d0c3fe4df4e2f68b81eb86b8e1f624090c46f2 100644 (file)
@@ -303,7 +303,7 @@ static switch_status_t es_say_time(switch_core_session_t *session, char *tosay,
                        }
                } else {
                        if ((seconds = atoi(tosay)) <= 0) {
-                               seconds = (int64_t) time(NULL);
+                               seconds = (int64_t) switch_timestamp(NULL);
                        }
 
                        if (seconds >= 60) {
index 39fda5a526ea6a6d9272f1cb6d86558424920807..71b389ff705d1f23ea20697a5632c071395e8ca8 100644 (file)
@@ -303,7 +303,7 @@ static switch_status_t fr_say_time(switch_core_session_t *session, char *tosay,
                        }
                } else {
                        if ((seconds = atoi(tosay)) <= 0) {
-                               seconds = (int64_t) time(NULL);
+                               seconds = (int64_t) switch_timestamp(NULL);
                        }
 
                        if (seconds >= 60) {
index c7a40c54a8360282e68871eecf46cfca21fa0cec..4576e9a86395cb66447e839ca0b373b90ab7aaa7 100644 (file)
@@ -303,7 +303,7 @@ static switch_status_t it_say_time(switch_core_session_t *session, char *tosay,
                        }
                } else {
                        if ((seconds = atoi(tosay)) <= 0) {
-                               seconds = (int64_t) time(NULL);
+                               seconds = (int64_t) switch_timestamp(NULL);
                        }
 
                        if (seconds >= 60) {
index 4664ae83504df0eb9892106f4438856c06bfcb1e..f2350c992226414b21fd79a256aabb826ec2b5e7 100644 (file)
@@ -303,7 +303,7 @@ static switch_status_t nl_say_time(switch_core_session_t *session, char *tosay,
                        }
                } else {
                        if ((seconds = atoi(tosay)) <= 0) {
-                               seconds = (int64_t) time(NULL);
+                               seconds = (int64_t) switch_timestamp(NULL);
                        }
 
                        if (seconds >= 60) {
index 38948c2a45820b2ddd23515688376e836b38c6d4..fd6a03f8b93c46025f58c60e04e84003c55a05c6 100644 (file)
@@ -1048,7 +1048,7 @@ SWITCH_DECLARE(void) switch_channel_set_caller_profile(switch_channel_t *channel
        }
 
        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 @@ SWITCH_DECLARE(void) switch_channel_set_caller_profile(switch_channel_t *channel
                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 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_hangup(switch_chan
 
        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 @@ SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_answered(switch_chan
 
        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);
        }
 
index 99352360c9ad0f046c71f0984b8a6d0e6b1d8d11..d76c8b79a1bf59f8031a8d6ed421702aecf9cfeb 100644 (file)
@@ -80,13 +80,9 @@ SWITCH_STANDARD_SCHED_FUNC(heartbeat_callback)
        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_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc
        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(void) switch_core_measure_time(switch_time_t total_ms, switch_cor
 
 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_DECLARE(int32_t) switch_core_session_ctl(switch_session_ctl_t cmd, int32_
        }
 
        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);
index acb28bae1191112a023c8b3548139ab28d5cbfc7..75a912a6ec277d97805687900d10d32810f11ead 100644 (file)
@@ -270,7 +270,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi
                                        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 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi
                                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 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
                                        }
                                }
 
-                               if (bp->stop_time && bp->stop_time <= time(NULL)) {
+                               if (bp->stop_time && bp->stop_time <= switch_timestamp(NULL)) {
                                        ok = SWITCH_FALSE;
                                }
 
index 01f48bd9219c15665dcc99f761ab211f7f2ee0f5..668727527e8b67c001b4a157fa09f86b9c6d0890 100644 (file)
@@ -118,7 +118,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_port_allocator_request_port(switch_c
        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;
index 4e307eb2f7ed2920cf3f5adfd5e9cf4dc59e7e78..e3584ce9d64731fc89b723d830b5729af7fa348e 100644 (file)
@@ -40,17 +40,17 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session,
 {
        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 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_callback(switch_core_s
        }
 
        if (timeout) {
-               started = switch_time_now();
+               started = switch_timestamp_now();
        }
 
        while (switch_channel_ready(channel)) {
@@ -557,7 +557,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_callback(switch_core_s
                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 @@ SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stre
                                        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
index 6ea4f294b19b5d75f4d48d95e774e6fa071b6fd5..a7c44720315f99ef23cc7751ee97702b74108af9 100644 (file)
@@ -307,7 +307,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_displace_session(switch_core_session_
        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 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_session(switch_core_session_t
        }
 
        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) {
index b292e1bb39910d3084a5b056bfa6fc8537400b57..402e560f8bad25172035463db36d05aafee4c1e2 100644 (file)
@@ -899,7 +899,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess
 
                        }                       
 
-                       time(&start);
+                       switch_timestamp(&start);
 
                        for (;;) {
                                uint32_t valid_channels = 0;
@@ -922,7 +922,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess
                                                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 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess
                                }
 
                                // 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;
index 3d2ea3bced31d87b5001b35b3280dfb2a1dd03b0..9bea1f61d359ce96ca917e1e62822011a8864f04 100644 (file)
@@ -434,7 +434,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
        }
 
        if (limit) {
-               start = time(NULL);
+               start = switch_timestamp(NULL);
        }
 
        if (fh->thresh) {
@@ -464,7 +464,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
                        switch_ivr_parse_all_events(session);
                }
 
-               if (start && (time(NULL) - start) > limit) {
+               if (start && (switch_timestamp(NULL) - start) > limit) {
                        break;
                }
 
index 6b7f123ca7bcb361d223c0c190caaa3d26010908..db7604c8cae0079c1180386904aadce92e65df29 100644 (file)
@@ -1063,7 +1063,7 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
                        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 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
                                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 @@ static int rtp_common_write(switch_rtp_t *rtp_session, void *data, uint32_t data
                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 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_enable_vad(switch_rtp_t *rtp_session,
        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);
index 25e59be74a792fc1ddd91e80a2f596896e8b44e4..b9355759b156019562feb128e4ed1a34105eb81e 100644 (file)
@@ -77,7 +77,7 @@ static int task_thread_loop(int done)
                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_DECLARE(uint32_t) switch_scheduler_add_task(time_t task_runtime,
        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;
index 3e9315a89dd8c4a90bcd0d580fb0c3ca635d0a47..1333335830c93cb5575d9b0a62a3fc7190e159f8 100644 (file)
@@ -314,7 +314,7 @@ SWITCH_DECLARE(switch_status_t) switch_stun_lookup(char **ip,
        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 @@ SWITCH_DECLARE(switch_status_t) switch_stun_lookup(char **ip,
                        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);
index dc064bf4c6c046ea20882f59d0b991466a85706c..13daeb6e8c9d188535cf6ebd3461d75804fb49cc 100644 (file)
@@ -59,6 +59,9 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime);
 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 @@ typedef struct timer_matrix timer_matrix_t;
 
 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 @@ static switch_status_t timer_destroy(switch_timer_t *timer)
 }
 
 
-#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 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime)
        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);
index ce2b02688f73fb32b3ba368148558f3d3e140578..850ec0994aa3367c945caa70e99c789381a66447 100644 (file)
@@ -230,7 +230,7 @@ SWITCH_DECLARE(switch_bool_t) switch_simple_email(const char *to, const char *fr
        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_DECLARE(switch_time_t) switch_str_time(const char *in)
        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])))) {