(/home/sate/Testcases/c/cve/wireshark-1.2.0/mergecap.c) |
| |
| 141 | | | main(int argc, char *argv[]) |
| 142 | | | { |
| 143 | | | extern char *optarg; |
| 144 | | | extern int optind; |
| 145 | | | int opt; |
| 146 | | | gboolean do_append = FALSE; |
| 147 | | | gboolean verbose = FALSE; |
| 148 | | | int in_file_count = 0; |
| 149 | | | guint snaplen = 0; |
| 150 | | | int file_type = WTAP_FILE_PCAP; |
| 151 | | | int frame_type = -2; |
| 152 | | | int out_fd; |
| 153 | | | merge_in_file_t *in_files = NULL; |
| 154 | | | int i; |
| 155 | | | wtap *wth; |
| 156 | | | struct wtap_pkthdr *phdr, snap_phdr; |
| 157 | | | wtap_dumper *pdh; |
| 158 | | | int open_err, read_err, write_err, close_err; |
| 159 | | | gchar *err_info; |
| 160 | | | int err_fileno; |
| 161 | | | char *out_filename = NULL; |
| 162 | | | gboolean got_read_error = FALSE, got_write_error = FALSE; |
| 163 | | | int count; |
| 164 | | | |
| 165 | | | |
| 166 | | | while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) { |
| 167 | | | |
| 168 | | | switch (opt) { |
| 169 | | | case 'w': |
| 170 | | | out_filename = optarg; |
| 171 | | | break; |
| 172 | | | |
| 173 | | | case 'a': |
| 174 | | | do_append = !do_append; |
| 175 | | | break; |
| 176 | | | |
| 177 | | | case 'T': |
| 178 | | | frame_type = wtap_short_string_to_encap(optarg); |
| 179 | | | if (frame_type < 0) { |
| 180 | | | fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n", |
| 181 | | | optarg); |
| 182 | | | list_encap_types(); |
| 183 | | | exit(1); |
| 184 | | | } |
| 185 | | | break; |
| 186 | | | |
| 187 | | | case 'F': |
| 188 | | | file_type = wtap_short_string_to_file_type(optarg); |
| 189 | | | if (file_type < 0) { |
| 190 | | | fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n", |
| 191 | | | optarg); |
| 192 | | | list_capture_types(); |
| 193 | | | exit(1); |
| 194 | | | } |
| 195 | | | break; |
| 196 | | | |
| 197 | | | case 'v': |
| 198 | | | verbose = TRUE; |
| 199 | | | break; |
| 200 | | | |
| 201 | | | case 's': |
| 202 | | | snaplen = get_positive_int(optarg, "snapshot length"); |
| 203 | | | break; |
| 204 | | | |
| 205 | | | case 'h': |
| 206 | | | usage(); |
| 207 | | | exit(0); |
| 208 | | | break; |
| 209 | | | |
| 210 | | | case '?': |
| 211 | | | switch(optopt) { |
| 212 | | | case'F': |
| 213 | | | list_capture_types(); |
| 214 | | | break; |
| 215 | | | case'T': |
| 216 | | | list_encap_types(); |
| 217 | | | break; |
| 218 | | | default: |
| 219 | | | usage(); |
| 220 | | | } |
| 221 | | | exit(1); |
| 222 | | | break; |
Unreachable Control Flow
The highlighted code will not execute under any circumstances. |
|
| 223 | | | |
| 224 | | | } |
| 225 | | | |
| 226 | | | } |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | |
| 231 | | | in_file_count = argc - optind; |
| 232 | | | if (!out_filename) { |
| 233 | | | fprintf(stderr, "mergecap: an output filename must be set with -w\n"); |
| 234 | | | fprintf(stderr, " run with -h for help\n"); |
| 235 | | | return 1; |
| 236 | | | } |
| 237 | | | if (in_file_count < 1) { |
| 238 | | | fprintf(stderr, "mergecap: No input files were specified\n"); |
| 239 | | | return 1; |
| 240 | | | } |
| 241 | | | |
| 242 | | | |
| 243 | | | if (!merge_open_in_files(in_file_count, &argv[optind], &in_files, |
| 244 | | | &open_err, &err_info, &err_fileno)) { |
| 245 | | | fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno], |
| 246 | | | wtap_strerror(open_err)); |
| 247 | | | switch (open_err) { |
| 248 | | | |
| 249 | | | case WTAP_ERR_UNSUPPORTED: |
| 250 | | | case WTAP_ERR_UNSUPPORTED_ENCAP: |
| 251 | | | case WTAP_ERR_BAD_RECORD: |
| 252 | | | fprintf(stderr, "(%s)\n", err_info); |
| 253 | | | g_free(err_info); |
| 254 | | | break; |
| 255 | | | } |
| 256 | | | return 2; |
| 257 | | | } |
| 258 | | | |
| 259 | | | if (verbose) { |
| 260 | | | for (i = 0; i < in_file_count; i++) |
| 261 | | | fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i], |
| 262 | | | wtap_file_type_string(wtap_file_type(in_files[i].wth))); |
| 263 | | | } |
| 264 | | | |
| 265 | | | if (snaplen == 0) { |
| 266 | | | |
| 267 | | | |
| 268 | | | |
| 269 | | | |
| 270 | | | snaplen = merge_max_snapshot_length(in_file_count, in_files); |
| 271 | | | } |
| 272 | | | |
| 273 | | | |
| 274 | | | if (frame_type == -2) { |
| 275 | | | |
| 276 | | | |
| 277 | | | |
| 278 | | | frame_type = merge_select_frame_type(in_file_count, in_files); |
| 279 | | | if (verbose) { |
| 280 | | | if (frame_type == WTAP_ENCAP_PER_PACKET) { |
| 281 | | | |
| 282 | | | |
| 283 | | | |
| 284 | | | int first_frame_type, this_frame_type; |
| 285 | | | |
| 286 | | | first_frame_type = wtap_file_encap(in_files[0].wth); |
| 287 | | | for (i = 1; i < in_file_count; i++) { |
| 288 | | | this_frame_type = wtap_file_encap(in_files[i].wth); |
| 289 | | | if (first_frame_type != this_frame_type) { |
| 290 | | | fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n"); |
| 291 | | | fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n"); |
| 292 | | | fprintf(stderr, " %s had type %s (%s)\n", |
| 293 | | | in_files[0].filename, |
| 294 | | | wtap_encap_string(first_frame_type), |
| 295 | | | wtap_encap_short_string(first_frame_type)); |
| 296 | | | fprintf(stderr, " %s had type %s (%s)\n", |
| 297 | | | in_files[i].filename, |
| 298 | | | wtap_encap_string(this_frame_type), |
| 299 | | | wtap_encap_short_string(this_frame_type)); |
| 300 | | | break; |
| 301 | | | } |
| 302 | | | } |
| 303 | | | } |
| 304 | | | fprintf(stderr, "mergecap: selected frame_type %s (%s)\n", |
| 305 | | | wtap_encap_string(frame_type), |
| 306 | | | wtap_encap_short_string(frame_type)); |
| 307 | | | } |
| 308 | | | } |
| 309 | | | |
| 310 | | | |
| 311 | | | if (strncmp(out_filename, "-", 2) == 0) { |
| 312 | | | |
| 313 | | | out_fd = 1 ; |
| 314 | | | } else { |
| 315 | | | |
| 316 | | | out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
x /usr/include/bits/fcntl.h |
| |
38 | #define O_CREAT 0100 /* not fcntl */ |
| |
x /usr/include/bits/fcntl.h |
| |
41 | #define O_TRUNC 01000 /* not fcntl */ |
| |
x /home/sate/Testcases/c/cve/wireshark-1.2.0/wsutil/file_util.h |
| |
105 | #define O_BINARY 0 /* Win32 needs the O_BINARY flag for open() */ |
| |
|
| 317 | | | if (out_fd == -1) { |
| 318 | | | fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n", |
| 319 | | | out_filename, strerror(errno)); |
| 320 | | | exit(1); |
| 321 | | | } |
| 322 | | | } |
| 323 | | | |
| 324 | | | |
| 325 | | | pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE , &open_err); |
| 326 | | | if (pdh == NULL) { |
| 327 | | | merge_close_in_files(in_file_count, in_files); |
| 328 | | | g_free(in_files); |
| 329 | | | fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename, |
| 330 | | | wtap_strerror(open_err)); |
| 331 | | | exit(1); |
| 332 | | | } |
| 333 | | | |
| 334 | | | |
| 335 | | | count = 1; |
| 336 | | | for (;;) { |
| 337 | | | if (do_append) |
| 338 | | | wth = merge_append_read_packet(in_file_count, in_files, &read_err, |
| 339 | | | &err_info); |
| 340 | | | else |
| 341 | | | wth = merge_read_packet(in_file_count, in_files, &read_err, |
| 342 | | | &err_info); |
| 343 | | | if (wth == NULL) { |
| 344 | | | if (read_err != 0) |
| 345 | | | got_read_error = TRUE; |
| 346 | | | break; |
| 347 | | | } |
| 348 | | | |
| 349 | | | if (verbose) |
| 350 | | | fprintf(stderr, "Record: %u\n", count++); |
| 351 | | | |
| 352 | | | |
| 353 | | | |
| 354 | | | phdr = wtap_phdr(wth); |
| 355 | | | if (snaplen != 0 && phdr->caplen > snaplen) { |
| 356 | | | snap_phdr = *phdr; |
| 357 | | | snap_phdr.caplen = snaplen; |
| 358 | | | phdr = &snap_phdr; |
| 359 | | | } |
| 360 | | | |
| 361 | | | if (!wtap_dump(pdh, phdr, (wth), |
| 362 | | | wtap_buf_ptr(wth), &write_err)) { |
| 363 | | | got_write_error = TRUE; |
| 364 | | | break; |
| 365 | | | } |
| 366 | | | } |
| 367 | | | |
| 368 | | | merge_close_in_files(in_file_count, in_files); |
| 369 | | | if (!got_read_error && !got_write_error) { |
| 370 | | | if (!wtap_dump_close(pdh, &write_err)) |
| 371 | | | got_write_error = TRUE; |
| 372 | | | } else |
| 373 | | | wtap_dump_close(pdh, &close_err); |
| 374 | | | |
| 375 | | | if (got_read_error) { |
| 376 | | | |
| 377 | | | |
| 378 | | | |
| 379 | | | for (i = 0; i < in_file_count; i++) { |
| 380 | | | if (in_files[i].state == GOT_ERROR) { |
| 381 | | | fprintf(stderr, "mergecap: Error reading %s: %s\n", |
| 382 | | | in_files[i].filename, wtap_strerror(read_err)); |
| 383 | | | switch (read_err) { |
| 384 | | | |
| 385 | | | case WTAP_ERR_UNSUPPORTED: |
| 386 | | | case WTAP_ERR_UNSUPPORTED_ENCAP: |
| 387 | | | case WTAP_ERR_BAD_RECORD: |
| 388 | | | fprintf(stderr, "(%s)\n", err_info); |
| 389 | | | g_free(err_info); |
| 390 | | | break; |
| 391 | | | } |
| 392 | | | } |
| 393 | | | } |
| 394 | | | } |
| 395 | | | |
| 396 | | | if (got_write_error) { |
| 397 | | | fprintf(stderr, "mergecap: Error writing to outfile: %s\n", |
| 398 | | | wtap_strerror(write_err)); |
| 399 | | | } |
| 400 | | | |
| 401 | | | g_free(in_files); |
| 402 | | | |
| 403 | | | return (!got_read_error && !got_write_error) ? 0 : 2; |
| 404 | | | } |
| |