pla_util_2.1.2_815e4700/net/src/packets-pcap-devices.ads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
--  SPDX-License-Identifier: GPL-3.0-or-later
------------------------------------------------------------------------
--  pla-util - A power line adapter utility
--  Copyright (C) 2016-2023 John Serock
--
--  This file is part of pla-util.
--
--  pla-util is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  pla-util is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
package Packets.Pcap.Devices is

   type Socket_Address_Type is
      record
         SA_Family             : aliased Interfaces.C.unsigned_short;
         SLL_Protocol          : aliased Interfaces.C.unsigned_short;
         SLL_Interface_Index   : aliased Interfaces.C.int;
         SLL_ARP_Hardware_Kind : aliased Interfaces.C.unsigned_short;
         SLL_Packet_Kind       : aliased Interfaces.C.unsigned_char;
         SLL_Address_Length    : aliased Interfaces.C.unsigned_char;
         SLL_Address           : aliased Octets.Octets_Type (1 .. 8);
      end record
     with
       Convention => C_Pass_By_Copy;

   type Socket_Address_Access_Type is access Socket_Address_Type;

   type Interface_Type;
   type Interface_Access_Type is access Interface_Type;

   type Address_Type;
   type Address_Access_Type is access Address_Type;

   type Interface_Type is
      record
         Next        : Interface_Access_Type;
         Name        : Interfaces.C.Strings.chars_ptr;
         Description : Interfaces.C.Strings.chars_ptr;
         Addresses   : Address_Access_Type;
         Flags       : aliased Interfaces.C.unsigned;
      end record
     with
       Convention => C_Pass_By_Copy;

   type Address_Type is
      record
         Next                : Address_Access_Type;
         Socket_Address      : Socket_Address_Access_Type;
         Netmask             : Socket_Address_Access_Type;
         Broadcast_Address   : Socket_Address_Access_Type;
         Destination_Address : Socket_Address_Access_Type;
      end record
     with
       Convention => C_Pass_By_Copy;

   function Find_All_Devices (Network_Devices : out Interface_Access_Type;
                              Error_Buffer    : out Error_Buffer_Type) return Interfaces.C.int
     with
       Import        => True,
       Convention    => C,
       External_Name => "pcap_findalldevs";

   procedure Free_All_Devices (Network_Devices : Interface_Access_Type)
     with
       Import        => True,
       Convention    => C,
       External_Name => "pcap_freealldevs";

   function Is_Down (Network_Device : Interface_Access_Type) return Boolean;

   function Is_Loopback (Network_Device : Interface_Access_Type) return Boolean;

   function Is_Not_Packet_Address (Socket_Address : Socket_Address_Access_Type) return Boolean;

   function Is_Not_Running (Network_Device : Interface_Access_Type) return Boolean;

private

   use type Interfaces.C.unsigned;
   use type Interfaces.C.unsigned_short;

   AF_PACKET   : constant Interfaces.C.unsigned_short := 17;
   IF_LOOPBACK : constant Interfaces.C.unsigned       := 16#0000_0001#;
   IF_UP       : constant Interfaces.C.unsigned       := 16#0000_0002#;
   IF_RUNNING  : constant Interfaces.C.unsigned       := 16#0000_0004#;

   function Is_Down (Network_Device : Interface_Access_Type) return Boolean is
     ((Network_Device.all.Flags and IF_UP) = 0);

   function Is_Loopback (Network_Device : Interface_Access_Type) return Boolean is
     ((Network_Device.all.Flags and IF_LOOPBACK) /= 0);

   function Is_Not_Packet_Address (Socket_Address : Socket_Address_Access_Type) return Boolean is
     (Socket_Address.all.SA_Family /= AF_PACKET);

   function Is_Not_Running (Network_Device : Interface_Access_Type) return Boolean is
     ((Network_Device.all.Flags and IF_RUNNING) = 0);

end Packets.Pcap.Devices;