comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Broadcasting UDP
Date: Sun, 24 Apr 2016 19:22:39 +0200
Date: 2016-04-24T19:22:39+02:00	[thread overview]
Message-ID: <nfivct$128v$1@gioia.aioe.org> (raw)
In-Reply-To: 276b8c43-7280-48ad-94be-c8324c6ae326@googlegroups.com

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

  reply	other threads:[~2016-04-24 17:22 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 [this message]
2016-04-25 14:18   ` ahlan
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