Answer the question
In order to leave comments, you need to log in
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);
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
Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question