#include #include #include #include #include #include #include void do_header(int in, int out); void do_packets(int in, int out); int do_packet(int in, int out); int main(int argc, char **argv) { int in, out; if(argc != 3) { fputs("Usage: hdlc2en infile outfile\n", stderr); exit(1); } in = open(argv[1], O_RDONLY); if(!in) { perror(argv[1]); exit(1); } out = open(argv[2], O_WRONLY | O_TRUNC, 0660); if(!out) { perror(argv[2]); exit(1); } do_header(in, out); do_packets(in, out); close(in); close(out); } typedef struct pcap_hdr_s { uint32_t magic_number; /* magic number */ uint16_t version_major; /* major version number */ uint16_t version_minor; /* minor version number */ int32_t thiszone; /* GMT to local correction */ uint32_t sigfigs; /* accuracy of timestamps */ uint32_t snaplen; /* max length of captured packets, in octets */ uint32_t network; /* data link type */ } pcap_hdr_t; void do_header(int in, int out) { pcap_hdr_t buf; read(in, &buf, sizeof(buf)); printf("Type: %x\n", buf.network); buf.snaplen += 10; // Increase frame size to ethernet size buf.network = 0x1; // Set type to ethernet write(out, &buf, sizeof(buf)); } typedef struct pcaprec_hdr_s { uint32_t ts_sec; /* timestamp seconds */ uint32_t ts_usec; /* timestamp microseconds */ uint32_t incl_len; /* number of octets of packet saved in file */ uint32_t orig_len; /* actual length of packet */ } pcaprec_hdr_t; char dummy_ethernet[] = { 0x00, 0x16, 0x6f, 0x35, 0xe7, 0xbd, // Destination MAC 0x00, 0x0f, 0x66, 0xe3, 0xfc, 0xa6, // Source MAC 0x08, 0x00 // Protocol Type }; void do_packets(int in, int out) { while(do_packet(in, out)); } int do_packet(int in, int out) { pcaprec_hdr_t hdr; int ret; char *buf; read(in, &hdr, sizeof(hdr)); printf("Packet size: %i\n", hdr.incl_len); if(hdr.incl_len > 1600) { printf("Bad incl_len"); exit(1); } buf = malloc(hdr.incl_len); ret = read(in, buf, hdr.incl_len); hdr.incl_len += 10; hdr.orig_len += 10; write(out, &hdr, sizeof(hdr)); write(out, dummy_ethernet, 14); write(out, buf + 4, hdr.incl_len - 14); // write without HDLC header free(buf); return ret; }