0
Guys I’m seeing a code of a network Sniffer and I’m not understanding the following lines :
ethernet = (struct sniff_ethernet*)(packet);
ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
how do these cast with struct?
//////////////////////////função
got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
static int count = 1;
/* declare pointers to packet headers */
const struct sniff_ethernet *ethernet;
const struct sniff_ip *ip;
const struct sniff_tcp *tcp;
const char *payload;
int size_ip;
int size_tcp;
int size_payload;
printf("\nPacket number %d:\n", count);
count++;
ethernet = (struct sniff_ethernet*)(packet); ////////
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;
}
printf(" From: %s\n", inet_ntoa(ip->ip_src));
printf(" To: %s\n", inet_ntoa(ip->ip_dst));
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;
}
The Casts has no reason to be in the code presented. When making an assignment to a variable of a different but compatible type value the compiler makes an automatic conversation. The presence of the cast does not help or help before the opposite.
– pmg