#include #include #include #include "protocol.h" #include "utility.h" int main(int argc, char ** argv) { char errorstr[100]; // Check command line if (argc <= 1) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } // Open the file given on the command line, exiting on error int fd = open(argv[1], O_RDONLY); if (fd < 0) { sprintf(errorstr, "open() failed on file: %s", argv[1]); perror(errorstr); exit(1); } // Create a buffer large enough for the pcap header and read from file into // buffer. Then use a pointer to a struct to be able to "interpret" the bytes. int hdr_size = sizeof(struct pcap_hdr); char * buffer = malloc(hdr_size); int nread; nread = read(fd, buffer, hdr_size); if (nread < 0) { perror("Failed read() on file"); exit(1); } else if (nread == 0) { fprintf(stderr, "Unexpected end-of-file on read()\n"); exit(1); } else if (nread < hdr_size) { fprintf(stderr, "Unable to get entire header on read()\n"); exit(1); } dumpBytes(stdout, buffer, hdr_size); pcap_hdr_t * pcapHdr = (pcap_hdr_t *)buffer; fprintf(stdout, "Magic: %8.8lx\n", (unsigned long)pcapHdr->magic); // Be careful about aliasing and then freeing the underlying buffer! free(buffer); buffer = NULL; // Repeat above, but for first pcap record in the file int rec_size = sizeof(struct pcap_rec); buffer = malloc(rec_size); nread = read(fd, buffer, rec_size); if (nread < 0) { perror("Failed read() on file"); exit(1); } else if (nread == 0) { fprintf(stderr, "Unexpected end-of-file on read()\n"); exit(1); } else if (nread < rec_size) { fprintf(stderr, "Unable to get entire header on read()\n"); exit(1); } dumpBytes(stdout, buffer, rec_size); return 0; }