2 * Routines for "Time Shift" window
3 * Submitted by Edwin Groothuis <wireshark@mavetju.org>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include "time_shift.h"
37 #include "ui/ui_util.h"
41 #define SHIFT_SETTOZERO 1
42 #define SHIFT_KEEPOFFSET 0
44 #define CHECK_YEARS(Y) \
46 return "Years must be larger than 1970"; \
48 #define CHECK_MONTHS(M) \
49 if (*M < 1 || *M > 12) { \
50 return "Months must be between [1..12]"; \
52 #define CHECK_DAYS(D) \
53 if (*D < 1 || *D > 31) { \
54 return "Days must be between [1..31]"; \
56 #define CHECK_HOURS(h) \
57 if (*h < 0 || *h > 23) { \
58 return "Hours must be between [0..23]"; \
60 #define CHECK_HOUR(h) \
62 return "Negative hours. Have you specified more than " \
63 "one minus character?"; \
65 #define CHECK_MINUTE(m) \
66 if (*m < 0 || *m > 59) { \
67 return "Minutes must be between [0..59]"; \
69 #define CHECK_SECOND(s) \
70 if (*s < 0 || *s > 59) { \
71 return "Seconds must be between [0..59]"; \
75 modify_time_perform(frame_data *fd, int neg, nstime_t *offset, int settozero)
77 /* The actual shift */
78 if (settozero == SHIFT_SETTOZERO) {
79 nstime_subtract(&(fd->abs_ts), &(fd->shift_offset));
80 nstime_set_zero(&(fd->shift_offset));
83 if (neg == SHIFT_POS) {
84 nstime_add(&(fd->abs_ts), offset);
85 nstime_add(&(fd->shift_offset), offset);
86 } else if (neg == SHIFT_NEG) {
87 nstime_subtract(&(fd->abs_ts), offset);
88 nstime_subtract(&(fd->shift_offset), offset);
90 fprintf(stderr, "Modify_time_perform: neg = %d?\n", neg);
95 * If the line between (OT1, NT1) and (OT2, NT2) is a straight line
96 * and (OT3, NT3) is on that line,
97 * then (NT2 - NT1) / (OT2 - OT2) = (NT3 - NT1) / (OT3 - OT1) and
98 * then (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT2) = (NT3 - NT1) and
99 * then NT1 + (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT2) = NT3 and
100 * then NT3 = NT1 + (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT2) and
101 * thus NT3 = NT1 + (OT3 - OT1) * (NT2 - NT1) / (OT2 - OT1)
102 * or NT3 = NT1 + (OT3 - OT1) * ( deltaNT12 / deltaOT12)
104 * All the things you come up when waiting for the train to come...
107 calcNT3(nstime_t *OT1, nstime_t *OT3, nstime_t *NT1, nstime_t *NT3,
108 nstime_t *deltaOT, nstime_t *deltaNT)
110 long double fnt, fot, f, secs, nsecs;
112 fnt = (long double)deltaNT->secs + (deltaNT->nsecs / 1000000000.0L);
113 fot = (long double)deltaOT->secs + (deltaOT->nsecs / 1000000000.0L);
116 nstime_copy(NT3, OT3);
117 nstime_subtract(NT3, OT1);
119 secs = f * (long double)NT3->secs;
120 nsecs = f * (long double)NT3->nsecs;
121 nsecs += (secs - floorl(secs)) * 1000000000.0L;
122 while (nsecs > 1000000000L) {
124 nsecs -= 1000000000L;
128 nsecs += 1000000000L;
130 NT3->secs = (time_t)secs;
131 NT3->nsecs = (int)nsecs;
132 nstime_add(NT3, NT1);
136 time_string_parse(const gchar *time_text, int *year, int *month, int *day, gboolean *negative, int *hour, int *minute, long double *second) {
137 const gchar *pts = time_text;
139 if (!time_text || !hour || !minute || !second)
140 return "Unable to convert time.";
142 /* strip whitespace */
143 while (isspace(pts[0]))
146 if (year && month && day) {
148 * The following time format is allowed:
149 * [YYYY-MM-DD] hh:mm:ss(.decimals)?
151 * Since Wireshark doesn't support regular expressions (please prove me
152 * wrong :-) we will have to figure it out ourselves in the
155 * 1. YYYY-MM-DD hh:mm:ss.decimals
156 * 2. hh:mm:ss.decimals
160 /* check for empty string */
162 return "Time is empty.";
164 if (sscanf(pts, "%d-%d-%d %d:%d:%Lf", year, month, day, hour, minute, second) == 6) {
165 /* printf("%%d-%%d-%%d %%d:%%d:%%f\n"); */
170 CHECK_MINUTE(minute);
171 CHECK_SECOND(second);
172 } else if (sscanf(pts, "%d:%d:%Lf", hour, minute, second) == 3) {
173 /* printf("%%d:%%d:%%f\n"); */
174 *year = *month = *day = 0;
176 CHECK_MINUTE(minute);
177 CHECK_SECOND(second);
179 return "Could not parse the time. Expected [YYYY-MM-DD] "
184 return "Unable to convert time.";
187 * The following offset types are allowed:
188 * -?((hh:)mm:)ss(.decimals)?
190 * Since Wireshark doesn't support regular expressions (please prove me
191 * wrong :-) we will have to figure it out ourselves in the
194 * 1. hh:mm:ss.decimals
200 /* check for minus sign */
207 /* check for empty string */
209 return "Time is empty.";
211 if (sscanf(pts, "%d:%d:%Lf", hour, minute, second) == 3) {
212 /* printf("%%d:%%d:%%d.%%d\n"); */
214 CHECK_MINUTE(minute);
215 CHECK_SECOND(second);
216 } else if (sscanf(pts, "%d:%Lf", minute, second) == 2) {
217 /* printf("%%d:%%d.%%d\n"); */
218 CHECK_MINUTE(minute);
219 CHECK_SECOND(second);
221 } else if (sscanf(pts, "%Lf", second) == 1) {
222 /* printf("%%d.%%d\n"); */
223 CHECK_SECOND(second);
226 return "Could not parse the time: Expected [[hh:]mm:]ss.[dec].";
234 time_string_to_nstime(const gchar *time_text, nstime_t *packettime, nstime_t *nstime)
238 struct tm tm, *tmptm;
240 const gchar *err_str;
242 if ((err_str = time_string_parse(time_text, &Y, &M, &D, NULL, &h, &m, &f)) != NULL)
245 /* Convert the time entered in an epoch offset */
246 tmptm = localtime(&(packettime->secs));
250 memset (&tm, 0, sizeof (tm));
253 tm.tm_year = Y - 1900;
259 tm.tm_sec = (int)floorl(f);
262 return "Mktime went wrong. Is the time valid?";
267 nstime->nsecs = (int)(f * 1000000000);
273 time_shift_all(capture_file *cf, const gchar *offset_text)
276 long double offset_float = 0;
282 const gchar *err_str;
284 if (!cf || !offset_text)
285 return "Nothing to work with.";
287 if ((err_str = time_string_parse(offset_text, NULL, NULL, NULL, &neg, &h, &m, &f)) != NULL)
290 offset_float = h * 3600 + m * 60 + f;
292 if (offset_float == 0)
293 return "Offset is zero.";
295 nstime_set_zero(&offset);
296 offset.secs = (time_t)floorl(offset_float);
297 offset_float -= offset.secs;
298 offset.nsecs = (int)(offset_float * 1000000000);
300 if (!frame_data_sequence_find(cf->frames, 1))
301 return "No frames found."; /* Shouldn't happen */
303 for (i = 1; i <= cf->count; i++) {
304 if ((fd = frame_data_sequence_find(cf->frames, i)) == NULL)
305 continue; /* Shouldn't happen */
306 modify_time_perform(fd, neg ? SHIFT_NEG : SHIFT_POS, &offset, SHIFT_KEEPOFFSET);
308 packet_list_queue_draw();
314 time_shift_settime(capture_file *cf, guint packet_num, const gchar *time_text)
316 nstime_t set_time, diff_time, packet_time;
317 frame_data *fd, *packetfd;
319 const gchar *err_str;
321 if (!cf || !time_text)
322 return "Nothing to work with.";
324 if (packet_num < 1 || packet_num > cf->count)
325 return "Packet out of range.";
328 * Get a copy of the real time (abs_ts - shift_offset) do we can find out the
329 * difference between the specified time and the original packet
331 if ((packetfd = frame_data_sequence_find(cf->frames, packet_num)) == NULL)
332 return "No packets found.";
333 nstime_delta(&packet_time, &(packetfd->abs_ts), &(packetfd->shift_offset));
335 if ((err_str = time_string_to_nstime(time_text, &packet_time, &set_time)) != NULL)
338 /* Calculate difference between packet time and requested time */
339 nstime_delta(&diff_time, &set_time, &packet_time);
341 /* Up to here nothing is changed */
343 if (!frame_data_sequence_find(cf->frames, 1))
344 return "No frames found."; /* Shouldn't happen */
346 /* Set everything back to the original time */
347 for (i = 1; i <= cf->count; i++) {
348 if ((fd = frame_data_sequence_find(cf->frames, i)) == NULL)
349 continue; /* Shouldn't happen */
350 modify_time_perform(fd, SHIFT_POS, &diff_time, SHIFT_SETTOZERO);
353 packet_list_queue_draw();
358 time_shift_adjtime(capture_file *cf, guint packet1_num, const gchar *time1_text, guint packet2_num, const gchar *time2_text)
360 nstime_t nt1, nt2, ot1, ot2, nt3;
361 nstime_t dnt, dot, d3t;
362 frame_data *fd, *packet1fd, *packet2fd;
364 const gchar *err_str;
366 if (!cf || !time1_text || !time2_text)
367 return "Nothing to work with.";
369 if (packet1_num < 1 || packet1_num > cf->count || packet2_num < 1 || packet2_num > cf->count)
370 return "Packet out of range.";
373 * The following time format is allowed:
374 * [YYYY-MM-DD] hh:mm:ss(.decimals)?
376 * Since Wireshark doesn't support regular expressions (please prove me
377 * wrong :-) we will have to figure it out ourselves in the
380 * 1. YYYY-MM-DD hh:mm:ss.decimals
381 * 2. hh:mm:ss.decimals
386 * Get a copy of the real time (abs_ts - shift_offset) do we can find out the
387 * difference between the specified time and the original packet
389 if ((packet1fd = frame_data_sequence_find(cf->frames, packet1_num)) == NULL)
390 return "No frames found.";
391 nstime_copy(&ot1, &(packet1fd->abs_ts));
392 nstime_subtract(&ot1, &(packet1fd->shift_offset));
394 if ((err_str = time_string_to_nstime(time1_text, &ot1, &nt1)) != NULL)
398 * Get a copy of the real time (abs_ts - shift_offset) do we can find out the
399 * difference between the specified time and the original packet
401 if ((packet2fd = frame_data_sequence_find(cf->frames, packet2_num)) == NULL)
402 return "No frames found.";
403 nstime_copy(&ot2, &(packet2fd->abs_ts));
404 nstime_subtract(&ot2, &(packet2fd->shift_offset));
406 if ((err_str = time_string_to_nstime(time2_text, &ot2, &nt2)) != NULL)
409 nstime_copy(&dot, &ot2);
410 nstime_subtract(&dot, &ot1);
412 nstime_copy(&dnt, &nt2);
413 nstime_subtract(&dnt, &nt1);
415 /* Up to here nothing is changed */
416 if (!frame_data_sequence_find(cf->frames, 1))
417 return "No frames found."; /* Shouldn't happen */
419 for (i = 1; i <= cf->count; i++) {
420 if ((fd = frame_data_sequence_find(cf->frames, i)) == NULL)
421 continue; /* Shouldn't happen */
423 /* Set everything back to the original time */
424 nstime_subtract(&(fd->abs_ts), &(fd->shift_offset));
425 nstime_set_zero(&(fd->shift_offset));
427 /* Add the difference to each packet */
428 calcNT3(&ot1, &(fd->abs_ts), &nt1, &nt3, &dot, &dnt);
430 nstime_copy(&d3t, &nt3);
431 nstime_subtract(&d3t, &(fd->abs_ts));
433 modify_time_perform(fd, SHIFT_POS, &d3t, SHIFT_SETTOZERO);
436 packet_list_queue_draw();
441 time_shift_undo(capture_file *cf)
448 return "Nothing to work with.";
450 nulltime.secs = nulltime.nsecs = 0;
452 if (!frame_data_sequence_find(cf->frames, 1))
453 return "No frames found."; /* Shouldn't happen */
455 for (i = 1; i <= cf->count; i++) {
456 if ((fd = frame_data_sequence_find(cf->frames, i)) == NULL)
457 continue; /* Shouldn't happen */
458 modify_time_perform(fd, SHIFT_NEG, &nulltime, SHIFT_SETTOZERO);
460 packet_list_queue_draw();
470 * indent-tabs-mode: nil
473 * ex: set shiftwidth=4 tabstop=8 expandtab:
474 * :indentSize=4:tabSize=8:noTabs=true: