raw socket listener

12,593

Solution 1

You are right, the only thing you will need to do is call socket() and then recvfrom(). Nevertheless be aware of the fact that there are some limitations with listening using SOCK_RAW.

If you're not using raw sockets on a "send-and-forget" basis, you will be interested in reading the reply packet(s) for your raw packet(s). The decision logic for whether a packet will be delivered to a raw socket can be enumarated as such:

  1. TCP and UDP packets are never delivered to raw sockets, they are always handled by the kernel protocol stack.

  2. Copies of ICMP packets are delivered to a matching raw socket. For some of the ICMP types (ICMP echo request, ICMP timestamp request, mask request) the kernel, at the same time, may wish to do some processing and generate replies.

  3. All IGMP packets are delivered to raw sockets: e.g. OSPF packets.

  4. All other packets destined for protocols that are not processed by a kernel subsystem are delivered to raw sockets.

The fact that you're dealing with a protocol for which reply packets are delivered to your raw socket does not necessarily mean that you'll get the reply packet. For this you may also need to consider:

  1. setting the protocol accordingly while creating your socket via socket(2)system call. For instance, if you're sending an ICMP echo-request packet, and want to receive ICMP echo-reply, you can set the protocol argument (3rd argument) to IPPROTO_ICMP).

  2. setting the protocol argument in socket(2) to 0, so any protocol number in the received packet header will match.

  3. defining a local address for your socket (via e.g. bind(2)), so if the destination address matches the socket's local address, it'll be delivered to your application also.

For more details you can read e.g. this.

Solution 2

If you meant to capture the traffic on a interface, you can use libpcap.

Share:
12,593
Dr.Knowitall
Author by

Dr.Knowitall

I'm a fullstack engineering breaking into the data science world. I'm interested in computer visualization, linguistics processing, machine learning and their applications in quantitative finance.

Updated on June 09, 2022

Comments

  • Dr.Knowitall
    Dr.Knowitall almost 2 years

    This is a quick question for linux c programming raw sockets. If I wanted to just listen to any interface with a raw socket, must I actually bind to an ip address or interface to listen to traffic? From what I understand, I feel like I should be able to just call sock(); and then start recvfrom() traffic. Maybe I'm wrong, but I've seen some programs that don't use it.

  • Dr.Knowitall
    Dr.Knowitall over 11 years
    1 million thank you's!:) I couldn't find any good information about recieving packets on a raw socket, but thanks again for enlightening me. Now I know that I should use bpf if I want to into raw sockets.
  • Dr.Knowitall
    Dr.Knowitall over 11 years
    Thanks for your suggestion, I will probably use libpcap over bpf since it seems much easier to analyze packets. I learned something to day!
  • natevw
    natevw almost 3 years
    Great answer. The final link is unavailable at the moment (perhaps just temporarily?) but is mirrored at web.archive.org/web/20190716060924/http://www.enderunix.org/‌​…