winpcap +vs2013 抓包程序

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>


char *iptos(u_long in);       //u_long即为 unsigned long
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
//struct tm *ltime;                    //和时间处理有关的变量

/* 4字节的IP地址 */
typedef struct ip_address
{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
} ip_address;

/* IPv4 首部 */
typedef struct ip_header
{
    u_char  ver_ihl;        // 版本 (4 bits) + 首部长度 (4 bits)
    u_char  tos;            // 服务类型(Type of service)
    u_short tlen;           // 总长(Total length)
    u_short identification; // 标识(Identification)
    u_short flags_fo;       // 标志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
    u_char  ttl;            // 存活时间(Time to live)
    u_char  proto;          // 协议(Protocol)
    u_short crc;            // 首部校验和(Header checksum)
    ip_address  saddr;      // 源地址(Source address)
    ip_address  daddr;      // 目的地址(Destination address)
    u_int   op_pad;         // 选项与填充(Option + Padding)
} ip_header;

/* UDP 首部*/
typedef struct udp_header
{
    u_short sport;          // 源端口(Source port)
    u_short dport;          // 目的端口(Destination port)
    u_short len;            // UDP数据包长度(Datagram length)
    u_short crc;            // 校验和(Checksum)
} udp_header;

int main(){

    pcap_if_t  * alldevs;       //所有网络适配器
    pcap_if_t  *d;                    //选中的网络适配器
    char errbuf[PCAP_ERRBUF_SIZE];   //错误缓冲区,大小为256
    char source[PCAP_ERRBUF_SIZE];
    pcap_t *adhandle;           //捕捉实例,是pcap_open返回的对象
    int i = 0;                            //适配器计数变量
    struct pcap_pkthdr *header;    //接收到的数据包的头部
    const u_char *pkt_data;              //接收到的数据包的内容
    int res;                                    //表示是否接收到了数据包
    u_int netmask;                       //过滤时用的子网掩码
    char packet_filter[] = "ip and udp";        //过滤字符
    struct bpf_program fcode;                     //pcap_compile所调用的结构体
    ip_header *ih;                                    //ip头部
    udp_header *uh;                                //udp头部
    u_int ip_len;                                       //ip地址有效长度
    u_short sport, dport;                  

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值