#ifndef ARP_H #define ARP_H #include #include #include #include #include #include #include #include #include #include #include #include #include /* default snap length (maximum bytes per packet to capture) */ #define SNAP_LEN 1518 /* Definitions of codes used in operation field of ARP packet */ #define ARP_AR_REQUEST 1 /* ARP request to resolve address */ #define ARP_AR_REPLY 2 /* reply to a resolve request */ #define ARP_RA_REQUEST 3 /* reverse ARP request (RARP packets) */ #define ARP_RA_REPLY 4 /* reply to a reverse request (RARP ") */ #define ARP_DRARP_REQUEST 5 /* RFC 1931 */ #define ARP_DRARP_REPLY 6 /* RFC 1931 */ #define ARP_DRARP_ERROR 7 /* RFC 1931 */ #define ARP_INARP_REQUEST 8 /* RFC 1293 */ #define ARP_INARP_REPLY 9 /* RFC 1293 */ #define ARP_ARP_NAK 10 /* RFC 1577 */ #define ARP_MARS_REQUEST 11 #define ARP_MARS_MULTI 12 #define ARP_MARS_MSERV 13 #define ARP_MARS_JOIN 14 #define ARP_MARS_LEAVE 15 #define ARP_MARS_NAK 16 #define ARP_MARS_UNSERV 17 #define ARP_MARS_SJOIN 18 #define ARP_MARS_SLEAVE 19 #define ARP_MARS_GROUPLIST_REQUEST 20 #define ARP_MARS_GROUPLIST_REPLY 21 #define ARP_MARS_REDIRECT_MAP 22 #define ARP_MAPOS_UNARP 23 /* RFC 2176 */ #define ARP_OP_EXP1 25 /* RFC 5494 */ #define ARP_OP_EXP2 26 /* definition of MAC and IP address size */ #ifndef ETH_ALEN #define ETH_ALEN 6 #endif #ifndef IP_ALEN #define IP_ALEN 4 #endif /* definition of protocols */ #define HW_TYPE_ETHERNET 0x0001 #define PROTO_TYPE_IP 0x0800 /* ARP Header Structure */ struct arp_header { u_int16_t hw_type; // hardware type u_int16_t proto_type; // protocol type u_int8_t ha_len; // hardware address length u_int8_t pa_len; // protocol address length u_int16_t opcode; // arp opcode u_int8_t source_add[ETH_ALEN]; // source mac unsigned char source_ip[IP_ALEN]; // source ip u_int8_t dest_add[ETH_ALEN]; // destination mac unsigned char dest_ip[IP_ALEN]; // destination ip }; /* Custom struct for signal */ typedef struct { QDateTime time; QString ether_mac_src; QString ether_mac_dst; int arp_operation; QString arp_mac_src; QString arp_ip_src; QString arp_mac_dst; QString arp_ip_dst; } arp_t; class Arp : public QThread { Q_OBJECT public: Arp(); ~Arp(); /* Starts receiving arp packets on device in parameter */ bool start(QString device); /* Stops receiving arp packets */ void stop(); /* Return list of devices on computer */ QStringList getDeviceList(); /* Return true if is receiving packets */ bool isRunning(); /* Send arp packet descreibing in parameter */ bool sendArp(const arp_t &data); /* Copy content of arp_t struct */ static void copyArp_t(arp_t *dst, const arp_t &src); /* Return textual information of arp opcode */ static QString arpOperationtoQString(int arp_operation); /* Return description of the arp_t struct */ static QString arp_tToQString(const arp_t &packet); /* Return MAC address on selected device */ QString mac(); /* Return IP address on selected device */ QString ip(); /* Return IP mask address on selected device */ QString ipMask(); /* Return IP broadcast address on selected device */ QString ipBcast(); /* Return first usable IP address on selected device */ QString ipFrom(); /* Return last usable IP address on selected device */ QString ipTo(); /* Return last error message */ QString lastError(); /* Convert 6 bytes representation of MAC to QString */ static QString macToQString(const u_int8_t *pbyMacAddressInBytes); /* Convert 4 bytes representation of IP to QString */ static QString ipToQString(const unsigned char *pbyIPAddressInBytes); /* Convert QString to 6 bytes representation of MAC */ static u_int8_t* QStringToMac(QString mac); /* Convert QString to 4 bytes representation of IP */ static u_int8_t* QStringToIp(QString ip); signals: /* ARP packet received */ void received(arp_t packet); /* ARP packet with opcode Request received */ void requestArp(arp_t packet); /* ARP packet with opcode Reply received */ void replyArp(arp_t packet); /* ARP packet with opcode Request reverse received */ void requestRArp(arp_t packet); /* ARP packet with opcode Reply reverse received */ void replyRArp(arp_t packet); private: void parsePacket(const struct pcap_pkthdr *header, const u_char *packet); void run(); void fillIfInfo(); pcap_t *handle; struct bpf_program fp; bool terminated; char *device; char *errbuf; unsigned char _ip[IP_ALEN]; unsigned char _ipMask[IP_ALEN]; unsigned char _ipBcast[IP_ALEN]; }; #endif // ARP_H