2015-05-25 50 views
1

在Ada中,如何以独立于操作系统的方式最佳地遍历适配器(查找分配的IP和子网)?是否有像我可以使用的Gnat Sockets套件? 下面是我们目前使用的代码 - 但是这是直接使用Windows API。我怎样才能最好地实现相同的事情,而不是Windows特定的?Ada Os独立适配器迭代

function Get_Adapters_Info (The_Adapter_Info  : Ip_Adapter_Info_Access; 
          Output_Buffer_Length : Win32.PULONG) return Win32.DWORD; 
pragma Import (Stdcall, Get_Adapters_Info, "GetAdaptersInfo"); 


procedure Iterate_Ip_Addresses (Handler : not null access procedure (The_Information : Ip_Address_Info)) is 
    Return_Code : Win32.DWORD; 
    The_Size : aliased Win32.ULONG := 0; 
    use type Win32.DWORD; 
    use type Win32.UINT; 
begin 
    Return_Code := Get_Adapters_Info (null, The_Size'unchecked_access); --' 
    if The_Size > 0 then 
     declare 
     function Convert is new Ada.Unchecked_Conversion (System.Address, Ip_Adapter_Info_Access); 
     The_Buffer : Unsigned.Byte_String (1..Natural(The_Size)); 
     The_Info : Ip_Adapter_Info_Access := Convert (The_Buffer(The_Buffer'first)'address); 
     begin 
      Return_Code := Get_Adapters_Info (The_Info, The_Size'unchecked_access); --' 
      if Return_Code = Win32.Winerror.NO_ERROR then 
       loop 
        if (The_Info.Kind = Mib_If_Type_Ethernet) or (The_Info.Kind = Mib_If_Type_Ieee80211) then 
         declare 
          Ip_Address_List : Ip_Addr_String_Access := The_Info.Ip_Address_List'unchecked_access; --' 
         begin 
          loop 
           declare 
            The_Address : constant Ip_Address := Convert(Ip_Address_List.Ip_Address); 
            use type Ip_Address; 

           begin 
            if The_Address /= Any_Address then -- Active 
             Handler.all (The_Information => (The_Address => The_Address, 
                Subnet_Mask  => Convert(Ip_Address_List.Ip_Mask))); 
            end if; 
           end; 
           Ip_Address_List := Ip_Address_List.Next; 
           exit when Ip_Address_List = null; 
          end loop; 
         end; 
        end if; 
        The_Info := The_Info.Next; 
        exit when The_Info = null; 
       end loop; 
      end if; 
     end; 
    end if; 
end Iterate_Ip_Addresses; 
+0

除非现有的Ada库为您处理,否则没有便携式或独立于操作系统的方式。 –

+0

我会先看看Gnat.Sockets包。 http://en.wikibooks.org/wiki/Ada_Programming/Libraries/GNAT.Sockets我不认为它提供了如此低层次的网络硬件接口,但它必须在内部使用某种类型的接口。或者,如果您可以使用其他语言(可能是C)找到这样的库,则可以轻松地为其生成绑定。 –

+0

注意:我只是查找'GetAdaptersInfo'来查看它是什么,我发现的微软页面说在XP和更高版本上使用'GetAdaptersAddresses'。 XP已经死了,所以'GetAdaptersInfo'必须是过时的。 https://msdn.microsoft.com/en-us/library/windows/desktop/aa365917%28v=vs.85%29.aspx – ajb

回答

2

ADA的标准不包括网络编程,因此短期,不令人满意的答案是没有

由于这些操作依赖操作系统,我能提供的最好建议是隐藏操作系统的依赖:

  • 将您的Get_Adapters_Info规范成Iterate_IP_Addresses身体。
  • 使Iterate_IP_Addresses的主体分开。
  • 为您想要支持的每个操作系统提供一个实现(主体)Iterate_IP_Addresses
  • 在编译时选择合适的Iterate_IP_Addresses作为构建过程的一部分。