TEST_END TEST_BEGIN(test_nstime_divide) { nstime_t nsta, nstb, nstc; nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); nstime_imultiply(&nsta, 10); assert_u64_eq(nstime_divide(&nsta, &nstb), 10, "Incorrect division result"); nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); nstime_imultiply(&nsta, 10); nstime_init(&nstc, 1); nstime_add(&nsta, &nstc); assert_u64_eq(nstime_divide(&nsta, &nstb), 10, "Incorrect division result"); nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); nstime_imultiply(&nsta, 10); nstime_init(&nstc, 1); nstime_subtract(&nsta, &nstc); assert_u64_eq(nstime_divide(&nsta, &nstb), 9, "Incorrect division result"); }
/* * If the line between (OT1, NT1) and (OT2, NT2) is a straight line * and (OT3, NT3) is on that line, * then (NT2 - NT1) / (OT2 - OT2) = (NT3 - NT1) / (OT3 - OT1) and * then (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT2) = (NT3 - NT1) and * then NT1 + (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT2) = NT3 and * then NT3 = NT1 + (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT2) and * thus NT3 = NT1 + (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT1) * or NT3 = NT1 + (OT3 - OT1) * ( deltaNT12 / deltaOT12) * * All the things you come up when waiting for the train to come... */ static void calcNT3(nstime_t *OT1, nstime_t *OT3, nstime_t *NT1, nstime_t *NT3, nstime_t *deltaOT, nstime_t *deltaNT) { long double fnt, fot, f, secs, nsecs; fnt = (long double)deltaNT->secs + (deltaNT->nsecs / 1000000000.0L); fot = (long double)deltaOT->secs + (deltaOT->nsecs / 1000000000.0L); f = fnt / fot; nstime_copy(NT3, OT3); nstime_subtract(NT3, OT1); secs = f * (long double)NT3->secs; nsecs = f * (long double)NT3->nsecs; nsecs += (secs - floorl(secs)) * 1000000000.0L; while (nsecs > 1000000000L) { secs += 1; nsecs -= 1000000000L; } while (nsecs < 0) { secs -= 1; nsecs += 1000000000L; } NT3->secs = (time_t)secs; NT3->nsecs = (int)nsecs; nstime_add(NT3, NT1); }
TEST_END TEST_BEGIN(test_nstime_compare) { nstime_t nsta, nstb; nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Times should be equal"); assert_d_eq(nstime_compare(&nstb, &nsta), 0, "Times should be equal"); nstime_init2(&nstb, 42, 42); assert_d_eq(nstime_compare(&nsta, &nstb), 1, "nsta should be greater than nstb"); assert_d_eq(nstime_compare(&nstb, &nsta), -1, "nstb should be less than nsta"); nstime_init2(&nstb, 42, 44); assert_d_eq(nstime_compare(&nsta, &nstb), -1, "nsta should be less than nstb"); assert_d_eq(nstime_compare(&nstb, &nsta), 1, "nstb should be greater than nsta"); nstime_init2(&nstb, 41, BILLION - 1); assert_d_eq(nstime_compare(&nsta, &nstb), 1, "nsta should be greater than nstb"); assert_d_eq(nstime_compare(&nstb, &nsta), -1, "nstb should be less than nsta"); nstime_init2(&nstb, 43, 0); assert_d_eq(nstime_compare(&nsta, &nstb), -1, "nsta should be less than nstb"); assert_d_eq(nstime_compare(&nstb, &nsta), 1, "nstb should be greater than nsta"); }
TEST_END TEST_BEGIN(test_nstime_update) { nstime_t nst; nstime_init(&nst, 0); assert_false(nstime_update(&nst), "Basic time update failed."); /* Only Rip Van Winkle sleeps this long. */ { nstime_t addend; nstime_init2(&addend, 631152000, 0); nstime_add(&nst, &addend); } { nstime_t nst0; nstime_copy(&nst0, &nst); assert_true(nstime_update(&nst), "Update should detect time roll-back."); assert_d_eq(nstime_compare(&nst, &nst0), 0, "Time should not have been modified"); } }
uint64_t timer_usec(const timedelta_t *timer) { nstime_t delta; nstime_copy(&delta, &timer->t1); nstime_subtract(&delta, &timer->t0); return (nstime_ns(&delta) / 1000); }
static void modify_time_perform(frame_data *fd, int neg, nstime_t *offset, int settozero) { static frame_data *first_packet = NULL; static nstime_t nulltime; /* Only for initializing */ if (offset == NULL) { first_packet = fd; nulltime.secs = nulltime.nsecs = 0; return; } if (first_packet == NULL) { fprintf(stderr, "Modify_time_perform: not initialized?\n"); return; } /* The actual shift */ if (settozero == SHIFT_SETTOZERO) { nstime_subtract(&(fd->abs_ts), &(fd->shift_offset)); nstime_copy(&(fd->shift_offset), &nulltime); } if (neg == SHIFT_POS) { nstime_add(&(fd->abs_ts), offset); nstime_add(&(fd->shift_offset), offset); } else if (neg == SHIFT_NEG) { nstime_subtract(&(fd->abs_ts), offset); nstime_subtract(&(fd->shift_offset), offset); } else { fprintf(stderr, "Modify_time_perform: neg = %d?\n", neg); } /* * rel_ts - Relative timestamp to first packet */ if (first_packet != NULL) { nstime_copy(&(fd->rel_ts), &(fd->abs_ts)); nstime_subtract(&(fd->rel_ts), &(first_packet->abs_ts)); } else nstime_copy(&(fd->rel_ts), &nulltime); }
TEST_END TEST_BEGIN(test_nstime_add) { nstime_t nsta, nstb; nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); nstime_add(&nsta, &nstb); nstime_init2(&nstb, 84, 86); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect addition result"); nstime_init2(&nsta, 42, BILLION - 1); nstime_copy(&nstb, &nsta); nstime_add(&nsta, &nstb); nstime_init2(&nstb, 85, BILLION - 2); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect addition result"); }
TEST_END TEST_BEGIN(test_nstime_idivide) { nstime_t nsta, nstb; nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); nstime_imultiply(&nsta, 10); nstime_idivide(&nsta, 10); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect division result"); nstime_init2(&nsta, 42, 666666666); nstime_copy(&nstb, &nsta); nstime_imultiply(&nsta, 3); nstime_idivide(&nsta, 3); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect division result"); }
TEST_END TEST_BEGIN(test_nstime_copy) { nstime_t nsta, nstb; nstime_init2(&nsta, 42, 43); nstime_init(&nstb, 0); nstime_copy(&nstb, &nsta); assert_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied"); assert_u64_eq(nstime_nsec(&nstb), 43, "nsec incorrectly copied"); }
bool nstime_update(nstime_t *time) { nstime_t old_time; nstime_copy(&old_time, time); #ifdef _WIN32 { FILETIME ft; uint64_t ticks; GetSystemTimeAsFileTime(&ft); ticks = (((uint64_t)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; time->ns = ticks * 100; } #elif JEMALLOC_CLOCK_GETTIME { struct timespec ts; if (sysconf(_SC_MONOTONIC_CLOCK) > 0) clock_gettime(CLOCK_MONOTONIC, &ts); else clock_gettime(CLOCK_REALTIME, &ts); time->ns = ts.tv_sec * BILLION + ts.tv_nsec; } #else struct timeval tv; gettimeofday(&tv, NULL); time->ns = tv.tv_sec * BILLION + tv.tv_usec * 1000; #endif /* Handle non-monotonic clocks. */ if (unlikely(nstime_compare(&old_time, time) > 0)) { nstime_copy(time, &old_time); return (true); } return (false); }
TEST_END TEST_BEGIN(test_nstime_subtract) { nstime_t nsta, nstb; nstime_init2(&nsta, 42, 43); nstime_copy(&nstb, &nsta); nstime_subtract(&nsta, &nstb); nstime_init(&nstb, 0); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect subtraction result"); nstime_init2(&nsta, 42, 43); nstime_init2(&nstb, 41, 44); nstime_subtract(&nsta, &nstb); nstime_init2(&nstb, 0, BILLION - 1); assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Incorrect subtraction result"); }
void timer_stop(timedelta_t *timer) { nstime_copy(&timer->t1, &timer->t0); nstime_update(&timer->t1); }
const gchar * time_shift_adjtime(capture_file *cf, guint packet1_num, const gchar *time1_text, guint packet2_num, const gchar *time2_text) { nstime_t nt1, nt2, ot1, ot2, nt3; nstime_t dnt, dot, d3t; frame_data *fd, *packet1fd, *packet2fd; guint32 i; const gchar *err_str; if (!cf || !time1_text || !time2_text) return "Nothing to work with."; if (packet1_num < 1 || packet1_num > cf->count || packet2_num < 1 || packet2_num > cf->count) return "Packet out of range."; /* * The following time format is allowed: * [YYYY-MM-DD] hh:mm:ss(.decimals)? * * Since Wireshark doesn't support regular expressions (please prove me * wrong :-) we will have to figure it out ourselves in the * following order: * * 1. YYYY-MM-DD hh:mm:ss.decimals * 2. hh:mm:ss.decimals * */ /* * Get a copy of the real time (abs_ts - shift_offset) do we can find out the * difference between the specified time and the original packet */ if ((packet1fd = frame_data_sequence_find(cf->frames, packet1_num)) == NULL) return "No frames found."; nstime_copy(&ot1, &(packet1fd->abs_ts)); nstime_subtract(&ot1, &(packet1fd->shift_offset)); if ((err_str = time_string_to_nstime(time1_text, &ot1, &nt1)) != NULL) return err_str; /* * Get a copy of the real time (abs_ts - shift_offset) do we can find out the * difference between the specified time and the original packet */ if ((packet2fd = frame_data_sequence_find(cf->frames, packet2_num)) == NULL) return "No frames found."; nstime_copy(&ot2, &(packet2fd->abs_ts)); nstime_subtract(&ot2, &(packet2fd->shift_offset)); if ((err_str = time_string_to_nstime(time2_text, &ot2, &nt2)) != NULL) return err_str; nstime_copy(&dot, &ot2); nstime_subtract(&dot, &ot1); nstime_copy(&dnt, &nt2); nstime_subtract(&dnt, &nt1); /* Up to here nothing is changed */ if (!frame_data_sequence_find(cf->frames, 1)) return "No frames found."; /* Shouldn't happen */ for (i = 1; i <= cf->count; i++) { if ((fd = frame_data_sequence_find(cf->frames, i)) == NULL) continue; /* Shouldn't happen */ /* Set everything back to the original time */ nstime_subtract(&(fd->abs_ts), &(fd->shift_offset)); nstime_set_zero(&(fd->shift_offset)); /* Add the difference to each packet */ calcNT3(&ot1, &(fd->abs_ts), &nt1, &nt3, &dot, &dnt); nstime_copy(&d3t, &nt3); nstime_subtract(&d3t, &(fd->abs_ts)); modify_time_perform(fd, SHIFT_POS, &d3t, SHIFT_SETTOZERO); } packet_list_queue_draw(); return NULL; }