winpcap demo

最近要写一个程序捕获物理层的包,用到了winpcap,使用很简单。
详见:http://www.cnblogs.com/phinecos/archive/2008/10/20/1315176.html

// Demo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#define HAVE_REMOTE
#include <pcap.h>

#define GET_ADAPTER_LIST
#define GET_PACKET
#define SEND_PACKET

#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"Packet.lib")

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef GET_ADAPTER_LIST
    pcap_if_t * allAdapters;//适配器列表
    pcap_if_t * adapter;
    char errorBuffer[ PCAP_ERRBUF_SIZE ];//错误信息缓冲区
    if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, 
                &allAdapters, errorBuffer ) == -1 )
    {//检索机器连接的所有网络适配器
        fprintf( stderr, "Error in pcap_findalldevs_ex function: %s\n", errorBuffer );
        return -1;
    }
    if( allAdapters == NULL )
    {//不存在人任何适配器
        printf( "\nNo adapters found! Make sure WinPcap is installed.\n" );
        return 0;
    }
    int crtAdapter = 0;
    for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)
    {//遍历输入适配器信息(名称和描述信息)
        printf( "\n%d.%s ", ++crtAdapter, adapter->name );
        printf( "-- %s\n", adapter->description );
    }
    printf( "\n" );
    pcap_freealldevs( allAdapters );//释放适配器列表
    system( "PAUSE" );
#elif defined GET_PACKET
    pcap_if_t * allAdapters;//适配器列表
    pcap_if_t * adapter;
    pcap_t           * adapterHandle;//适配器句柄
    struct pcap_pkthdr * packetHeader;
    const u_char       * packetData;
    char errorBuffer[ PCAP_ERRBUF_SIZE ];//错误信息缓冲区
    if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, 
                &allAdapters, errorBuffer ) == -1 )
    {//检索机器连接的所有网络适配器
        fprintf( stderr, "Error in pcap_findalldevs_ex function: %s\n", errorBuffer );
        return -1;
    }
    if( allAdapters == NULL )
    {//不存在任何适配器
        printf( "\nNo adapters found! Make sure WinPcap is installed.\n" );
        return 0;
    }
    int crtAdapter = 0;
    for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)
    {//遍历输入适配器信息(名称和描述信息)
        printf( "\n%d.%s ", ++crtAdapter, adapter->name ); 
        printf( "-- %s\n", adapter->description );
    }
    printf( "\n" );
    //选择要捕获数据包的适配器
    int adapterNumber;
    printf( "Enter the adapter number between 1 and %d:", crtAdapter );
    scanf_s( "%d", &adapterNumber );
    if( adapterNumber < 1 || adapterNumber > crtAdapter )
    {
        printf( "\nAdapter number out of range.\n" );
        // 释放适配器列表
        pcap_freealldevs( allAdapters );
        return -1;
    }
    adapter = allAdapters;
    for( crtAdapter = 0; crtAdapter < adapterNumber - 1; crtAdapter++ )
    adapter = adapter->next;
    // 打开指定适配器
    adapterHandle = pcap_open( adapter->name, // name of the adapter
                               65536,         // portion of the packet to capture
                                              // 65536 guarantees that the whole 
                          // packet will be captured
                               PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
                               1000,             // read timeout - 1 millisecond
                               NULL,          // authentication on the remote machine
                               errorBuffer    // error buffer
                              );
    if( adapterHandle == NULL )
    {//指定适配器打开失败
        fprintf( stderr, "\nUnable to open the adapter\n", adapter->name );
        // 释放适配器列表
        pcap_freealldevs( allAdapters );
        return -1;
    }
    printf( "\nCapture session started on  adapter %s\n", adapter->name );
    pcap_freealldevs( allAdapters );//释放适配器列表
    // 开始捕获数据包
    int retValue;
    while( ( retValue = pcap_next_ex( adapterHandle, 
                      &packetHeader, 
                      &packetData ) ) >= 0 )
    {
        // timeout elapsed if we reach this point
        if( retValue == 0 )
                continue;
        //打印捕获数据包的信息
        printf( "length of packet: %d\n", packetHeader->len );
    }
    // if we get here, there was an error reading the packets
    if( retValue == -1 )
    {
        printf( "Error reading the packets: %s\n", pcap_geterr( adapterHandle ) );
        return -1;
    }
    system( "PAUSE" );
#elif defined SEND_PACKET
    pcap_if_t * allAdapters;//适配器列表
    pcap_if_t * adapter;
    pcap_t           * adapterHandle;//适配器句柄
    u_char         packet[ 20 ]; //待发送的数据封包
    char errorBuffer[ PCAP_ERRBUF_SIZE ];//错误信息缓冲区
    if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, 
                &allAdapters, errorBuffer ) == -1 )
    {//检索机器连接的所有网络适配器
        fprintf( stderr, "Error in pcap_findalldevs_ex function: %s\n", errorBuffer );
        return -1;
    }
    if( allAdapters == NULL )
    {//不存在人任何适配器
        printf( "\nNo adapters found! Make sure WinPcap is installed.\n" );
        return 0;
    }
    int crtAdapter = 0;
    for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)
    {//遍历输入适配器信息(名称和描述信息)
        printf( "\n%d.%s ", ++crtAdapter, adapter->name ); 
        printf( "-- %s\n", adapter->description );
    }
    printf( "\n" );
    //选择适配器
    int adapterNumber;
    printf( "Enter the adapter number between 1 and %d:", crtAdapter );
    scanf_s( "%d", &adapterNumber );
    if( adapterNumber < 1 || adapterNumber > crtAdapter )
    {
        printf( "\nAdapter number out of range.\n" );
        // 释放适配器列表
        pcap_freealldevs( allAdapters );
        return -1;
    }
    adapter = allAdapters;
    for( crtAdapter = 0; crtAdapter < adapterNumber - 1; crtAdapter++ )
    adapter = adapter->next;
    // 打开指定适配器
    adapterHandle = pcap_open( adapter->name, // name of the adapter
                               65536,         // portion of the packet to capture
                                              // 65536 guarantees that the whole 
                          // packet will be captured
                               PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
                               1000,             // read timeout - 1 millisecond
                               NULL,          // authentication on the remote machine
                               errorBuffer    // error buffer
                              );
    if( adapterHandle == NULL )
    {//指定适配器打开失败
        fprintf( stderr, "\nUnable to open the adapter\n", adapter->name );
        // 释放适配器列表
        pcap_freealldevs( allAdapters );
        return -1;
    }
    pcap_freealldevs( allAdapters );//释放适配器列表
    //创建数据封包
    // 设置目标的MAC地址为01 : 01 : 01 : 01 : 01 : 01
    packet[0] = 0x01;
    packet[1] = 0x01;
    packet[2] = 0x01;
    packet[3] = 0x01;
    packet[4] = 0x01;
    packet[5] = 0x01;
    // 设置源的MAC地址为02 : 02 : 02 : 02 : 02 : 02
    packet[6]  = 0x02;
    packet[7]  = 0x02;
    packet[8]  = 0x02;
    packet[9]  = 0x02;
    packet[10] = 0x02;
    packet[11] = 0x02;
    // 设置封包其他部分内容
    for( int index = 12; index < 20; index++ )
    {
        packet[index] = 0xC4;
    }
    //发送数据封包
    if( pcap_sendpacket( adapterHandle, // the adapter handle
             packet, // the packet
             20 // the length of the packet
               ) != 0 )
    {
        fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
        return -1;
    }
    system( "PAUSE" );
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值