/* * 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); }
/* Update a timestat_t struct with a new sample */ void time_stat_update(timestat_t *stats, const nstime_t *delta, packet_info *pinfo) { if(stats->num==0){ stats->max=*delta; stats->max_num=pinfo->fd->num; stats->min=*delta; stats->min_num=pinfo->fd->num; } if( (delta->secs<stats->min.secs) ||( (delta->secs==stats->min.secs) &&(delta->nsecs<stats->min.nsecs) ) ){ stats->min=*delta; stats->min_num=pinfo->fd->num; } if( (delta->secs>stats->max.secs) ||( (delta->secs==stats->max.secs) &&(delta->nsecs>stats->max.nsecs) ) ){ stats->max=*delta; stats->max_num=pinfo->fd->num; } nstime_add(&stats->tot, delta); stats->num++; }
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"); } }
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"); }
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_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"); }