What do I get at the start of my buffer in case my call to recvfrom returns a valid amount of bytes (e.g. 1400)?
Do I get an ethhdr? This code doesn't work for example even though I should be receiving UDP-Packets:
void read_packets(struct config *cfg) {
int64_t read_bytes = recvfrom(cfg->socket_fd, buffer, BUFFER_SIZE, 0, NULL, NULL);
if(read_bytes == -1) {
return;
}
cfg->stats.amnt_of_packets += 1;
cfg->stats.amnt_of_bytes += read_bytes;
struct ethhdr *eth = (struct ethhdr*)(buffer);
if(ntohs(eth->h_proto) == ETH_P_IP) {
struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
if(iph->protocol == IPPROTO_UDP) {
/* doesn't work */
}
}
}
socket_fd was created as:
cfg->socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);