1 /* Combine multiple dump files, either by appending or by merging by timestamp
5 * Written by Scott Renfro <scott@renfro.org> based on
6 * editcap by Richard Sharpe and Guy Harris
21 #ifdef HAVE_SYS_TIME_H
30 * Scan through the arguments and open the input files
33 merge_open_in_files(int in_file_count, char *const *in_file_names,
34 merge_in_file_t **in_files, int *err, gchar **err_info,
38 size_t files_size = in_file_count * sizeof(merge_in_file_t);
39 merge_in_file_t *files;
42 files = (merge_in_file_t *)g_malloc(files_size);
45 for (i = 0; i < in_file_count; i++) {
46 files[i].filename = in_file_names[i];
47 files[i].wth = wtap_open_offline(in_file_names[i], err, err_info, FALSE);
48 files[i].data_offset = 0;
49 files[i].state = PACKET_NOT_PRESENT;
50 files[i].packet_num = 0;
52 /* Close the files we've already opened. */
53 for (j = 0; j < i; j++)
54 wtap_close(files[j].wth);
58 size = wtap_file_size(files[i].wth, err);
60 for (j = 0; j <= i; j++)
61 wtap_close(files[j].wth);
71 * Scan through and close each input file
74 merge_close_in_files(int count, merge_in_file_t in_files[])
77 for (i = 0; i < count; i++) {
78 wtap_close(in_files[i].wth);
83 * Select an output frame type based on the input files
84 * From Guy: If all files have the same frame type, then use that.
85 * Otherwise select WTAP_ENCAP_PER_PACKET. If the selected
86 * output file type doesn't support per packet frame types,
87 * then the wtap_dump_open call will fail with a reasonable
91 merge_select_frame_type(int count, merge_in_file_t files[])
94 int selected_frame_type;
96 selected_frame_type = wtap_file_encap(files[0].wth);
98 for (i = 1; i < count; i++) {
99 int this_frame_type = wtap_file_encap(files[i].wth);
100 if (selected_frame_type != this_frame_type) {
101 selected_frame_type = WTAP_ENCAP_PER_PACKET;
106 return selected_frame_type;
110 * Scan through input files and find maximum snapshot length
113 merge_max_snapshot_length(int count, merge_in_file_t in_files[])
116 int max_snapshot = 0;
119 for (i = 0; i < count; i++) {
120 snapshot_length = wtap_snapshot_length(in_files[i].wth);
121 if (snapshot_length == 0) {
122 /* Snapshot length of input file not known. */
123 snapshot_length = WTAP_MAX_PACKET_SIZE;
125 if (snapshot_length > max_snapshot)
126 max_snapshot = snapshot_length;
132 * returns TRUE if first argument is earlier than second
135 is_earlier(struct wtap_nstime *l, struct wtap_nstime *r) {
136 if (l->secs > r->secs) { /* left is later */
138 } else if (l->secs < r->secs) { /* left is earlier */
140 } else if (l->nsecs > r->nsecs) { /* tv_sec equal, l.usec later */
143 /* either one < two or one == two
144 * either way, return one
150 * Read the next packet, in chronological order, from the set of files
153 * On success, set *err to 0 and return a pointer to the merge_in_file_t
154 * for the file from which the packet was read.
156 * On a read error, set *err to the error and return a pointer to the
157 * merge_in_file_t for the file on which we got an error.
159 * On an EOF (meaning all the files are at EOF), set *err to 0 and return
163 merge_read_packet(int in_file_count, merge_in_file_t in_files[],
164 int *err, gchar **err_info)
168 struct wtap_nstime tv = { sizeof(time_t) > sizeof(int) ? LONG_MAX : INT_MAX, INT_MAX };
169 struct wtap_pkthdr *phdr;
172 * Make sure we have a packet available from each file, if there are any
173 * packets left in the file in question, and search for the packet
174 * with the earliest time stamp.
176 for (i = 0; i < in_file_count; i++) {
177 if (in_files[i].state == PACKET_NOT_PRESENT) {
179 * No packet available, and we haven't seen an error or EOF yet,
180 * so try to read the next packet.
182 if (!wtap_read(in_files[i].wth, err, err_info, &in_files[i].data_offset)) {
184 in_files[i].state = GOT_ERROR;
187 in_files[i].state = AT_EOF;
189 in_files[i].state = PACKET_PRESENT;
192 if (in_files[i].state == PACKET_PRESENT) {
193 phdr = wtap_phdr(in_files[i].wth);
194 if (is_earlier(&phdr->ts, &tv)) {
202 /* All the streams are at EOF. Return an EOF indication. */
207 /* We'll need to read another packet from this file. */
208 in_files[ei].state = PACKET_NOT_PRESENT;
210 /* Count this packet. */
211 in_files[ei].packet_num++;
214 * Return a pointer to the merge_in_file_t of the file from which the
218 return &in_files[ei];
222 * Read the next packet, in file sequence order, from the set of files
225 * On success, set *err to 0 and return a pointer to the merge_in_file_t
226 * for the file from which the packet was read.
228 * On a read error, set *err to the error and return a pointer to the
229 * merge_in_file_t for the file on which we got an error.
231 * On an EOF (meaning all the files are at EOF), set *err to 0 and return
235 merge_append_read_packet(int in_file_count, merge_in_file_t in_files[],
236 int *err, gchar **err_info)
241 * Find the first file not at EOF, and read the next packet from it.
243 for (i = 0; i < in_file_count; i++) {
244 if (in_files[i].state == AT_EOF)
245 continue; /* This file is already at EOF */
246 if (wtap_read(in_files[i].wth, err, err_info, &in_files[i].data_offset))
247 break; /* We have a packet */
249 /* Read error - quit immediately. */
250 in_files[i].state = GOT_ERROR;
253 /* EOF - flag this file as being at EOF, and try the next one. */
254 in_files[i].state = AT_EOF;
256 if (i == in_file_count) {
257 /* All the streams are at EOF. Return an EOF indication. */
263 * Return a pointer to the merge_in_file_t of the file from which the