Why do you say the good program doesn't work on my HiSilicon development board? Is it really a character problem?

As for the following programs, a packet capture program using libpcap and a packet capture program using raw socket

The HiSilicon development board is mapped, and the Polycom mcu rmx1800 is mapped. The terminal is currently using a soft terminal, and the hardware terminal is still on the way. There should be no difference between software and hardware.

The current situation is that the video data sent by the mcu to the terminal can be captured, but the part of the data sent by the terminal to the mcu, and the data sent by the device under the switch to 255.255.255.255 can not be captured.

Did I make it clear what I meant by saying this?

It's okay for libpcap to refer to this, because I wrote it with reference to this

http://www.oschina.net/code/snippet_2323523_51192

The code is as follows, there are two ways


int do_promisc(char *nif,int sock)
{
    struct ifreq ifr;


    strncpy(ifr.ifr_name,nif,strlen(nif)+1);


    if(ioctl(sock,SIOCGIFFLAGS,&ifr)==-1)
    {
        perror("ioctl");
        exit(2);
    }


    ifr.ifr_flags |= IFF_PROMISC;
    if(ioctl(sock, SIOCSIFFLAGS, &ifr)==-1)
    {
        perror("ioctl");
        exit(3);
    }


    if(ioctl(sock, SIOCGIFHWADDR, &ifr) < 0 ) {
        perror("failed to get interface hw address!" );
        return -1;
    }
    if(ioctl(sock, SIOCGIFINDEX, &ifr) < 0 )  {
        perror("failed to get interface index!" );
        return -1;
    }
    return 0;
}

u_char buf_rec[40960];//buffer to recev data

int  main()
{
#if 1
pcap_t *pcap_handle;
char error_content[PCAP_ERRBUF_SIZE];
char *net_interface;
struct bpf_program bpf_filter;
int dlt;
char bpf_filter_string[]="";
///Entry monitoring success parameters
//char bpf_filter_string[]="udp dst port 3232 and src host 192.168.2.181";
//char bpf_filter_string[]="src port 3232";// and src port 3232";
bpf_u_int32 ip, mask;
net_interface=pcap_lookupdev(error_content);
if(net_interface == NULL){ 
        printf("cannot get  the network device info: %s\n", error_content); 
        return -1; 
    } 
    printf("the net_interface is : %s\n", net_interface);
//The second parameter of the function pcap_lookupnet, the network number is not obtained instead of ip
if(pcap_lookupnet(net_interface,&ip,&mask,error_content) == -1)
{
printf("error\n"); 
        return 1; 
}


struct in_addr addr;
char *ip_addr, *net_mask;
addr.s_addr = ip; 
    ip_addr = inet_ntoa(addr);
if(ip_addr == NULL)

        printf("inet_ntoa()\n"); 
        return -1; 
    } 
 
printf("ip is (%d)%s ,", ip, ip_addr);
addr.s_addr = mask; 
    net_mask = inet_ntoa(addr); 
    if(net_mask == NULL)

        perror("inet_ntoa();\n"); 
        return -1; 
    } 
     
    printf("mask is %s\n", net_mask);

pcap_handle=pcap_open_live(net_interface,65535,1,1000,error_content);
//set filter condition
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,1,mask);


pcap_setfilter(pcap_handle,&bpf_filter);


dlt = pcap_datalink(pcap_handle);
//if(pcap_datalink(pcap_handle)!=DLT_EN10MB)
// return -1;
const char *dlt_name = pcap_datalink_val_to_name(dlt);
if (dlt_name == NULL) {
(void)fprintf(stderr, "listening on %s, link-type %u\n",
   net_interface, dlt);
} else {
(void)fprintf(stderr, "listening on %s, link-type %s (%s), \n",
   net_interface, dlt_name,
   pcap_datalink_val_to_description(dlt));
}
fflush(stderr);

InitAddrList();
HI_U32 ret=SAMPLE_VDEC_Process( PIC_HD1080, PT_H264, 4, SAMPLE_VO_DEV_DHD0);
if(HI_SUCCESS !=ret)
{
SAMPLE_COMM_SYS_Exit();
}
pcap_loop(pcap_handle,-1,ethernet_protocol_packet_callback,NULL);
pcap_close(pcap_handle);
//pcap_stats(pcap_t *,struct pcap_stat *);


#else
struct sockaddr_in addr;
// time_t tw;
  int i,sockfd,r,str_size;
unsigned int len;
  unsigned char *ptemp;
  //struct tm *now;
struct timeval tvs;
    struct timeval tve;
struct ether_header *peth; 
struct ip_header *pip;   
struct tcp_header *ptcp; 
struct udp_header *pudp;


signal(SIGPIPE,SIG_IGN);


  //get socket
  //socket(PF_PACKET, SOCK_RAW|SOCK_DGRAM, htons(ETH_P_IP|ETH_P_ARP|ETH_P_ALL))
  if((sockfd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))==-1)
  {
  perror("socket");
  exit(1);
  }


  do_promisc("eth0",sockfd);//change the netcart to promisc mod
  //system("ifconfig");
  //InitAddrList();
  //fes = fopen("rtp.g711u","wb");
//fclose(fes);
  //recev messages and write log
  while(1){
fd_set rfds;
        struct timeval tv;
        int retval;


gettimeofday (&tve, NULL);
//printf("allbeatHeart\n", tve.tv_sec, tvs.tv_sec);


        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET(sockfd, &rfds);


        /* Wait up to five seconds. */
        tv.tv_sec = 0;
        tv.tv_usec = 500000;


        retval = select(sockfd+1, &rfds, NULL, NULL, &tv);
        //printf("%d\n", retval);
        if (retval == -1){
//printf("select -1\n");
            continue;
        }else if (retval==0){
//printf("recvfrom timeout\n");
            continue;
}


  len = sizeof(addr);
  if((r = recvfrom(sockfd,(u_char*)buf_rec,sizeof(buf_rec),0,(struct sockaddr*)&addr,&len))==-1)
  {
    perror("recvfrom");
    continue;
  }
buf_rec[r] = 0;
ptemp = buf_rec;
peth = (struct ether_header *)ptemp;
ptemp += sizeof(struct ether_header); //The pointer is moved back by the length of the eth header
pip = (struct ip_header*)ptemp; //pip points to the header of the ip layer
ptemp += sizeof(struct ip_header);//The pointer moves backward by the length of the ip header

switch(pip->ip_protocol) //Determine the pointer type according to different protocols
{


case 6://tcp
printf("tcp");
ptcp = (struct tcp_header*)ptemp; //ptcp points to the tcp header
        //printf("TCP pkt :FORM:[%s]:[%d] ",inet_ntoa(*(struct in_addr*)&(pip->ip_source_address)),ntohs(ptcp->source)); 
       // printf("TCP pkt :TO:[%s]:[%d] ",inet_ntoa(*(struct in_addr*)&(pip->daddr)),ntohs(ptcp->dest));
//tcp_protocol_packet_callback(argument,packet_header,packet_content);
break;
case 17://udp
printf("udp");
pudp = (struct udp_header*)ptemp;

//udp_protocol_packet_callback(argument,packet_header,packet_content, id);
break;
case 1://icmp
default:
printf("other %d\n", pip->ip_protocol);
break;




}

#if 1
printf("source address :%d.%d.%d.%d ",pip->ip_source_address.byte1,
pip->ip_source_address.byte2, pip->ip_source_address.byte3, pip->ip_source_address.byte4);
printf("destination address :%d.%d.%d.%d\n",pip->ip_destination_address.byte1, pip->ip_destination_address.byte2,
pip->ip_destination_address.byte3, pip->ip_destination_address.byte4);
#endif
  //ethernet_protocol_packet_callback(NULL, NULL, buf_rec);
  }


return 0;
#endif


return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325905305&siteId=291194637