mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Risacher <magnus@alum.mit.edu>
To: linux-kernel@vger.kernel.org
Subject: source address for UDP broadcasts with aliased interfaces?
Date: Fri, 12 Nov 2004 16:49:50 -0500	[thread overview]
Message-ID: <87lld6n3ep.fsf@m5.risacher.org> (raw)


I'm not sure if this is a bug or a feature.

There seems to be no way to get the kernel to send a UDP broadcast
packet to INADDR_BROADCAST from a specific address, short of using
iptables.  

I would think that bind()ing a socket to a local address would
determine the source address, but the kernel seems to ignore the bound
address.  

I'm trying to write a program that communicates with multiple hosts on
a local net, several of which have firewall rules that drop packets
from external addresses.  The source computer has multiple aliased
addresses on the eth0 interface.  I therefore want to broadcast from a
specific non-routable internal address.  I've experimented with bind()
and setsockopt(,SO_BINDTODEVICE,) to determine the source address
and/or interface but the kernel seems to pick whatever it wants.

Is there a correct way to specify the sender address/interface?

Dan Risacher

(reference code follows; testing was done on Linux 2.6.9)

  struct sockaddr_in to_addr;
  struct sockaddr_in my_addr;
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(0);
  my_addr.sin_addr.s_addr = find_if_addr(interface);
  to_addr.sin_family = AF_INET;
  to_addr.sin_port = htons(11415);
  to_addr.sin_addr.s_addr = INADDR_BROADCAST;

  fd = socket(PF_INET, SOCK_DGRAM, 0);
 
  res = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
  if (res) perror ("setsockopt (SO_BROADCAST)");

  res = bind(fd, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in));
  if (res) perror ("bind");  
  
  res = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, interface, 1+strlen(interface));
  if (res) perror ("setsockopt (SO_BINDTODEVICE)");

  res = sendto(fd, mesg, strlen(mesg), MSG_DONTROUTE, 
	 (struct sockaddr *)&to_addr, 
	 sizeof(struct sockaddr_in));
  if (res == -1) perror ("sendto");




             reply	other threads:[~2004-11-12 21:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-12 21:49 Daniel Risacher [this message]
2004-11-15 14:13 ` Alan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lld6n3ep.fsf@m5.risacher.org \
    --to=magnus@alum.mit.edu \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome