pla_util_2.1.2_815e4700/net/src/packets-device_locators.adb

  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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--  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/>.
------------------------------------------------------------------------
with Interfaces.C.Strings;

package body Packets.Device_Locators is

   function Create return Basic_Search_Type is

      Algorithm : Basic_Search_Type;

   begin

      return Algorithm;

   end Create;

   function Create (Device_Name : String) return Named_Search_Type is

      Algorithm    : Named_Search_Type;
      Target_Count : Interfaces.C.size_t;

   begin

      Interfaces.C.To_C (Item   => Device_Name,
                         Target => Algorithm.Network_Device_Name,
                         Count  => Target_Count);

      return Algorithm;

   end Create;

   overriding procedure Finalize (Self : in out Device_Locator_Type) is
   begin

      if Self.Devices_Access /= null then

         Pcap.Devices.Free_All_Devices (Network_Devices => Self.Devices_Access);

         Self.Devices_Access := null;

      end if;

   end Finalize;

   procedure Find (Self              :     Device_Locator_Type;
                   Device_Name       :     String;
                   Interface_Name    : out Interface_Name_Strings.Bounded_String;
                   Interface_Address : out MAC_Addresses.MAC_Address_Type) is

      use type Pcap.Devices.Address_Access_Type;

      Address_Access   : Pcap.Devices.Address_Access_Type   := null;
      Device_Access    : Pcap.Devices.Interface_Access_Type := null;
      Search_Algorithm : constant Search_Type'Class         := (if Device_Name = "" then Create else Create (Device_Name => Device_Name));

   begin

      Device_Access  := Search_Algorithm.Run (Network_Device => Self.Devices_Access);
      Interface_Name := Interface_Name_Strings.To_Bounded_String (Source => Interfaces.C.Strings.Value (Item => Device_Access.all.Name));
      Address_Access := Device_Access.all.Addresses;

      while Address_Access /= null and then Pcap.Devices.Is_Not_Packet_Address (Socket_Address => Address_Access.all.Socket_Address) loop
         Address_Access := Address_Access.all.Next;
      end loop;

      if Address_Access = null then
         raise Packet_Error with "MAC address of device " & Interface_Name_Strings.To_String (Source => Interface_Name) & " not found";
      end if;

      Interface_Address := MAC_Addresses.Create_MAC_Address (MAC_Address_Octets => Address_Access.all.Socket_Address.all.SLL_Address (1 .. 6));

   end Find;

   procedure Find_All_Devices (Self : in out Device_Locator_Type) is

      use type Interfaces.C.int;

      Error_Buffer : aliased Pcap.Error_Buffer_Type;
      Return_Code  : Interfaces.C.int;

   begin

      Return_Code := Pcap.Devices.Find_All_Devices (Network_Devices => Self.Devices_Access,
                                                    Error_Buffer    => Error_Buffer);

      if Return_Code /= 0 then
         raise Packet_Error with Interfaces.C.To_Ada (Item => Error_Buffer);
      end if;

      if Self.Devices_Access = null then
         raise Packet_Error with "No devices found";
      end if;

   end Find_All_Devices;

   pragma Warnings (Off, "formal parameter*is not referenced");
   overriding function Run (Self           : Basic_Search_Type;
                            Network_Device : Pcap.Devices.Interface_Access_Type) return Pcap.Devices.Interface_Access_Type is
   pragma Warnings (On,  "formal parameter*is not referenced");

      Device_Access : Pcap.Devices.Interface_Access_Type := Network_Device;

   begin

      while Device_Access /= null and then
        (Pcap.Devices.Is_Loopback (Network_Device => Device_Access) or else
         Pcap.Devices.Is_Down (Network_Device => Device_Access) or else
         Pcap.Devices.Is_Not_Running (Network_Device => Device_Access)) loop

         Device_Access := Device_Access.all.Next;

      end loop;

      if Device_Access = null then
         raise Packet_Error with "No up and running non-loopback network device found";
      end if;

      return Device_Access;

   end Run;

   overriding function Run (Self           : Named_Search_Type;
                            Network_Device : Pcap.Devices.Interface_Access_Type) return Pcap.Devices.Interface_Access_Type is

      Device_Access : Pcap.Devices.Interface_Access_Type := Network_Device;

   begin

      while Device_Access /= null and then Interfaces.C.Strings.Value (Item => Device_Access.all.Name) /= Interfaces.C.To_Ada (Item => Self.Network_Device_Name) loop
         Device_Access := Device_Access.all.Next;
      end loop;

      if Device_Access = null then
         raise Packet_Error with "Device " & Interfaces.C.To_Ada (Item => Self.Network_Device_Name) & " not found";
      end if;

      return Device_Access;

   end Run;

end Packets.Device_Locators;