Пример #1
0
Файл: com.c Проект: hkusb/hust
/* 打印链表,链表的遍历*/
void printList(Node *pHead)
{
    if(NULL == pHead)   //链表为空
    {
        printf("PrintList函数执行,链表为空\n");
    }
    else
    {
        while(NULL != pHead)
        {
            printf("原IP:%s",inet_ntoa((struct in_addr*)(pHead->flow.SrcIP)));
            printf("目的ip:%s",inet_ntoa((struct in_addr*)(pHead->flow.DstIP)));
            printf("原端口:%d",ntohs(pHead->flow.SrcPort));
            printf("目的端口:%d\n",ntohs(pHead->flow.DstPort));
            printf("上层协议:%d\n",ntohs(pHead->flow.Protocol));
            printf("cs方向:%d个packets;%d个bytes\n", pHead->cs_packets, pHead->cs_bytes);

            ListInfo *p=pHead->cs;
            while(p!=NULL)
            {
			puts("11");
            printfPcapHeader(&(p->time_len));
            //if(p->next!=NULL)
            //{
            p=p->next;
		  //  }
		  //  else
		  //  break;
            puts("12");
            sleep(1);
		    }

            pHead = pHead->next;
            puts("pHead next");
        }
        printf("\n");
    }
}
Пример #2
0
int main (int argc, const char * argv[])
{

    printf("sizeof:int %lu,unsigned int %lu,char %lu,unsigned char %lu,short:%lu,unsigned short:%lu\n",
           sizeof(int),sizeof(unsigned int),sizeof(char),sizeof(unsigned char),sizeof(short),sizeof(unsigned short));

    pcap_file_header  pfh;
    pcap_header  ph;
    int count=0;
    void * buff = NULL;
    int readSize=0;
    int ret = 0;

    FILE *fp = fopen(PCAP_FILE, "rw");

    if (fp==NULL) {
        fprintf(stderr, "Open file %s error.",PCAP_FILE);
        ret = ERROR_FILE_OPEN_FAILED;
        goto ERROR;
    }

    fread(&pfh, sizeof(pcap_file_header), 1, fp);
    prinfPcapFileHeader(&pfh);
    //fseek(fp, 0, sizeof(pcap_file_header));

    buff = (void *)malloc(MAX_ETH_FRAME);
    for (count=1; ; count++) {
        memset(buff,0,MAX_ETH_FRAME);
        //read pcap header to get a packet
        //get only a pcap head count .
        readSize=fread(&ph, sizeof(pcap_header), 1, fp);
        if (readSize<=0) {
            break;
        }
        printfPcapHeader(&ph);


        if (buff==NULL) {
            fprintf(stderr, "malloc memory failed.\n");
            ret = ERROR_MEM_ALLOC_FAILED;
            goto ERROR;
        }

        //get a packet contents.
        //read ph.capture_len bytes.
        readSize=fread(buff,1,ph.capture_len, fp);
        if (readSize != ph.capture_len) {
            free(buff);
            fprintf(stderr, "pcap file parse error.\n");
            ret = ERROR_PCAP_PARSE_FAILED;
            goto ERROR;
        }
        printPcap(buff, ph.capture_len);


        printf("===count:%d,readSize:%d===\n",count,readSize);

        if (feof(fp) || readSize <=0 ) {
            break;
        }
    }

ERROR:
    //free
    if (buff) {
        free(buff);
        buff=NULL;
    }
    if (fp) {
        fclose(fp);
        fp=NULL;
    }

    return ret;
}