[PATCH] mbimcli: report IP configuration on connect

Dan Williams dcbw at redhat.com
Mon May 4 10:16:11 PDT 2015


If we have it, report it.

---
 src/mbimcli/mbimcli-basic-connect.c |  65 ++++++++++++++++-
 src/mbimcli/mbimcli-dss.c           |  94 +++++++++++++++++++-----
 src/mbimcli/mbimcli-helpers.c       | 142 ++++++++++++++++++++++++++++++++++++
 src/mbimcli/mbimcli-helpers.h       |   4 +
 4 files changed, 284 insertions(+), 21 deletions(-)

diff --git a/src/mbimcli/mbimcli-basic-connect.c b/src/mbimcli/mbimcli-basic-connect.c
index 088d5db..f093c3b 100644
--- a/src/mbimcli/mbimcli-basic-connect.c
+++ b/src/mbimcli/mbimcli-basic-connect.c
@@ -31,6 +31,7 @@
 #include <libmbim-glib.h>
 
 #include "mbimcli.h"
+#include "mbimcli-helpers.h"
 
 /* Context */
 typedef struct {
@@ -657,6 +658,31 @@ enum {
 };
 
 static void
+ip_configuration_query_ready (MbimDevice *device,
+                              GAsyncResult *res,
+                              gpointer unused)
+{
+    GError *error = NULL;
+    MbimMessage *response;
+    gboolean success = FALSE;
+
+    response = mbim_device_command_finish (device, res, &error);
+    if (!response ||
+        !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
+        g_printerr ("error: couldn't get IP configuration response message: %s\n", error->message);
+    } else {
+        success = mbimcli_print_ip_config (device, response, &error);
+        if (!success)
+            g_printerr ("error: couldn't parse IP configuration response message: %s\n", error->message);
+    }
+
+    g_clear_error (&error);
+    if (response)
+        mbim_message_unref (response);
+    shutdown (success);
+}
+
+static void
 connect_ready (MbimDevice   *device,
                GAsyncResult *res,
                gpointer user_data)
@@ -694,6 +720,7 @@ connect_ready (MbimDevice   *device,
         shutdown (FALSE);
         return;
     }
+    mbim_message_unref (response);
 
     switch (GPOINTER_TO_UINT (user_data)) {
     case CONNECT:
@@ -723,7 +750,43 @@ connect_ready (MbimDevice   *device,
              VALIDATE_UNKNOWN (mbim_context_type_get_string (mbim_uuid_to_context_type (context_type))),
              VALIDATE_UNKNOWN (mbim_nw_error_get_string (nw_error)));
 
-    mbim_message_unref (response);
+    if (GPOINTER_TO_UINT (user_data) == CONNECT) {
+        MbimMessage *message;
+
+        message = (mbim_message_ip_configuration_query_new (
+                   session_id,
+                   MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv4configurationavailable */
+                   MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv6configurationavailable */
+                   0, /* ipv4addresscount */
+                   NULL, /* ipv4address */
+                   0, /* ipv6addresscount */
+                   NULL, /* ipv6address */
+                   NULL, /* ipv4gateway */
+                   NULL, /* ipv6gateway */
+                   0, /* ipv4dnsservercount */
+                   NULL, /* ipv4dnsserver */
+                   0, /* ipv6dnsservercount */
+                   NULL, /* ipv6dnsserver */
+                   0, /* ipv4mtu */
+                   0, /* ipv6mtu */
+                   &error));
+        if (message) {
+            mbim_device_command (device,
+                                 message,
+                                 60,
+                                 NULL,
+                                 (GAsyncReadyCallback)ip_configuration_query_ready,
+                                 NULL);
+            mbim_message_unref (message);
+        } else {
+            g_printerr ("error: couldn't create IP config request: %s\n", error->message);
+            g_error_free (error);
+            mbim_message_unref (message);
+            shutdown (FALSE);
+        }
+        return;
+    }
+
     shutdown (TRUE);
 }
 
diff --git a/src/mbimcli/mbimcli-dss.c b/src/mbimcli/mbimcli-dss.c
index 8144b0b..c7424e6 100644
--- a/src/mbimcli/mbimcli-dss.c
+++ b/src/mbimcli/mbimcli-dss.c
@@ -38,6 +38,7 @@
 typedef struct {
     MbimDevice *device;
     GCancellable *cancellable;
+    guint32 session_id;
 } Context;
 static Context *ctx;
 
@@ -120,11 +121,36 @@ enum {
 };
 
 static void
+ip_configuration_query_ready (MbimDevice *device,
+                              GAsyncResult *res,
+                              gpointer unused)
+{
+    GError *error = NULL;
+    MbimMessage *response;
+    gboolean success = FALSE;
+
+    response = mbim_device_command_finish (device, res, &error);
+    if (!response ||
+        !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
+        g_printerr ("error: couldn't get IP configuration response message: %s\n", error->message);
+    } else {
+        success = mbimcli_print_ip_config (device, response, &error);
+        if (!success)
+            g_printerr ("error: couldn't parse IP configuration response message: %s\n", error->message);
+    }
+
+    g_clear_error (&error);
+    if (response)
+        mbim_message_unref (response);
+    shutdown (success);
+}
+
+static void
 set_dss_ready (MbimDevice *device,
                GAsyncResult *res,
                gpointer user_data)
 {
-    MbimMessage *response;
+    MbimMessage *response, *message;
     GError *error = NULL;
 
     response = mbim_device_command_finish (device, res, &error);
@@ -142,23 +168,52 @@ set_dss_ready (MbimDevice *device,
         shutdown (FALSE);
         return;
     }
-
-    switch (GPOINTER_TO_UINT (user_data)) {
-    case CONNECT:
-        g_print ("[%s] Successfully connected\n",
-                 mbim_device_get_path_display (device));
-        break;
-    case DISCONNECT:
-        g_print ("[%s] Successfully disconnected\n",
-                 mbim_device_get_path_display (device));
-        break;
-    default:
-        g_assert_not_reached ();
-        break;
-    }
-
     mbim_message_unref (response);
-    shutdown (TRUE);
+
+    if (GPOINTER_TO_UINT (user_data) == DISCONNECT) {
+        g_print ("[%s] Successfully disconnected\n",
+                 mbim_device_get_path_display (device));
+        shutdown (TRUE);
+        return;
+    }
+
+    g_assert (GPOINTER_TO_UINT (user_data) == CONNECT);
+
+    g_print ("[%s] Successfully connected\n",
+             mbim_device_get_path_display (device));
+
+    message = (mbim_message_ip_configuration_query_new (
+               ctx->session_id,
+               MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv4configurationavailable */
+               MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv6configurationavailable */
+               0, /* ipv4addresscount */
+               NULL, /* ipv4address */
+               0, /* ipv6addresscount */
+               NULL, /* ipv6address */
+               NULL, /* ipv4gateway */
+               NULL, /* ipv6gateway */
+               0, /* ipv4dnsservercount */
+               NULL, /* ipv4dnsserver */
+               0, /* ipv6dnsservercount */
+               NULL, /* ipv6dnsserver */
+               0, /* ipv4mtu */
+               0, /* ipv6mtu */
+               &error));
+    if (!message) {
+        g_printerr ("error: couldn't create IP config request: %s\n", error->message);
+        g_error_free (error);
+        mbim_message_unref (message);
+        shutdown (FALSE);
+        return;
+    }
+
+    mbim_device_command (device,
+                         message,
+                         60,
+                         NULL,
+                         (GAsyncReadyCallback)ip_configuration_query_ready,
+                         NULL);
+    mbim_message_unref (message);
 }
 
 static gboolean
@@ -208,15 +263,14 @@ mbimcli_dss_run (MbimDevice   *device,
     /* Connect? */
     if (connect_str) {
         MbimUuid service_id;
-        guint32 session_id;
 
-        if (!common_parse (connect_str, &service_id, &session_id)) {
+        if (!common_parse (connect_str, &service_id, &ctx->session_id)) {
             shutdown (FALSE);
             return;
         }
 
         request = mbim_message_dss_connect_set_new (&service_id,
-                                                    session_id,
+                                                    ctx->session_id,
                                                     MBIM_DSS_LINK_STATE_ACTIVATE,
                                                     &error);
 
diff --git a/src/mbimcli/mbimcli-helpers.c b/src/mbimcli/mbimcli-helpers.c
index aa37d5a..21aea9b 100644
--- a/src/mbimcli/mbimcli-helpers.c
+++ b/src/mbimcli/mbimcli-helpers.c
@@ -47,3 +47,145 @@ mbimcli_read_uint_from_string (const gchar *str,
     }
     return FALSE;
 }
+
+
+gboolean
+mbimcli_print_ip_config (MbimDevice *device,
+                         MbimMessage *response,
+                         GError **error)
+{
+    MbimIPConfigurationAvailableFlag ipv4configurationavailable;
+    MbimIPConfigurationAvailableFlag ipv6configurationavailable;
+    guint32 ipv4addresscount;
+    MbimIPv4Element **ipv4address;
+    guint32 ipv6addresscount;
+    MbimIPv6Element **ipv6address;
+    const MbimIPv4 *ipv4gateway;
+    const MbimIPv6 *ipv6gateway;
+    guint32 ipv4dnsservercount;
+    MbimIPv4 *ipv4dnsserver;
+    guint32 ipv6dnsservercount;
+    MbimIPv6 *ipv6dnsserver;
+    guint32 ipv4mtu;
+    guint32 ipv6mtu;
+    gchar *str;
+    GInetAddress *addr;
+
+    if (!mbim_message_ip_configuration_response_parse (
+            response,
+            NULL, /* sessionid */
+            &ipv4configurationavailable,
+            &ipv6configurationavailable,
+            &ipv4addresscount,
+            &ipv4address,
+            &ipv6addresscount,
+            &ipv6address,
+            &ipv4gateway,
+            &ipv6gateway,
+            &ipv4dnsservercount,
+            &ipv4dnsserver,
+            &ipv6dnsservercount,
+            &ipv6dnsserver,
+            &ipv4mtu,
+            &ipv6mtu,
+            error))
+        return FALSE;
+
+    /* IPv4 info */
+
+    str = mbim_ip_configuration_available_flag_build_string_from_mask (ipv4configurationavailable);
+    g_print ("\n[%s] IPv4 configuration available: '%s'\n", mbim_device_get_path_display (device), str);
+    g_free (str);
+
+    if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_ADDRESS) {
+        guint i;
+
+        for (i = 0; i < ipv4addresscount; i++) {
+            addr = g_inet_address_new_from_bytes ((guint8 *)&ipv4address[i]->ipv4_address, G_SOCKET_FAMILY_IPV4);
+            str = g_inet_address_to_string (addr);
+            g_print ("     IP [%u]: '%s/%u'\n",
+                     i,
+                     str,
+                     ipv4address[i]->on_link_prefix_length);
+            g_free (str);
+            g_object_unref (addr);
+        }
+    }
+
+    if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_GATEWAY) {
+        addr = g_inet_address_new_from_bytes ((guint8 *)ipv4gateway, G_SOCKET_FAMILY_IPV4);
+        str = g_inet_address_to_string (addr);
+        g_print ("    Gateway: '%s'\n", str);
+        g_free (str);
+        g_object_unref (addr);
+    }
+
+    if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_DNS) {
+        guint i;
+
+        for (i = 0; i < ipv4dnsservercount; i++) {
+            addr = g_inet_address_new_from_bytes ((guint8 *)&ipv4dnsserver[i], G_SOCKET_FAMILY_IPV4);
+            if (!g_inet_address_get_is_any (addr)) {
+                str = g_inet_address_to_string (addr);
+                g_print ("    DNS [%u]: '%s'\n", i, str);
+                g_free (str);
+            }
+            g_object_unref (addr);
+        }
+    }
+
+    if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_MTU)
+        g_print ("        MTU: '%u'\n", ipv4mtu);
+
+    /* IPv6 info */
+    str = mbim_ip_configuration_available_flag_build_string_from_mask (ipv6configurationavailable);
+    g_print ("\n[%s] IPv6 configuration available: '%s'\n", mbim_device_get_path_display (device), str);
+    g_free (str);
+
+    if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_ADDRESS) {
+        guint i;
+
+        for (i = 0; i < ipv6addresscount; i++) {
+            addr = g_inet_address_new_from_bytes ((guint8 *)&ipv6address[i]->ipv6_address, G_SOCKET_FAMILY_IPV6);
+            str = g_inet_address_to_string (addr);
+            g_print ("     IP [%u]: '%s/%u'\n",
+                     i,
+                     str,
+                     ipv6address[i]->on_link_prefix_length);
+            g_free (str);
+            g_object_unref (addr);
+        }
+    }
+
+    if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_GATEWAY) {
+        addr = g_inet_address_new_from_bytes ((guint8 *)ipv6gateway, G_SOCKET_FAMILY_IPV6);
+        str = g_inet_address_to_string (addr);
+        g_print ("    Gateway: '%s'\n", str);
+        g_free (str);
+        g_object_unref (addr);
+    }
+
+    if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_DNS) {
+        guint i;
+
+        for (i = 0; i < ipv6dnsservercount; i++) {
+            addr = g_inet_address_new_from_bytes ((guint8 *)&ipv6dnsserver[i], G_SOCKET_FAMILY_IPV6);
+            if (!g_inet_address_get_is_any (addr)) {
+                str = g_inet_address_to_string (addr);
+                g_print ("    DNS [%u]: '%s'\n", i, str);
+                g_free (str);
+            }
+            g_object_unref (addr);
+        }
+    }
+
+    if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_MTU)
+        g_print ("        MTU: '%u'\n", ipv6mtu);
+
+    mbim_ipv4_element_array_free (ipv4address);
+    mbim_ipv6_element_array_free (ipv6address);
+    g_free (ipv4dnsserver);
+    g_free (ipv6dnsserver);
+    return TRUE;
+}
+
diff --git a/src/mbimcli/mbimcli-helpers.h b/src/mbimcli/mbimcli-helpers.h
index d7dabdc..969f416 100644
--- a/src/mbimcli/mbimcli-helpers.h
+++ b/src/mbimcli/mbimcli-helpers.h
@@ -28,4 +28,8 @@
 gboolean mbimcli_read_uint_from_string (const gchar *str,
                                         guint *out);
 
+gboolean mbimcli_print_ip_config (MbimDevice *device,
+                                  MbimMessage *response,
+                                  GError **error);
+
 #endif /* __MBIMCLI_H__ */
-- 
2.1.0




More information about the libmbim-devel mailing list