mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-05 07:40:49 +00:00
dhcp: move D-Bus DHCP listener into separate class
This simplifies the manager and ensures that only the clients that use D-Bus-based DHCP helpers need to care about them.
This commit is contained in:
parent
ee25503636
commit
0b98dc4387
|
@ -172,6 +172,8 @@ nm_sources = \
|
|||
dhcp-manager/nm-dhcp-client.h \
|
||||
dhcp-manager/nm-dhcp-utils.c \
|
||||
dhcp-manager/nm-dhcp-utils.h \
|
||||
dhcp-manager/nm-dhcp-listener.c \
|
||||
dhcp-manager/nm-dhcp-listener.h \
|
||||
dhcp-manager/nm-dhcp-dhclient.c \
|
||||
dhcp-manager/nm-dhcp-dhclient.h \
|
||||
dhcp-manager/nm-dhcp-dhclient-utils.c \
|
||||
|
|
|
@ -673,10 +673,13 @@ copy_option (const char * key,
|
|||
g_hash_table_insert (hash, g_strdup (key), str_value);
|
||||
}
|
||||
|
||||
void
|
||||
nm_dhcp_client_new_options (NMDhcpClient *self,
|
||||
GHashTable *options,
|
||||
const char *reason)
|
||||
gboolean
|
||||
nm_dhcp_client_handle_event (gpointer unused,
|
||||
const char *iface,
|
||||
gint64 pid,
|
||||
GHashTable *options,
|
||||
const char *reason,
|
||||
NMDhcpClient *self)
|
||||
{
|
||||
NMDhcpClientPrivate *priv;
|
||||
guint32 old_state;
|
||||
|
@ -684,11 +687,19 @@ nm_dhcp_client_new_options (NMDhcpClient *self,
|
|||
GHashTable *str_options = NULL;
|
||||
GObject *ip_config = NULL;
|
||||
|
||||
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
|
||||
g_return_if_fail (options != NULL);
|
||||
g_return_if_fail (reason != NULL);
|
||||
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
|
||||
g_return_val_if_fail (iface != NULL, FALSE);
|
||||
g_return_val_if_fail (pid > 0, FALSE);
|
||||
g_return_val_if_fail (options != NULL, FALSE);
|
||||
g_return_val_if_fail (reason != NULL, FALSE);
|
||||
|
||||
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
|
||||
|
||||
if (g_strcmp0 (priv->iface, iface) != 0)
|
||||
return FALSE;
|
||||
if (priv->pid != pid)
|
||||
return FALSE;
|
||||
|
||||
old_state = priv->state;
|
||||
new_state = reason_to_state (priv->iface, reason);
|
||||
|
||||
|
@ -719,6 +730,8 @@ nm_dhcp_client_new_options (NMDhcpClient *self,
|
|||
if (str_options)
|
||||
g_hash_table_destroy (str_options);
|
||||
g_clear_object (&ip_config);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/********************************************/
|
||||
|
|
|
@ -128,10 +128,6 @@ gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self,
|
|||
|
||||
void nm_dhcp_client_stop (NMDhcpClient *self, gboolean release);
|
||||
|
||||
void nm_dhcp_client_new_options (NMDhcpClient *self,
|
||||
GHashTable *options,
|
||||
const char *reason);
|
||||
|
||||
/* Backend helpers for subclasses */
|
||||
void nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name);
|
||||
|
||||
|
@ -144,5 +140,12 @@ void nm_dhcp_client_set_state (NMDhcpClient *self,
|
|||
GObject *ip_config, /* NMIP4Config or NMIP6Config */
|
||||
GHashTable *options); /* str:str hash */
|
||||
|
||||
gboolean nm_dhcp_client_handle_event (gpointer unused,
|
||||
const char *iface,
|
||||
gint64 pid,
|
||||
GHashTable *options,
|
||||
const char *reason,
|
||||
NMDhcpClient *self);
|
||||
|
||||
#endif /* __NETWORKMANAGER_DHCP_CLIENT_H__ */
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
#include "nm-dhcp-manager.h"
|
||||
#include "nm-posix-signals.h"
|
||||
#include "NetworkManagerUtils.h"
|
||||
#include "nm-dhcp-listener.h"
|
||||
|
||||
G_DEFINE_TYPE (NMDhcpDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT)
|
||||
|
||||
|
@ -612,6 +613,11 @@ nm_dhcp_dhclient_init (NMDhcpDhclient *self)
|
|||
/* Fallback option */
|
||||
if (!priv->def_leasefile)
|
||||
priv->def_leasefile = SYSCONFDIR "/dhclient6.leases";
|
||||
|
||||
g_signal_connect (nm_dhcp_listener_get (),
|
||||
NM_DHCP_LISTENER_EVENT,
|
||||
G_CALLBACK (nm_dhcp_client_handle_event),
|
||||
self);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -619,6 +625,10 @@ dispose (GObject *object)
|
|||
{
|
||||
NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (object);
|
||||
|
||||
g_signal_handlers_disconnect_by_func (nm_dhcp_listener_get (),
|
||||
G_CALLBACK (nm_dhcp_client_handle_event),
|
||||
NM_DHCP_DHCLIENT (object));
|
||||
|
||||
g_free (priv->pid_file);
|
||||
g_free (priv->conf_file);
|
||||
g_free (priv->lease_file);
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
#include "nm-logging.h"
|
||||
#include "nm-posix-signals.h"
|
||||
#include "NetworkManagerUtils.h"
|
||||
#include "nm-dhcp-listener.h"
|
||||
|
||||
G_DEFINE_TYPE (NMDhcpDhcpcd, nm_dhcp_dhcpcd, NM_TYPE_DHCP_CLIENT)
|
||||
|
||||
|
@ -190,6 +191,10 @@ stop (NMDhcpClient *client, gboolean release, const GByteArray *duid)
|
|||
static void
|
||||
nm_dhcp_dhcpcd_init (NMDhcpDhcpcd *self)
|
||||
{
|
||||
g_signal_connect (nm_dhcp_listener_get (),
|
||||
NM_DHCP_LISTENER_EVENT,
|
||||
G_CALLBACK (nm_dhcp_client_handle_event),
|
||||
self);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -197,6 +202,10 @@ dispose (GObject *object)
|
|||
{
|
||||
NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (object);
|
||||
|
||||
g_signal_handlers_disconnect_by_func (nm_dhcp_listener_get (),
|
||||
G_CALLBACK (nm_dhcp_client_handle_event),
|
||||
NM_DHCP_DHCPCD (object));
|
||||
|
||||
g_free (priv->pid_file);
|
||||
|
||||
G_OBJECT_CLASS (nm_dhcp_dhcpcd_parent_class)->dispose (object);
|
||||
|
|
289
src/dhcp-manager/nm-dhcp-listener.c
Normal file
289
src/dhcp-manager/nm-dhcp-listener.c
Normal file
|
@ -0,0 +1,289 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* This program 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright 2014 Red Hat, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include <glib.h>
|
||||
#include <glib/gi18n.h>
|
||||
#include <dbus/dbus.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "nm-dhcp-listener.h"
|
||||
#include "nm-logging.h"
|
||||
#include "nm-dbus-manager.h"
|
||||
#include "nm-dbus-glib-types.h"
|
||||
#include "nm-glib-compat.h"
|
||||
#include "NetworkManagerUtils.h"
|
||||
|
||||
#define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client"
|
||||
#define PRIV_SOCK_PATH NMRUNDIR "/private-dhcp"
|
||||
#define PRIV_SOCK_TAG "dhcp"
|
||||
|
||||
typedef struct {
|
||||
NMDBusManager * dbus_mgr;
|
||||
guint new_conn_id;
|
||||
guint dis_conn_id;
|
||||
GHashTable * proxies;
|
||||
DBusGProxy * proxy;
|
||||
} NMDhcpListenerPrivate;
|
||||
|
||||
#define NM_DHCP_LISTENER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_LISTENER, NMDhcpListenerPrivate))
|
||||
|
||||
G_DEFINE_TYPE (NMDhcpListener, nm_dhcp_listener, G_TYPE_OBJECT)
|
||||
|
||||
enum {
|
||||
EVENT,
|
||||
LAST_SIGNAL
|
||||
};
|
||||
static guint signals[LAST_SIGNAL] = { 0 };
|
||||
|
||||
/***************************************************/
|
||||
|
||||
static char *
|
||||
garray_to_string (GArray *array, const char *key)
|
||||
{
|
||||
GString *str;
|
||||
int i;
|
||||
unsigned char c;
|
||||
char *converted = NULL;
|
||||
|
||||
g_return_val_if_fail (array != NULL, NULL);
|
||||
|
||||
/* Since the DHCP options come through environment variables, they should
|
||||
* already be UTF-8 safe, but just make sure.
|
||||
*/
|
||||
str = g_string_sized_new (array->len);
|
||||
for (i = 0; i < array->len; i++) {
|
||||
c = array->data[i];
|
||||
|
||||
/* Convert NULLs to spaces and non-ASCII characters to ? */
|
||||
if (c == '\0')
|
||||
c = ' ';
|
||||
else if (c > 127)
|
||||
c = '?';
|
||||
str = g_string_append_c (str, c);
|
||||
}
|
||||
str = g_string_append_c (str, '\0');
|
||||
|
||||
converted = str->str;
|
||||
if (!g_utf8_validate (converted, -1, NULL))
|
||||
nm_log_warn (LOGD_DHCP, "DHCP option '%s' couldn't be converted to UTF-8", key);
|
||||
g_string_free (str, FALSE);
|
||||
return converted;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_option (GHashTable *hash, const char *key)
|
||||
{
|
||||
GValue *value;
|
||||
|
||||
value = g_hash_table_lookup (hash, key);
|
||||
if (value == NULL)
|
||||
return NULL;
|
||||
|
||||
if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) {
|
||||
nm_log_warn (LOGD_DHCP, "unexpected key %s value type was not "
|
||||
"DBUS_TYPE_G_UCHAR_ARRAY",
|
||||
(char *) key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return garray_to_string ((GArray *) g_value_get_boxed (value), key);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_event (DBusGProxy *proxy,
|
||||
GHashTable *options,
|
||||
gpointer user_data)
|
||||
{
|
||||
NMDhcpListener *self = NM_DHCP_LISTENER (user_data);
|
||||
char *iface = NULL;
|
||||
char *pid_str = NULL;
|
||||
char *reason = NULL;
|
||||
gint32 pid;
|
||||
gboolean handled = FALSE;
|
||||
|
||||
iface = get_option (options, "interface");
|
||||
if (iface == NULL) {
|
||||
nm_log_warn (LOGD_DHCP, "DHCP event: didn't have associated interface.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
pid_str = get_option (options, "pid");
|
||||
pid = (gint32) nm_utils_ascii_str_to_int64 (pid_str, 10, 0, G_MAXINT32, -1);
|
||||
if (pid == -1 || pid != (GPid) pid) {
|
||||
nm_log_warn (LOGD_DHCP, "DHCP event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)");
|
||||
goto out;
|
||||
}
|
||||
|
||||
reason = get_option (options, "reason");
|
||||
if (reason == NULL) {
|
||||
nm_log_warn (LOGD_DHCP, "(pid %d) DHCP event didn't have a reason", pid);
|
||||
goto out;
|
||||
}
|
||||
|
||||
g_signal_emit (self, signals[EVENT], 0, iface, pid, options, reason, &handled);
|
||||
if (!handled) {
|
||||
if (g_ascii_strcasecmp (reason, "RELEASE") == 0) {
|
||||
/* Ignore event when the dhcp client gets killed and we receive its last message */
|
||||
nm_log_dbg (LOGD_DHCP, "(pid %d) unhandled RELEASE DHCP event for interface %s", pid, iface);
|
||||
} else
|
||||
nm_log_warn (LOGD_DHCP, "(pid %d) unhandled DHCP event for interface %s", pid, iface);
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (iface);
|
||||
g_free (pid_str);
|
||||
g_free (reason);
|
||||
}
|
||||
|
||||
#if HAVE_DBUS_GLIB_100
|
||||
static void
|
||||
new_connection_cb (NMDBusManager *mgr,
|
||||
DBusGConnection *connection,
|
||||
NMDhcpListener *self)
|
||||
{
|
||||
DBusGProxy *proxy;
|
||||
|
||||
/* Create a new proxy for the client */
|
||||
proxy = dbus_g_proxy_new_for_peer (connection, "/", NM_DHCP_CLIENT_DBUS_IFACE);
|
||||
dbus_g_proxy_add_signal (proxy, "Event", DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (proxy, "Event", G_CALLBACK (handle_event), self, NULL);
|
||||
|
||||
g_hash_table_insert (NM_DHCP_LISTENER_GET_PRIVATE (self)->proxies, connection, proxy);
|
||||
}
|
||||
|
||||
static void
|
||||
dis_connection_cb (NMDBusManager *mgr,
|
||||
DBusGConnection *connection,
|
||||
NMDhcpListener *self)
|
||||
{
|
||||
NMDhcpListenerPrivate *priv = NM_DHCP_LISTENER_GET_PRIVATE (self);
|
||||
DBusGProxy *proxy;
|
||||
|
||||
proxy = g_hash_table_lookup (priv->proxies, connection);
|
||||
if (proxy) {
|
||||
dbus_g_proxy_disconnect_signal (proxy, "Event", G_CALLBACK (handle_event), self);
|
||||
g_hash_table_remove (priv->proxies, connection);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/***************************************************/
|
||||
|
||||
NMDhcpListener *
|
||||
nm_dhcp_listener_get (void)
|
||||
{
|
||||
static NMDhcpListener *singleton = NULL;
|
||||
|
||||
if (G_UNLIKELY (singleton == NULL))
|
||||
singleton = g_object_new (NM_TYPE_DHCP_LISTENER, NULL);
|
||||
g_assert (singleton);
|
||||
return singleton;
|
||||
}
|
||||
|
||||
static void
|
||||
nm_dhcp_listener_init (NMDhcpListener *self)
|
||||
{
|
||||
NMDhcpListenerPrivate *priv = NM_DHCP_LISTENER_GET_PRIVATE (self);
|
||||
#if !HAVE_DBUS_GLIB_100
|
||||
DBusGConnection *g_connection;
|
||||
#endif
|
||||
|
||||
/* Maps DBusGConnection :: DBusGProxy */
|
||||
priv->proxies = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
|
||||
|
||||
priv->dbus_mgr = nm_dbus_manager_get ();
|
||||
|
||||
#if HAVE_DBUS_GLIB_100
|
||||
/* Register the socket our DHCP clients will return lease info on */
|
||||
nm_dbus_manager_private_server_register (priv->dbus_mgr, PRIV_SOCK_PATH, PRIV_SOCK_TAG);
|
||||
priv->new_conn_id = g_signal_connect (priv->dbus_mgr,
|
||||
NM_DBUS_MANAGER_PRIVATE_CONNECTION_NEW "::" PRIV_SOCK_TAG,
|
||||
G_CALLBACK (new_connection_cb),
|
||||
self);
|
||||
priv->dis_conn_id = g_signal_connect (priv->dbus_mgr,
|
||||
NM_DBUS_MANAGER_PRIVATE_CONNECTION_DISCONNECTED "::" PRIV_SOCK_TAG,
|
||||
G_CALLBACK (dis_connection_cb),
|
||||
self);
|
||||
#else
|
||||
g_connection = nm_dbus_manager_get_connection (priv->dbus_mgr);
|
||||
priv->proxy = dbus_g_proxy_new_for_name (g_connection,
|
||||
"org.freedesktop.nm_dhcp_client",
|
||||
"/",
|
||||
NM_DHCP_CLIENT_DBUS_IFACE);
|
||||
g_assert (priv->proxy);
|
||||
dbus_g_proxy_add_signal (priv->proxy, "Event", DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (priv->proxy, "Event", G_CALLBACK (handle_event), self, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
dispose (GObject *object)
|
||||
{
|
||||
NMDhcpListenerPrivate *priv = NM_DHCP_LISTENER_GET_PRIVATE (object);
|
||||
|
||||
if (priv->new_conn_id) {
|
||||
g_signal_handler_disconnect (priv->dbus_mgr, priv->new_conn_id);
|
||||
priv->new_conn_id = 0;
|
||||
}
|
||||
if (priv->dis_conn_id) {
|
||||
g_signal_handler_disconnect (priv->dbus_mgr, priv->dis_conn_id);
|
||||
priv->dis_conn_id = 0;
|
||||
}
|
||||
priv->dbus_mgr = NULL;
|
||||
|
||||
if (priv->proxies) {
|
||||
g_hash_table_destroy (priv->proxies);
|
||||
priv->proxies = NULL;
|
||||
}
|
||||
g_clear_object (&priv->proxy);
|
||||
|
||||
G_OBJECT_CLASS (nm_dhcp_listener_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static void
|
||||
nm_dhcp_listener_class_init (NMDhcpListenerClass *listener_class)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (listener_class);
|
||||
|
||||
g_type_class_add_private (listener_class, sizeof (NMDhcpListenerPrivate));
|
||||
|
||||
/* virtual methods */
|
||||
object_class->dispose = dispose;
|
||||
|
||||
/* signals */
|
||||
signals[EVENT] =
|
||||
g_signal_new (NM_DHCP_LISTENER_EVENT,
|
||||
G_OBJECT_CLASS_TYPE (object_class),
|
||||
G_SIGNAL_RUN_LAST, 0,
|
||||
g_signal_accumulator_true_handled,
|
||||
NULL, NULL,
|
||||
G_TYPE_BOOLEAN, /* listeners return TRUE if handled */
|
||||
4,
|
||||
G_TYPE_STRING, /* iface */
|
||||
G_TYPE_INT64, /* pid */
|
||||
G_TYPE_HASH_TABLE, /* options */
|
||||
G_TYPE_STRING); /* reason */
|
||||
}
|
39
src/dhcp-manager/nm-dhcp-listener.h
Normal file
39
src/dhcp-manager/nm-dhcp-listener.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* This program 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright 2014 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
#ifndef __NETWORKMANAGER_DHCP_LISTENER_H__
|
||||
#define __NETWORKMANAGER_DHCP_LISTENER_H__
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
|
||||
#define NM_TYPE_DHCP_LISTENER (nm_dhcp_listener_get_type ())
|
||||
#define NM_DHCP_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_LISTENER, NMDhcpListener))
|
||||
#define NM_IS_DHCP_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_LISTENER))
|
||||
#define NM_DHCP_LISTENER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_LISTENER, NMDhcpListenerClass))
|
||||
|
||||
#define NM_DHCP_LISTENER_EVENT "event"
|
||||
|
||||
typedef GObject NMDhcpListener;
|
||||
typedef GObjectClass NMDhcpListenerClass;
|
||||
|
||||
GType nm_dhcp_listener_get_type (void);
|
||||
|
||||
NMDhcpListener *nm_dhcp_listener_get (void);
|
||||
|
||||
#endif /* __NETWORKMANAGER_DHCP_LISTENER_H__ */
|
|
@ -23,7 +23,6 @@
|
|||
#include "config.h"
|
||||
#include <glib.h>
|
||||
#include <glib/gi18n.h>
|
||||
#include <dbus/dbus.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
|
@ -39,19 +38,13 @@
|
|||
#include "nm-dhcp-dhcpcd.h"
|
||||
#include "nm-dhcp-systemd.h"
|
||||
#include "nm-logging.h"
|
||||
#include "nm-dbus-manager.h"
|
||||
#include "nm-config.h"
|
||||
#include "nm-dbus-glib-types.h"
|
||||
#include "nm-glib-compat.h"
|
||||
#include "NetworkManagerUtils.h"
|
||||
|
||||
#define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client"
|
||||
|
||||
#define DHCP_TIMEOUT 45 /* default DHCP timeout, in seconds */
|
||||
|
||||
#define PRIV_SOCK_PATH NMRUNDIR "/private-dhcp"
|
||||
#define PRIV_SOCK_TAG "dhcp"
|
||||
|
||||
/* default to installed helper, but can be modified for testing */
|
||||
const char *nm_dhcp_helper_path = LIBEXECDIR "/nm-dhcp-helper";
|
||||
|
||||
|
@ -60,76 +53,15 @@ typedef GSList * (*GetLeaseConfigFunc) (const char *iface, const char *uuid, gbo
|
|||
typedef struct {
|
||||
GType client_type;
|
||||
GetLeaseConfigFunc get_lease_ip_configs_func;
|
||||
|
||||
NMDBusManager * dbus_mgr;
|
||||
guint new_conn_id;
|
||||
guint dis_conn_id;
|
||||
GHashTable * proxies;
|
||||
|
||||
GHashTable * clients;
|
||||
DBusGProxy * proxy;
|
||||
char * default_hostname;
|
||||
} NMDhcpManagerPrivate;
|
||||
|
||||
|
||||
#define NM_DHCP_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_MANAGER, NMDhcpManagerPrivate))
|
||||
|
||||
G_DEFINE_TYPE (NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT)
|
||||
|
||||
static char *
|
||||
garray_to_string (GArray *array, const char *key)
|
||||
{
|
||||
GString *str;
|
||||
int i;
|
||||
unsigned char c;
|
||||
char *converted = NULL;
|
||||
|
||||
g_return_val_if_fail (array != NULL, NULL);
|
||||
|
||||
/* Since the DHCP options come through environment variables, they should
|
||||
* already be UTF-8 safe, but just make sure.
|
||||
*/
|
||||
str = g_string_sized_new (array->len);
|
||||
for (i = 0; i < array->len; i++) {
|
||||
c = array->data[i];
|
||||
|
||||
/* Convert NULLs to spaces and non-ASCII characters to ? */
|
||||
if (c == '\0')
|
||||
c = ' ';
|
||||
else if (c > 127)
|
||||
c = '?';
|
||||
str = g_string_append_c (str, c);
|
||||
}
|
||||
str = g_string_append_c (str, '\0');
|
||||
|
||||
converted = str->str;
|
||||
if (!g_utf8_validate (converted, -1, NULL))
|
||||
nm_log_warn (LOGD_DHCP, "DHCP option '%s' couldn't be converted to UTF-8", key);
|
||||
g_string_free (str, FALSE);
|
||||
return converted;
|
||||
}
|
||||
|
||||
static NMDhcpClient *
|
||||
get_client_for_pid (NMDhcpManager *manager, GPid pid)
|
||||
{
|
||||
NMDhcpManagerPrivate *priv;
|
||||
GHashTableIter iter;
|
||||
gpointer value;
|
||||
|
||||
g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL);
|
||||
|
||||
priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
|
||||
|
||||
g_hash_table_iter_init (&iter, priv->clients);
|
||||
while (g_hash_table_iter_next (&iter, NULL, &value)) {
|
||||
NMDhcpClient *candidate = NM_DHCP_CLIENT (value);
|
||||
|
||||
if (nm_dhcp_client_get_pid (candidate) == pid)
|
||||
return candidate;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
/***************************************************/
|
||||
|
||||
static NMDhcpClient *
|
||||
get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6)
|
||||
|
@ -155,123 +87,6 @@ get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_option (GHashTable *hash, const char *key)
|
||||
{
|
||||
GValue *value;
|
||||
|
||||
value = g_hash_table_lookup (hash, key);
|
||||
if (value == NULL)
|
||||
return NULL;
|
||||
|
||||
if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) {
|
||||
nm_log_warn (LOGD_DHCP, "unexpected key %s value type was not "
|
||||
"DBUS_TYPE_G_UCHAR_ARRAY",
|
||||
(char *) key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return garray_to_string ((GArray *) g_value_get_boxed (value), key);
|
||||
}
|
||||
|
||||
static void
|
||||
nm_dhcp_manager_handle_event (DBusGProxy *proxy,
|
||||
GHashTable *options,
|
||||
gpointer user_data)
|
||||
{
|
||||
NMDhcpManager *manager = NM_DHCP_MANAGER (user_data);
|
||||
NMDhcpClient *client;
|
||||
char *iface = NULL;
|
||||
char *pid_str = NULL;
|
||||
char *reason = NULL;
|
||||
long pid;
|
||||
|
||||
iface = get_option (options, "interface");
|
||||
if (iface == NULL) {
|
||||
nm_log_warn (LOGD_DHCP, "DHCP event: didn't have associated interface.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
pid_str = get_option (options, "pid");
|
||||
pid = nm_utils_ascii_str_to_int64 (pid_str, 10, 0, LONG_MAX, -1);
|
||||
if (pid == -1 || pid != (GPid)pid) {
|
||||
nm_log_warn (LOGD_DHCP, "DHCP event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)");
|
||||
goto out;
|
||||
}
|
||||
|
||||
reason = get_option (options, "reason");
|
||||
client = get_client_for_pid (manager, (GPid) pid);
|
||||
if (client == NULL) {
|
||||
if (reason && g_ascii_strcasecmp (reason, "RELEASE") == 0) {
|
||||
/* This happens regularly, when the dhcp client gets killed and we receive its last message.
|
||||
* Don't log a warning in this case. */
|
||||
nm_log_dbg (LOGD_DHCP, "(pid %ld) unhandled RELEASE DHCP event for interface %s", pid, iface);
|
||||
} else
|
||||
nm_log_warn (LOGD_DHCP, "(pid %ld) unhandled DHCP event for interface %s", pid, iface);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (strcmp (iface, nm_dhcp_client_get_iface (client))) {
|
||||
nm_log_warn (LOGD_DHCP, "(pid %ld) received DHCP event from unexpected interface '%s' (expected '%s')",
|
||||
pid, iface, nm_dhcp_client_get_iface (client));
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (reason == NULL) {
|
||||
nm_log_warn (LOGD_DHCP, "(pid %ld) DHCP event didn't have a reason", pid);
|
||||
goto out;
|
||||
}
|
||||
|
||||
nm_dhcp_client_new_options (client, options, reason);
|
||||
|
||||
out:
|
||||
g_free (iface);
|
||||
g_free (pid_str);
|
||||
g_free (reason);
|
||||
}
|
||||
|
||||
#if HAVE_DBUS_GLIB_100
|
||||
static void
|
||||
new_connection_cb (NMDBusManager *mgr,
|
||||
DBusGConnection *connection,
|
||||
NMDhcpManager *self)
|
||||
{
|
||||
NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
|
||||
DBusGProxy *proxy;
|
||||
|
||||
/* Create a new proxy for the client */
|
||||
proxy = dbus_g_proxy_new_for_peer (connection, "/", NM_DHCP_CLIENT_DBUS_IFACE);
|
||||
dbus_g_proxy_add_signal (proxy,
|
||||
"Event",
|
||||
DBUS_TYPE_G_MAP_OF_VARIANT,
|
||||
G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (proxy,
|
||||
"Event",
|
||||
G_CALLBACK (nm_dhcp_manager_handle_event),
|
||||
self,
|
||||
NULL);
|
||||
g_hash_table_insert (priv->proxies, connection, proxy);
|
||||
}
|
||||
|
||||
static void
|
||||
dis_connection_cb (NMDBusManager *mgr,
|
||||
DBusGConnection *connection,
|
||||
NMDhcpManager *self)
|
||||
{
|
||||
NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
|
||||
DBusGProxy *proxy;
|
||||
|
||||
proxy = g_hash_table_lookup (priv->proxies, connection);
|
||||
if (proxy) {
|
||||
dbus_g_proxy_disconnect_signal (proxy,
|
||||
"Event",
|
||||
G_CALLBACK (nm_dhcp_manager_handle_event),
|
||||
self);
|
||||
g_hash_table_remove (priv->proxies, connection);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static GType
|
||||
get_client_type (const char *client, GError **error)
|
||||
{
|
||||
|
@ -529,12 +344,6 @@ nm_dhcp_manager_init (NMDhcpManager *self)
|
|||
NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
|
||||
const char *client;
|
||||
GError *error = NULL;
|
||||
#if !HAVE_DBUS_GLIB_100
|
||||
DBusGConnection *g_connection;
|
||||
#endif
|
||||
|
||||
/* Maps DBusGConnection :: DBusGProxy */
|
||||
priv->proxies = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
|
||||
|
||||
/* Client-specific setup */
|
||||
client = nm_config_get_dhcp_client (nm_config_get ());
|
||||
|
@ -554,33 +363,6 @@ nm_dhcp_manager_init (NMDhcpManager *self)
|
|||
NULL,
|
||||
(GDestroyNotify) g_object_unref);
|
||||
g_assert (priv->clients);
|
||||
|
||||
priv->dbus_mgr = nm_dbus_manager_get ();
|
||||
|
||||
#if HAVE_DBUS_GLIB_100
|
||||
/* Register the socket our DHCP clients will return lease info on */
|
||||
nm_dbus_manager_private_server_register (priv->dbus_mgr, PRIV_SOCK_PATH, PRIV_SOCK_TAG);
|
||||
priv->new_conn_id = g_signal_connect (priv->dbus_mgr,
|
||||
NM_DBUS_MANAGER_PRIVATE_CONNECTION_NEW "::" PRIV_SOCK_TAG,
|
||||
(GCallback) new_connection_cb,
|
||||
self);
|
||||
priv->dis_conn_id = g_signal_connect (priv->dbus_mgr,
|
||||
NM_DBUS_MANAGER_PRIVATE_CONNECTION_DISCONNECTED "::" PRIV_SOCK_TAG,
|
||||
(GCallback) dis_connection_cb,
|
||||
self);
|
||||
#else
|
||||
g_connection = nm_dbus_manager_get_connection (priv->dbus_mgr);
|
||||
priv->proxy = dbus_g_proxy_new_for_name (g_connection,
|
||||
"org.freedesktop.nm_dhcp_client",
|
||||
"/",
|
||||
NM_DHCP_CLIENT_DBUS_IFACE);
|
||||
g_assert (priv->proxy);
|
||||
dbus_g_proxy_add_signal (priv->proxy, "Event", DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (priv->proxy, "Event",
|
||||
G_CALLBACK (nm_dhcp_manager_handle_event),
|
||||
self,
|
||||
NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -596,23 +378,6 @@ dispose (GObject *object)
|
|||
g_list_free (values);
|
||||
}
|
||||
|
||||
if (priv->new_conn_id) {
|
||||
g_signal_handler_disconnect (priv->dbus_mgr, priv->new_conn_id);
|
||||
priv->new_conn_id = 0;
|
||||
}
|
||||
if (priv->dis_conn_id) {
|
||||
g_signal_handler_disconnect (priv->dbus_mgr, priv->dis_conn_id);
|
||||
priv->dis_conn_id = 0;
|
||||
}
|
||||
priv->dbus_mgr = NULL;
|
||||
|
||||
if (priv->proxies) {
|
||||
g_hash_table_destroy (priv->proxies);
|
||||
priv->proxies = NULL;
|
||||
}
|
||||
if (priv->proxy)
|
||||
g_object_unref (priv->proxy);
|
||||
|
||||
G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue