comp.lang.ada
 help / color / mirror / Atom feed
* Making a Inet_Addr_Type out of octects (GNAT.Sockets)
@ 2008-10-07 19:43 mockturtle
  2008-10-07 20:11 ` Jeffrey R. Carter
  2008-10-08 10:28 ` anon
  0 siblings, 2 replies; 4+ messages in thread
From: mockturtle @ 2008-10-07 19:43 UTC (permalink / raw)


Dear.all,
a very simple (almost silly) question whose answer, I fear, is
"no."...

I have 4 octects (read by a received packet) which rappresent
an IP address and  I want to convert them into a Inet_Addr_Type
(using GNAT.Sockets). Of course I could convert the 4 octects
into a string to be given to function Inet_Addr_Type, but it seems
a bit "convoluted" (it sounds like the joke about the mathematician's
algorithm to make a pot of hot water :-).   My question is: there
is a  function which  allows me to do such a conversion in a
more direct way? I looked at g-sockets, searching for
"Inet_Addr_Type", but I could not  find anything.

Thank you in advance.



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

* Re: Making a Inet_Addr_Type out of octects (GNAT.Sockets)
  2008-10-07 19:43 Making a Inet_Addr_Type out of octects (GNAT.Sockets) mockturtle
@ 2008-10-07 20:11 ` Jeffrey R. Carter
  2008-10-08  1:53   ` anon
  2008-10-08 10:28 ` anon
  1 sibling, 1 reply; 4+ messages in thread
From: Jeffrey R. Carter @ 2008-10-07 20:11 UTC (permalink / raw)


mockturtle wrote:
> 
> I have 4 octects (read by a received packet) which rappresent
> an IP address and  I want to convert them into a Inet_Addr_Type
> (using GNAT.Sockets). Of course I could convert the 4 octects
> into a string to be given to function Inet_Addr_Type, but it seems
> a bit "convoluted" (it sounds like the joke about the mathematician's
> algorithm to make a pot of hot water :-).   My question is: there
> is a  function which  allows me to do such a conversion in a
> more direct way? I looked at g-sockets, searching for
> "Inet_Addr_Type", but I could not  find anything.

I'm not aware of any way provided by the pkg other than that you outlined above.

There are ways using Unchecked_Conversion and address overlays, but they are Not 
Recommended.

-- 
Jeff Carter
"My legs are gray, my ears are gnarled, my eyes are old and bent."
Monty Python's Life of Brian
81



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

* Re: Making a Inet_Addr_Type out of octects (GNAT.Sockets)
  2008-10-07 20:11 ` Jeffrey R. Carter
@ 2008-10-08  1:53   ` anon
  0 siblings, 0 replies; 4+ messages in thread
From: anon @ 2008-10-08  1:53 UTC (permalink / raw)


if you mean "4 octects" is 4 byte array, then
with GNAT.Sockets.Thin ;
use  GNAT.Sockets.Thin ;

        Bytes    : array ( 0..3 ) of Interfaces.C.unsigned_char;
        Sin_Addr : In_Addr ;

        Sin_Addr.S_B1 := Bytes ( 0 ) ;
        Sin_Addr.S_B2 := Bytes ( 1 ) ;
        Sin_Addr.S_B3 := Bytes ( 2 ) ;
        Sin_Addr.S_B4 := Bytes ( 3 ) ;


-- located with in GNAT.Socket package

with GNAT.Sockets.Thin ;
use  GNAT.Sockets.Thin ;

      Sin : aliased Sockaddr_In ;

      Sin.Sin_Addr.S_B1 := 127 ; 
      Sin.Sin_Addr.S_B2 := 0 ;
      Sin.Sin_Addr.S_B3 := 0 ;
      Sin.Sin_Addr.S_B4 := 1 ;


Then there a string version:

    with GNAT.Sockets ;
    use  GNAT.Sockets ;

    localhost : constant Inet_Addr_Type := Inet_Addr ( "127.0.0.1" ) ;


As for "Inet_Addr_Type" it is defined in in GNAT.Sockets
copy edit out Family_Inet6 ( IPv6 ). Following is copied from 
GNAT.Sockets.ads .

   type Family_Type is (Family_Inet, Family_Inet6);

   type Inet_Addr_Type (Family : Family_Type := Family_Inet) is private;
   --  An Internet address depends on an address family (IPv4 contains 4
   --  octets and Ipv6 contains 16 octets). Any_Inet_Addr is a special value
   --  treated like a wildcard enabling all addresses. No_Inet_Addr provides a
   --  special value to denote uninitialized inet addresses.

   Broadcast_Inet_Addr : constant Inet_Addr_Type;

private


   subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;
   --  Octet for Internet address

   type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;

   subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 ..  4);
   subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);

   type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
      case Family is
         when Family_Inet =>
            Sin_V4 : Inet_Addr_V4_Type := (others => 0);

         when Family_Inet6 =>
            Sin_V6 : Inet_Addr_V6_Type := (others => 0);
      end case;
   end record;


   -- set address value to IPv4 "255.255.255.255"

   Broadcast_Inet_Addr : constant Inet_Addr_Type :=
                           (Family_Inet, (others => 255)); 



In <joPGk.376724$yE1.161337@attbi_s21>, "Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:
>mockturtle wrote:
>> 
>> I have 4 octects (read by a received packet) which rappresent
>> an IP address and  I want to convert them into a Inet_Addr_Type
>> (using GNAT.Sockets). Of course I could convert the 4 octects
>> into a string to be given to function Inet_Addr_Type, but it seems
>> a bit "convoluted" (it sounds like the joke about the mathematician's
>> algorithm to make a pot of hot water :-).   My question is: there
>> is a  function which  allows me to do such a conversion in a
>> more direct way? I looked at g-sockets, searching for
>> "Inet_Addr_Type", but I could not  find anything.
>
>I'm not aware of any way provided by the pkg other than that you outlined above.
>
>There are ways using Unchecked_Conversion and address overlays, but they are Not 
>Recommended.
>
>-- 
>Jeff Carter
>"My legs are gray, my ears are gnarled, my eyes are old and bent."
>Monty Python's Life of Brian
>81




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

* Re: Making a Inet_Addr_Type out of octects (GNAT.Sockets)
  2008-10-07 19:43 Making a Inet_Addr_Type out of octects (GNAT.Sockets) mockturtle
  2008-10-07 20:11 ` Jeffrey R. Carter
@ 2008-10-08 10:28 ` anon
  1 sibling, 0 replies; 4+ messages in thread
From: anon @ 2008-10-08 10:28 UTC (permalink / raw)


--
-- A.adb -- a simple test program that converts 4 8-bit words (Octets)
--            into GNAT.Sockets.Inet_Addr_Type variable and prints results.
--
with Ada.Text_IO ;
use  Ada.Text_IO ;
with Ada.Unchecked_Conversion ;

with GNAT.Sockets ;
use  GNAT.Sockets ;


procedure a is

   subtype Inet_Addr_Comp_Type is Natural range 0 .. 255;

   type Inet_Addr_VN_Type is array (Natural range <>) of Inet_Addr_Comp_Type;

   subtype Inet_Addr_V4_Type is Inet_Addr_VN_Type (1 ..  4);
   subtype Inet_Addr_V6_Type is Inet_Addr_VN_Type (1 .. 16);

   type Inet_Addr_Type (Family : Family_Type := Family_Inet) is record
      case Family is
         when Family_Inet =>
            Sin_V4 : Inet_Addr_V4_Type := (others => 0);

         when Family_Inet6 =>
            Sin_V6 : Inet_Addr_V6_Type := (others => 0);
      end case;
   end record;


   function To_Gnat is new Ada.Unchecked_Conversion
                      ( Inet_Addr_Type, GNAT.Sockets.Inet_Addr_Type ) ;


  newip : Inet_Addr_Type ;
  sysip : GNAT.Sockets.Inet_Addr_Type ;

begin -- a 

  -- Set 'newip' to 127.0.0.1

  newip.Sin_V4  ( 1 ) := 16#7F# ;
  newip.Sin_V4  ( 2 ) := 16#00# ;
  newip.Sin_V4  ( 3 ) := 16#00# ;
  newip.Sin_V4  ( 4 ) := 16#01# ;

  sysip := To_Gnat ( newip );

  Put_Line ( Image (  sysip ) ) ;

end a ;

In <910108d9-03a0-4e35-86d2-572501a82906@z6g2000pre.googlegroups.com>, mockturtle <framefritti@gmail.com> writes:
>Dear.all,
>a very simple (almost silly) question whose answer, I fear, is
>"no."...
>
>I have 4 octects (read by a received packet) which rappresent
>an IP address and  I want to convert them into a Inet_Addr_Type
>(using GNAT.Sockets). Of course I could convert the 4 octects
>into a string to be given to function Inet_Addr_Type, but it seems
>a bit "convoluted" (it sounds like the joke about the mathematician's
>algorithm to make a pot of hot water :-).   My question is: there
>is a  function which  allows me to do such a conversion in a
>more direct way? I looked at g-sockets, searching for
>"Inet_Addr_Type", but I could not  find anything.
>
>Thank you in advance.




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

end of thread, other threads:[~2008-10-08 10:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-07 19:43 Making a Inet_Addr_Type out of octects (GNAT.Sockets) mockturtle
2008-10-07 20:11 ` Jeffrey R. Carter
2008-10-08  1:53   ` anon
2008-10-08 10:28 ` anon

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