1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
my_callback(const struct pcap_pkthdr* hdr,const u_char* packet)
{
u_char *ptr;
const u_char *packet_data;
struct ether_header *eptr;
int i;
const struct ip* ippkt;
struct in_addr source_ip, destination_ip;
char ipstr_src[INET_ADDRSTRLEN], ipstr_dst[INET_ADDRSTRLEN];
const struct tcphdr *tcpheader, *tcphdr2;
//do some typecastings to access the received data as valid headers
eptr = (struct ether_header*)(packet);
ippkt = (struct ip*)(packet + sizeof(struct ether_header));
tcpheader = (struct tcphdr*)(packet + sizeof(struct ether_header) + (ippkt->ip_hl*4));
tcphdr2 = (struct tcphdr*)(packet + sizeof(struct ether_header) + sizeof(struct ip));
packet_data = packet + sizeof(struct ether_header) + (ippkt->ip_hl*4) + (tcpheader->doff*4);
//obtain ips and convert to string
source_ip = ippkt->ip_src;
destination_ip = ippkt->ip_dst;
inet_ntop(AF_INET, &(source_ip), ipstr_src, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(destination_ip), ipstr_dst, INET_ADDRSTRLEN);
//print ethernet macs
ptr = eptr->ether_shost;
i = ETHER_ADDR_LEN;
printf("Packet: Eth/IP/TCP Received: %s Offset=%u (total length: %u bytes)\n", ctime((const time_t*)&hdr->ts.tv_sec), hdr->ts.tv_usec, hdr->len);
printf("Destination:");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>0);
printf(" Source:");
ptr = eptr->ether_dhost;
i = ETHER_ADDR_LEN;
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>0);
printf(" (type %d)\n", eptr->ether_type);
//build tcp flags string
std::stringstream tcp_flags;
tcp_flags << (tcpheader->urg==1 ? "URG " : "") << (tcpheader->ack==1 ? "ACK " : "") << (tcpheader->psh==1 ? "PSH " : "") << (tcpheader->rst==1 ? "RST " : "") << (tcpheader->syn==1 ? "SYN " : "") << (tcpheader->fin==1 ? "FIN " : "");
//print out information
printf("IP from: %s to: %s length=%u bytes (protocol %u)\n", ipstr_src, ipstr_dst, ippkt->ip_len, ippkt->ip_p);
printf("Port from: %u to: %u Flags: %s\n", (tcpheader->source), (tcpheader->dest), tcp_flags.str().c_str());
printf("seq %u ack %u (tcp header is %u bytes)\n", (tcpheader->seq), (tcpheader->ack_seq), tcpheader->doff*4);
printf("\n");
}
|