Search code examples
clinuxsocketsnetwork-programmingraw-sockets

How to receive bpdu traffic using raw socket in Linux C


How to get packets using recvfrom()? And how to know is it bdpu traffic? I read somesource but didn't find listen ethernet traffic.

int data_size, addr_size;
char reply[6000];
struct sockaddr_in addr;

addr_size = sizeof addr;
datasize = recvfrom(sock, reply, 6000, 0, &addr, &addr_size);

if(data_size<0)
{
    printf("nothing recv");
}
printf("%x",reply);

Output:

651a7ec0

What is this? I don't know.


Solution

  • the reply variable is a pointer to a 6000 byte array. When recvfrom returns, if fills this array with datasize bytes. To see the content, iterate over reply from index 0, until datasize:

    for(i=0;i<datasize;i++)
    {
        printf("%02x ",reply[i]);
    }
    

    I suggest declaring reply as unsinged char reply[6000].