// JP opened flex table

Click to See Complete Forum and Search --> : send UDP broadcast to broadcast MAC address?


Elis
December 12th, 2007, 08:12 PM
Hello.

I´m programming C in FreeBSD.
When I try to send a broadcast datagram something does not work as I want it to.
When I look at the datagram with wireshark it says that it was sent to IP-number 255.255.255.255 (which you would expect, since that is the broadcast address).

NOW TO MY PROBLEM:
But the ethernet address destination is the MAC address of my router.. =(
This means that only my router gets the datagram even though it is a broadcast datagram.

It should be ff:ff:ff:ff:ff:ff <- broadcast MAC address.

My code works fine as long as I don´t send broadcast datagram. So I hope I only need to do some small adjustment to get it going.. =)
Anyone have any idea of what I might be doing wrong?

Here is the code I use to send it:

int socket_fd;

struct sockaddr_in encoder_addr;
struct sockaddr_in my_addr;


char send_buf[2000];


if ((socket_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MX_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

bind(socket_fd, (struct sockaddr *)&my_addr, sizeof my_addr);


//mark the socket for broadcasting
int broadcast=1;

if (setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) == -1)
{
perror("setsockopt (SO_BROADCAST)");
exit(1);
}


encoder_addr.sin_family = AF_INET;
encoder_addr.sin_port = htons(MX_PORT);
encoder_addr.sin_addr.s_addr = INADDR_BROADCAST; //inet_addr(IP_address);
memset(encoder_addr.sin_zero, '\0', sizeof encoder_addr.sin_zero);






/* Send the MX-packet */
int totalBytesSent = 0;
int bytesSent;
int packetSize = sizeof(mx_hdr_t) + sizeof(mx_frm_t)*mx_hdr->cnt + mx_frm->len;
printf("Total packetSize = %d\n", packetSize);
int totalBytesLeft = packetSize;
while(totalBytesSent < packetSize)
{
if ((bytesSent = sendto(socket_fd, mx_hdr+totalBytesSent, totalBytesLeft, 0,
(struct sockaddr *)&encoder_addr, sizeof encoder_addr)) == -1)
{
perror("sendto");
exit(1);
}
totalBytesSent += bytesSent;
totalBytesLeft -= bytesSent;
printf("sent %d bytes to %s\n", bytesSent, inet_ntoa(encoder_addr.sin_addr));
}
close(socket_fd);

upredsun
December 27th, 2007, 02:57 AM
IP-number 255.255.255.255 is not real broadcast address.

you should figure it out by your ip address and your network mask address.

Elis
January 8th, 2008, 07:39 PM
But shouldn´t it still work with 255.255.255.255 ?
I know there is another broadcast address aswell.. but this is the one that my program send to. And it should work, if only the destination MAC-address was not set to the MAC-address of my router.. =(

I´m still stuck on this one.. any tips are appreciated.

MrViggy
January 9th, 2008, 12:00 PM
Hello.

I´m programming C in FreeBSD.
When I try to send a broadcast datagram something does not work as I want it to.
When I look at the datagram with wireshark it says that it was sent to IP-number 255.255.255.255 (which you would expect, since that is the broadcast address).

NOW TO MY PROBLEM:
But the ethernet address destination is the MAC address of my router.. =(
This means that only my router gets the datagram even though it is a broadcast datagram.
I'm curions as to why your router has a MAC address of ff:ff:ff:ff:ff:ff :confused:

Viggy

upredsun
January 10th, 2008, 04:14 AM
255.255.255.255,this broadcast address will work with one router,i think,it will not work if the destination address reside in another router.

Elis
January 10th, 2008, 09:54 AM
MrViggy: "I'm curions as to why your router has a MAC address of ff:ff:ff:ff:ff:ff"

My router does not have that MAC address. But the broadcast datagrams I send end up with a dest ethernet address (MAC address) that is the MAC address of my router. This should be ff:ff:ff:ff:ff:ff for bradcast.


upredsun: "255.255.255.255,this broadcast address will work with one router,i think,it will not work if the destination address reside in another router."

Yes, 255.255.255.255 will NOT send the broadcast all over the internet. Only within your own network, which means that the next router will probably block the datagram/packet.

//JP added flex table