我为什么不能通过pcap从外部设备接收GVSP / UDP数据?

问题描述 投票:1回答:1

eBUS播放器通过LAN将UDP数据发送到我的PC。我必须编写一个嗅探此UDP连接并将其保存到文件的程序。

我当前的代码捕获了我网络上的所有内容,包括传入和传出的GigE视觉控制协议(GVCP)。但不是GigE视觉流协议(GVSP)。在Wireshark中,我可以看到两者。

该代码正在统计传入的数据报。我希望这个数字在几秒钟内达到千分之一。我有一些工作代码,可以用十六进制正确打印相同的数据(使用wireshark测试)。

我已经尝试过here中的代码。

有人知道为什么以下代码在我的情况下不起作用吗?

#include "winsock2.h"
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h>
#include <Ws2tcpip.h>
#include <Mstcpip.h>
#include <ctype.h>
#include <pcap.h>

#pragma comment(lib, "Ws2_32.lib")
#pragma warning(disable:4996) 

#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define BUFFSIZE 65535

void packet_counter(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data);

int count = 1;
FILE* fp;
//=========================================================================


/* IPv4 header */
typedef struct ip_hdr {
    u_char ip_header_len:4;   //4 bit Internet header length 
    u_char  ver_ihl:4;        // Version (4 bits) 
    u_char  tos:8;            // Type of service 
    u_short tlen:16;           // Total length 
    u_short identification:16; // Identification
    u_short flags_fo:16;       // Flags (3 bits) + Fragment offset (13 bits)
    u_char  ttl:8;            // Time to live
    u_char  proto:8;          // Protocol
    u_short crc:16;            // Header checksum
    unsigned int saddr : 32; // Source address
    unsigned int  daddr : 32; // Dest address
    u_int   op_pad:32;         // Option + Padding
}IP4_HDR;


//=========================================================================
/* UDP header*/
typedef struct udp_hdr {
    unsigned short source_port : 16; // Source port no.
    unsigned short dest_port : 16; // Dest. port no.
    unsigned short udp_length : 16; // Udp packet length
    unsigned short udp_checksum : 16; // Udp checksum (optional)
} UDP_HDR;
//=========================================================================
int main(int argc, char* argv[]){


    pcap_if_t* alldevs;
    pcap_if_t *d;
    pcap_t* adhandle;
    struct pcap_pkthdr* packetHeader;
    const u_char* packetData;
    struct bpf_program fcode;


    char packet_filter[] = "udp";
    //src net 192.168.200.107 and src port 20202 and

    char szSource[] = PCAP_SRC_IF_STRING;
    char errbuf[PCAP_ERRBUF_SIZE];
    int i=0;
    int j = 1;
    int input;
    int retValue;
    //u_int netmask;
    bpf_u_int32 netmask;        
    bpf_u_int32 netip;           
    //struct in_addr ip_addr;


    //open file
    fp = fopen("log.txt", "w");
    if (fp == NULL)
    {
        printf("Unable to create file.");
        exit(1);
    }

    //find all network devices
    if (pcap_findalldevs_ex(szSource, NULL, &alldevs, errbuf) == -1) {
        printf("Error in pcap_finalldevs_ex: %p\n", errbuf);
        exit(1);
    }


    //list all network devices + description
    for (d = alldevs; d != NULL; 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");
        exit(1);
    }


    while (1) {
        printf("Enter the interface 1-%d : ", i);
        scanf_s("%d", &input);

            if (input < 1 || input > i){
                printf("\nInterface number out of range.\n\n");
            }
            else {
                break;
            }   
    }


    for (d = alldevs, i = 0; i < input - 1; d = d->next, i++);


    if (d == NULL) {
        printf("Err");
        exit(1);
    }



    //--------------------------------open network device for packet capture-----------------------------

    //adhandle = pcap_open(d->name, BUFFSIZE, PCAP_OPENFLAG_PROMISCUOUS, 1,NULL,errbuf);

    adhandle = pcap_open_live(d->name, BUFFSIZE, 0, -1, errbuf);



    if (adhandle == NULL) {

        printf("\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        pcap_freealldevs(alldevs);
        exit(1);
    }

if (pcap_lookupnet(d->name, &netip, &netmask, errbuf) != 0) {
        fprintf(stderr, "\nError finding netmask. Errorcode : %d\n", PCAP_ERROR);
        exit(1);
    }

if (d->addresses != NULL) {
        //printf("%p", d->addresses);
        netmask = ((struct sockaddr_in*)(d->addresses->netmask))->sin_addr.S_un.S_addr ; 
    }
    else {
        netmask = 0xffffff00;
    }


    //compile the filter
    if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) != 0){
        fprintf(stderr, "\nUnable to compile the packet filter. Check the syntax. Errorcode : %d\n", PCAP_ERROR);
        pcap_freealldevs(alldevs);
        return -1;
    }

    //set the filter
    if (pcap_setfilter(adhandle, &fcode) != 0){
        fprintf(stderr, "\nError setting the filter. Errorcode : %d\n",PCAP_ERROR);
        pcap_freealldevs(alldevs);
        return -1;
    }

    pcap_freealldevs(alldevs);


    pcap_loop(adhandle, 0, packet_counter, NULL);//count the data 
    fclose(fp);
    return 0;
}

//=========================================================================
void packet_counter(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data) {
    printf("%d\n", count);
    count++;

}
c udp pcap
1个回答
0
投票

也许有人知道如何读取传入的GVSP数据?不知何故,pcap不起作用,但我知道应该。可能有不同的方式吗?

© www.soinside.com 2019 - 2024. All rights reserved.