#include
#include
#include
#define SNAP_LEN 65536
static char g_ftag[6] = {0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE};
#define dPrint(fmt, args...) printf("[%s:%d] "fmt"\r\n", __FUNCTION__, __LINE__, ##args)
#define HexPrint(_buf, _len) \
{\
int _m_i = 0;\
char *_m_buf = (char *)(_buf);\
int _m_len = (int)(_len);\
printf("[%s:%d] \r\n", __FUNCTION__, __LINE__);\
printf("*****************************\n");\
for(_m_i = 0; _m_i < _m_len; _m_i++)\
{\
printf("\033[32m%02x \033[0m", _m_buf[_m_i] & 0xff);\
if(!((_m_i+1) % 10)) printf("\n");\
}\
printf("\nsize = %d\n*****************************\n", _m_len);\
}
int send_pkt_pcap(const u_char *pkt, int len)
{
static pcap_t *handle = NULL;
char dev_str[64];
char err_buf[PCAP_ERRBUF_SIZE];
strcpy(dev_str, "linux_dal");
if(NULL == handle)
{
handle = pcap_open_live(dev_str, SNAP_LEN, 1, 1000, err_buf);
if (handle == NULL)
{
fprintf(stderr, "Couldn't open device %s: %s\n", dev_str, err_buf);
return 0;
}
}
return pcap_sendpacket(handle, pkt, len);
}
void dispatcher_handler(u_char *temp1, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
HexPrint((const char *)pkt_data, header->caplen);
u_char pkt_send[header->caplen + sizeof(g_ftag)];
memset(pkt_send, 0x00, sizeof(pkt_send));
memcpy(pkt_send, g_ftag, sizeof(g_ftag));
memcpy(pkt_send + sizeof(g_ftag), pkt_data, header->caplen);
HexPrint((const char *)pkt_send, sizeof(pkt_send));
dPrint("Send ret : %d", send_pkt_pcap((const u_char *)pkt_send, sizeof(pkt_send)));
}
int main()
{
pcap_t *handle;
char err_buf[PCAP_ERRBUF_SIZE];
char dev_str[64];
struct bpf_program fp;
strcpy(dev_str, "ens33");
handle = pcap_open_live(dev_str, SNAP_LEN, 1, 1000, err_buf);
if (handle == NULL)
{
fprintf(stderr, "Couldn't open device %s: %s\n", dev_str, err_buf);
return 0;
}
if (pcap_datalink(handle) != DLT_EN10MB)
{
fprintf(stderr, "%s is not an Ethernet\n", dev_str);
return 0;
}
if (pcap_compile(handle, &fp, "src host 192.168.245.1 and icmp", 0, 24) == -1)
{
fprintf(stderr, "Couldn't parse filter %s: %s\n",
"***", pcap_geterr(handle));
return 0;
}
if (pcap_setfilter(handle, &fp) == -1)
{
fprintf(stderr, "Couldn't install filter %s: %s\n",
"***", pcap_geterr(handle));
return 0;
}
pcap_loop(handle, -1, dispatcher_handler, NULL);
pcap_freecode(&fp);
pcap_close(handle);
dPrint("Capture complete.");
return 0;
}