/* * 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_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"); }
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); }
WSLUA_METAMETHOD NSTime__unm(lua_State* L) { /* Calculates the negative NSTime */ NSTime time1 = checkNSTime(L,1); NSTime time2 = g_malloc (sizeof (nstime_t)); nstime_set_zero (time2); nstime_subtract (time2, time1); pushNSTime (L, time2); return 1; }
static void modify_time_perform(frame_data *fd, int neg, nstime_t *offset, int settozero) { /* The actual shift */ if (settozero == SHIFT_SETTOZERO) { nstime_subtract(&(fd->abs_ts), &(fd->shift_offset)); nstime_set_zero(&(fd->shift_offset)); } 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); } }
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_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"); }
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; }