八、发送数据包

尽管WinPcap从名字上来看表明他的主要目的是捕获数据包,但是他还为原始网络提供了一些其他的功能,其中

之一就是用户可以发送数据包,这也就是本节的主要内容。需要指出的是原来的libpcap并不提供数据包 的发

送功能,这里所说的功能都是WinPcap的扩展功能,所以并不能够工作在UNIX下。

用pcap_sendpacket来发送一个数据包:

下面的代码是一个最简单的发送数据的方法。打开一个适配器后就可以用 pcap_sendpacket()来手工发送一

个数据包了。这个函数需要的参数:一个装有要发送数据的缓冲区,要发送的长度,和一个适配器。注意缓冲

区中的数据将不被内核协议处理,只是作为最原始的数据流被发送,所以我门必须填充好正确的协议头以便正

确的将数据发送。



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

#include <pcap.h>

void usage();

void main(int argc, char **argv) {
    pcap_t *fp;
    char error[PCAP_ERRBUF_SIZE];
    u_char packet[100];
    int i;

    /* Check the validity of the command line */
    if (argc != 2)
    {
        printf("usage: %s inerface", argv[0]);
        return;
    }

    /* 打开指定网卡 */
if((fp = pcap_open_live(argv[1], 100, 1, 1000, error) ) == NULL)
    {
        fprintf(stderr,"\nError opening adapter: %s\n", error);
        return;
    }

    /* 假设网络环境为ethernet,我门把目的MAC设为1:1:1:1:1:1*/
    packet[0]=1;
    packet[1]=1;
    packet[2]=1;
    packet[3]=1;
    packet[4]=1;
    packet[5]=1;

    /* 假设源MAC为 2:2:2:2:2:2 */
    packet[6]=2;
    packet[7]=2;
    packet[8]=2;
    packet[9]=2;
    packet[10]=2;
    packet[11]=2;

    /* 填充发送包的剩余部分 */
for(i=12;i<100;i++){
        packet=i%256;
    }

    /* 发送包 */
pcap_sendpacket(fp,
        packet,
        100);

    return;
}

发送队列:
pcap_sendpacket()只是提供一个简单的直接的发送数据的方法,而发送队列提供一个高级的强大的和最优的机

制来发送一组数据包,队列实际上是一个装有要发送数据的一个容器,他有一个最大值来表明他所能够 容纳的

最大比特数。
pcap_sendqueue_alloc()用来创建一个队列,并指定该队列的大小。
一旦队列被创建就可以调用pcap_sendqueue_queue()来将数据存储到队列中,这个函数接受一个带有时间戳和

长度的pcap_pkthdr结构和一个装有数据报的缓冲区。这些参数同样也应用于pcap_next_ex() 和

pcap_handler()中,所以给要捕获的数据包或要从文件读取的数据包排队就是pcap_sendqueue_queue()的事情

了。
WinPcap调用pcap_sendqueue_transmit()来发送数据包,注意,第三个参数如果非零,那么发送将是同步的,

这将站用很大的CPU资源,因为发生在内核驱动的同步发送是通过"brute force"loops的,但是一般情况下能够

精确到微秒。
需要指出的是用pcap_sendqueue_transmit()来发送比用pcap_sendpacket()来发送一系列的数据要高效的多,

因为他的数据是在内核级上被缓冲。
当不再需要队列时可以用pcap_sendqueue_destroy()来释放掉所有的队列资源。

下面的代码演示了如何用发送队列来发送数据,该示例用pcap_open_offline()打开了一个文件,然后将数据

从文件移动到已分配的队列,这时就同步地传送队列(如果用户指定为同步的话)。


/*
* Copyright (c) 1999 - 2002
*  Politecnico di Torino.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the Politecnico
* di Torino, and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

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

#include <pcap.h>

void usage();

void main(int argc, char **argv) {
    pcap_t *indesc,*outdesc;
    char error[PCAP_ERRBUF_SIZE];
    FILE *capfile;
    int caplen,
        sync;
    u_int res;
    pcap_send_queue *squeue;
    struct pcap_pkthdr *pktheader;
    u_char *pktdata;

    /* Check the validity of the command line */
    if (argc <= 2 || argc >= 5)
    {
        usage();
        return;
    }

    /* 得到文件长度 */
capfile=fopen(argv[1],"rb");
    if(!capfile){
        printf("Capture file not found!\n");
        return;
    }

    fseek(capfile , 0, SEEK_END);
    caplen= ftell(capfile)- sizeof(struct pcap_file_header);
    fclose(capfile);

    /* 检查确保时间戳被忽略 */
if(argc == 4 && argv[3][0] == 's')
        sync = TRUE;
    else
        sync = FALSE;

    /* Open the capture */
    if((indesc = pcap_open_offline(argv[1], error)) == NULL){
        fprintf(stderr,"\nError opening the input file: %s\n", error);
        return;
    }

    /* Open the output adapter */
    if((outdesc = pcap_open_live(argv[2], 100, 1, 1000, error) ) == NULL)
    {
        fprintf(stderr,"\nError opening adapter: %s\n", error);
        return;
    }

    /* 检测MAC类型 */
if(pcap_datalink(indesc) != pcap_datalink(outdesc)){
        printf("Warning: the datalink of the capture differs from the one of the selected

interface.\n");
        printf("Press a key to continue, or CTRL+C to stop.\n");
        getchar();
    }

    /* 给对列分配空间 */
squeue = pcap_sendqueue_alloc(caplen);

    /* 从文件获得包来填充队列 */
while((res = pcap_next_ex( indesc, &pktheader, &pktdata)) == 1){
        if(pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1){
            printf("Warning: packet buffer too small, not all the packets will be sent.\n");
            break;
        }
    }

    if(res == -1){
        printf("Corrupted input file.\n");
        pcap_sendqueue_destroy(squeue);
        return;
    }

    /* 传送队列数据 */

if((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) < squeue->len)
    {
        printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", error,

res);
    }

    /* free the send queue */
    pcap_sendqueue_destroy(squeue);

    return;
}


void usage()
{

    printf("\nSendcap, sends a libpcap/tcpdump capture file to the net. Copyright (C) 2002 Loris

Degioanni.\n");
    printf("\nUsage:\n");
    printf("\t sendcap file_name adapter [ s ] \n");
    printf("\nParameters:\n");
    printf("\nfile_name: the name of the dump file that will be sent to the network\n");
    printf("\nadapter: the device to use. Use \"WinDump -D\" for a list of valid devices\n");
    printf("\ns: if present, forces the packets to be sent synchronously, i.e. respecting the

timestamps in the dump file. This option will work only under Windows NTx.\n\n");

    exit(0);
}


九、收集并统计网络流量

这一节将展示WinPcap的另一高级功能:收集网络流量的统计信息。WinPcap的统计引擎在内核层次上对到来的数据进行分类。如果你想了解更多的细节请查看NPF驱动指南。
为了利用这个功能来监视网络,我门的程序必须打开一个网卡并用pcap_setmode()将其设置为统计模式。注意pcap_setmode()要用 MODE_STAT来将网卡设置为统计模式。
在统计模式下编写一个程序来监视TCP流量只是几行代码的事情,下面的例子说明了如何来实现该功能的。


/*
* Copyright (c) 1999 - 2002
*  Politecnico di Torino.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the Politecnico
* di Torino, and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

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

#include <pcap.h>

void usage();

void dispatcher_handler(u_char *,
    const struct pcap_pkthdr *, const u_char *);


void main(int argc, char **argv) {
    pcap_t *fp;
    char error[PCAP_ERRBUF_SIZE];
    struct timeval st_ts;
    u_int netmask;
    struct bpf_program fcode;

    /* Check the validity of the command line */
    if (argc != 2)
    {
        usage();
        return;
    }

    /* Open the output adapter */
    if((fp = pcap_open_live(argv[1], 100, 1, 1000, error) ) == NULL)
    {
        fprintf(stderr,"\nError opening adapter: %s\n", error);
        return;
    }

    /* Don't care about netmask, it won't be used for this filter */
    netmask=0xffffff;

    //compile the filter
    if(pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 ){
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
        /* Free the device list */
        return;
    }

    //set the filter
    if(pcap_setfilter(fp, &fcode)<0){
        fprintf(stderr,"\nError setting the filter.\n");
        /* Free the device list */
        return;
    }

    /* 将网卡设置为统计模式 */
pcap_setmode(fp, MODE_STAT);

    printf("TCP traffic summary:\n");

    /* Start the main loop */
    pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);

    return;
}

void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    struct timeval *old_ts = (struct timeval *)state;
    u_int delay;
    LARGE_INTEGER Bps,Pps;
    struct tm *ltime;
    char timestr[16];

    /* 从最近一次的采样以微秒计算延迟时间 */
    /* This value is obtained from the timestamp that the associated with the sample. */
    delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
    /* 获得每秒的比特数 */
Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
    /*                                            ^      ^
                                                  |      |
                                                  |      |
                                                  |      |
                         converts bytes in bits --       |
                                                         |
delay is expressed in microseconds --
*/

    /* 获得每秒的数据包数 */
Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay));

    /* 将时间戳转变为可读的标准格式 */
ltime=localtime(&header->ts.tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

    /* Print timestamp*/
    printf("%s ", timestr);

    /* Print the samples */
    printf("BPS=%I64u ", Bps.QuadPart);
    printf("PPS=%I64u\n", Pps.QuadPart);

    //store current timestamp
    old_ts->tv_sec=header->ts.tv_sec;
    old_ts->tv_usec=header->ts.tv_usec;
}


void usage()
{

    printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
    printf("\nUsage:\n");
    printf("\t tcptop adapter\n");
    printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");

    exit(0);
}

在设置为统计模式前可以设置一个过滤器来指定要捕获的协议包。如果没有设置过滤器那么整个网络数据都将被监视。一旦设置了 过滤器就可以调用pcap_setmode()来设置为统计模式,之后网卡开始工作在统计模式下。
需要指出的是pcap_open_live()的第四个参数(to_ms)定义了采样的间隔,回调函数pcap_loop()每隔一定间隔就获取一次采样统计,这个采样被装入pcap_loop()的第二和第三个参数,过程如下图所示:
______________
|struct timeval ts |                                
|_____________|
|bpf_u_int32       |
|caplen=16          |        struct pcap_pkthdr*
|_____________|         (参数2)
| bpf_u_int32      |
|  len=16             |
|_____________|

__________________________
|large_integer  Accepted packet |
|_________________________|          uchar *
| large_integer  Accepted bits     |          (参数3)
|_________________________|



用两个64位的计数器分别记录最近一次间隔数据包数量和比特数量。
本例子中,网卡打开时设置超时为1000毫秒,也就是说dispatcher_handler()每隔1秒就被调用一次。过滤器也

设置为只监视TCP包,然后pcap_setmode() and pcap_loop()被调用,注意一个指向timeval的指针 作为参数传

送到pcap_loop()。这个timeval结构将用来存储个时间戳以计算两次采样的时间间隔。
dispatcher_handler()用该间隔来获取每秒的比特数和数据包数,并把着两个数显示在显示器上。
最后指出的是目前这个例子是比任何一个利用传统方法在用户层统计的包捕获程序都高效。因为统计模式需要

最小数量的数据拷贝和上下环境交换,同时还有最小的内存需求,所以CPU是最优的。

------------------------
(完)
posted on 2012-04-18 09:48  pulove  阅读(767)  评论(0编辑  收藏  举报