#include <stdio.h>
#include <pcap.h>
#include <time.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/if_arp.h>
#include <net/ethernet.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#define SIZE_ETHERNET 14
#define SNAP_LEN 1518
#define ETHER_ADDR_LEN 6
#define ETH_HDRLEN 14
#define IP4_HDRLEN 20
#define UDP_HDRLEN 8
/* Ethernet header */
struct sniff_ethernet
{
u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
u_short ether_type; /* IP? ARP? RARP? etc */
};
/* IP header */
struct sniff_ip
{
u_char ip_vhl; /* version << 4 | header length >> 2 */
u_char ip_tos; /* type of service */
u_short ip_len; /* total length */
u_short ip_id; /* identification */
u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src,ip_dst; /* source and dest address */
};
#define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
#define IP_V(ip) (((ip)->ip_vhl) >> 4)
/* TCP header */
typedef u_int tcp_seq;
struct sniff_tcp
{
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
tcp_seq th_seq; /* sequence number */
tcp_seq th_ack; /* acknowledgement number */
u_char th_offx2; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
u_short th_win; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
};
struct sniff_udp
{
u_int16_t source;
u_int16_t dest;
u_int16_t len;
u_int16_t check;
};
int getPacket(u_char * arg, struct pcap_pkthdr * pkthdr,u_char * packet);
void print_payload( u_char *payload, int len);
uint16_t checksum (uint16_t *addr, int len);
//unsigned short checksum(unsigned short* buffer, int size);//for checksum
uint16_t udp4_checksum (struct sniff_ip *iphdr, struct sniff_udp *udphdr, uint8_t *payload, int payloadlen);
static char *dev;
static char eth_source[ETH_ALEN]={0x00,0x0C,0x43,0x30,0x50,0x77};//板子 MAC
//static char eth_dest[ETH_ALEN]={0x10,0x60,0x4B,0x86,0x45,0x9F}; //gongyong MAC
static char eth_dest[ETH_ALEN]={0xC8,0x1F,0x66,0x06,0x56,0xD7}; //my MAC
//static char eth_dest[ETH_ALEN]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; //broadcast MAC
int main()
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
struct bpf_program fp;
char filter_exp[] = "src host 192.168.10.13 or src host 192.168.0.13";
// char filter_exp[] = "udp dst port 6000";
int id = 0;
bpf_u_int32 mask;
bpf_u_int32 net;
struct pcap_pkthdr header;
dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{
printf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
printf("look up dev\n");
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1)
{
printf(stderr, "Couldn't get mask for device %s\n", dev, errbuf);
net = 0;
mask = 0;
}
handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
if (handle == NULL)
{
printf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
if (pcap_compile(handle, &fp, filter_exp, 1, net) == -1)
{
printf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
if (pcap_setfilter(handle, &fp) == -1)
{
printf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
pcap_loop(handle, -1, getPacket, (u_char*)&id);
pcap_freecode(&fp);
pcap_close(handle);
printf("\n Capturn complete.\n");
return(0);
}
int getPacket(u_char * arg, struct pcap_pkthdr * pkthdr, u_char * packet)
{
int * id =(int *)arg;
int i;
struct sniff_ethernet *ethernet; /* The ethernet header */
struct sniff_ip *ip;
struct sniff_tcp *tcp;
struct sniff_udp *udp;
char *payload;
int size_ip, size_tcp, dst_PORT;
int size_payload;
char *dst_IP;
int size_packet;
//uint8_t *data = (uint8_t *)malloc( 1500 * sizeof(uint8_t));
printf("*******************************************\n");
printf("id: %d\n",++(*id));
printf("Packet length: %d\n", pkthdr->len);
printf("Number of bytes: %d\n", pkthdr->caplen);
printf("Recieved time: %s", ctime((const time_t *)&pkthdr->ts.tv_sec));
ethernet = (struct sniff_ethernet*)(packet);
fprintf(stdout, "MAC src: %s\n", ether_ntoa(ethernet->ether_shost));
fprintf(stdout, "MAC dst: %s\n", ether_ntoa(ethernet->ether_dhost));
memcpy(ethernet->ether_shost, eth_source, ETHER_ADDR_LEN);
memcpy(ethernet->ether_dhost, eth_dest, ETHER_ADDR_LEN);
ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
size_packet = ntohs(ip->ip_len) + 14;
printf("size_packet = %d\n",size_packet);
size_ip = IP_HL(ip) * 4;
if (size_ip < 20)
{
printf(" * Invilid IP header length: %u bytes\n", size_ip);
return;
}
printf(" From: %s\n", inet_ntoa(ip->ip_src));
printf(" To: %s\n", inet_ntoa(ip->ip_dst));
dst_IP = inet_ntoa(ip->ip_dst);
ip->ip_src.s_addr = inet_addr("192.168.0.1"); //板子 IP地址
ip->ip_dst.s_addr = inet_addr("192.168.10.13");//PC2 IP地址
//ip->ip_ttl = 64;
printf(" IP checksun: %0x\n", ip->ip_sum);
ip->ip_sum = 0;
ip->ip_sum = checksum((uint16_t *)ip,IP4_HDRLEN);
//ip1->ip_sum =checksum((unsigned short*)ip1, IP4_HDRLEN);
printf(" IP new checksun: %0x\n", ip->ip_sum);
printf(" From1: %s\n", inet_ntoa(ip->ip_src));
printf(" To1: %s\n", inet_ntoa(ip->ip_dst));
/* determine protocol */
switch(ip->ip_p)
{
case IPPROTO_TCP:
printf(" Protocol: TCP\n");
return;
case IPPROTO_UDP:
printf(" Protocol: UDP\n");
break;
case IPPROTO_ICMP:
printf(" Protocol: ICMP\n");
return;
case IPPROTO_IP:
printf(" Protocol: IP\n");
return;
default:
printf(" Protocol: unknown\n");
return;
}
/* tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
size_tcp = TH_OFF(tcp) * 4;
if (size_tcp < 20)
{
printf(" * Invilid TCP header length: %u bytes\n", size_tcp);
return;
}
printf(" Src port: %d\n", ntohs(tcp->th_sport));
printf(" Dst port: %d\n", ntohs(tcp->th_dport));
payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp);
size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp); */
udp = (struct sniff_udp*)(packet + SIZE_ETHERNET + size_ip);
printf(" Src port: %d\n", ntohs(udp->source));
printf(" Dst port: %d\n", ntohs(udp->dest));
dst_PORT = ntohs(udp->dest);
payload = (u_char *)
libpcap_sendpacket.zip_libpcap_libpcap_sendpacket_libpcap修改_send
版权申诉
5星 · 超过95%的资源 143 浏览量
2022-07-14
08:39:29
上传
评论 1
收藏 4KB ZIP 举报
JaniceLu
- 粉丝: 78
- 资源: 1万+
最新资源
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- 基于JSP毕业设计-基于WEB操作系统课程教学网站的设计与实现(源代码+论文).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈