Re: UDP Broadcasts Don't Work
Re: UDP Broadcasts Don't Work
- Subject: Re: UDP Broadcasts Don't Work
- From: Quinn <email@hidden>
- Date: Tue, 21 Aug 2007 10:24:13 +0100
At 0:24 -0400 21/8/07, Brian Barnes wrote:
Thanks for the info, I know a bit more now, but still haven't gotten
it running.
I'm not sure what's going on with your code but I just tried this on
a machine here in my office. My IP address is 10.40.0.15 with a
subnet mask of 255.255.255.0.
1. Run GDB and attach to a random program (in this case TextEdit).
guy-smiley$ gdb
GNU gdb 6.3.50-20050815 (Apple version gdb-573) [...]
(gdb) attach TextEdit
[...]
Create a UDP socket. The first two parameters are the numeric values
for AF_INET and SOCK_DGRAM.
(gdb) set $s=(int)socket(2, 2, 0)
Allocate a sockaddr_in.
(gdb) set $sa=(char *)malloc(16)
Set up sin_len.
(gdb) set $sa[0]=16
Set up sin_family (AF_INET is 2).
(gdb) set $sa[1]=2
I want to broadcast to port 12345. Get that as hex and then put it
into sin_port. We do this byte-by-byte because the value has to be
in network byte order. In real code you'd use sin_port =
htons(12345).
(gdb) p/x 12345
$2 = 0x3039
(gdb) set $sa[2]=0x30
(gdb) set $sa[3]=0x39
I want to broadcast to 10.0.40.255. Set up sin_addr.s_addr. Again,
we do this byte-by-byte because the value has to be in network byte
order. In real code you'd use sin_addr.s_addr = htonl(0x100028ff).
(gdb) set $sa[4]=10
(gdb) set $sa[5]=0
(gdb) set $sa[6]=40
(gdb) set $sa[7]=255
Set the SO_BROADCAST option. We first allocate an "int" and set it to 1.
(gdb) set $one=(int *)malloc(4)
(gdb) set *$one=1
Then we call setsockopt. The second and third parameters are the
numeric values for SOL_SOCKET and SO_BROADCAST.
(gdb) call (int)setsockopt($s, 0xffff, 0x20, $one, 4)
$5 = 0
Now send some data!
(gdb) call (int)sendto($s, "Hello Cruel World!", 18, 0, $sa, 16)
$12 = 18
On another machine on the same subnet I ran <x-man-page://1/tcpdump>
to verify my results.
victim2$ sudo tcpdump -n -i en0 udp port 12345
Password:********
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on en0, link-type EN10MB (Ethernet), capture size 96 bytes
10:21:53.293854 IP 10.0.40.15.60275 > 10.0.40.255.12345: UDP, length 18
[...]
Sweet!
* * *
Some questions to consider:
o Have you used <x-man-page://1/tcpdump> to verify whether the
problem you're experiencing is on the send or receive side?
o Are you checking the error result from <x-man-page://2/sendto>?
o On the receive side, are you binding to INADDR_ANY? If you don't
do this, you'll only receive packets specifically targetted at your
IP address (that is, not broadcasts).
o On the send side, are you sure you're using network byte order for
the IP address and port in the sockaddr_in?
Share and Enjoy
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden