On Wed, 2020-02-26 at 17:45 +0000, António Mendes wrote:
> Hi once again :)
> I can finally see the print coming from connection_updated(). I added
> loop = g_main_loop_new(NULL, FALSE) and g_main_loop_run(loop) to my
> networkManager(). 

Hi,


It looks mostly good. Some minor comments below, but the overall
structure is sensible.



> I still don't understand when you are saying to free GVariant
> returned from nm_remote_connection_commit_changes_finish(). In
> documentation, this function returns a gboolean.

You are right. I missed that.


> Sorry for my noobish questions and thanks again for your help.
> 
> Code updated:
> 
> #include <glib.h>
> #include <stdio.h>
> #include <stdlib.h>
> 
> #include <NetworkManager.h>
> #include <networkManagement.h>
> 
> static void connection_updated(GObject *connection,
>                    GAsyncResult *result,
>                    gpointer user_data)
> {
>     GMainLoop *loop = user_data;
>     GError *error = NULL;
>     gboolean success = FALSE;
> 
>     success =
> nm_remote_connection_commit_changes_finish(NM_REMOTE_CONNECTION(conne
> ction), result, &error);
>     
>     if (success == TRUE)
>     {
>         g_print("Connection Updated successfully\n");
>     }
>     else
>     {
>         g_print("Error updating connection: %s", error->message);
>         g_error_free(error);
>     }
>     /* Tell the mainloop we're done and we can quit now */
>     g_main_loop_quit(loop);
> }
> 
> static void changeIpAddress(NMConnection *connection, GMainLoop
> *loop)
> {
>     GError *error = NULL;
>     NMSettingIPConfig *s_ip4;
>     NMIPAddress *address;
> 
>     /* Build up the 'ipv4' Setting */
>     s_ip4 = NM_SETTING_IP_CONFIG(nm_setting_ip4_config_new());
> 
>     address = nm_ip_address_new(AF_INET, "192.168.123.2", 24,
> &error);
>     if (address != NULL)

btw, this isn't going to fail. You see the arguments you pass to
nm_ip_address_new(), and they are clearly correct. libnm will never
fail the function in this case. You can assert against that.

Or... if you handle the theoretical error, then be aware that in the
error case the async operation is not started. That means, nobody is
going to call g_main_loop_quit(), and the run below will block forever.



>     {
>         g_object_set(G_OBJECT(s_ip4),
>                      NM_SETTING_IP_CONFIG_METHOD,
> NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
>                      NM_SETTING_IP_CONFIG_GATEWAY, "192.168.123.254",
>                      NULL);
> 
>         nm_setting_ip_config_clear_addresses(s_ip4); //clear all
> addresses
> 
>         printf("%s\n", nm_ip_address_get_address(address));
> 
>         nm_setting_ip_config_add_address(s_ip4, address);
> 
>         g_object_set(G_OBJECT(s_ip4),
>                      NM_SETTING_IP_CONFIG_GATEWAY, "192.168.123.254",
>                      NULL);
> 
>         nm_connection_add_setting(connection,
> nm_setting_duplicate(NM_SETTING(s_ip4)));
> 
>         nm_remote_connection_commit_changes_async(NM_REMOTE_CONNECTIO
> N(connection),
>                                                   TRUE, NULL,
> connection_updated, loop);
>         nm_ip_address_unref(address);
>     }
> }
> 
> void networkManager()
> {
>     GError *error = NULL;
>     NMConnection *connection;
>     GMainLoop *loop;
>     NMClient *client;
> 
>     loop = g_main_loop_new(NULL, FALSE);

You will leak @loop. You could use

"g_autoptr(GMainLoop) loop = NULL;"

above (on recent glib), or ensure to explicitly call
g_main_loop_unref().


>     if (!(client = nm_client_new(NULL, &error)))
>     {
>         g_message("Error: Could not connect to NetworkManager: %s.",
> error->message);
>         g_error_free(error);
>         return;
>     }
> 
>     if (!nm_client_get_nm_running(client))
>     {
>         g_message("Error: Can't obtain connections: NetworkManager is
> not running.");

leaks client.

>         return;
>     }
> 
>     connection = (NMConnection
> *)nm_client_get_connection_by_id(client, "Wired connection 2");

Maybe handle @connection being NULL.



>     changeIpAddress(connection, loop);
> 
>     g_main_loop_run(loop);

> 
>     g_object_unref(client);
> }
> _

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
networkmanager-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/networkmanager-list

Reply via email to