Ottenere informazioni generali sui dispositivi network mediante le risorse del API di Libnm-glib

Da Gambas-it.org - Wikipedia.

Di seguito mostriamo un esempio che consente di ottenere informazioni sui dispositivi Wi-Fi mediante la libreria libnm-glib.

E' necessario avere installate nel sistema e richiamare in Gambas le librerie condivise:

  • "libgio-2.0.so.0.7200.4 "
  • "libnm-glib.so.4.9.0 "
Library "libgio-2.0:0.7200.4"

Public Struct GPtrArray
  pdata As Pointer
  glen As Integer
End Struct

' void g_free (gpointer mem)
' Frees the memory pointed to by mem.
Private Extern g_free(mem As Pointer)

' void g_ptr_array_unref (GPtrArray *array)
' Atomically decrements the reference count of array by one.
Private Extern g_ptr_array_unref(garray As GPtrArray)

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(_object As Pointer)


Library "libnm-glib:4.9.0"

Private Enum NM_DEVICE_STATE_UNKNOWN = 0, NM_DEVICE_STATE_UNMANAGED = 10, NM_DEVICE_STATE_UNAVAILABLE = 20,
             NM_DEVICE_STATE_DISCONNECTED = 30, NM_DEVICE_STATE_PREPARE = 40, NM_DEVICE_STATE_CONFIG = 50, 
             NM_DEVICE_STATE_NEED_AUTH = 60, NM_DEVICE_STATE_IP_CONFIG = 70, NM_DEVICE_STATE_IP_CHECK = 80,
             NM_DEVICE_STATE_SECONDARIES = 90, NM_DEVICE_STATE_ACTIVATED = 100, NM_DEVICE_STATE_DEACTIVATING = 110,
             NM_DEVICE_STATE_FAILED = 120
Private tipo As String[] = ["unknown device", "wired ethernet device", "802.11 WiFi device", "not used", "not used", "Bluetooth device",
                            "OLPC XO mesh networking device", "802.16e Mobile WiMAX broadband device", "modem", "IP-over-InfiniBand device",
                            "bond master interface", "802.1Q VLAN interface", "ADSL modem", "bridge master interface",
                            "generic support for unrecognized device types", "team master interface", "TUN or TAP interface",
                            "IP tunnel interface", "MACVLAN interface", "VXLAN interface", "VETH interface", "MACsec interface",
                            "dummy interface", "PPP interface", "Open vSwitch interface", "Open vSwitch port", "Open vSwitch bridge",
                            " IEEE 802.15.4 (WPAN) MAC Layer Device", "6LoWPAN interface", "WireGuard interface"]
Private capa As String[] = ["device has no special capabilities", "NetworkManager supports this device", "this device can indicate carrier status",
                            "this device is a software device", "this device supports single-root I/O virtualization"]

' NMClient * nm_client_new (void)
' Creates a new NMClient.
Private Extern nm_client_new() As Pointer

' const GPtrArray * nm_client_get_devices (NMClient *client)
' Gets all the known network devices.
Private Extern nm_client_get_devices(client As Pointer) As GPtrArray

' NMDeviceState nm_device_get_state (NMDevice *device)
' Gets the current NMDevice state.
Private Extern nm_device_get_state(device As Pointer) As Integer

' NMAccessPoint * nm_device_wifi_get_active_access_point (NMDeviceWifi *device)
' Gets the active NMAccessPoint.
Private Extern nm_device_wifi_get_active_access_point(device As Pointer) As Pointer

' const GByteArray * nm_access_point_get_ssid (NMAccessPoint *ap)
' Gets the SSID of the access point.
Private Extern nm_access_point_get_ssid(ap As Pointer) As Pointer

' char * nm_utils_ssid_to_utf8 (const GByteArray *ssid)
' Convert the input into a printable UTF-8 string.
Private Extern nm_utils_ssid_to_utf8(ssid As Pointer) As Pointer

' const char * nm_device_get_description (NMDevice *device)
' Gets a description of device, based on its vendor and product names.
Private Extern nm_device_get_description(device As Pointer) As String

' const char * nm_device_get_iface (NMDevice *device)
' Gets the interface name of the NMDevice.
Private Extern nm_device_get_iface(device As Pointer) As String

' const char * nm_device_get_ip_iface (NMDevice *device)
' Gets the IP interface name of the NMDevice over which IP traffic flows when the device is in the ACTIVATED state.
Private Extern nm_device_get_ip_iface(device As Pointer) As String

' const char * nm_device_get_type_description (NMDevice *device)
' Gets a (non-localized) description of the type of device that device is.
Private Extern nm_device_get_type_description(device As Pointer) As String

' NMDeviceType nm_device_get_device_type (NMDevice *device)
' Returns the numeric type of the NMDevice, ie Ethernet, Wi-Fi, etc.
Private Extern nm_device_get_device_type(device As Pointer) As Integer

' const char * nm_device_get_udi (NMDevice *device)
' Gets the Unique Device Identifier of the NMDevice.
Private Extern nm_device_get_udi(device As Pointer) As String

' const char * nm_device_get_driver (NMDevnm_device_get_udiice *device)
' Gets the driver of the NMDevice.
Private Extern nm_device_get_driver(device As Pointer) As String

' const char * nm_device_get_firmware_version (NMDevice *device)
' Gets the firmware version of the NMDevice.
Private Extern nm_device_get_firmware_version(device As Pointer) As String

' guint32 nm_device_wifi_get_bitrate (NMDeviceWifi *device)
' Gets the bit rate of the NMDeviceWifi in kbit/s.
Private Extern nm_device_wifi_get_bitrate(device As Pointer) As Integer

' const char * nm_device_get_hw_address (NMDevice *device)
' Gets the current a hardware address (MAC) for the device.
Private Extern nm_device_get_hw_address(device As Pointer) As String

' NMIP4Config * nm_device_get_ip4_config (NMDevice *device)
' Gets the current NMIP4Config associated with the NMDevice.
Private Extern nm_device_get_ip4_config(device As Pointer) As Pointer

' const char * nm_ip4_config_get_gateway (NMIP4Config *config)
' Gets the IP4 gateway address.
Private Extern nm_ip4_config_get_gateway(config As Pointer) As String

' NMIP6Config * nm_device_get_ip6_config (NMDevice *device)
' Gets the current NMIP6Config associated with the NMDevice.
Private Extern nm_device_get_ip6_config(device As Pointer) As Pointer

' const char * nm_ip6_config_get_gateway (NMIP6Config *config)
' Gets the IP6 gateway.
Private Extern nm_ip6_config_get_gateway(config As Pointer) As String

' NMDeviceCapabilities nm_device_get_capabilities (NMDevice *device)
' Gets the device' capabilities.
Private Extern nm_device_get_capabilities(device As Pointer) As Integer

' NMDeviceWifiCapabilities nm_device_wifi_get_capabilities (NMDeviceWifi *device)
' Gets the Wi-Fi capabilities of the NMDeviceWifi.
Private Extern nm_device_wifi_get_capabilities(device As Pointer) As Integer


Public Sub Main()
 
 Dim cl, dv, acp, ssi, sss, ipc As Pointer
 Dim devices As GPtrArray
 Dim i As Integer
 
' Acquisisce l'oggetto NMClient:
 cl = nm_client_new()
 If cl = 0 Then Error.Raise("Errore !")
 
' Acquisisce tutti i dispositivi gestiti dalla libreria NetworkManager:
 devices = nm_client_get_devices(cl)
 
' Verifica l'array ed elabora i dispositivi network:
 For i = 0 To devices.glen
   dv = Pointer@(devices.pdata + (i * 8))
   If nm_device_get_state(dv) < NM_DEVICE_STATE_UNAVAILABLE Then Continue
     Print "Dispositivo:  #"; i
     Print "Descrizione:  "; nm_device_get_description(dv)
     Print "Interfaccia:  "; nm_device_get_iface(dv)
     Print "IP interface: "; nm_device_get_ip_iface(dv)
     Print "Tipo:         "; nm_device_get_type_description(dv); ": "; tipo[nm_device_get_device_type(dv)]
     Print "U.D.I.:       "; nm_device_get_udi(dv)
     Print "Driver:       "; nm_device_get_driver(dv)
     Print "Firmware:     "; nm_device_get_firmware_version(dv)
     Print "Velocità:     "; nm_device_wifi_get_bitrate(dv) / 1000; " kbit/s"
     Print "Indirizzo HW: "; nm_device_get_hw_address(dv)
     
     ipc = nm_device_get_ip4_config(dv)
     Print "IP4 gateway:  "; nm_ip4_config_get_gateway(ipc)
     ipc = nm_device_get_ip6_config(dv)
     Print "IP6 gateway:  "; nm_ip6_config_get_gateway(ipc)
     Print "Caratterist.: "; capa[nm_device_get_capabilities(dv)]
     Print "Caratt. wifi: ";
     Select Case nm_device_wifi_get_capabilities(dv)
       Case 0
         Write "device has no encryption/authentication capabilities"
       Case 1
         Write "device supports 40/64-bit WEP encryption"
       Case 2
         Write "device supports 104/128-bit WEP encryption"
       Case 4
         Write "device supports TKIP encryption"
       Case 8
         Write "device supports AES/CCMP encryption"
       Case 16
         Write "device supports WPA1 authentication"
       Case 32
         Write "device supports WPA2/RSN authentication"
       Case 64
         Write "device supports Access Point mode"
       Case 128
         Write "device supports Ad-Hoc mode"
       Case 256
         Write "device reports frequency capabilities"
       Case 512
         Write "device supports 2.4GHz frequencies"
       Case 1024
         Write "device supports 5GHz frequencies"
     End Select
     If nm_device_get_state(dv) == NM_DEVICE_STATE_ACTIVATED Then
     acp = nm_device_wifi_get_active_access_point(dv)
     If acp > 0 Then
       ssi = nm_access_point_get_ssid(acp)
       sss = nm_utils_ssid_to_utf8(ssi)
       Print "\nActive AP:    "; IIf(Not IsNull(String@(sss)), String@(sss), "Nessuno")
       g_free(sss)
     Endif
   Endif
   Print "\n=============================================================\n"
 Next
 
' Libera la memoria precedentemente oocupata:
 g_ptr_array_unref(devices)
 g_object_unref(cl)
 
End


Riferimenti