您的位置:首页 > 其它

为什么你们说好的程序在我的海思开发板上就是不行呢,难道真的是人品有问题

2017-05-10 16:45 609 查看
就下面的程序,一个利用libpcap做的抓包程序,一个用raw socket 做的抓包程序

海思开发板 是通过映射过来的,被映射的是宝利通mcu rmx1800,终端目前用的是软终端,硬件终端还在路上,软硬应该没区别的

现在的情况是,可以抓取到mcu发给终端的视频数据,但是抓不到终端发给mcu的这部分数据,还有能抓到交换机下设备发往255.255.255.255 的数据

我这么说是否已经将我的意思表达清楚了?

libpcap参考这个也行,因为我就是参考这个写的

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

代码如下,两种方式的 都有

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[]="";
///入口监控成功参数
//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);
//函数pcap_lookupnet的第二个参数,获得的不是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);
//设置过滤条件
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); //指针后移eth头的长度
pip = (struct ip_header*)ptemp; //pip指向ip层的包头
ptemp += sizeof(struct ip_header);//指针后移ip头的长度

switch(pip->ip_protocol)   //根据不同协议判断指针类型
{

case 6://tcp
printf("tcp");
ptcp = (struct tcp_header*)ptemp;       //ptcp指向tcp头部
        //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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐