RE: Change IP address with libnm
- From: António Mendes <antonio mendes eid pt>
- To: Thomas Haller <thaller redhat com>, "networkmanager-list gnome org" <networkmanager-list gnome org>
- Subject: RE: Change IP address with libnm
- Date: Thu, 27 Feb 2020 11:01:49 +0000
Hi,
I started with those fixed IPs but the idea is to pass these values to the function.
I think that this is probably the final version :)
changeIpAddress() and change change8021x() are called from another file.
Thanks for your support, Thomas!
networkManagement.c
#include <glib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <NetworkManager.h>
#include <networkManagement.h>
static void connection_updated(GObject *connection,
GAsyncResult *result,
gpointer user_data)
{
NM_CALLBACK_T *test = user_data;
GError *error = NULL;
gboolean success = FALSE;
success = nm_remote_connection_commit_changes_finish(NM_REMOTE_CONNECTION(connection), result, &error);
if (success == TRUE)
{
test->error = SUCCESS_NM;
}
else
{
test->error = ERROR_NM;
g_error_free(error);
}
/* Tell the mainloop we're done and we can quit now */
g_main_loop_quit(test->loop);
}
uint8_t changeIpAddress(const char* ipAddress, const uint8_t netMask, const char* gatewayAddress)
{
uint8_t returnError = ERROR_NM;
GError *error = NULL;
NMClient *client;
if (!(client = nm_client_new(NULL, &error)))
{
g_message("Error: Could not connect to NetworkManager: %s.", error->message);
g_error_free(error);
return returnError;
}
if (!nm_client_get_nm_running(client))
{
g_message("Error: Can't obtain connections: NetworkManager is not running.");
return returnError;
}
NMConnection *connection;
connection = (NMConnection *)nm_client_get_connection_by_id(client, "Wired connection 1");
if (connection != NULL)
{
NMSettingIPConfig *s_ip4;
NMIPAddress *address;
NM_CALLBACK_T test;
test.loop = g_main_loop_new(NULL, FALSE);
/* Build up the 'ipv4' Setting */
s_ip4 = NM_SETTING_IP_CONFIG(nm_setting_ip4_config_new());
address = nm_ip_address_new(AF_INET, ipAddress, netMask, &error);
if (address != NULL)
{
g_object_set(G_OBJECT(s_ip4),
NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
NM_SETTING_IP_CONFIG_GATEWAY, gatewayAddress,
NULL);
nm_setting_ip_config_clear_addresses(s_ip4); //clear all addresses
nm_setting_ip_config_add_address(s_ip4, address);
nm_connection_add_setting(connection, nm_setting_duplicate(NM_SETTING(s_ip4)));
nm_remote_connection_commit_changes_async(NM_REMOTE_CONNECTION(connection),
TRUE, NULL, connection_updated, &test);
g_main_loop_run(test.loop);
if(test.error == SUCCESS_NM)
{
returnError = SUCCESS_NM;
}
nm_ip_address_unref(address);
g_main_loop_unref(test.loop);
}
}
g_object_unref(client);
return returnError;
}
uint8_t change8021x(const char* user, const char* password, const char* eap)
{
uint8_t returnError = ERROR_NM;
GError *error = NULL;
NMClient *client;
if (!(client = nm_client_new(NULL, &error)))
{
g_message("Error: Could not connect to NetworkManager: %s.", error->message);
g_error_free(error);
return returnError;
}
if (!nm_client_get_nm_running(client))
{
g_message("Error: Can't obtain connections: NetworkManager is not running.");
g_object_unref(client);
return returnError;
}
NMConnection *connection;
connection = (NMConnection *)nm_client_get_connection_by_id(client, "Wired connection 1");
if (connection != NULL)
{
NM_CALLBACK_T test;
NMSetting8021x *s_8021x;
test.loop = g_main_loop_new(NULL, FALSE);
s_8021x = NM_SETTING_802_1X(nm_setting_802_1x_new());
nm_setting_802_1x_add_eap_method(s_8021x, eap);
g_object_set(G_OBJECT(s_8021x),
NM_SETTING_802_1X_IDENTITY, user,
NM_SETTING_802_1X_PASSWORD, password,
NULL);
nm_connection_add_setting(connection, nm_setting_duplicate(NM_SETTING(s_8021x)));
nm_remote_connection_commit_changes_async(NM_REMOTE_CONNECTION(connection),
TRUE, NULL, connection_updated, &test);
g_main_loop_run(test.loop);
if(test.error == SUCCESS_NM)
{
returnError = SUCCESS_NM;
}
g_main_loop_unref(test.loop);
}
g_object_unref(client);
return returnError;
}
networkManagement.h
#ifndef NETWORK_MANAGEMENT_H
#define NETWORK_MANAGEMENT_H
#include <glib.h>
#include <stdint.h>
#define SUCCESS_NM 0U
#define ERROR_NM 1U
typedef struct nm_callback
{
uint8_t error;
GMainLoop *loop;
} NM_CALLBACK_T;
uint8_t changeIpAddress(const char* ipAddress, const uint8_t netMask, const char* gatewayAddress);
uint8_t change8021x(const char* user, const char* password, const char* eap);
#endif // NETWORK_MANAGEMENT_Hwww.eid.pt
EID, S.A.
Capital Social â¬1.100.000
N° de matricula unico 501 400 699
DISCLAIMER
[
Date Prev][
Date Next] [
Thread Prev][Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]