如何使用winpcap如何捕获RTP的包?

解决方案 »

  1.   

    RTP的包是什么?设置一下过滤器,直接进行捕获就可以了
      

  2.   

    这个没法设置过滤器的,
    他是一个实时控制协议,运行在UDP之上的
      

  3.   

    怎么没法设置了?
    pcap可以针对数据偏移内容设置
      

  4.   

    不明白了,请大侠指点
    /* 4 bytes IP address */
    typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
    }ip_address;/* IPv4 header */
    typedef struct ip_header{
    u_char ver_ihl; // Version (4 bits) + Internet header length (4 bits) // add by fxj 4位版本
    u_char tos; // Type of service // add by fxj 8位服务类型
    u_short tlen; // Total length // add by fxj 16位总长度
    u_short identification; // Identification // add by 3位标识
    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 header*/
    typedef struct udp_header{
    u_short sport; // Source port
    u_short dport; // Destination port
    u_short len; // Datagram length
    u_short crc; // Checksum
    }udp_header;
    main()
    {
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int inum;
    int i=0;
    pcap_t *adhandle;
    char errbuf[PCAP_ERRBUF_SIZE];
    u_int netmask;
    char packet_filter[] = "ip and udp";
    // char packet_filter[] = "src: 192.168.100.224";
    struct bpf_program fcode;

        /* 获得设备列表 */
        if (pcap_findalldevs_ex("file:\\", NULL, &alldevs, errbuf) == -1)
        {
            fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
            exit(1);
        }
        
        /* 打印列表 */
        for(d=alldevs; d; d=d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }    if(i==0)
        {
            printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
            return -1;
        }
        
        printf("Enter the interface number (1-%d):",i);
        scanf("%d", &inum);
        
        if(inum < 1 || inum > i)
        {
            printf("\nInterface number out of range.\n");
            /* 释放设备列表 */
            pcap_freealldevs(alldevs);
            return -1;
        }    /* 跳转到已选设备 */
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
        
        /* 打开适配器 */
        if ( (adhandle= pcap_open(d->name,  // 设备名
                                 65536,     // 要捕捉的数据包的部分 
                                //  1,          // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
                                 PCAP_OPENFLAG_NOCAPTURE_LOCAL,         // 混杂模式
                                 1000,      // 读取超时时间
                                 NULL,      // 远程机器验证
                                 errbuf     // 错误缓冲池
                                 ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
            /* 释放设备列表 */
            pcap_freealldevs(alldevs);
            return -1;
        }
        
        /* 检查数据链路层,为了简单,我们只考虑以太网 */
        if(pcap_datalink(adhandle) != DLT_EN10MB)
        {
            fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
            /* 释放设备列表 */
            pcap_freealldevs(alldevs);
            return -1;
        }
        
        if(d->addresses != NULL)
            /* 获得接口第一个地址的掩码 */
            netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
        else
            /* 如果接口没有地址,那么我们假设一个C类的掩码 */
            netmask=0xffffff; 
        //编译过滤器
        if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
        {
            fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
            /* 释放设备列表 */
          //  pcap_freealldevs(alldevs);
            return -1;
        }
        
        //设置过滤器
        if (pcap_setfilter(adhandle, &fcode)<0)
        {
            fprintf(stderr,"\nError setting the filter.\n");
            /* 释放设备列表 */
            pcap_freealldevs(alldevs);
            return -1;
        }
        
        printf("\nlistening on %s...\n", d->description);
        
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        
        /* 开始捕捉 */
        pcap_loop(adhandle, 0, packet_handler, NULL);
        
        return 0;
    }/* 回调函数,当收到每一个数据包时会被libpcap所调用 */
    void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
        struct tm *ltime;
        char timestr[16];
        ip_header *ih;
        udp_header *uh;
        u_int ip_len;
        u_short sport,dport;
        time_t local_tv_sec;

        /* 将时间戳转换成可识别的格式 */
        local_tv_sec = header->ts.tv_sec;
        ltime=localtime(&local_tv_sec);
        strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

        /* 打印数据包的时间戳和长度 */
        printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

        /* 获得IP数据包头部的位置 */
        ih = (ip_header *) (pkt_data +
            14); //以太网头部长度

        /* 获得UDP首部的位置 */
        ip_len = (ih->ver_ihl & 0xf) * 4;
        uh = (udp_header *) ((u_char*)ih + ip_len);

    /* 将网络字节序列转换成主机字节序列 */
        sport = ntohs( uh->sport );
        dport = ntohs( uh->dport );

        /* 打印IP地址和UDP端口 */
        printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
            ih->saddr.byte1,
            ih->saddr.byte2,
            ih->saddr.byte3,
            ih->saddr.byte4,
            sport,
            ih->daddr.byte1,
            ih->daddr.byte2,
            ih->daddr.byte3,
            ih->daddr.byte4,
            dport);
    }这是获得UDP数据包的看看从这里能不能得到RTP的呢?
      

  5.   

    我不知道RTP包是什么,你把pcap过滤器表达式的那章文档好好看下,不难的