/* Print the IP header for IP packets */ void PrintIpHeader(unsigned char* Buffer, int Size) { int iphdrlen = 0; iphdr = (IPV4_HDR *)(Buffer + sizeof(ETHER_HDR)); iphdrlen = iphdr->ip_header_len * 4; memset(&source, 0, sizeof(source)); source.sin_addr.s_addr = iphdr->ip_srcaddr; memset(&dest, 0, sizeof(dest)); dest.sin_addr.s_addr = iphdr->ip_destaddr; print_ethernet_header(Buffer); fprintf(logfile, "\n"); fprintf(logfile, "IP Header\n"); fprintf(logfile, " |-IP Version : %d\n", (unsigned int)iphdr->ip_version); fprintf(logfile, " |-IP Header Length : %d DWORDS or %d Bytes\n", (unsigned int)iphdr->ip_header_len, ((unsigned int)(iphdr->ip_header_len)) * 4); fprintf(logfile, " |-Type Of Service : %d\n", (unsigned int)iphdr->ip_tos); fprintf(logfile, " |-IP Total Length : %d Bytes(Size of Packet)\n", ntohs(iphdr->ip_total_length)); fprintf(logfile, " |-Identification : %d\n", ntohs(iphdr->ip_id)); fprintf(logfile, " |-Reserved ZERO Field : %d\n", (unsigned int)iphdr->ip_reserved_zero); fprintf(logfile, " |-Dont Fragment Field : %d\n", (unsigned int)iphdr->ip_dont_fragment); fprintf(logfile, " |-More Fragment Field : %d\n", (unsigned int)iphdr->ip_more_fragment); fprintf(logfile, " |-TTL : %d\n", (unsigned int)iphdr->ip_ttl); fprintf(logfile, " |-Protocol : %d\n", (unsigned int)iphdr->ip_protocol); fprintf(logfile, " |-Checksum : %d\n", ntohs(iphdr->ip_checksum)); fprintf(logfile, " |-Source IP : %s\n", inet_ntoa(source.sin_addr)); fprintf(logfile, " |-Destination IP : %s\n", inet_ntoa(dest.sin_addr)); }
void print_ip_header(const u_char * Buffer, int Size) { print_ethernet_header(Buffer , Size); unsigned short iphdrlen; struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr) ); iphdrlen =iph->ihl*4; memset(&source, 0, sizeof(source)); source.sin_addr.s_addr = iph->saddr; memset(&dest, 0, sizeof(dest)); dest.sin_addr.s_addr = iph->daddr; fprintf(logfile , "\n"); fprintf(logfile , "IP Header\n"); fprintf(logfile , " |-IP Version : %d\n",(unsigned int)iph->version); fprintf(logfile , " |-IP Header Length : %d DWORDS or %d Bytes\n",(unsigned int)iph->ihl,((unsigned int)(iph->ihl))*4); fprintf(logfile , " |-Type Of Service : %d\n",(unsigned int)iph->tos); fprintf(logfile , " |-IP Total Length : %d Bytes(Size of Packet)\n",ntohs(iph->tot_len)); fprintf(logfile , " |-Identification : %d\n",ntohs(iph->id)); //fprintf(logfile , " |-Reserved ZERO Field : %d\n",(unsigned int)iphdr->ip_reserved_zero); //fprintf(logfile , " |-Dont Fragment Field : %d\n",(unsigned int)iphdr->ip_dont_fragment); //fprintf(logfile , " |-More Fragment Field : %d\n",(unsigned int)iphdr->ip_more_fragment); fprintf(logfile , " |-TTL : %d\n",(unsigned int)iph->ttl); fprintf(logfile , " |-Protocol : %d\n",(unsigned int)iph->protocol); fprintf(logfile , " |-Checksum : %d\n",ntohs(iph->check)); fprintf(logfile , " |-Source IP : %s\n" , inet_ntoa(source.sin_addr) ); fprintf(logfile , " |-Destination IP : %s\n" , inet_ntoa(dest.sin_addr) ); }
int monitor_once(int iffd, const char ifname[]) { static char buffer[65535]; int size = read(iffd, buffer, sizeof (buffer)); if (size <= 0) perror("read"); else print_ethernet_header((struct ether_header *)buffer, ifname); return size; }
void print_ip_header(unsigned char* buffer, int size) { print_ethernet_header(buffer, size); unsigned short iphdrlen; struct iphdr *ip_header = (struct iphdr*)(buffer + sizeof(struct ethhdr)); iphdrlen = ip_header->ihl*4; memset(&source, 0, sizeof(source)); source.sin_addr.s_addr = ip_header->saddr; memset(&dest, 0, sizeof(dest)); dest.sin_addr.s_addr = ip_header->daddr; fprintf(logfile, "\n"); fprintf(logfile, "IP Header\n"); fprintf(logfile, "\t|-IP Version\t\t: %d\n", (unsigned int)ip_header->version); fprintf(logfile, "\t|-IP Header Length\t: %d DWORDS or %d Bytes\n", (unsigned int)ip_header->ihl, ((unsigned int)(ip_header->ihl))*4); fprintf(logfile, "\t|-Type of Service\t: %d\n", (unsigned int)ip_header->tos); fprintf(logfile, "\t|-IP Total Length\t: %d Bytes (Size of Packet)\n", ntohs(ip_header->tot_len)); fprintf(logfile, "\t|-Source IP\t\t: %s\n", inet_ntoa(source.sin_addr)); fprintf(logfile, "\t|-Destination IP\t: %s\n", inet_ntoa(dest.sin_addr)); }