comp.lang.ada
 help / color / mirror / Atom feed
From: ahlan@marriott.org
Subject: Re: Broadcasting UDP
Date: Mon, 25 Apr 2016 07:18:59 -0700 (PDT)
Date: 2016-04-25T07:18:59-07:00	[thread overview]
Message-ID: <0ea06785-65e5-4c33-b8b1-1fc6e7e66740@googlegroups.com> (raw)
In-Reply-To: <nfivct$128v$1@gioia.aioe.org>

On Sunday, April 24, 2016 at 7:22:45 PM UTC+2, Dmitry A. Kazakov wrote:
> On 2016-04-24 18:31, ahlan.marriott@gmail.com wrote:
> > I asked this question sometime ago but I can no longer find the post.
> > In any case it was never really resolved so let me try again.
> > I am looking for a platform independent Ada solution on how to broadcast a UDP packet.
> > I would be satisfied with a GNAT only solution, i.e. one that uses Gnat.Sockets and/or Gnat specific libraries.
> > Attempting to broadcast to the limited broadcast address 255.255.255.255 has no effect (under Windows at least)
> > To successfully broadcast one needs to use the subnet directed broadcast address.
> > As I want to support PCs that have multiple ethernet adapters this
> > means that I must iterate over the ethernet adapters, discover my
> > ethernet address and subnet for each adapter, calculate the broadcast
> > address and then send the UDP packet to that address.
> > So far so good.
> > My problem is that I don't know how to iterate over my adapters and
> > obtain the address and subnet mask for each adapter using just Ada.
> > Can anyone tell me how I can do this using Ada?
> 
> declare
>     Host    : Host_Entry_Type := Get_Host_By_Name (Host_Name);
>     Address : aliased Sock_Addr_Type;
> begin
>     for Index in 1..Addresses_Length (Host) loop
>        Address.Addr := Addresses (Host, Index)));
>        Address.Port := <port>;
>        declare
>           Socket : Socket_Type := No_Socket;
>           Pier   : Sock_Addr_Type;
>        begin
>           Create_Socket (Socket, Family_Inet, Socket_Datagram);
>           Set_Socket_Option
>           (  Socket,
>              Socket_Level,
>              (Reuse_Address, True)
>           );
>           Set_Socket_Option
>           (  Socket,
>              Socket_Level,
>              (Broadcast, True)
>           );
>           Set_Socket_Option
>           (  Socket,
>              Socket_Level,
>              (Receive_Timeout, <timeout>)
>           );
>           Bind_Socket (Socket, Address);
>           Address.Addr := Broadcast_Inet_Addr;
>           Send_Socket (Socket, <request-packet>, Last, Address'Access);
>           loop -- Collecting responses, time-limited <timeout>
>              ...
>              Receive_Socket (Socket, <response-packet>, Last, Pier);
>              ...
>           end loop;
>           Close_Socket (Socket);
>        end;
>     end loop;
> end;
> 
> > In which case I would be grateful if anyone could tell me how I can iterate my adapters under Linux
> 
> Under Linux it is through the proc file system, if I correctly remember. 
> I don't have it in the head right now.
> 
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de

Dear Dmitry,

This of course works but by using Broadcast_Inet_Addr (255.255.255.255) then we actually transmit more UDP messages than is strictly necessary.

For example if I have two adapters one with 100.13.5.200 and the other 169.254.7.7 and I send a UDP broadcast on each adapter then four UDP messages are actually transmitted.
1) Src=100.13.5.200 Dest=255.255.255.255 on adapter 1
2) Src=100.13.5.200 Dest=255.255.255.255 on adapter 2
3) Src=169.254.7.7 Dest=255.255.255.255 on adapter 1
4) Src=169.254.7.7 Dest=255.255.255.255 on adapter 2

This is because the destination address 255.255.255.255 is for all adapters.
Whereas if I could somehow find out the subnet mask for the adapter I could refine the destination address to be a subnet directed broadcast address and thereby could prevent the unnecessary traffic.
In my example if the subnet for both adapters was 255.255.0.0 then the broadcast address for adapter 1 would be 100.13.255.255 and for adapter 2 169.254.255.255.

If I use subnet directed broadcast addresses then on my test PCs running Ms-Windows messages are only sent on the adapter if the subnet matches.
Ie only two messages are transmitted, one per adapter.

This is what I was aiming for.
However, although I can find out the host address for each adapter, I haven't been able to find out how I can determine out their subnet masks.

If I could find out the subnet masks I could derive the subnet directed broadcast addresses and use these as the destination addresses and thereby reduce the traffic.

Not by much I grant you but every little helps and besides it would be a little more elegant than using the crude broadcast to all on all adapters approach.

Any ideas?
MfG
Ahlan

  reply	other threads:[~2016-04-25 14:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-24 16:31 Broadcasting UDP ahlan.marriott
2016-04-24 17:22 ` Dmitry A. Kazakov
2016-04-25 14:18   ` ahlan [this message]
2016-04-25 14:51     ` Dmitry A. Kazakov
2016-04-25 17:11       ` ahlan.marriott
2016-04-26 16:21         ` ahlan
2016-04-29 23:35 ` douty.chris
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox