简体   繁体   中英

How to receive IPv4 packets using SOCK_RAW in Linux?

How do I receive network layer (OSI Layer 3) packets in Linux? More specifically, IPv4 packets regardless of transport layer protocol (UDP, TCP, ...). Preferably in C without libraries.

Using socket(AF_INET, SOCK_RAW, IPPROTO_RAW) , I'm only able to send IPv4 packets; not receive.

Did you try with recvfrom from socket library. Like in this example :

sock_raw = socket(AF_INET , SOCK_RAW , protocol);
while(1)
{
    data_size = recvfrom(sock_raw , buffer , 65536 , 0 , &addr , &addr_size);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM