Timer 需要关于数据包捕获程序的帮助吗

Timer 需要关于数据包捕获程序的帮助吗,timer,packet-capture,packet-sniffers,file-writing,Timer,Packet Capture,Packet Sniffers,File Writing,以下程序捕获TCP数据包,并在控制台中为每个数据包打印与报头相关的信息。我还包括一个计时器,以便在每1000毫秒(即1秒)之后,各种标志的出现频率以及遇到的Src IP、Ack nos和Seq nos的不同数量都写入一个文件。我在fedora core 5工作。我遇到以下问题: 1.文件写入部分在某些执行期间工作正常,但在其他大多数情况下,在同一台机器中,文件根本不写入 2.当我在家里执行这个程序时,每秒大约捕获30个数据包。但当我在实验室运行相同的程序时,每秒只捕获1个数据包。(尽管我在这两个

以下程序捕获TCP数据包,并在控制台中为每个数据包打印与报头相关的信息。我还包括一个计时器,以便在每1000毫秒(即1秒)之后,各种标志的出现频率以及遇到的Src IP、Ack nos和Seq nos的不同数量都写入一个文件。我在fedora core 5工作。我遇到以下问题:

1.文件写入部分在某些执行期间工作正常,但在其他大多数情况下,在同一台机器中,文件根本不写入

2.当我在家里执行这个程序时,每秒大约捕获30个数据包。但当我在实验室运行相同的程序时,每秒只捕获1个数据包。(尽管我在这两个地方浏览的次数相同)

#定义间隔1000/*要关闭的毫秒数*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括//用于设置项
#包含//用于暂停
#包括/*用于信号*/
/*默认快照长度(要捕获的每个数据包的最大字节数)*/
#定义SNAP_LEN 1518
/*以太网报头始终正好是14个字节[1]*/
#定义以太网14的大小
/*以太网地址为6字节*/
#定义以太地址列6
/*以太网报头*/
结构嗅探以太网{
u_char ether_dhost[以太地址];/*目标主机地址*/
u_char ether_shost[以太地址];/*源主机地址*/
短以太类型;/*IP?ARP?RARP?等*/
};
/*IP报头*/
结构嗅探ip{
u_char ip_vhl;/*版本>2*/
u_char ip_tos;/*服务类型*/
u_短ip_len;/*总长度*/
u_短ip_id;/*标识*/
u_短ip_关闭;/*碎片偏移字段*/
#定义IP_RF 0x8000/*保留片段标志*/
#定义IP_DF 0x4000/*不分段标志*/
#定义IP_MF 0x2000/*更多片段标志*/
#为碎片位定义IP_OFFMASK 0x1FF/*掩码*/
u_char ip_ttl;/*生存时间*/
u_char ip_p;/*协议*/
u_short ip_sum;/*校验和*/
结构地址ip地址src,ip地址;/*源地址和目标地址*/
};
#定义IP_HL(IP)((IP)->IP_vhl)和0x0f)
#定义IP_V(IP)((IP)->IP_vhl)>>4)
/*TCP报头*/
typedef u_int tcp_seq;
结构嗅探\u tcp{
u_short th_sport;/*源端口*/
u_short th_dport;/*目标端口*/
tcp_seq th_seq;/*序列号*/
tcp_seq th_ack;/*确认号*/
u_char th_offx2;/*数据偏移量,rsvd*/
#定义TH OFF(TH)(((TH)->TH OFF2和0xf0)>>4)
u_char th_标志;
#定义THU FIN 0x01
#定义THU SYN 0x02
#定义第一个0x04
#定义推送0x08
#定义THU ACK 0x10
#定义THU URG 0x20
#定义THU ECE 0x40
#定义TH_CWR 0x80
#定义标志(THU FIN | TH SYN | TH RST | TH ACK | TH URG | TH ECE | TH CWR)
u_short_win;/*窗口*/
u_short th_sum;/*校验和*/
u_short th_urp;/*紧急指针*/
};
u_short sport[100];int spd=0;
u_int seq[100];int-seqd=0;
u_short win[100];int风=0;
文件*urlfile;
int count=1,flag=0,t=0;
浮点数sc=0,ac=0,fc=0,pc=0,uc=0,rc=0;
无效的
got_数据包(u_char*args,const struct pcap_pkthdr*头,const u_char*packet);
无效的
打印有效载荷(常量字符*有效载荷,整数长度);
无效的
打印十六进制ascii行(常量字符*有效载荷、整数长度、整数偏移量);
无效的
打印应用程序使用情况(无效);
空隙度凝灰岩(空隙);
空隙度凝灰岩(空隙){
t++;
现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场#####\n“,t);
//fprintf(urlfile,“\n hi hi”);
fprintf(urlfile,“\n时间%d 1:%f 2:%f 3:%f 4:%f 5:%f 6:%f 7:%f 8:%f 9:%f”、t、sc/count、ac/count、fc/count、pc/count、uc/count、rc/count、(浮动)spd/count、(浮动)风/计数、(浮动)seqd/count);
printf(“\n时间%d 1:%f 2:%f 3:%f 4:%f 5:%f 6:%f 7:%f 8:%f 9:%f”,t,sc/count,ac/count,fc/count,pc/count,uc/count,rc/count,(float)spd/count,(float)wind/count,(float)seqd/count);
printf(“\n a\u计数:%f,数据包总数:%d,频率:%f”,ac,count,ac/count);
printf(“\n r\u计数:%f,数据包总数:%d,频率:%f”,rc,count,rc/count);
printf(“\n p\u计数:%f,数据包总数:%d,频率:%f”,pc,计数,pc/计数);
printf(“\n存储单元计数:%f,数据包总数:%d,频率:%f”,sc,count,sc/count);
printf(“\n u\u计数:%f,数据包总数:%d,频率:%f”,uc,计数,uc/计数);
printf(“\n f\u计数:%f,数据包总数:%d,频率:%f”,fc,计数,fc/计数);
printf(“\n不同序号的计数:%d编号/p计数:%f”,序号(浮动)序号/p计数);
printf(“\n不同运动的计数:%d个/p计数:%f”,spd,(float)spd/count);
printf(“\n不同赢号码的计数:%d编号/p计数:%f\n\n”,风,(浮)风/计数);
ac=rc=pc=fc=sc=uc=0;计数=1;
spd=seqd=wind=0;
}
无效的
打印应用程序使用情况(无效)
{
printf(“用法:./a.out[接口]\n”);
printf(“\n”);
printf(“选项:\n”);
printf(“接口侦听数据包。\n”);
printf(“\n”);
返回;
}
/*
*剖析/打印数据包
*/
无效的
got_数据包(u_char*args,const struct pcap_pkthdr*头,const u_char*packet)
{
/*包计数器*/
int j;
/*声明指向数据包头的指针*/
const struct sniff_ethernet*以太网;/*以太网头[1]*/
const struct sniff_ip*ip;/*ip头*/
const struct sniff_tcp*tcp;/*tcp头*/
常量字符*有效负载;/*数据包有效负载*/
int size_ip;
int-size_-tcp;
#define INTERVAL 1000        /* number of milliseconds to go off */

#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h> // for setitimer
#include<unistd.h>    // for pause
#include <signal.h>     /* for signal */

/* default snap length (maximum bytes per packet to capture) */
#define SNAP_LEN 1518

/* ethernet headers are always exactly 14 bytes [1] */
#define SIZE_ETHERNET 14

/* Ethernet addresses are 6 bytes */
#define ETHER_ADDR_LEN  6

/* Ethernet header */
struct sniff_ethernet {
        u_char  ether_dhost[ETHER_ADDR_LEN];    /* destination host address */
        u_char  ether_shost[ETHER_ADDR_LEN];    /* source host address */
        u_short ether_type;                     /* IP? ARP? RARP? etc */
};

/* IP header */
struct sniff_ip {
        u_char  ip_vhl;                 /* version << 4 | header length >> 2 */
        u_char  ip_tos;                 /* type of service */
        u_short ip_len;                 /* total length */
        u_short ip_id;                  /* identification */
        u_short ip_off;                 /* fragment offset field */
        #define IP_RF 0x8000            /* reserved fragment flag */
        #define IP_DF 0x4000            /* dont fragment flag */
        #define IP_MF 0x2000            /* more fragments flag */
        #define IP_OFFMASK 0x1fff       /* mask for fragmenting bits */
        u_char  ip_ttl;                 /* time to live */
        u_char  ip_p;                   /* protocol */
        u_short ip_sum;                 /* checksum */
        struct  in_addr ip_src,ip_dst;  /* source and dest address */
};
#define IP_HL(ip)               (((ip)->ip_vhl) & 0x0f)
#define IP_V(ip)                (((ip)->ip_vhl) >> 4)

/* TCP header */
typedef u_int tcp_seq;

struct sniff_tcp {
        u_short th_sport;               /* source port */
        u_short th_dport;               /* destination port */
        tcp_seq th_seq;                 /* sequence number */
        tcp_seq th_ack;                 /* acknowledgement number */
        u_char  th_offx2;               /* data offset, rsvd */
#define TH_OFF(th)      (((th)->th_offx2 & 0xf0) >> 4)
        u_char  th_flags;
        #define TH_FIN  0x01
        #define TH_SYN  0x02
        #define TH_RST  0x04
        #define TH_PUSH 0x08
        #define TH_ACK  0x10
        #define TH_URG  0x20
        #define TH_ECE  0x40
        #define TH_CWR  0x80
        #define TH_FLAGS        (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
        u_short th_win;                 /* window */
        u_short th_sum;                 /* checksum */
        u_short th_urp;                 /* urgent pointer */
};

u_short sport[100];int spd=0;
u_int seq[100];int seqd=0;
u_short win[100];int wind=0;

FILE* urlfile;
int count = 1,flag=0,t=0; 
float sc=0,ac=0,fc=0,pc=0,uc=0,rc=0;

void
got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);

void
print_payload(const u_char *payload, int len);

void
print_hex_ascii_line(const u_char *payload, int len, int offset);



void
print_app_usage(void);

void DoStuff(void);

void DoStuff(void) {
    t++;
  printf("Timer %d went off.########################################\n",t);
 // fprintf(urlfile,"\n hi hi");
  fprintf(urlfile,"\ntime %d 1:%f 2:%f 3:%f 4:%f 5:%f 6:%f 7:%f 8:%f 9:%f",t,sc/count,ac/count,fc/count,pc/count,uc/count,rc/count,(float)spd/count,(float)wind/count,(float)seqd/count);
  printf("\ntime %d 1:%f 2:%f 3:%f 4:%f 5:%f 6:%f 7:%f 8:%f 9:%f",t,sc/count,ac/count,fc/count,pc/count,uc/count,rc/count,(float)spd/count,(float)wind/count,(float)seqd/count);
  printf("\n a_count : %f , total_packets : %d , frequency : %f",ac,count,ac/count);
  printf("\n r_count : %f , total_packets : %d , frequency : %f",rc,count,rc/count);
  printf("\n p_count : %f , total_packets : %d , frequency : %f",pc,count,pc/count);
  printf("\n s_count : %f , total_packets : %d , frequency : %f",sc,count,sc/count);
  printf("\n u_count : %f , total_packets : %d , frequency : %f",uc,count,uc/count);
  printf("\n f_count : %f , total_packets : %d , frequency : %f",fc,count,fc/count);
  printf("\ncount of distinct seq nos : %d no/pcount : %f ",seqd,(float)seqd/count);
  printf("\ncount of distinct sports : %d no/pcount : %f ",spd,(float)spd/count);
  printf("\ncount of distinct win nos : %d no/pcount : %f\n\n ",wind,(float)wind/count);
  ac=rc=pc=fc=sc=uc=0;count=1;  
  spd=seqd=wind=0;
}


void
print_app_usage(void)
{

    printf("Usage: ./a.out [interface]\n");
    printf("\n");
    printf("Options:\n");
    printf("    interface    Listen on <interface> for packets.\n");
    printf("\n");

return;
}


/*
 * dissect/print packet
 */
void
got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{

                      /* packet counter */
    int j;
    /* declare pointers to packet headers */
    const struct sniff_ethernet *ethernet;  /* The ethernet header [1] */
    const struct sniff_ip *ip;              /* The IP header */
    const struct sniff_tcp *tcp;            /* The TCP header */
    const char *payload;                    /* Packet payload */

    int size_ip;
    int size_tcp;
    int size_payload;

    printf("\nPacket number %d:\n", count);
    count++;

    /* define ethernet header */
    ethernet = (struct sniff_ethernet*)(packet);

    /* define/compute ip header offset */
    ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
    size_ip = IP_HL(ip)*4;
    if (size_ip < 20) {
        printf("   * Invalid IP header length: %u bytes\n", size_ip);
        return;
    }

    switch(ip->ip_p) {
        case IPPROTO_TCP:
            printf("   Protocol: TCP\n");
            break;
        case IPPROTO_UDP:
            printf("   Protocol: UDP\n");
            return;
        case IPPROTO_ICMP:
            printf("   Protocol: ICMP\n");
            return;
        case IPPROTO_IP:
            printf("   Protocol: IP\n");
            return;
        default:
            printf("   Protocol: unknown\n");
            return;
    }

    /* define/compute tcp header offset */
    tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
    size_tcp = TH_OFF(tcp)*4;
    if (size_tcp < 20) {
        printf("   * Invalid TCP header length: %u bytes\n", size_tcp);
        return;
    }

    flag=0;
    for(j=0;j<spd;j++)
    {      
       if(sport[j]==ntohs(tcp->th_sport)) { flag=1; break; }
    }
    if(flag==0) sport[spd++]=ntohs(tcp->th_sport);

    flag=0;
    for(j=0;j<seqd;j++)
    {      
       if(seq[j]==ntohs(tcp->th_seq)) { flag=1; break; }
    }
    if(flag==0) seq[seqd++]=ntohs(tcp->th_seq);

    flag=0;
    for(j=0;j<wind;j++)
    {      
       if(win[j]==ntohs(tcp->th_win)) { flag=1; break; }
    }
    if(flag==0) win[wind++]=ntohs(tcp->th_win);

    printf("   Src port: %d\n", ntohs(tcp->th_sport));
    printf("   Window: %d\n", ntohs(tcp->th_win));
    printf("   Sequence no: %d\n", ntohs(tcp->th_seq));

    if (tcp->th_flags & TH_URG){
        printf("   Flag: TH_URG");uc++;
    }
    if (tcp->th_flags & TH_RST){
        printf("   Flag: TH_RST");rc++;
    }
    if (tcp->th_flags & TH_ACK){
        printf("   Flag: TH_ACK");ac++;
    }
    if (tcp->th_flags & TH_PUSH){
        printf("   Flag: TH_PUSH");pc++;
    }
    if (tcp->th_flags & TH_SYN){
        printf("   Flag: TH_SYN");sc++;
    }
    if (tcp->th_flags & TH_FIN){
        printf("   Flag: TH_FIN");fc++;
    }


    if (size_payload > 0) {
        printf("   Payload (%d bytes):\n", size_payload);

    }

return;
}

int main(int argc, char **argv)
{

    char *dev = NULL;           /* capture device name */
    char errbuf[PCAP_ERRBUF_SIZE];      /* error buffer */
    pcap_t *handle;             /* packet capture handle */
    char filter_exp[] = "tcp port 80";
    struct bpf_program fp;          /* compiled filter program (expression) */
    bpf_u_int32 mask;           /* subnet mask */
    bpf_u_int32 net;            /* ip */
    //int num_packets = 10;         /* number of packets to capture */

    /* check for capture device name on command-line */
    if (argc == 2) {
        dev = argv[1];
    }
    else if (argc > 2) {
        fprintf(stderr, "error: unrecognized command-line options\n\n");
        print_app_usage();
        exit(EXIT_FAILURE);
    }
    else {
        /* find a capture device if not specified on command-line */
        dev = pcap_lookupdev(errbuf);
        if (dev == NULL) {
            fprintf(stderr, "Couldn't find default device: %s\n",
                errbuf);
            exit(EXIT_FAILURE);
        }
    }

    /* get network number and mask associated with capture device */
    if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
        fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
            dev, errbuf);
        net = 0;
        mask = 0;
    }

    /* print capture info */
    printf("Device: %s\n", dev);
    printf("Filter expression: %s\n", filter_exp);

    /* open capture device */
    handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
    if (handle == NULL) {
        fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
        exit(EXIT_FAILURE);
    }

    /* make sure we're capturing on an Ethernet device [2] */
    if (pcap_datalink(handle) != DLT_EN10MB) {
        fprintf(stderr, "%s is not an Ethernet\n", dev);
        exit(EXIT_FAILURE);
    }

    /* compile the filter expression */
    if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
        fprintf(stderr, "Couldn't parse filter %s: %s\n",
            filter_exp, pcap_geterr(handle));
        exit(EXIT_FAILURE);
    }

    /* apply the compiled filter */
    if (pcap_setfilter(handle, &fp) == -1) {
        fprintf(stderr, "Couldn't install filter %s: %s\n",
            filter_exp, pcap_geterr(handle));
        exit(EXIT_FAILURE);
    }

    urlfile=fopen("output.txt","w");
    if(urlfile==NULL) printf("Unable to create file.");

    // timer code

    struct itimerval it_val;  /* for setting itimer */

  /* Upon SIGALRM, call DoStuff().
   * Set interval timer.  We want frequency in ms, 
   * but the setitimer call needs seconds and useconds. */
  if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) {
    perror("Unable to catch SIGALRM");
    exit(1);
  }
  it_val.it_value.tv_sec =     INTERVAL/1000;
  it_val.it_value.tv_usec =    (INTERVAL*1000) % 1000000;   
  it_val.it_interval = it_val.it_value;
  if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
    perror("error calling setitimer()");
    exit(1);
  }

    /* now we can set our callback function */
    //pcap_loop(handle, num_packets, got_packet, NULL);
    pcap_loop(handle,-1, got_packet, NULL);// set num_packets to -1 to capture indefinitely.
    /* cleanup */
    pcap_freecode(&fp);
    pcap_close(handle);
    fclose(urlfile);
    printf("\nCapture complete.\n");

return 0;
}