Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UTILS
- #define UTILS
- #include <string>
- #include <fstream>
- #include <sstream>
- namespace Utils {
- /**
- * Функция читает 6 байт из файла и возвращает строку с MAC адресом
- * @param file - файл
- * @return строка с MAC адресом
- */
- std::string readMAC(std::fstream &file) {
- char MAC[6];
- file.read(MAC, 6);
- std::ostringstream ss;
- for (int i = 0; i < 6; ++i) {
- if (i != 0) ss << ':';
- ss.width(2); //< Два символа для каждого байта
- ss.fill('0'); //< Дополняем нулем, если не хватает нужного количества символов
- ss << std::hex << (MAC[i] & 0xFF);
- }
- // TODO: FIX IT! Пустой MAC
- if (ss.str() == "00:00:00:00:00:00") return Utils::readMAC(file);
- else return (ss.str());
- }
- /**
- * Функция читает 2 байта из файла и определяет тип кадра
- * 0x0800 - IPv4, 0x8060 - ARP
- * @param file - файл
- * @return строка - тип
- */
- std::string getFrameType(std::fstream &file) {
- char type[2];
- file.read(type, 2);
- if (type[0] == 8 && type[1] == 0) return std::string("IPv4");
- if (type[0] == 8 && type[1] == 6) return std::string("ARP");
- return std::string("Unknown");
- }
- std::string readIP(std::fstream &file) {
- char ip[4];
- file.read(ip, 4);
- std::ostringstream ss;
- for (int i = 0; i < 4; ++i) {
- if (i != 0) ss << '.';
- ss << std::dec << (ip[i] & 0xFF);
- }
- return ss.str();
- }
- /**
- * Функция читает два байта и возвращает число
- * @param file - файл
- * @return целове число
- */
- int readShort(std::fstream &file) {
- char bytes[2];
- file.read(bytes, 2);
- // return (unsigned char)bytes[1] << 0 | (unsigned char)bytes[0] << 8;
- return (unsigned char)bytes[0] << 8 | (unsigned char)bytes[1] << 0;
- }
- /**
- * Функция читает байт и определяет по нему тип пакета
- * 0x01 - ICMP, 0x06 - TCP, 0x17 - UDP
- * @return строка - тип пакета
- */
- std::string getPacketType(std::fstream &file) {
- char byte;
- file.read(&byte, 1);
- switch (byte) {
- case 0x01: return std::string("ICMP");
- case 0x06: return std::string("TCP");
- case 0x11: return std::string("UDP");
- default: return std::string("Unknown");
- }
- }
- }
- #endif //UTILS
- #ifndef PACKETS
- #define PACKETS
- #include <fstream>
- #include <string>
- #include <iostream>
- using std::string;
- using std::cout;
- using std::endl;
- namespace Packets {
- /**
- * Функция парсит IPv4 пакет и выводит его данные
- * @link https://en.wikipedia.org/wiki/IPv4#Header
- * @param file - файл
- */
- void IPv4(std::fstream &file) {
- file.ignore(2); // Пропускаем 2 байта (Version, IHL, DSCP, ECN)
- int packetSize = Utils::readShort(file); // 2 байта - Размер IPv4 пакета
- cout << "IPv4 packet size: " << packetSize << endl;
- file.ignore(5); // Пропускаем 5 байт (Identification, Flags, Fragment Offset, TTL)
- cout << "Packet type: " << Utils::getPacketType(file) << endl; // 1 байт - Протокол
- file.ignore(2); // Пропускаем 2 байта (Header Checksum)
- cout << "Source IP4 : " << Utils::readIP(file) << endl; // 4 байта
- cout << "Destination IP4: " << Utils::readIP(file) << endl; // 4 байта
- file.ignore(packetSize - 20); // Пропускаем данные пакета (размер пакета - 20 байт на Header)
- }
- // Размер ARP пакета меньше 46 байт
- // Но т.к. минимальное значение пакета должно быть 46 байт
- // То пропускаем 46!
- void ARP(std::fstream &file) {
- file.ignore(46);
- }
- }
- #endif //PACKETS
- #include <iostream>
- #include <string>
- #include <fstream>
- using std::string;
- using std::cout;
- using std::endl;
- int main() {
- string filePath = "C:/Users/Alex/Downloads/ethers/ethers08.bin";
- std::fstream file(filePath, std::ios::in | std::ios::binary);
- int frameNumber = 1;
- while (file.good() && !file.eof()) {
- // cout << "File pointer:" << file.tellg() << endl;
- cout << "Frame number: " << frameNumber++ << endl;
- // Читаем два MAC адреса из файла
- cout << "Destination MAC: " << Utils::readMAC(file) << endl;
- cout << "Source MAC: " << Utils::readMAC(file) << endl;
- string frameType = Utils::getFrameType(file);
- // !! string frameDescriptor = Utils::getFrameDescriptor(file);
- cout << "Frame type: " << frameType << endl;
- if (frameType == "IPv4") {
- Packets::IPv4(file);
- } else if (frameType == "ARP") {
- Packets::ARP(file);
- } else if (frameType == "Unknown") {
- // Вернуться на 2 байта назад
- file.seekg(-2, std::ios_base::cur);
- int packetSize = Utils::readShort(file);
- cout << "Packet size: " << packetSize << endl;
- file.seekg(packetSize, std::ios_base::cur);
- }
- // TODO: Legacy code: eof ставится только после попытки чтения
- file.ignore(1);
- if (file.eof()) {
- system("pause");
- return 0;
- }
- file.seekg(-1, std::ios_base::cur);
- cout << endl << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement