#ifndef ARP_HPP
#define ARP_HPP
#include <netinet/ether.h>
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <sstream>
#include <algorithm>
#include <iterator>
/*
* Ethernet Address Resolution Protocol.
*
* See RFC 826 for protocol description. Structure below is adapted
* to resolving internet addresses. Field names used correspond to
* RFC 826.
*
struct ether_arp {
struct arphdr ea_hdr; // fixed-size header
u_int8_t arp_sha[ETH_ALEN]; // sender hardware address
u_int8_t arp_spa[4]; // sender protocol address
u_int8_t arp_tha[ETH_ALEN]; // target hardware address
u_int8_t arp_tpa[4]; //target protocol address
};
#define arp_hrd ea_hdr.ar_hrd //Format of hardware address,unsigned short int
#define arp_pro ea_hdr.ar_pro //Format of protocol addres,unsigned short int
#define arp_hln ea_hdr.ar_hln //Length of hardware address,unsigned char
#define arp_pln ea_hdr.ar_pln //Length of protocol address,unsigned char
#define arp_op ea_hdr.ar_op //ARP opcode (command),unsigned short int
// ARP protocol opcodes
#define ARPOP_REQUEST 1 // ARP request
#define ARPOP_REPLY 2 // ARP reply
#define ARPOP_RREQUEST 3 // RARP request
#define ARPOP_RREPLY 4 // RARP reply
#define ARPOP_InREQUEST 8 // InARP request
#define ARPOP_InREPLY 9 // InARP reply
#define ARPOP_NAK 10 // (ATM)ARP NAK
*/
#define get_dest_mac(ptr_arp) get_mac_address(ptr_arp->arp_tha)
#define get_src_mac(ptr_arp) get_mac_address(ptr_arp->arp_sha)
#define get_src_ip(ptr_arp) get_ip_address(ptr_arp->arp_spa)
#define get_target_ip(ptr_arp) get_ip_address(ptr_arp->arp_tpa)
std::string get_mac_address(u_int8_t* ptr)
{
return std::string(ether_ntoa((const struct ether_addr*)(ptr)));
}
std::string get_ip_address(u_int8_t* ptr)
{
std::ostringstream result;
std::copy(ptr, ptr+3, std::ostream_iterator<int>(result, "."));
result<<(int)ptr[3];
return result.str();
}
//arp protocol:from ip adrress get mac address
void del_arp(const u_char* ptr_packet, size_t used_len)
{
struct ether_arp* ptr_arp = (struct ether_arp*)(ptr_packet+used_len);
unsigned int type = ntohs(ptr_arp->arp_op);//network data
std::string src_ip, target_ip,src_mac,target_mac;
if (type == ARPOP_REQUEST)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
target_ip = get_target_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
}
std::cout<<_("arp request:/tsrc ip is:")<<src_ip<<"/tsrc_mac is:"<<src_mac<<"/t target_ip is:"<<target_ip<<std::endl;
}
else if (type == ARPOP_REPLY)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
target_ip = get_target_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
target_mac = get_dest_mac(ptr_arp);
}
std::cout<<"arp reply"<<std::endl;
}
else if (type == ARPOP_InREQUEST)
{
std::cout<<"InARP request"<<std::endl;
}
else if (type == ARPOP_InREPLY)
{
std::cout<<"InARP reply"<<std::endl;
}
else if (type == ARPOP_NAK)
{
std::cout<<"ATM ARP NAK"<<std::endl;
}
else
{
std::cout<<"arp undefined command:"<<type<<std::endl;
return;
}
}
//rarp protocol:from mac address get ipaddress
void del_rarp(const u_char* ptr_packet, size_t used_len)
{
struct ether_arp* ptr_arp = (struct ether_arp*)(ptr_packet+used_len);
unsigned int type = ntohs(ptr_arp->arp_op);
std::string src_ip, target_ip,src_mac,target_mac;
if (type == ARPOP_RREQUEST)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
target_mac = get_dest_mac(ptr_arp);
}
std::cout<<"rarp request"<<std::endl;
}
else if(type == ARPOP_RREPLY)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
target_ip = get_target_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
target_mac = get_dest_mac(ptr_arp);
}
std::cout<<"rarp reply"<<std::endl;
}
else if (type == ARPOP_InREQUEST)
{
std::cout<<"InARP request"<<std::endl;
}
else if (type== ARPOP_InREPLY)
{
std::cout<<"InARP reply"<<std::endl;
}
else if (type == ARPOP_NAK)
{
std::cout<<"ATM RARP NAK"<<std::endl;
}
else
{
std::cout<<"rarp undefined command:"<<type<<std::endl;
return;
}
}
#endif
#define ARP_HPP
#include <netinet/ether.h>
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <sstream>
#include <algorithm>
#include <iterator>
/*
* Ethernet Address Resolution Protocol.
*
* See RFC 826 for protocol description. Structure below is adapted
* to resolving internet addresses. Field names used correspond to
* RFC 826.
*
struct ether_arp {
struct arphdr ea_hdr; // fixed-size header
u_int8_t arp_sha[ETH_ALEN]; // sender hardware address
u_int8_t arp_spa[4]; // sender protocol address
u_int8_t arp_tha[ETH_ALEN]; // target hardware address
u_int8_t arp_tpa[4]; //target protocol address
};
#define arp_hrd ea_hdr.ar_hrd //Format of hardware address,unsigned short int
#define arp_pro ea_hdr.ar_pro //Format of protocol addres,unsigned short int
#define arp_hln ea_hdr.ar_hln //Length of hardware address,unsigned char
#define arp_pln ea_hdr.ar_pln //Length of protocol address,unsigned char
#define arp_op ea_hdr.ar_op //ARP opcode (command),unsigned short int
// ARP protocol opcodes
#define ARPOP_REQUEST 1 // ARP request
#define ARPOP_REPLY 2 // ARP reply
#define ARPOP_RREQUEST 3 // RARP request
#define ARPOP_RREPLY 4 // RARP reply
#define ARPOP_InREQUEST 8 // InARP request
#define ARPOP_InREPLY 9 // InARP reply
#define ARPOP_NAK 10 // (ATM)ARP NAK
*/
#define get_dest_mac(ptr_arp) get_mac_address(ptr_arp->arp_tha)
#define get_src_mac(ptr_arp) get_mac_address(ptr_arp->arp_sha)
#define get_src_ip(ptr_arp) get_ip_address(ptr_arp->arp_spa)
#define get_target_ip(ptr_arp) get_ip_address(ptr_arp->arp_tpa)
std::string get_mac_address(u_int8_t* ptr)
{
return std::string(ether_ntoa((const struct ether_addr*)(ptr)));
}
std::string get_ip_address(u_int8_t* ptr)
{
std::ostringstream result;
std::copy(ptr, ptr+3, std::ostream_iterator<int>(result, "."));
result<<(int)ptr[3];
return result.str();
}
//arp protocol:from ip adrress get mac address
void del_arp(const u_char* ptr_packet, size_t used_len)
{
struct ether_arp* ptr_arp = (struct ether_arp*)(ptr_packet+used_len);
unsigned int type = ntohs(ptr_arp->arp_op);//network data
std::string src_ip, target_ip,src_mac,target_mac;
if (type == ARPOP_REQUEST)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
target_ip = get_target_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
}
std::cout<<_("arp request:/tsrc ip is:")<<src_ip<<"/tsrc_mac is:"<<src_mac<<"/t target_ip is:"<<target_ip<<std::endl;
}
else if (type == ARPOP_REPLY)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
target_ip = get_target_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
target_mac = get_dest_mac(ptr_arp);
}
std::cout<<"arp reply"<<std::endl;
}
else if (type == ARPOP_InREQUEST)
{
std::cout<<"InARP request"<<std::endl;
}
else if (type == ARPOP_InREPLY)
{
std::cout<<"InARP reply"<<std::endl;
}
else if (type == ARPOP_NAK)
{
std::cout<<"ATM ARP NAK"<<std::endl;
}
else
{
std::cout<<"arp undefined command:"<<type<<std::endl;
return;
}
}
//rarp protocol:from mac address get ipaddress
void del_rarp(const u_char* ptr_packet, size_t used_len)
{
struct ether_arp* ptr_arp = (struct ether_arp*)(ptr_packet+used_len);
unsigned int type = ntohs(ptr_arp->arp_op);
std::string src_ip, target_ip,src_mac,target_mac;
if (type == ARPOP_RREQUEST)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
target_mac = get_dest_mac(ptr_arp);
}
std::cout<<"rarp request"<<std::endl;
}
else if(type == ARPOP_RREPLY)
{
if (ntohs(ptr_arp->arp_pro) == ETHERTYPE_IP)//ip
{
src_ip = get_src_ip(ptr_arp);
target_ip = get_target_ip(ptr_arp);
}
if (ntohs(ptr_arp->arp_hrd) == 0x1)//ether
{
src_mac = get_src_mac(ptr_arp);
target_mac = get_dest_mac(ptr_arp);
}
std::cout<<"rarp reply"<<std::endl;
}
else if (type == ARPOP_InREQUEST)
{
std::cout<<"InARP request"<<std::endl;
}
else if (type== ARPOP_InREPLY)
{
std::cout<<"InARP reply"<<std::endl;
}
else if (type == ARPOP_NAK)
{
std::cout<<"ATM RARP NAK"<<std::endl;
}
else
{
std::cout<<"rarp undefined command:"<<type<<std::endl;
return;
}
}
#endif