[CentOS] playing with SO_BROADCAST on centos

Mon Feb 12 13:48:10 UTC 2007
Jerry Geis <geisj at pagestation.com>

I have a small program that I am broadcasting out on port 36 and
attempting to receive information back.

Using "tcpdump port 36" I can see 2 devices responding to me
but I get no data.

The response my program gets is:

Size IP Address      Subnet          MAC Address           ID
 20  0.0.0.0         0.0.0.0         00-00-00-00-00-00

Does someone know what I might not be doing correct?
Running the same program under windows works, running it under wine on 
linux works
it just doesnt work on linux.

I have iptables disabled at this time.

Thanks for nay suggestions.

Jerry
----------------------------

tcpdump -x port 36
-------------------------
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
08:42:49.286701 IP devcentos64.msgnet.com.36 > 255.255.255.255.36: UDP, 
length 20
        0x0000:  4500 0030 0000 4000 4011 790b c0a8 010a  E..0.. at .@.y.....
        0x0010:  ffff ffff 0024 0024 001c ca61 4321 3039  .....$.$...aC!09
        0x0020:  0000 0000 0000 0000 0000 0000 0000 0000  ................
08:42:49.286976 IP ecov132.msgnet.com.36 > devcentos64.msgnet.com.36: 
UDP, length 20
        0x0000:  4500 0030 03c2 0000 8011 b2f2 c0a8 01ae  E..0............
        0x0010:  c0a8 010a 0024 0024 001c a0e2 4321 3039  .....$.$....C!09
        0x0020:  c0a8 01ae ffff 0000 0001 3cd0 67ff 0001  ..........<.g...
08:42:49.286990 IP ecov432_testing.msgnet.com.36 > 
devcentos64.msgnet.com.36: UDP, length 20
        0x0000:  4500 0030 091a 0000 8011 ad8f c0a8 01b9  E..0............
        0x0010:  c0a8 010a 0024 0024 001c b581 4321 3039  .....$.$....C!09
        0x0020:  c0a8 01b9 ffff ff00 0001 3cd0 5449 0001  ..........<.TI..

My program is:

int main(int argc, char *argv[])
{
int error;
int total = 0;
int socket_handle;

enum 
	{
	NMP_PORT = 36,
	NMP_MAGIC = 0x4321,
	NMP_LIST = 12345,
	NMP_SET_IP,
	NMP_SET_MASK
	};

typedef union 
	{
	unsigned char b[4];
	uint32_t v;
	} IPADDR;

typedef struct 
	{
	unsigned char b[6];
	} MACADDR;

typedef struct 
	{
	unsigned short magic;
	unsigned short code;
	IPADDR ip;
	IPADDR mask;
	MACADDR mac;
	ushort Deviceid;
	} HD_NMP;

HD_NMP nmp;

struct sockaddr_in broadcast_address;

bzero((char *) &broadcast_address, sizeof(broadcast_address));
broadcast_address.sin_family = AF_INET;
broadcast_address.sin_port = htons(NMP_PORT);
broadcast_address.sin_addr.s_addr = inet_addr("255.255.255.255");

socket_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(socket_handle >= 0)
	{
	int byes = 1;

	fd_set read_set;

	struct timeval tm;

	if(setsockopt(socket_handle, SOL_SOCKET, SO_BROADCAST, &byes, sizeof(byes)) >= 0)
		{
		if(bind(socket_handle, (struct sockaddr *) &broadcast_address, sizeof(broadcast_address)) < 0)
			{
			printf("bind error\n");
			}

		memset(&nmp, 0, sizeof(nmp));
		nmp.magic = IWord(NMP_MAGIC);
		nmp.code = IWord(NMP_LIST);
		sendto(socket_handle, &nmp, sizeof(nmp), 0, (struct sockaddr *) &broadcast_address, sizeof(broadcast_address));
	
		printf("%-4s %-15s %-15s %-17s %6s\n", "Size", "IP Address", "Subnet", "MAC Address", "ID");
	
		while(1)
			{
			tm.tv_sec = 0;
			tm.tv_usec = 500000;
	
			FD_ZERO(&read_set);
			FD_SET(socket_handle, &read_set);
			error = select(socket_handle + 1, &read_set, NULL, NULL, &tm);
			if(error <= 0)
				{
				break;
				}
			else
				{
				char ip_str[100];
				char mac_str[100];
				char mask_str[100];

				error = recvfrom(socket_handle, &nmp, sizeof(nmp), 0, NULL, 0);

				if(error < 0)
					{
					printf("errno=%d %s\n", errno, strerror(errno));
					continue;
					}
 				else if(nmp.code != IWord(NMP_LIST))
					{
					printf("unknown return\n");
					continue;
					}
				else
					{
					sprintf(ip_str, "%d.%d.%d.%d ", nmp.ip.b[0], nmp.ip.b[1], nmp.ip.b[2], nmp.ip.b[3]);
					sprintf(mask_str, "%d.%d.%d.%d ", nmp.mask.b[0], nmp.mask.b[1], nmp.mask.b[2], nmp.mask.b[3]);
					sprintf(mac_str, "%02X-%02X-%02X-%02X-%02X-%02X", nmp.mac.b[0], nmp.mac.b[1], nmp.mac.b[2], nmp.mac.b[3], nmp.mac.b[4], nmp.mac.b[5]);
					printf(" %2d  %-15s %-15s %-15s\n", error, ip_str, mask_str, mac_str);

					total++;
					}
				}
			}
		}

	close(socket_handle);
	}

return(0);
}