public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* Re: [ECOS] Re: How to send UDP broadcast to 255.255.255.255?
@ 2009-08-11 13:32 Stanislav Meduna
  2009-08-12  5:02 ` Mandeep Sandhu
  0 siblings, 1 reply; 11+ messages in thread
From: Stanislav Meduna @ 2009-08-11 13:32 UTC (permalink / raw)
  To: eCos Discussion

Jay Foster wrote:

> You can try the following patch that someone added
> to my local source.  It adds a CDL option
> (CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST

I also stumbled upon the problem mentioned in the
older post and I'd like to confirm that the
patch from

  http://www.mail-archive.com/ecos-discuss@ecos.sourceware.org/msg09992.html

seems to really help. Using this and setting the default
route to my interface (code borrowed from the ppp)

static int setDefaultRoute(u_int32_t g, int cmd)
{
  static int rtm_seq;
  int routes;
  struct {
    struct rt_msghdr	hdr;
    struct sockaddr_in	dst;
    struct sockaddr_in	gway;
    struct sockaddr_in	mask;
  } rtmsg;

  if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {

    diag_printf("Couldn't %s default route: socket: %d\n",
      cmd=='s'? "add": "delete",errno);
    return 0;
  }

  memset(&rtmsg, 0, sizeof(rtmsg));
  rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
  rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
  rtmsg.hdr.rtm_version = RTM_VERSION;
  rtmsg.hdr.rtm_seq = ++rtm_seq;
  rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
  rtmsg.dst.sin_len = sizeof(rtmsg.dst);
  rtmsg.dst.sin_family = AF_INET;
  rtmsg.gway.sin_len = sizeof(rtmsg.gway);
  rtmsg.gway.sin_family = AF_INET;
  rtmsg.gway.sin_addr.s_addr = g;
  rtmsg.mask.sin_len = sizeof(rtmsg.dst);
  rtmsg.mask.sin_family = AF_INET;

  rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
  if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
    diag_printf("Couldn't %s default route: %d\n",
      cmd=='s'? "add": "delete",errno);
    close(routes);
    return 0;
  }

  close(routes);

  return 1;
}


I am able to send the broadcasts as I intended.

I'd vote for the inclusion of the option in the main tree.

Thanks
-- 
                                Stano

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [ECOS] Re: How to send UDP broadcast to 255.255.255.255?
  2009-08-11 13:32 [ECOS] Re: How to send UDP broadcast to 255.255.255.255? Stanislav Meduna
@ 2009-08-12  5:02 ` Mandeep Sandhu
  2009-08-12 13:19   ` Mandeep Sandhu
  2009-08-12 13:25   ` Stanislav Meduna
  0 siblings, 2 replies; 11+ messages in thread
From: Mandeep Sandhu @ 2009-08-12  5:02 UTC (permalink / raw)
  To: Stanislav Meduna; +Cc: eCos Discussion

>
> I am able to send the broadcasts as I intended.

Are you using UDP or RAW sockets?

I've also setup my default route to point to eth0.

But when the UDP packet goes out, I see that it's corrupted.

Still debugging as to where the screwup is happening?

Thanks,
-mandeep

>
> I'd vote for the inclusion of the option in the main tree.
>
> Thanks
> --
>                                Stano
>
> --
> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
>
>

--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [ECOS] Re: How to send UDP broadcast to 255.255.255.255?
  2009-08-12  5:02 ` Mandeep Sandhu
@ 2009-08-12 13:19   ` Mandeep Sandhu
  2009-08-13  5:54     ` Mandeep Sandhu
  2009-08-12 13:25   ` Stanislav Meduna
  1 sibling, 1 reply; 11+ messages in thread
From: Mandeep Sandhu @ 2009-08-12 13:19 UTC (permalink / raw)
  To: Stanislav Meduna; +Cc: eCos Discussion

Hi All,

I've managed to get UDP broadcasts working w/o making any changes to
the FreeBSD stack of ecos.

Just wanted to detail out here the steps I've taken and get a feedback
from you guys on whether it's
proper or not...

I'm writing a DHCP server with limited-capability (which serves only 1
client). For this I need to send
out all my responses on the broadcast address.

* I've setup my servers eth0 statically as follows:

IP: 10.1.1.1
netmask: 255.255.255.0
Broadcast addr: 10.1.1.255
Gateway: 0
Server: 0

* After a call to init_all_network_interfaces() I setup a static "default" route
as follows:

route info:
Dest IP: 0.0.0.0/0.0.0.0 (any IP)
Gw  : 10.1.1.1 (me)
Dev : eth0

Flags: RTF_UP <--- VERY IMPORTANT!

I found that, previously I was setting the flags as "RTF_UP |
RTF_GATEWAY". Now this causes
the problem that in ip_output() function, the stack uses the
"destination" address of the gateway
(i.e 10.1.1.1) instead of the actual one (i.e the bcast IP
255.255.255.255) if RTF_GATEWAY flag
is set. This causes the packet transmission to fail when trying to
resolve the ARP. I don't know why
the ARP should fail, since its only trying to resolve for itself!

Anyways, I found this to be the cure for my problem, i.e setting the
default route and NOT putting
this flag.

* For sending bcast packets, I use raw sockets (with the relevant ioctl to set
the IP_HDRINCL flag).

This seems to be working. I've tested it on my Ubuntu machine with dhclient.

Do you think this is an acceptable workaround?

Thanks,
-mandeep


On Wed, Aug 12, 2009 at 10:32 AM, Mandeep
Sandhu<mandeepsandhu.chd@gmail.com> wrote:
>>
>> I am able to send the broadcasts as I intended.
>
> Are you using UDP or RAW sockets?
>
> I've also setup my default route to point to eth0.
>
> But when the UDP packet goes out, I see that it's corrupted.
>
> Still debugging as to where the screwup is happening?
>
> Thanks,
> -mandeep
>
>>
>> I'd vote for the inclusion of the option in the main tree.
>>
>> Thanks
>> --
>>                                Stano
>>
>> --
>> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
>> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
>>
>>
>

--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [ECOS] Re: How to send UDP broadcast to 255.255.255.255?
  2009-08-12  5:02 ` Mandeep Sandhu
  2009-08-12 13:19   ` Mandeep Sandhu
@ 2009-08-12 13:25   ` Stanislav Meduna
  1 sibling, 0 replies; 11+ messages in thread
From: Stanislav Meduna @ 2009-08-12 13:25 UTC (permalink / raw)
  To: Mandeep Sandhu, eCos Discussion

Mandeep Sandhu wrote:

>> I am able to send the broadcasts as I intended.
> 
> Are you using UDP or RAW sockets?

UDP

> I've also setup my default route to point to eth0.
> 
> But when the UDP packet goes out, I see that it's corrupted.

I did not inspect it carefully, but the receiver was able
to receive it.

-- 
                                  Stano

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [ECOS] Re: How to send UDP broadcast to 255.255.255.255?
  2009-08-12 13:19   ` Mandeep Sandhu
@ 2009-08-13  5:54     ` Mandeep Sandhu
  0 siblings, 0 replies; 11+ messages in thread
From: Mandeep Sandhu @ 2009-08-13  5:54 UTC (permalink / raw)
  To: eCos Discussion; +Cc: Stanislav Meduna

People!

Any comments about the below mail?

Thanks for your time.

Regards,
-mandeep


On Wed, Aug 12, 2009 at 6:49 PM, Mandeep
Sandhu<mandeepsandhu.chd@gmail.com> wrote:
> Hi All,
>
> I've managed to get UDP broadcasts working w/o making any changes to
> the FreeBSD stack of ecos.
>
> Just wanted to detail out here the steps I've taken and get a feedback
> from you guys on whether it's
> proper or not...
>
> I'm writing a DHCP server with limited-capability (which serves only 1
> client). For this I need to send
> out all my responses on the broadcast address.
>
> * I've setup my servers eth0 statically as follows:
>
> IP: 10.1.1.1
> netmask: 255.255.255.0
> Broadcast addr: 10.1.1.255
> Gateway: 0
> Server: 0
>
> * After a call to init_all_network_interfaces() I setup a static "default" route
> as follows:
>
> route info:
> Dest IP: 0.0.0.0/0.0.0.0 (any IP)
> Gw  : 10.1.1.1 (me)
> Dev : eth0
>
> Flags: RTF_UP <--- VERY IMPORTANT!
>
> I found that, previously I was setting the flags as "RTF_UP |
> RTF_GATEWAY". Now this causes
> the problem that in ip_output() function, the stack uses the
> "destination" address of the gateway
> (i.e 10.1.1.1) instead of the actual one (i.e the bcast IP
> 255.255.255.255) if RTF_GATEWAY flag
> is set. This causes the packet transmission to fail when trying to
> resolve the ARP. I don't know why
> the ARP should fail, since its only trying to resolve for itself!
>
> Anyways, I found this to be the cure for my problem, i.e setting the
> default route and NOT putting
> this flag.
>
> * For sending bcast packets, I use raw sockets (with the relevant ioctl to set
> the IP_HDRINCL flag).
>
> This seems to be working. I've tested it on my Ubuntu machine with dhclient.
>
> Do you think this is an acceptable workaround?
>
> Thanks,
> -mandeep
>
>
> On Wed, Aug 12, 2009 at 10:32 AM, Mandeep
> Sandhu<mandeepsandhu.chd@gmail.com> wrote:
>>>
>>> I am able to send the broadcasts as I intended.
>>
>> Are you using UDP or RAW sockets?
>>
>> I've also setup my default route to point to eth0.
>>
>> But when the UDP packet goes out, I see that it's corrupted.
>>
>> Still debugging as to where the screwup is happening?
>>
>> Thanks,
>> -mandeep
>>
>>>
>>> I'd vote for the inclusion of the option in the main tree.
>>>
>>> Thanks
>>> --
>>>                                Stano
>>>
>>> --
>>> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
>>> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
>>>
>>>
>>
>

--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ECOS]  Re: How to send UDP broadcast to 255.255.255.255?
  2009-06-16 19:56   ` Gary Thomas
@ 2009-06-16 20:26     ` Grant Edwards
  0 siblings, 0 replies; 11+ messages in thread
From: Grant Edwards @ 2009-06-16 20:26 UTC (permalink / raw)
  To: ecos-discuss

On 2009-06-16, Gary Thomas <gary@mlbassoc.com> wrote:
> Grant Edwards wrote:
>> On 2009-06-16, Jay Foster <jay@systech.com> wrote:
>> 
>>> You can try the following patch that someone added to my local source.  It
>>> adds a CDL option (CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST) to modify this
>>> behavior (the default being the same behavior you have now).  Sorry it isn't
>>> in "patch" format, but it is straight forward.  What is not shown here is
>>> configuring this option to '0' in your applications .ecm or template file.
>> 
>> Thanks.  I'll have to check to see if the need for the feature
>> is significant enough to justify forking the network stack
>> (something I'm pretty ruluctant to do).
>
> If this works and is useful to you, we can just check it in.
> After all, it's controlled by CDL, so the default behaviour
> will remain the same.

Cool. I'll check to see if it's something we still need to do.

-- 
Grant Edwards                   grante             Yow! I smell like a wet
                                  at               reducing clinic on Columbus
                               visi.com            Day!


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [ECOS]  Re: How to send UDP broadcast to 255.255.255.255?
  2009-06-16 19:01 ` Grant Edwards
@ 2009-06-16 19:56   ` Gary Thomas
  2009-06-16 20:26     ` Grant Edwards
  0 siblings, 1 reply; 11+ messages in thread
From: Gary Thomas @ 2009-06-16 19:56 UTC (permalink / raw)
  To: Grant Edwards; +Cc: ecos-discuss

Grant Edwards wrote:
> On 2009-06-16, Jay Foster <jay@systech.com> wrote:
> 
>> You can try the following patch that someone added to my local source.  It
>> adds a CDL option (CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST) to modify this
>> behavior (the default being the same behavior you have now).  Sorry it isn't
>> in "patch" format, but it is straight forward.  What is not shown here is
>> configuring this option to '0' in your applications .ecm or template file.
> 
> 
> Thanks.  I'll have to check to see if the need for the feature
> is significant enough to justify forking the network stack
> (something I'm pretty ruluctant to do).
> 

If this works and is useful to you, we can just check it in.
After all, it's controlled by CDL, so the default behaviour
will remain the same.

-- 
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ECOS]  Re: How to send UDP broadcast to 255.255.255.255?
  2009-06-16 16:14 Jay Foster
@ 2009-06-16 19:01 ` Grant Edwards
  2009-06-16 19:56   ` Gary Thomas
  0 siblings, 1 reply; 11+ messages in thread
From: Grant Edwards @ 2009-06-16 19:01 UTC (permalink / raw)
  To: ecos-discuss

On 2009-06-16, Jay Foster <jay@systech.com> wrote:

> You can try the following patch that someone added to my local source.  It
> adds a CDL option (CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST) to modify this
> behavior (the default being the same behavior you have now).  Sorry it isn't
> in "patch" format, but it is straight forward.  What is not shown here is
> configuring this option to '0' in your applications .ecm or template file.


Thanks.  I'll have to check to see if the need for the feature
is significant enough to justify forking the network stack
(something I'm pretty ruluctant to do).

-- 
Grant Edwards                   grante             Yow! Are the STEWED PRUNES
                                  at               still in the HAIR DRYER?
                               visi.com            


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [ECOS]  Re: How to send UDP broadcast to 255.255.255.255?
@ 2009-06-16 16:14 Jay Foster
  2009-06-16 19:01 ` Grant Edwards
  0 siblings, 1 reply; 11+ messages in thread
From: Jay Foster @ 2009-06-16 16:14 UTC (permalink / raw)
  To: 'Grant Edwards', ecos-discuss

You can try the following patch that someone added to my local source.  It
adds a CDL option (CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST) to modify this
behavior (the default being the same behavior you have now).  Sorry it isn't
in "patch" format, but it is straight forward.  What is not shown here is
configuring this option to '0' in your applications .ecm or template file.

int
in_pcbladdr(inp, nam, plocal_sin)
	register struct inpcb *inp;
	struct sockaddr *nam;
	struct sockaddr_in **plocal_sin;
{
	struct in_ifaddr *ia;
	register struct sockaddr_in *sin = (struct sockaddr_in *)nam;

	if (nam->sa_len != sizeof (*sin))
		return (EINVAL);
	if (sin->sin_family != AF_INET)
		return (EAFNOSUPPORT);
	if (sin->sin_port == 0)
		return (EADDRNOTAVAIL);
	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
		/*
		 * If the destination address is INADDR_ANY,
		 * use the primary local address.
		 * If the supplied address is INADDR_BROADCAST,
		 * and the primary interface supports broadcast,
		 * choose the broadcast address for that interface.
		 */
#define	satosin(sa)	((struct sockaddr_in *)(sa))
#define sintosa(sin)	((struct sockaddr *)(sin))
#define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
		if (sin->sin_addr.s_addr == INADDR_ANY)
		    sin->sin_addr =
IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
#ifdef CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST
		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
		  (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
IFF_BROADCAST))
		    sin->sin_addr =
satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
#endif
	}
	if (inp->inp_laddr.s_addr == INADDR_ANY) {
		register struct route *ro;


From net.cdl:

    cdl_component CYGPKG_NET_INET {
        display       "INET support"
        active_if     CYGPKG_NET_STACK_INET
        flavor        bool
        no_define
        default_value 1
        description   "
            This option enables support for INET (IP) network processing."
        define INET
        compile \
            inet_addr.c \
            inet_ntoa.c \
            inet_ntop.c \
            inet_pton.c \
            bootp_support.c \
            dhcp_support.c \
            dhcp_prot.c \
            network_support.c \
            getproto.c \
            getserv.c 
        compile   getaddrinfo.c \
            ifaddrs.c

        cdl_option CYGOPT_NET_INET_FORCE_DIRECTED_BROADCAST {
            display       "Convert limited broadcast addresses into directed
broadcasts"
            flavor        bool
            default_value 1
            description   "
                De-facto IP stack implementations often convert
INADDR_BROADCAST
                destination addresses into a more specific directed
broadcast
                address according to the address configuration of the
primary
                network interface.  Over time, this behavior has been
criticized
                for various reasons.  The primary reason is that it becomes
                impossible to send UDP packets to 255.255.255.255 once the
                primary network (broadcast) interface is configured with
address
                information."
        }

Jay



-----Original Message-----
From: Grant Edwards [mailto:grante@visi.com]
Sent: Tuesday, June 16, 2009 7:23 AM
To: ecos-discuss@sources.redhat.com
Subject: [ECOS] Re: How to send UDP broadcast to 255.255.255.255?


On 2009-06-16, Andrew Lunn <andrew@lunn.ch> wrote:
> On Mon, Jun 15, 2009 at 05:41:54PM +0000, Grant Edwards wrote:
>> I've been asked by one of my internal customers how to send a
>> UDP broadcast packet to IP address 255.255.255.255.
>
> Hi Grant
>
> If you have a debugger handy, try putting a break point here:

I don't have one handy, but it certainly looks like the right
spot.

> src/sys/netinet/in_pcb.c
> int
> in_pcbladdr(inp, nam, plocal_sin)
>         register struct inpcb *inp;
>         struct sockaddr *nam;
>         struct sockaddr_in **plocal_sin;
> {
>         struct in_ifaddr *ia;
>         register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
>
>         if (nam->sa_len != sizeof (*sin))
>                 return (EINVAL);
>         if (sin->sin_family != AF_INET)
>                 return (EAFNOSUPPORT);
>         if (sin->sin_port == 0)
>                 return (EADDRNOTAVAIL);
>         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
>                 /*
>                  * If the destination address is INADDR_ANY,
>                  * use the primary local address.
>                  * If the supplied address is INADDR_BROADCAST,
>                  * and the primary interface supports broadcast,
>                  * choose the broadcast address for that interface.
>                  */
> #define satosin(sa)     ((struct sockaddr_in *)(sa))
> #define sintosa(sin)    ((struct sockaddr *)(sin))
> #define ifatoia(ifa)    ((struct in_ifaddr *)(ifa))
>                 if (sin->sin_addr.s_addr == INADDR_ANY)
>                     sin->sin_addr =
IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
>                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST
&&
>                   (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
IFF_BROADCAST))
>                     sin->sin_addr =
satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
>
> Unfortunately, if this is the place the address is being changed, i
> don't see an obvious way around this.


I've been doing some experiments on a FreeBSD 6.1 system, and
it won't allow applications to send a 255.255.255.255 broadcast
either.  In fact, it _requires_ you to specify the interface's
broadcast address as a destination before in order to broadcast
a packet.  FreeBSD 6.1 won't "convert" 255.255.255.255 into the
interface broadcast address the way that eCos does.

Still, the Linux implementation seems to be a lot more useful.
You can specify either 255.255.255.255 or the interface's
broadcast address as the destination, and the packet is
broadcast to that destination address.

I found complaints dating back many years regarding FreeBSD's
lack of ability to send broadcast UDP packets to
255.255.255.255.  It seems you have to use the raw socket
interface to do that.

-- 
Grant Edwards                   grante             Yow! Pardon me, but do
you
                                  at               know what it means to be
                               visi.com            TRULY ONE with your
BOOTH!


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ECOS]  Re: How to send UDP broadcast to 255.255.255.255?
  2009-06-16  6:44 ` [ECOS] " Andrew Lunn
@ 2009-06-16 14:23   ` Grant Edwards
  0 siblings, 0 replies; 11+ messages in thread
From: Grant Edwards @ 2009-06-16 14:23 UTC (permalink / raw)
  To: ecos-discuss

On 2009-06-16, Andrew Lunn <andrew@lunn.ch> wrote:
> On Mon, Jun 15, 2009 at 05:41:54PM +0000, Grant Edwards wrote:
>> I've been asked by one of my internal customers how to send a
>> UDP broadcast packet to IP address 255.255.255.255.
>
> Hi Grant
>
> If you have a debugger handy, try putting a break point here:

I don't have one handy, but it certainly looks like the right
spot.

> src/sys/netinet/in_pcb.c
> int
> in_pcbladdr(inp, nam, plocal_sin)
>         register struct inpcb *inp;
>         struct sockaddr *nam;
>         struct sockaddr_in **plocal_sin;
> {
>         struct in_ifaddr *ia;
>         register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
>
>         if (nam->sa_len != sizeof (*sin))
>                 return (EINVAL);
>         if (sin->sin_family != AF_INET)
>                 return (EAFNOSUPPORT);
>         if (sin->sin_port == 0)
>                 return (EADDRNOTAVAIL);
>         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
>                 /*
>                  * If the destination address is INADDR_ANY,
>                  * use the primary local address.
>                  * If the supplied address is INADDR_BROADCAST,
>                  * and the primary interface supports broadcast,
>                  * choose the broadcast address for that interface.
>                  */
> #define satosin(sa)     ((struct sockaddr_in *)(sa))
> #define sintosa(sin)    ((struct sockaddr *)(sin))
> #define ifatoia(ifa)    ((struct in_ifaddr *)(ifa))
>                 if (sin->sin_addr.s_addr == INADDR_ANY)
>                     sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
>                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
>                   (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
>                     sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
>
> Unfortunately, if this is the place the address is being changed, i
> don't see an obvious way around this.


I've been doing some experiments on a FreeBSD 6.1 system, and
it won't allow applications to send a 255.255.255.255 broadcast
either.  In fact, it _requires_ you to specify the interface's
broadcast address as a destination before in order to broadcast
a packet.  FreeBSD 6.1 won't "convert" 255.255.255.255 into the
interface broadcast address the way that eCos does.

Still, the Linux implementation seems to be a lot more useful.
You can specify either 255.255.255.255 or the interface's
broadcast address as the destination, and the packet is
broadcast to that destination address.

I found complaints dating back many years regarding FreeBSD's
lack of ability to send broadcast UDP packets to
255.255.255.255.  It seems you have to use the raw socket
interface to do that.

-- 
Grant Edwards                   grante             Yow! Pardon me, but do you
                                  at               know what it means to be
                               visi.com            TRULY ONE with your BOOTH!


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ECOS]  Re: How to send UDP broadcast to 255.255.255.255?
  2009-06-15 20:36 ` Sergei Gavrikov
@ 2009-06-15 22:14   ` Grant Edwards
  0 siblings, 0 replies; 11+ messages in thread
From: Grant Edwards @ 2009-06-15 22:14 UTC (permalink / raw)
  To: ecos-discuss

On 2009-06-15, Sergei Gavrikov <sergei.gavrikov@gmail.com> wrote:
> Grant Edwards wrote:
>> I've been asked by one of my internal customers how to send a
>> UDP broadcast packet to IP address 255.255.255.255.
>> 
>> I tried setting the SO_BROADCAST option on the socket and then
>> using sendto() with a destination address of 255.255.255.255,
>> but it sends to the subnet broadcast address (in my case
>> 10.255.255.255) not to the global broadcast address of
>> 255.255.255.255 that I specified in the sendto() call.
>
> [snip]
>
>>   if ((bytesSent = sendto(socket_fd, send_buf, packetSize, 0, (struct sockaddr *)&encoder_addr, sizeof encoder_addr)) == -1)
>
> Hi,
>
> I noticed one thing only. What's about sendto's flags? they talk about
> `MSG_DONTROUTE' sendto flag for your needs:
>
> packages/net/tcpip/current/doc/sendto.html
>
> perhaps, that's it.

I just tried it, and it doesn't change the behavior of
broadcasts. Specifying a destination address of 255.255.255.255
still produces a packet that's addressed to 10.255.255.255.

The description of MSG_DONTROUTE is

       Bypasses the usual routing table lookup and sends the
       packet directly to the interface described by the
       destination address. This is usually used only by
       diagnostic or routing programs.

In my case, the destination doesn't describe a particular
interface.  One might presume a broadcast sent to
255.255.255.255 would be sent out on all interfaces.
       
Google did find me a posting to a BSD mailing list that seemed
to claim that the option IP_ONESBCAST would do what I wanted to
do. I looked at the ip(4) man page on a FreeBSD 6.1 system, and
the description of IP_ONESBCAST didn't agree with what the
mailing list posting said.  I tested it on FreeBSD 6.1, and it
doesn't do anything relevent.  eCos doesn't have the
IP_ONESBCAST option anyway....

-- 
Grant Edwards                   grante             Yow! I own seven-eighths of
                                  at               all the artists in downtown
                               visi.com            Burbank!


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-08-13  5:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-11 13:32 [ECOS] Re: How to send UDP broadcast to 255.255.255.255? Stanislav Meduna
2009-08-12  5:02 ` Mandeep Sandhu
2009-08-12 13:19   ` Mandeep Sandhu
2009-08-13  5:54     ` Mandeep Sandhu
2009-08-12 13:25   ` Stanislav Meduna
  -- strict thread matches above, loose matches on Subject: below --
2009-06-16 16:14 Jay Foster
2009-06-16 19:01 ` Grant Edwards
2009-06-16 19:56   ` Gary Thomas
2009-06-16 20:26     ` Grant Edwards
2009-06-15 17:42 [ECOS] " Grant Edwards
2009-06-15 20:36 ` Sergei Gavrikov
2009-06-15 22:14   ` [ECOS] " Grant Edwards
2009-06-16  6:44 ` [ECOS] " Andrew Lunn
2009-06-16 14:23   ` [ECOS] " Grant Edwards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).