Receiving Packets Using A Raw Socket

Remember - we receive _every_ packet sent to the machine, not just "expected responses".
/* the received packet contains the IP header... */
char rbuf[sizeof(struct iphdr) + sizeof(struct icmp)];
struct sockaddr_in raddr;
socklen_t raddr_len;

// receive the packet that we sent (since we sent it to ourselves,
// and a raw socket sees everything...).
rc = recvfrom(s,
              rbuf, sizeof(rbuf),
              0 /* flags */,
              (struct sockaddr*)&raddr, &raddr_len);
if (rc == -1) {
    perror("recvfrom 1:");
    exit(1);
}
Originally written by Valid HTML 4.01!guy keren