Created
October 29, 2016 20:42
-
-
Save hempnall/e300421f4bd57b8f8ccbf4bbf91139b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <iostream> | |
#include <sstream> | |
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; | |
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; | |
std::istream& operator>>(std::istream& istr, const pcaprec_hdr_t& t) { | |
istr.read(( char*) &t, sizeof(pcaprec_hdr_t)); | |
return istr; | |
} | |
std::ostream& operator<<(std::ostream& istr, const pcaprec_hdr_t& t) { | |
istr.write((const char*) &t, sizeof(pcaprec_hdr_t)); | |
return istr; | |
} | |
std::istream& operator>>(std::istream& istr, const pcap_hdr_t& t) { | |
istr.read(( char*) &t, sizeof(pcap_hdr_t)); | |
return istr; | |
} | |
std::ostream& operator<<(std::ostream& istr, const pcap_hdr_t& t) { | |
istr.write((const char*) &t, sizeof(pcap_hdr_t)); | |
return istr; | |
} | |
void grab_record(std::istream& istr, std::ostream& ostr, const pcaprec_hdr_t& rec) { | |
ostr << rec; | |
char* buffer = new char[ rec.incl_len ]; | |
istr.read(buffer, rec.incl_len); | |
ostr.write(buffer, rec.incl_len); | |
delete[] buffer; | |
} | |
int main(int argc , const char** argv) { | |
if (argc != 3) { | |
std::cerr << "wrong number of args\n"; | |
return 1; | |
} | |
int first_packet = std::atoi(argv[1]); | |
int second_packet = std::atoi(argv[2]); | |
if (first_packet >= second_packet) { | |
std::cerr << "packets are wrong\n"; | |
return 1; | |
} | |
pcap_hdr_t header; | |
pcaprec_hdr_t record; | |
pcaprec_hdr_t record1; | |
pcaprec_hdr_t record2; | |
std::cin >> header; | |
std::cout << header; | |
int current_packet=0; | |
std::ostringstream ostrstr; | |
while (std::cin >> record) { | |
if (current_packet == second_packet ) { | |
std::cerr << "swapping packets\n"; | |
grab_record(std::cin , std::cout, record); | |
std::cout << ostrstr.str(); | |
} else if (current_packet >= first_packet && current_packet < second_packet) { | |
std::cerr << "diverting packets\n"; | |
grab_record(std::cin , ostrstr, record); | |
} else { | |
std::cerr << "transferring packets\n"; | |
grab_record(std::cin , std::cout, record); | |
} | |
current_packet++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment