struct udpiphdr *udp_read(void) { int len; char *ptr; struct ether_header *eptr; for ( ; ; ) { // 从分组捕获设备获取下一个分组 ptr = next_pcap(&len); switch (datalink) { case DLT_NULL: // loopback header = 4 bytes return(udp_check(ptr + 4, len - 4)); case DLT_EN10MB: eptr = (struct ether_header *) ptr; if (ntohs(eptr->ether_type) != ETHERTYPE_IP) { err_quit("Ethernet type %x not IP", ntohs(eptr->ether_type)); } return(udp_check(ptr + 14, len - 14)); case DLT_SLIP: // SLIP header = 24 bytes return(udp_check(ptr + 24, len - 24)); case DLT_PPP: // PPP header = 24 bytes return(udp_check(ptr + 24, len - 24)); default: err_quit("unsupported datalink (%d)", datalink); } } }
void capture_packet(pcap_t *pd) { int len; char *ptr = NULL; for(;;) { ptr = next_pcap(&len, pd); if(ptr == NULL) { log_print(LOG_TYPE_ERROR,"ptr is NULL!\n"); exit(1); } if (len < (ETHER_LEN + IP_LEN + UDP_LEN)) { log_print(LOG_TYPE_ERROR, "packet is too small\n"); continue; } insert_packet(ptr, len); } }
void tcp_read(void) { int len; char *ptr; struct ether_header *eptr; for ( ; ; ) { ptr = next_pcap(&len); switch (datalink) { case DLT_NULL: /* loopback header = 4 bytes */ printf("case 1 packet\n"); return ; //(check(ptr + 4, len - 4)); case DLT_EN10MB: eptr = (struct ether_header *) ptr; if (ntohs(eptr->ether_type) != ETHERTYPE_IP) printf("Ethernet type %x not IP", ntohs(eptr->ether_type)); printf("\n\ncase 2 packet IPHEADER\n"); check(ptr + 14, len - 14); return ; // (check(ptr + 14, len - 14)); case DLT_SLIP: /* SLIP header = 24 bytes */ printf("case 3 packet\n"); return ; //(check(ptr + 24, len - 24)); case DLT_PPP: /* PPP header = 24 bytes */ printf("case 4 packet\n"); return ; //(check(ptr + 24, len - 24)); default: printf("unsupported datalink (%d)", datalink); } } }