K
K
Kirill Margorin2016-07-08 11:34:26
linux
Kirill Margorin, 2016-07-08 11:34:26

How to receive a UDP broadcast packet from a "foreign" subnet?

There is a GigE Vision camera connected by a direct cable to the computer.
Camera IP: 10.0.0.234/24
Computer IP: 192.168.42.1/24
Standard code for transmitting a broadcast packet (I won’t write the full code yet, only key points in pseudocode:

int s = socket(AF_INET, SOCK_DGRAM, 0);
bind(s, {adapter->IP});
setsockopt(s, SOL_SOCKET, SO_BROADCAST, true);
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, {1sec});
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, {1sec});
sendto(s, GVCPCmdDeviceDiscovery, {INADDR_BROADCAST});
sleep();
recvfrom(s);

In WireShark, I see that the command leaves and the response comes back:
15	59.752591431	192.168.42.1	255.255.255.255	GVCP	50	> DISCOVERY_CMD 
16	59.752820098	10.0.0.234	255.255.255.255	GVCP	298	< DISCOVERY_ACK

The program does not see the answer.
If you transfer the computer to the same subnet with the camera, then everything, of course, works. But this is not always possible (especially from a consumer point of view).
Under Windows (they say ... haven't checked it yet) everything works (I'll check it now, I'll add information).
Pleora software was installed on the same computer.
When installing Pleora, an interesting phrase was said about net.ipv4.conf.all.rp_filter. All three values ​​(0, 1, 2) were tried. In modes 0 and 2, Pleora sees the camera, in mode 1 it does not. (as stated in the installation comment).
However, the above code does not work at all. I didn't see any difference in wireshark packets.
Actually the question is: Which of the options is missing in the test code? How to receive a Broadcast packet from a foreign subnet?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Margorin, 2016-07-08
@comargo

In general, in the end, I set on Pleora strace.
The answer is interesting (for me):
Two sockets are created (with the REUSEADDR flag):
Packets leave through the first socket, and are read through the second.
<code from strace>

socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("192.168.42.1")}, 16) = 0
setsockopt(3, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0
getsockname(3, {sa_family=AF_INET, sin_port=htons(43861), sin_addr=inet_addr("192.168.42.1")}, [16]) = 0
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
setsockopt(4, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
bind(4, {sa_family=AF_INET, sin_port=htons(43861), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
sendto(3, "B\21\0\2\0\0P\1", 8, 0, {sa_family=AF_INET, sin_port=htons(3956), sin_addr=inet_addr("255.255.255.255")}, 16) = 8
nanosleep({0, 200000000}, NULL)         = 0
recvfrom(4, "\0\0\0\3\0\370P\1\0\1\0\2\200\0\0\1\0\0\0\21\34\0\352C\0\0\0\7\0\0\0\7"..., 256, 0, {sa_family=AF_INET, sin_port=htons(3956), sin_addr=inet_addr("10.0.0.234")}, [16]) = 256

A
Armenian Radio, 2016-07-08
@gbg

To receive a broadcast from a foreign subnet, it must be a multicast and the router must be configured to allow it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question