Why Does a Broadcast on a Raw Socket get echoed back to me?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why Does a Broadcast on a Raw Socket get echoed back to me?
I am working on a project that has 2 computers connected via Gig Ethernet both running linux. The idea is that one computer (Host A) will receive a bunch of data process it and send it back to the other machine (Host B)..
I am using a raw socket and broadcasting the data between the 2 machines. I want to use broadcast because I need to be able to replace Host A or B without changing any configuration (ie configuring the src an dest MAC Addresses), plus this is a point-to-point network without any other machines on it.
The problem is that when I send data this way, my recieve raw socket (on the same machine) also gets the data that I am sending out.
Why is this?
I know that I can filter the data out with recieve code but I would rather not have this happen in the first place. I don't want this data clogging up the system while I am trying to recieve the real data from the other host.
This is how my socket is created.
Code:
s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
I am binding it to an interface (ie eth0), and using sendto and recvfrom.
It sounds to me like you need to put the IP address of the second PC into the first PC and only send to that.
When you broadcast in a normal network, you are sending data to everyone, regardless of if they want it or not, all you are getting is the data you sent, being sent to you as a member of the network.
You could set your PC up as to ignore packets from your IP address, but I'm not sure if that is going to cripple something else.
I'm not very knowledgeable about this topic, but my first guess is: It's not a bug, it's a feature. Broadcasting means, sending some data to every interface on the subnet, and since the sending interface is part of the subnet, it receives the data as well.
To circumvent the problem (and still provide plug-'n'-play), you could get the addresses of all interfaces on the subnet (should be two) and filter the own one out, which leaves the destination address. However, I don't know whether and how this can be done.
Edit: Someone was faster. I should learn to type with 180 keystrokes/minute
It make some sense to me, I had just hoped that there was a way around it.. The man page for packet lead me to think that there should be a way.
Quote:
Packet sockets are used to receive or send raw packets at the device driver
(OSI Layer 2) level. They allow the user to implement protocol modules in user space on top of the physical layer.
I seems to me that if this was true, the raw_socket would be very deep in the network stack and not even be able to cause this "loopback" to happen since this is a direct interface to the device dirver..
I think that I am going to try to implement a system to detect and configure the dst MAC addresses automatically.
Originally posted by F0ul_Olli
You don't need the MAC address, just the IP address.
Not true. The interface does not even have an IP address set. This is not using the IP protocol. This is pure 802.3. In fact PF_PACKET with SOCK_RAW means that I have to provide the ethernet header (src MAC, dst MAC, Type/Len) in the buffer that is given to sendto. It just puts it on the wire as is.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.