I've written a small piece of code to read packets broadcast to a given
port. It has worked on RH8 through FC3. I built it on FC4 and it blocks
at recvfrom(). It works on Ubuntu - I copied the executable over from the
FC4 machine (where it doesn't work) to the Ubuntu machine & it works just
fine there. I also recompiled on the Ubuntu machine & it still works.
The idea is that another program is broadcasting packets to port 3000, which
I then want to read. Tcpdump confirms that they really are being
broadcast, and are making it to the FC4 machine:
# tcpdump -p -vvvX -s 250 -c 2 dst port 3000
tcpdump: listening on eth0, link-type EN10MB (Ethernet),
capture size 250 bytes
15:42:45.632378 IP (tos 0x0, ttl 64, id 33248, offset 0, flags [DF], proto
17, length: 172) ironsides2.myinternal.domain.com.32833 >
192.168.254.255.3000: [udp sum ok] UDP, length 144
0x0000: 4500 00ac 81e0 4000 4011 39ec c0a8 fe23 E.....@.@.9....#
0x0010: c0a8 feff 8041 0bb8 0098 98d3 0401 0101 .....A..........
0x0020: c03e 2662 0090 0000 03b3 960f 03ee 0100 .>&b............
0x0030: 0101 00e1 0601 0102 0101 00e1 0601 0102 ................
0x0040: 0000 0000 8000 0000 0000 0000 4114 3db2 ............A.=.
0x0050: a2dc 7580 c153 343f 9c91 99aa 414d ac97 ..u..S4?....AM..
0x0060: 3cdd 55e2 3fd1 db45 bf69 64fb c048 51dd <.U.?..E.id..HQ.
0x0070: 0000 0000 0200 0000 0000 0000 0000 0000 ................
0x0080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x0090: 0000 0000 0000 0000 0000 0000 0131 3032 .............102
0x00a0: 4531 3100 0000 0000 0000 0000 E11.........
15:42:45.700594 IP (tos 0x0, ttl 64, id 33249, offset 0, flags [DF], proto
17, length: 172) ironsides2.mynternal.domain.com.32833 >
192.168.254.255.3000: [udp sum ok] UDP, length 144
0x0000: 4500 00ac 81e1 4000 4011 39eb c0a8 fe23 E.....@.@.9....#
0x0010: c0a8 feff 8041 0bb8 0098 1a88 0401 0101 .....A..........
0x0020: c03f 634a 0090 0000 03b3 960f 03e9 0100 .?cJ............
0x0030: 0101 00e1 0601 0102 0101 00e1 0601 0102 ................
0x0040: 0000 0000 8000 0000 0000 0000 4114 56b8 ............A.V.
0x0050: 0d86 efa2 c153 3575 33e4 4f47 414d a92c .....S5u3.OGAM.,
0x0060: 6957 c1d9 3fd1 db42 bf65 4b12 4047 4b36 iW..?..B.eK.@GK6
0x0070: 0000 0000 0200 0000 0000 0000 0000 0000 ................
0x0080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x0090: 0000 0000 0000 0000 0000 0000 0131 3034 .............104
0x00a0: 4631 3100 0000 0000 0000 0000 F11.........
The configurations are as follows:
FC4 machine:
g++ 4.0.2
kernel 2.6.14-1.1637
Ubuntu machine:
g++ 4.0.1
kernel 2.6.10-5-686
On both machines I compile as follows:
g++ -Wall -Wextra tst.cpp -o tst
There are no compilation errors or warnings. Stripped of it's error
checking the code looks like this:
int localPort = 3000;
char m_bfr[8192];
int m_localSocket = socket(AF_INET, SOCK_DGRAM, 0);
int enable(1);
int len(sizeof(int));
int sts = setsockopt(m_localSocket, SOL_SOCKET, SO_BROADCAST,
&enable, len);
sts = setsockopt(m_localSocket, SOL_SOCKET, SO_REUSEADDR,
&enable, len);
// Bind m_localSocket to localPort.
struct sockaddr_in inname;
inname.sin_addr.s_addr = INADDR_ANY;
inname.sin_family = AF_INET;
inname.sin_port = htons(localPort);
bind(m_localSocket, (const sockaddr *)&inname, sizeof(inname)))
// Now read something.
struct sockaddr_in from;
unsigned int fromlen(sizeof(sockaddr_in));
int bytesRead = recvfrom(m_localSocket, m_bfr, m_maxBfrSize, 0,
(sockaddr *)&from, &fromlen);
On the FC4 machine t runs fine (no error returns anywhere), and blocks on
the recvfrom. I'm at a loss as to why it blocks when there _are_ packets
on the net, and other machines _can_ read them. The executable also runs
under RH8 running under Windows VMWare.
Any suggestions as to why this is so, or what I can do about it would be
greatly appreciated.
Thanks!
--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131