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);
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);