user3237732
user3237732

Reputation: 2028

Receive Ethernet packets in Linux with several Ethtypes in C

I want to receive ethernet packets from socket in Linux, but only those, which have one of two custom Ethtype values. As I know, if only 1 ethtype should be received, it's possible to specify this value while creating socket like this

int socket = socket(PF_PACKET, SOCK_RAW, htons(ETHERTYPE_CUSTOM_1);

But what if I have 2 different ethtypes? Should I use 2 sockets or write some custom filter? Or is there some any simple way?

Upvotes: 0

Views: 531

Answers (2)

Barmar
Barmar

Reputation: 780655

Create two sockets, one for each ethertype. Then you can use select() or epoll() to wait for packets on either socket at the same time.

Upvotes: 1

StxApp
StxApp

Reputation: 35

I think you should use libpcap library. You need to access bpf packer filter. This is easy one. Or you can use iptables rules an netfilter library. You need to set prerouting iptables rules to forward all packet to specific port and your application binding this port as listening mode and you can receive full packet.

Upvotes: 0

Related Questions