From 18d372272cdcdda8136a7a5602b28d612e392a5e Mon Sep 17 00:00:00 2001 From: Tambet Ingo Date: Wed, 10 Oct 2007 14:36:31 +0000 Subject: [PATCH] 2007-10-10 Tambet Ingo Move ppp-manager over to dbus-glib. The big deal is that it was the last piece of code that used NM's own version of dbus signal handling and custom dictionary marshalling/unmarshalling. With this change, all that obsolete code can disappear and we get to maintain over 2000 lines less code. * libnm-util/dbus-dict-helpers.c: * libnm-util/dbus-dict-helpers.h: Remove. * src/ppp-manager/nm-pppd-plugin.c: Convert it to use dbus-glib. * src/ppp-manager/nm-pppd-plugin.xml: Implement. * src/ppp-manager/nm-ppp-manager.c: Use dbus-glib instead of * home-brewed dbus signal handlers. * src/nm-dbus-manager.c: Remove all the manual dbus signal * handling. * configure.in: Remove test/libnm-util/Makefile creation. * test/Makefile.am: Remove libnm-util from SUBDIRS. * test/libnm-util/: Remove the whole directory. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2965 4912f4e0-d625-0410-9fb7-b9a5a253dbdc --- ChangeLog | 25 + configure.in | 1 - libnm-util/Makefile.am | 3 - libnm-util/dbus-dict-helpers.c | 1139 ----------------- libnm-util/dbus-dict-helpers.h | 177 --- src/NetworkManager.c | 9 - src/nm-dbus-manager.c | 559 +------- src/nm-dbus-manager.h | 9 - src/ppp-manager/Makefile.am | 7 + src/ppp-manager/nm-ppp-manager.c | 217 ++-- src/ppp-manager/nm-pppd-plugin.c | 414 +++--- src/ppp-manager/nm-pppd-plugin.h | 7 + src/ppp-manager/nm-pppd-plugin.xml | 14 + src/supplicant-manager/nm-supplicant-config.c | 1 - .../nm-supplicant-interface.c | 1 - test/Makefile.am | 2 +- test/libnm-util/.cvsignore | 5 - test/libnm-util/Makefile.am | 20 - test/libnm-util/test-dbus-dict-helpers.c | 355 ----- 19 files changed, 429 insertions(+), 2536 deletions(-) delete mode 100644 libnm-util/dbus-dict-helpers.c delete mode 100644 libnm-util/dbus-dict-helpers.h create mode 100644 src/ppp-manager/nm-pppd-plugin.xml delete mode 100644 test/libnm-util/.cvsignore delete mode 100644 test/libnm-util/Makefile.am delete mode 100644 test/libnm-util/test-dbus-dict-helpers.c diff --git a/ChangeLog b/ChangeLog index d5a728de64..4ef973eda2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,28 @@ +2007-10-10 Tambet Ingo + + Move ppp-manager over to dbus-glib. The big deal is that it was the last piece of + code that used NM's own version of dbus signal handling and custom dictionary + marshalling/unmarshalling. With this change, all that obsolete code can disappear + and we get to maintain over 2000 lines less code. + + * libnm-util/dbus-dict-helpers.c: + * libnm-util/dbus-dict-helpers.h: Remove. + + * src/ppp-manager/nm-pppd-plugin.c: Convert it to use dbus-glib. + + * src/ppp-manager/nm-pppd-plugin.xml: Implement. + + * src/ppp-manager/nm-ppp-manager.c: Use dbus-glib instead of home-brewed dbus signal + handlers. + + * src/nm-dbus-manager.c: Remove all the manual dbus signal handling. + + * configure.in: Remove test/libnm-util/Makefile creation. + + * test/Makefile.am: Remove libnm-util from SUBDIRS. + + * test/libnm-util/: Remove the whole directory. + 2007-10-10 Tambet Ingo * src/NetworkManagerPolicy.c (nm_policy_new): Initialize the diff --git a/configure.in b/configure.in index b4d33e9dfd..311a36fe5e 100644 --- a/configure.in +++ b/configure.in @@ -252,7 +252,6 @@ callouts/Makefile dispatcher-daemon/Makefile test/Makefile test/test-common/Makefile -test/libnm-util/Makefile initscript/Makefile initscript/RedHat/Makefile initscript/RedHat/NetworkManager diff --git a/libnm-util/Makefile.am b/libnm-util/Makefile.am index a3175ccc92..27eab0def2 100644 --- a/libnm-util/Makefile.am +++ b/libnm-util/Makefile.am @@ -12,8 +12,6 @@ libnm_util_la_CPPFLAGS = \ -DGNOMELOCALEDIR=\"$(datadir)/locale\" libnm_util_la_SOURCES= \ - dbus-dict-helpers.c \ - dbus-dict-helpers.h \ nm-connection.c \ nm-connection.h \ nm-setting.c \ @@ -28,7 +26,6 @@ libnm_util_la_CFLAGS=-fPIC libnm_util_includedir=$(includedir)/NetworkManager libnm_util_include_HEADERS = \ - dbus-dict-helpers.h \ nm-connection.h \ nm-setting.h \ nm-utils.h diff --git a/libnm-util/dbus-dict-helpers.c b/libnm-util/dbus-dict-helpers.c deleted file mode 100644 index 8944dd9815..0000000000 --- a/libnm-util/dbus-dict-helpers.c +++ /dev/null @@ -1,1139 +0,0 @@ -/* NetworkManager -- Network link manager - * - * Dan Williams - * - * 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 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * (C) Copyright 2006 Red Hat, Inc. - */ - -#include -#include -#include -#include - -#include "dbus-dict-helpers.h" - - -/** - * Start a dict in a dbus message. Should be paired with a call to - * {@link nmu_dbus_dict_close_write}. - * - * @param iter A valid dbus message iterator - * @param iter_dict (out) A dict iterator to pass to further dict functions - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_open_write (DBusMessageIter *iter, - DBusMessageIter *iter_dict) -{ - dbus_bool_t result; - - if (!iter || !iter_dict) - return FALSE; - - result = dbus_message_iter_open_container (iter, - DBUS_TYPE_ARRAY, - DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING - DBUS_TYPE_STRING_AS_STRING - DBUS_TYPE_VARIANT_AS_STRING - DBUS_DICT_ENTRY_END_CHAR_AS_STRING, - iter_dict); - return result; -} - - -/** - * End a dict element in a dbus message. Should be paired with - * a call to {@link nmu_dbus_dict_open_write}. - * - * @param iter valid dbus message iterator, same as passed to - * nmu_dbus_dict_open_write() - * @param iter_dict a dbus dict iterator returned from - * {@link nmu_dbus_dict_open_write} - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_close_write (DBusMessageIter *iter, - DBusMessageIter *iter_dict) -{ - if (!iter || !iter_dict) - return FALSE; - - return dbus_message_iter_close_container (iter, iter_dict); -} - - -static const char * -_nmu_get_type_as_string_from_type (const int type) -{ - switch (type) { - case DBUS_TYPE_BYTE: - return DBUS_TYPE_BYTE_AS_STRING; - case DBUS_TYPE_BOOLEAN: - return DBUS_TYPE_BOOLEAN_AS_STRING; - case DBUS_TYPE_INT16: - return DBUS_TYPE_INT16_AS_STRING; - case DBUS_TYPE_UINT16: - return DBUS_TYPE_UINT16_AS_STRING; - case DBUS_TYPE_INT32: - return DBUS_TYPE_INT32_AS_STRING; - case DBUS_TYPE_UINT32: - return DBUS_TYPE_UINT32_AS_STRING; - case DBUS_TYPE_INT64: - return DBUS_TYPE_INT64_AS_STRING; - case DBUS_TYPE_UINT64: - return DBUS_TYPE_UINT64_AS_STRING; - case DBUS_TYPE_DOUBLE: - return DBUS_TYPE_DOUBLE_AS_STRING; - case DBUS_TYPE_STRING: - return DBUS_TYPE_STRING_AS_STRING; - case DBUS_TYPE_OBJECT_PATH: - return DBUS_TYPE_OBJECT_PATH_AS_STRING; - case DBUS_TYPE_ARRAY: - return DBUS_TYPE_ARRAY_AS_STRING; - default: - return NULL; - } - return NULL; -} - - -static dbus_bool_t -_nmu_dbus_add_dict_entry_start (DBusMessageIter *iter_dict, - DBusMessageIter *iter_dict_entry, - const char *key, - const int value_type) -{ - if (!dbus_message_iter_open_container (iter_dict, - DBUS_TYPE_DICT_ENTRY, - NULL, - iter_dict_entry)) - return FALSE; - - if (!dbus_message_iter_append_basic (iter_dict_entry, - DBUS_TYPE_STRING, - &key)) - return FALSE; - - return TRUE; -} - - -static dbus_bool_t -_nmu_dbus_add_dict_entry_end (DBusMessageIter *iter_dict, - DBusMessageIter *iter_dict_entry, - DBusMessageIter *iter_dict_val) -{ - if (!dbus_message_iter_close_container (iter_dict_entry, iter_dict_val)) - return FALSE; - if (!dbus_message_iter_close_container (iter_dict, iter_dict_entry)) - return FALSE; - - return TRUE; -} - - -static dbus_bool_t -_nmu_dbus_add_dict_entry_basic (DBusMessageIter *iter_dict, - const char *key, - const int value_type, - const void *value) -{ - DBusMessageIter iter_dict_entry, iter_dict_val; - const char * type_as_string = NULL; - - type_as_string = _nmu_get_type_as_string_from_type (value_type); - if (!type_as_string) - return FALSE; - - if (!_nmu_dbus_add_dict_entry_start (iter_dict, - &iter_dict_entry, - key, - value_type)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_entry, - DBUS_TYPE_VARIANT, - type_as_string, - &iter_dict_val)) - return FALSE; - - if (!dbus_message_iter_append_basic (&iter_dict_val, value_type, value)) - return FALSE; - - if (!_nmu_dbus_add_dict_entry_end (iter_dict, - &iter_dict_entry, - &iter_dict_val)) - return FALSE; - - return TRUE; -} - - -static dbus_bool_t -_nmu_dbus_add_dict_entry_byte_array (DBusMessageIter *iter_dict, - const char *key, - const char *value, - const dbus_uint32_t value_len) -{ - DBusMessageIter iter_dict_entry, iter_dict_val, iter_array; - dbus_uint32_t i; - - if (!_nmu_dbus_add_dict_entry_start (iter_dict, - &iter_dict_entry, - key, - DBUS_TYPE_ARRAY)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_entry, - DBUS_TYPE_VARIANT, - DBUS_TYPE_ARRAY_AS_STRING - DBUS_TYPE_BYTE_AS_STRING, - &iter_dict_val)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_val, - DBUS_TYPE_ARRAY, - DBUS_TYPE_BYTE_AS_STRING, - &iter_array)) - return FALSE; - - for (i = 0; i < value_len; i++) { - if (!dbus_message_iter_append_basic (&iter_array, - DBUS_TYPE_BYTE, - &(value[i]))) - return FALSE; - } - - if (!dbus_message_iter_close_container (&iter_dict_val, &iter_array)) - return FALSE; - - if (!_nmu_dbus_add_dict_entry_end (iter_dict, - &iter_dict_entry, - &iter_dict_val)) - return FALSE; - - return TRUE; -} - - -static dbus_bool_t -_nmu_dbus_add_dict_entry_uint32_array (DBusMessageIter *iter_dict, - const char *key, - const dbus_uint32_t *value, - const dbus_uint32_t value_len) -{ - DBusMessageIter iter_dict_entry, iter_dict_val, iter_array; - dbus_uint32_t i; - - if (!_nmu_dbus_add_dict_entry_start (iter_dict, - &iter_dict_entry, - key, - DBUS_TYPE_ARRAY)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_entry, - DBUS_TYPE_VARIANT, - DBUS_TYPE_ARRAY_AS_STRING - DBUS_TYPE_UINT32_AS_STRING, - &iter_dict_val)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_val, - DBUS_TYPE_ARRAY, - DBUS_TYPE_UINT32_AS_STRING, - &iter_array)) - return FALSE; - - for (i = 0; i < value_len; i++) { - if (!dbus_message_iter_append_basic (&iter_array, - DBUS_TYPE_UINT32, - &(value[i]))) - return FALSE; - } - - if (!dbus_message_iter_close_container (&iter_dict_val, &iter_array)) - return FALSE; - - if (!_nmu_dbus_add_dict_entry_end (iter_dict, - &iter_dict_entry, - &iter_dict_val)) - return FALSE; - - return TRUE; -} - - -/** - * Add a string entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The string value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_string (DBusMessageIter *iter_dict, - const char * key, - const char * value) -{ - if (!key || !value) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_STRING, &value); -} - -/** - * Add a byte entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The byte value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_byte (DBusMessageIter *iter_dict, - const char * key, - const char value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_BYTE, &value); -} - -/** - * Add a boolean entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The boolean value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_bool (DBusMessageIter *iter_dict, - const char * key, - const dbus_bool_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_BOOLEAN, &value); -} - -/** - * Add a 16-bit signed integer entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The 16-bit signed integer value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_int16 (DBusMessageIter *iter_dict, - const char * key, - const dbus_int16_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_INT16, &value); -} - -/** - * Add a 16-bit unsigned integer entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The 16-bit unsigned integer value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_uint16 (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint16_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_UINT16, &value); -} - -/** - * Add a 32-bit signed integer to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The 32-bit signed integer value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_int32 (DBusMessageIter *iter_dict, - const char * key, - const dbus_int32_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_INT32, &value); -} - -/** - * Add a 32-bit unsigned integer entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The 32-bit unsigned integer value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_uint32 (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint32_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_UINT32, &value); -} - -/** - * Add a 64-bit integer entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The 64-bit integer value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_int64 (DBusMessageIter *iter_dict, - const char * key, - const dbus_int64_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_INT64, &value); -} - -/** - * Add a 64-bit unsigned integer entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The 64-bit unsigned integer value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_uint64 (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint64_t value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_UINT64, &value); -} - -/** - * Add a double-precision floating point entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The double-precision floating point value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_double (DBusMessageIter *iter_dict, - const char * key, - const double value) -{ - if (!key) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_DOUBLE, &value); -} - -/** - * Add a DBus object path entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The DBus object path value - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_object_path (DBusMessageIter *iter_dict, - const char * key, - const char * value) -{ - if (!key || !value) - return FALSE; - return _nmu_dbus_add_dict_entry_basic (iter_dict, key, DBUS_TYPE_OBJECT_PATH, &value); -} - -/** - * Add a byte array entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The byte array - * @param value_len The length of the byte array, in bytes - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_byte_array (DBusMessageIter *iter_dict, - const char * key, - const char * value, - const dbus_uint32_t value_len) -{ - if (!key) - return FALSE; - if (!value && (value_len != 0)) - return FALSE; - return _nmu_dbus_add_dict_entry_byte_array (iter_dict, key, value, value_len); -} - - -/** - * Add a uint32 array entry to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param value The uint32 array - * @param value_len The length of the uint32 array, in # of elements - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_uint32_array (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint32_t * value, - const dbus_uint32_t value_len) -{ - if (!key) - return FALSE; - if (!value && (value_len != 0)) - return FALSE; - return _nmu_dbus_add_dict_entry_uint32_array (iter_dict, key, value, value_len); -} - - -/** - * Begin a string array entry in the dict - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param iter_dict_entry A private DBusMessageIter provided by the caller to - * be passed to {@link nmu_dbus_dict_end_string_array} - * @param iter_dict_val A private DBusMessageIter provided by the caller to - * be passed to {@link nmu_dbus_dict_end_string_array} - * @param iter_array On return, the DBusMessageIter to be passed to - * {@link nmu_dbus_dict_string_array_add_element} - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_begin_string_array (DBusMessageIter *iter_dict, - const char *key, - DBusMessageIter *iter_dict_entry, - DBusMessageIter *iter_dict_val, - DBusMessageIter *iter_array) -{ - if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array) - return FALSE; - - if (!_nmu_dbus_add_dict_entry_start (iter_dict, - iter_dict_entry, - key, - DBUS_TYPE_ARRAY)) - return FALSE; - - if (!dbus_message_iter_open_container (iter_dict_entry, - DBUS_TYPE_VARIANT, - DBUS_TYPE_ARRAY_AS_STRING - DBUS_TYPE_STRING_AS_STRING, - iter_dict_val)) - return FALSE; - - if (!dbus_message_iter_open_container (iter_dict_val, - DBUS_TYPE_ARRAY, - DBUS_TYPE_BYTE_AS_STRING, - iter_array)) - return FALSE; - - return TRUE; -} - - -/** - * Add a single string element to a string array dict entry - * - * @param iter_array A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_begin_string_array}'s iter_array parameter - * @param elem The string element to be added to the dict entry's string array - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_string_array_add_element (DBusMessageIter *iter_array, - const char *elem) -{ - if (!iter_array || !elem) - return FALSE; - return dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, &elem); -} - - -/** - * End a string array dict entry - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param iter_dict_entry A private DBusMessageIter returned from - * {@link nmu_dbus_dict_end_string_array} - * @param iter_dict_val A private DBusMessageIter returned from - * {@link nmu_dbus_dict_end_string_array} - * @param iter_array A DBusMessageIter returned from - * {@link nmu_dbus_dict_end_string_array} - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_end_string_array (DBusMessageIter *iter_dict, - DBusMessageIter *iter_dict_entry, - DBusMessageIter *iter_dict_val, - DBusMessageIter *iter_array) -{ - if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array) - return FALSE; - - if (!dbus_message_iter_close_container (iter_dict_val, iter_array)) - return FALSE; - - if (!_nmu_dbus_add_dict_entry_end (iter_dict, - iter_dict_entry, - iter_dict_val)) - return FALSE; - - return TRUE; -} - - -/** - * Convenience function to add an entire string array to the dict. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_write} - * @param key The key of the dict item - * @param items The array of strings - * @param num_items The number of strings in the array - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_append_string_array (DBusMessageIter *iter_dict, - const char *key, - const char **items, - const dbus_uint32_t num_items) -{ - DBusMessageIter iter_dict_entry, iter_dict_val, iter_array; - dbus_uint32_t i; - - if (!key) - return FALSE; - if (!items && (num_items != 0)) - return FALSE; - - if (!nmu_dbus_dict_begin_string_array (iter_dict, - key, - &iter_dict_entry, - &iter_dict_val, - &iter_array)) - return FALSE; - - for (i = 0; i < num_items; i++) { - if (!nmu_dbus_dict_string_array_add_element (&iter_array, items[i])) - return FALSE; - } - - if (!nmu_dbus_dict_end_string_array (iter_dict, - &iter_dict_entry, - &iter_dict_val, - &iter_array)) - return FALSE; - - return TRUE; -} - - -/*****************************************************/ -/* Stuff for reading dicts */ -/*****************************************************/ - -/** - * Start reading from a dbus dict. - * - * @param iter A valid DBusMessageIter pointing to the start of the dict - * @param iter_dict (out) A DBusMessageIter to be passed to - * {@link nmu_dbus_dict_read_next_entry} - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_open_read (DBusMessageIter *iter, - DBusMessageIter *iter_dict) -{ - if (!iter || !iter_dict) return FALSE; - - if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_ARRAY || - dbus_message_iter_get_element_type (iter) != DBUS_TYPE_DICT_ENTRY) - return FALSE; - - dbus_message_iter_recurse (iter, iter_dict); - return TRUE; -} - - -#define BYTE_ARRAY_CHUNK_SIZE 34 -#define BYTE_ARRAY_ITEM_SIZE (sizeof (char)) - -static dbus_bool_t -_nmu_dbus_dict_entry_get_byte_array (DBusMessageIter *iter, - int array_type, - NMUDictEntry *entry) -{ - dbus_uint32_t count = 0; - dbus_bool_t success = FALSE; - char * buffer; - - entry->bytearray_value = NULL; - entry->array_type = DBUS_TYPE_BYTE; - - buffer = malloc (BYTE_ARRAY_ITEM_SIZE * BYTE_ARRAY_CHUNK_SIZE); - if (!buffer) { - fprintf (stderr, "%s out of memory trying to retrieve a byte " - "array.\n", __func__); - goto done; - } - - entry->bytearray_value = buffer; - entry->array_len = 0; - while (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE) { - char byte; - - if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) { - buffer = realloc (buffer, BYTE_ARRAY_ITEM_SIZE * (count + BYTE_ARRAY_CHUNK_SIZE)); - if (buffer == NULL) { - fprintf (stderr, "%s() out of memory trying to retrieve" - "the string array.\n", __func__); - goto done; - } - } - entry->bytearray_value = buffer; - - dbus_message_iter_get_basic (iter, &byte); - entry->bytearray_value[count] = byte; - entry->array_len = ++count; - dbus_message_iter_next (iter); - } - - /* Zero-length arrays are valid. */ - if (entry->array_len == 0) { - free (entry->bytearray_value); - entry->bytearray_value = NULL; - } - - success = TRUE; - -done: - return success; -} - -#define UINT32_ARRAY_CHUNK_SIZE 4 -#define UINT32_ARRAY_ITEM_SIZE (sizeof (dbus_uint32_t)) - -static dbus_bool_t -_nmu_dbus_dict_entry_get_uint32_array (DBusMessageIter *iter, - int array_type, - NMUDictEntry *entry) -{ - dbus_uint32_t count = 0; - dbus_bool_t success = FALSE; - dbus_uint32_t * buffer; - - entry->uint32array_value = NULL; - entry->array_type = DBUS_TYPE_UINT32; - - buffer = malloc (UINT32_ARRAY_ITEM_SIZE * UINT32_ARRAY_CHUNK_SIZE); - if (!buffer) { - fprintf (stderr, "%s out of memory trying to retrieve a uint32 " - "array.\n", __func__); - goto done; - } - - entry->uint32array_value = buffer; - entry->array_len = 0; - while (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32) { - dbus_uint32_t uint32; - - if ((count % UINT32_ARRAY_CHUNK_SIZE) == 0 && count != 0) { - buffer = realloc (buffer, UINT32_ARRAY_ITEM_SIZE * (count + UINT32_ARRAY_CHUNK_SIZE)); - if (buffer == NULL) { - fprintf (stderr, "%s() out of memory trying to retrieve" - "the string array.\n", __func__); - goto done; - } - } - entry->uint32array_value = buffer; - - dbus_message_iter_get_basic (iter, &uint32); - entry->uint32array_value[count] = uint32; - entry->array_len = ++count; - dbus_message_iter_next (iter); - } - - /* Zero-length arrays are valid. */ - if (entry->array_len == 0) { - free (entry->uint32array_value); - entry->uint32array_value = NULL; - } - - success = TRUE; - -done: - return success; -} - -#define STR_ARRAY_CHUNK_SIZE 8 -#define STR_ARRAY_ITEM_SIZE (sizeof (char *)) - -static dbus_bool_t -_nmu_dbus_dict_entry_get_string_array (DBusMessageIter *iter, - int array_type, - NMUDictEntry *entry) -{ - dbus_uint32_t count = 0; - dbus_bool_t success = FALSE; - char ** buffer; - - entry->strarray_value = NULL; - entry->array_type = DBUS_TYPE_STRING; - - buffer = (char **)malloc (STR_ARRAY_ITEM_SIZE * STR_ARRAY_CHUNK_SIZE); - if (buffer == NULL) { - fprintf (stderr, "%s() out of memory trying to retrieve a string" - " array.\n", __func__); - goto done; - } - - entry->strarray_value = buffer; - entry->array_len = 0; - while (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING) { - const char *value; - char *str; - - if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) { - buffer = realloc (buffer, STR_ARRAY_ITEM_SIZE * (count + STR_ARRAY_CHUNK_SIZE)); - if (buffer == NULL) { - fprintf (stderr, "%s() out of memory trying to retrieve" - "the string array.\n", __func__); - goto done; - } - } - entry->strarray_value = buffer; - - dbus_message_iter_get_basic (iter, &value); - str = strdup (value); - if (str == NULL) { - fprintf (stderr, "%s() out of memory trying to duplicate" - "the string array.\n", __func__); - goto done; - } - entry->strarray_value[count] = str; - entry->array_len = ++count; - dbus_message_iter_next (iter); - } - - /* Zero-length arrays are valid. */ - if (entry->array_len == 0) { - free (entry->strarray_value); - entry->strarray_value = NULL; - } - - success = TRUE; - -done: - return success; -} - - -static dbus_bool_t -_nmu_dbus_dict_entry_get_array (DBusMessageIter *iter_dict_val, - NMUDictEntry *entry) -{ - int array_type = dbus_message_iter_get_element_type (iter_dict_val); - dbus_bool_t success = FALSE; - DBusMessageIter iter_array; - - if (!entry) - return FALSE; - - dbus_message_iter_recurse (iter_dict_val, &iter_array); - - switch (array_type) { - case DBUS_TYPE_BYTE: - success = _nmu_dbus_dict_entry_get_byte_array (&iter_array, - array_type, - entry); - break; - case DBUS_TYPE_UINT32: - success = _nmu_dbus_dict_entry_get_uint32_array (&iter_array, - array_type, - entry); - break; - case DBUS_TYPE_STRING: - success = _nmu_dbus_dict_entry_get_string_array (&iter_array, - array_type, - entry); - break; - default: - break; - } - - return success; -} - - -static dbus_bool_t -_nmu_dbus_dict_fill_value_from_variant (NMUDictEntry *entry, - DBusMessageIter *iter_dict_val) -{ - dbus_bool_t success = TRUE; - - switch (entry->type) { - case DBUS_TYPE_STRING: { - const char *v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->str_value = strdup (v); - break; - } - case DBUS_TYPE_BOOLEAN: { - dbus_bool_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->bool_value = v; - break; - } - case DBUS_TYPE_BYTE: { - char v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->byte_value = v; - break; - } - case DBUS_TYPE_INT16: { - dbus_int16_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->int16_value = v; - break; - } - case DBUS_TYPE_UINT16: { - dbus_uint16_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->uint16_value = v; - break; - } - case DBUS_TYPE_INT32: { - dbus_int32_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->int32_value = v; - break; - } - case DBUS_TYPE_UINT32: { - dbus_uint32_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->uint32_value = v; - break; - } - case DBUS_TYPE_INT64: { - dbus_int64_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->int64_value = v; - break; - } - case DBUS_TYPE_UINT64: { - dbus_uint64_t v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->uint64_value = v; - break; - } - case DBUS_TYPE_DOUBLE: { - double v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->double_value = v; - break; - } - case DBUS_TYPE_OBJECT_PATH: { - char *v; - dbus_message_iter_get_basic (iter_dict_val, &v); - entry->str_value = strdup (v); - break; - } - case DBUS_TYPE_ARRAY: { - success = _nmu_dbus_dict_entry_get_array (iter_dict_val, entry); - break; - } - default: - success = FALSE; - break; - } - - return success; -} - - -/** - * Read the current key/value entry from the dict. Entries are dynamically - * allocated when needed and must be freed after use with the - * {@link nmu_dbus_dict_entry_clear} function. - * - * The returned entry object will be filled with the type and value of the next - * entry in the dict, or the type will be DBUS_TYPE_INVALID if an error - * occurred. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_read} - * @param entry A valid dict entry object into which the dict key and value - * will be placed - * @return TRUE on success, FALSE on failure - * - */ -dbus_bool_t -nmu_dbus_dict_get_entry (DBusMessageIter *iter_dict, - NMUDictEntry * entry) -{ - DBusMessageIter iter_dict_entry, iter_dict_val; - int type; - const char *key; - - if (!iter_dict || !entry) - goto error; - - if (dbus_message_iter_get_arg_type (iter_dict) != DBUS_TYPE_DICT_ENTRY) - goto error; - - dbus_message_iter_recurse (iter_dict, &iter_dict_entry); - dbus_message_iter_get_basic (&iter_dict_entry, &key); - entry->key = key; - - if (!dbus_message_iter_next (&iter_dict_entry)) - goto error; - type = dbus_message_iter_get_arg_type (&iter_dict_entry); - if (type != DBUS_TYPE_VARIANT) - goto error; - - dbus_message_iter_recurse (&iter_dict_entry, &iter_dict_val); - entry->type = dbus_message_iter_get_arg_type (&iter_dict_val); - if(!_nmu_dbus_dict_fill_value_from_variant (entry, &iter_dict_val)) - goto error; - - dbus_message_iter_next (iter_dict); - return TRUE; - -error: - nmu_dbus_dict_entry_clear (entry); - entry->type = DBUS_TYPE_INVALID; - entry->array_type = DBUS_TYPE_INVALID; - return FALSE; -} - - -/** - * Return whether or not there are additional dictionary entries. - * - * @param iter_dict A valid DBusMessageIter returned from - * {@link nmu_dbus_dict_open_read} - * @return TRUE if more dict entries exists, FALSE if no more dict entries - * exist - * - */ -dbus_bool_t -nmu_dbus_dict_has_dict_entry (DBusMessageIter *iter_dict) -{ - if (!iter_dict) { - fprintf (stderr, "%s called with invalid arguments; this is an " - "error in the program.\n", __func__); - return FALSE; - } - return dbus_message_iter_get_arg_type (iter_dict) == DBUS_TYPE_DICT_ENTRY; -} - - -/** - * Free any memory used by the entry object. - * - * @param entry The entry object - */ -void -nmu_dbus_dict_entry_clear (NMUDictEntry *entry) -{ - if (!entry) - return; - switch (entry->type) { - case DBUS_TYPE_OBJECT_PATH: - case DBUS_TYPE_STRING: - free (entry->str_value); - break; - case DBUS_TYPE_ARRAY: - switch (entry->array_type) { - case DBUS_TYPE_BYTE: { - free (entry->bytearray_value); - break; - } - case DBUS_TYPE_UINT32: { - free (entry->uint32array_value); - break; - } - case DBUS_TYPE_STRING: { - int i; - for (i = 0; i < entry->array_len; i++) - free (entry->strarray_value[i]); - free (entry->strarray_value); - break; - } - } - break; - } - - memset (entry, 0, sizeof (NMUDictEntry)); -} diff --git a/libnm-util/dbus-dict-helpers.h b/libnm-util/dbus-dict-helpers.h deleted file mode 100644 index 52cdedfcf2..0000000000 --- a/libnm-util/dbus-dict-helpers.h +++ /dev/null @@ -1,177 +0,0 @@ -/* NetworkManager -- Network link manager - * - * Dan Williams - * - * 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 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * (C) Copyright 2006 Red Hat, Inc. - */ - -#ifndef DBUS_DICT_HELPERS_H -#define DBUS_DICT_HELPERS_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * Adding a dict to a DBusMessage - */ - -dbus_bool_t -nmu_dbus_dict_open_write (DBusMessageIter *iter, - DBusMessageIter *iter_dict); - -dbus_bool_t -nmu_dbus_dict_close_write (DBusMessageIter *iter, - DBusMessageIter *iter_dict); - -dbus_bool_t -nmu_dbus_dict_append_string (DBusMessageIter *iter_dict, - const char * key, - const char * value); - -dbus_bool_t -nmu_dbus_dict_append_byte (DBusMessageIter *iter_dict, - const char * key, - const char value); - -dbus_bool_t -nmu_dbus_dict_append_bool (DBusMessageIter *iter_dict, - const char * key, - const dbus_bool_t value); - -dbus_bool_t -nmu_dbus_dict_append_int16 (DBusMessageIter *iter_dict, - const char * key, - const dbus_int16_t value); - -dbus_bool_t -nmu_dbus_dict_append_uint16 (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint16_t value); - -dbus_bool_t -nmu_dbus_dict_append_int32 (DBusMessageIter *iter_dict, - const char * key, - const dbus_int32_t value); - -dbus_bool_t -nmu_dbus_dict_append_uint32 (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint32_t value); - -dbus_bool_t -nmu_dbus_dict_append_int64 (DBusMessageIter *iter_dict, - const char * key, - const dbus_int64_t value); - -dbus_bool_t -nmu_dbus_dict_append_uint64 (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint64_t value); - -dbus_bool_t -nmu_dbus_dict_append_double (DBusMessageIter *iter_dict, - const char * key, - const double value); - -dbus_bool_t -nmu_dbus_dict_append_object_path (DBusMessageIter *iter_dict, - const char * key, - const char * value); - -dbus_bool_t -nmu_dbus_dict_append_byte_array (DBusMessageIter *iter_dict, - const char * key, - const char * value, - const dbus_uint32_t value_len); - -dbus_bool_t -nmu_dbus_dict_append_uint32_array (DBusMessageIter *iter_dict, - const char * key, - const dbus_uint32_t * value, - const dbus_uint32_t value_len); - -dbus_bool_t -nmu_dbus_dict_begin_string_array (DBusMessageIter *iter_dict, - const char *key, - DBusMessageIter *iter_dict_entry, - DBusMessageIter *iter_dict_val, - DBusMessageIter *iter_array); - -dbus_bool_t -nmu_dbus_dict_string_array_add_element (DBusMessageIter *iter_array, - const char *elem); - -dbus_bool_t -nmu_dbus_dict_end_string_array (DBusMessageIter *iter_dict, - DBusMessageIter *iter_dict_entry, - DBusMessageIter *iter_dict_val, - DBusMessageIter *iter_array); - -dbus_bool_t -nmu_dbus_dict_append_string_array (DBusMessageIter *iter_dict, - const char * key, - const char ** items, - const dbus_uint32_t num_items); - -/* - * Reading a dict from a DBusMessage - */ - -typedef struct NMUDictEntry { - int type; - int array_type; - const char *key; - - /** Possible values of the property */ - union { - char *str_value; - char byte_value; - dbus_bool_t bool_value; - dbus_int16_t int16_value; - dbus_uint16_t uint16_value; - dbus_int32_t int32_value; - dbus_uint32_t uint32_value; - dbus_int64_t int64_value; - dbus_uint64_t uint64_value; - double double_value; - char * bytearray_value; - dbus_uint32_t * uint32array_value; - char ** strarray_value; - }; - dbus_uint32_t array_len; -} NMUDictEntry; - -dbus_bool_t -nmu_dbus_dict_open_read (DBusMessageIter *iter, - DBusMessageIter *iter_dict); - -dbus_bool_t -nmu_dbus_dict_get_entry (DBusMessageIter *iter_dict, - NMUDictEntry * entry); - -dbus_bool_t -nmu_dbus_dict_has_dict_entry (DBusMessageIter *iter_dict); - -void -nmu_dbus_dict_entry_clear (NMUDictEntry *entry); - -#ifdef __cplusplus -} -#endif - -#endif /* DBUS_DICT_HELPERS_H */ diff --git a/src/NetworkManager.c b/src/NetworkManager.c index f368c6e99b..9bc29fdd37 100644 --- a/src/NetworkManager.c +++ b/src/NetworkManager.c @@ -221,7 +221,6 @@ main (int argc, char *argv[]) NMVPNManager *vpn_manager = NULL; NMNamedManager *named_mgr = NULL; NMDBusManager * dbus_mgr = NULL; - DBusConnection *dbus_connection; NMSupplicantManager * sup_mgr = NULL; GOptionEntry options[] = { @@ -302,14 +301,6 @@ main (int argc, char *argv[]) /* Initialize our DBus service & connection */ dbus_mgr = nm_dbus_manager_get (); - dbus_connection = nm_dbus_manager_get_dbus_connection (dbus_mgr); - if (!dbus_connection) { - nm_error ("Failed to initialize. " - "Either dbus is not running, or the " - "NetworkManager dbus security policy " - "was not loaded."); - goto done; - } manager = nm_manager_new (); if (manager == NULL) { diff --git a/src/nm-dbus-manager.c b/src/nm-dbus-manager.c index de20eb674d..9825da5a0c 100644 --- a/src/nm-dbus-manager.c +++ b/src/nm-dbus-manager.c @@ -36,56 +36,25 @@ enum { NAME_OWNER_CHANGED, NUMBER_OF_SIGNALS }; -static guint nm_dbus_manager_signals[NUMBER_OF_SIGNALS]; +static guint nm_dbus_manager_signals[NUMBER_OF_SIGNALS]; G_DEFINE_TYPE(NMDBusManager, nm_dbus_manager, G_TYPE_OBJECT) #define NM_DBUS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ - NM_TYPE_DBUS_MANAGER, \ - NMDBusManagerPrivate)) - -typedef struct SignalMatch { - guint32 refcount; - char * interface; - char * sender; - char * owner; - char * match; - gboolean enabled; -} SignalMatch; - -typedef struct SignalHandlerData { - guint32 id; - - NMDBusSignalHandlerFunc func; - gpointer user_data; - SignalMatch * match; -} SignalHandlerData; - -typedef struct MethodHandlerData { - NMDbusMethodList * list; - NMDBusManager * self; -} MethodHandlerData; + NM_TYPE_DBUS_MANAGER, \ + NMDBusManagerPrivate)) typedef struct { DBusConnection * connection; DBusGConnection *g_connection; DBusGProxy * proxy; gboolean started; - - GSList * matches; - GSList * signal_handlers; - guint32 sig_handler_id_counter; } NMDBusManagerPrivate; - static gboolean nm_dbus_manager_init_bus (NMDBusManager *self); static void nm_dbus_manager_cleanup (NMDBusManager *self); -static void free_signal_handler_data (SignalHandlerData * data, NMDBusManager * mgr); static void start_reconnection_timeout (NMDBusManager *self); -static void signal_match_unref (SignalMatch * match, NMDBusManager * mgr); -static void signal_match_disable (SignalMatch * match); - NMDBusManager * nm_dbus_manager_get (void) @@ -109,40 +78,10 @@ nm_dbus_manager_init (NMDBusManager *self) { } -static void -free_signal_handler_helper (gpointer item, - gpointer user_data) -{ - NMDBusManager * mgr = (NMDBusManager *) user_data; - SignalHandlerData * data = (SignalHandlerData *) item; - - free_signal_handler_data (data, mgr); -} - -static void -signal_match_dispose_helper (gpointer item, - gpointer user_data) -{ - NMDBusManager * mgr = (NMDBusManager *) user_data; - SignalMatch * match = (SignalMatch *) item; - - signal_match_unref (match, mgr); -} - static void nm_dbus_manager_finalize (GObject *object) { - NMDBusManager * self = NM_DBUS_MANAGER (object); - NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE (object); - - /* Must be done before the dbus connection is disposed */ - g_slist_foreach (priv->signal_handlers, free_signal_handler_helper, self); - g_slist_free (priv->signal_handlers); - - g_slist_foreach (priv->matches, signal_match_dispose_helper, self); - g_slist_free (priv->matches); - - nm_dbus_manager_cleanup (self); + nm_dbus_manager_cleanup (NM_DBUS_MANAGER (object)); G_OBJECT_CLASS (nm_dbus_manager_parent_class)->finalize (object); } @@ -205,9 +144,9 @@ nm_dbus_manager_reconnect (gpointer user_data) if (nm_dbus_manager_start_service (self)) { nm_info ("reconnected to the system bus."); g_signal_emit (self, - nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED], - 0, - NM_DBUS_MANAGER_GET_PRIVATE (self)->connection); + nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED], + 0, + NM_DBUS_MANAGER_GET_PRIVATE (self)->connection); return TRUE; } } @@ -216,185 +155,6 @@ nm_dbus_manager_reconnect (gpointer user_data) return FALSE; } -static SignalMatch * -signal_match_new (const char *interface, - const char *sender) -{ - SignalMatch * match; - - g_return_val_if_fail (interface || sender, NULL); - - match = g_slice_new0 (SignalMatch); - g_return_val_if_fail (match != NULL, NULL); - match->refcount = 1; - - if (interface) { - match->interface = g_strdup (interface); - if (!match->interface) - goto error; - } - - if (sender) { - match->sender = g_strdup (sender); - if (!match->sender) - goto error; - } - - if (interface && sender) { - match->match = g_strdup_printf ("type='signal',interface='%s',sender='%s'", - interface, sender); - } else if (interface && !sender) { - match->match = g_strdup_printf ("type='signal',interface='%s'", interface); - } else if (sender && !interface) { - match->match = g_strdup_printf ("type='signal',sender='%s'", sender); - } - - if (!match->match) - goto error; - - return match; - -error: - signal_match_unref (match, NULL); - return NULL; -} - -static void -signal_match_ref (SignalMatch * match) -{ - g_return_if_fail (match != NULL); - g_return_if_fail (match->refcount > 0); - - match->refcount++; -} - -static void -signal_match_unref (SignalMatch * match, - NMDBusManager * mgr) -{ - DBusError error; - - g_return_if_fail (match != NULL); - g_return_if_fail (match->refcount > 0); - - match->refcount--; - if (match->refcount > 0) - return; - - /* Remove the DBus bus match on dispose */ - if (mgr) { - dbus_error_init (&error); - dbus_bus_remove_match (NM_DBUS_MANAGER_GET_PRIVATE (mgr)->connection, match->match, &error); - if (dbus_error_is_set (&error)) { - nm_warning ("failed to remove signal match for sender '%s', " - "interface '%s'.", - match->sender ? match->sender : "(none)", - match->interface ? match->interface : "(none)"); - dbus_error_free (&error); - } - } - match->enabled = FALSE; - - g_free (match->interface); - g_free (match->sender); - g_free (match->owner); - g_free (match->match); - memset (match, 0, sizeof (SignalMatch)); - g_slice_free (SignalMatch, match); -} - -static SignalMatch * -find_signal_match (NMDBusManager *self, - const char *interface, - const char *sender) -{ - SignalMatch * found = NULL; - GSList * elt; - - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (interface || sender, NULL); - - for (elt = NM_DBUS_MANAGER_GET_PRIVATE (self)->matches; elt; elt = g_slist_next (elt)) { - SignalMatch * match = (SignalMatch *) elt->data; - - if (!match) - continue; - - if (interface && sender) { - if (!match->interface || !match->sender) - continue; - if (!strcmp (match->interface, interface) && !strcmp (match->sender, sender)) { - found = match; - break; - } - } else if (interface && !sender) { - if (!match->interface || match->sender) - continue; - if (!strcmp (match->interface, interface)) { - found = match; - break; - } - } else if (sender && !interface) { - if (!match->sender || match->interface) - continue; - if (!strcmp (match->sender, sender)) { - found = match; - break; - } - } - } - - return found; -} - -static void -signal_match_enable (NMDBusManager * mgr, - SignalMatch * match, - const char * owner) -{ - NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE (mgr); - DBusError error; - - g_return_if_fail (match != NULL); - - if (match->enabled == TRUE) - return; - - if (!priv->connection) - return; - - dbus_error_init (&error); - dbus_bus_add_match (priv->connection, match->match, &error); - if (dbus_error_is_set (&error)) { - nm_warning ("failed to add signal match for sender '%s', " - "interface '%s'.", - match->sender ? match->sender : "(none)", - match->interface ? match->interface : "(none)"); - dbus_error_free (&error); - signal_match_disable (match); - } else { - g_free (match->owner); - if (owner) { - match->owner = g_strdup (owner); - } else if (match->sender) { - match->owner = nm_dbus_manager_get_name_owner (mgr, match->sender); - if (match->owner == NULL) - nm_warning ("Couldn't get name owner for '%s'.", match->sender); - } - match->enabled = TRUE; - } -} - -static void -signal_match_disable (SignalMatch * match) -{ - g_return_if_fail (match != NULL); - - match->enabled = FALSE; - g_free (match->owner); - match->owner = NULL; -} - static void start_reconnection_timeout (NMDBusManager *self) { @@ -402,76 +162,6 @@ start_reconnection_timeout (NMDBusManager *self) g_timeout_add (3000, nm_dbus_manager_reconnect, self); } -static gboolean -dispatch_signal (NMDBusManager * self, - DBusMessage * message) -{ - NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE (self); - gboolean handled = FALSE; - GSList * elt; - const char * interface; - const char * sender; - - g_return_val_if_fail (self != NULL, FALSE); - g_return_val_if_fail (message != NULL, FALSE); - - interface = dbus_message_get_interface (message); - if (!interface) - return FALSE; - - sender = dbus_message_get_sender (message); - - g_return_val_if_fail (sender != NULL, FALSE); - - for (elt = priv->signal_handlers; elt; elt = g_slist_next (elt)) { - gboolean dispatch = FALSE; - SignalHandlerData * handler = (SignalHandlerData *) elt->data; - SignalMatch * match = handler->match; - - if (match->sender && !match->interface) { - if (!strcmp (match->sender, sender) - || (match->owner && !strcmp (match->owner, sender))) - dispatch = TRUE; - } else if (match->interface && !match->sender) { - if (!strcmp (match->interface, interface)) - dispatch = TRUE; - } else if (match->interface && match->sender) { - if (!strcmp (match->interface, interface) - && (!strcmp (match->sender, sender) - || !strcmp (match->owner, sender))) - dispatch = TRUE; - } - if (!dispatch) - continue; - - handled = (*handler->func) (priv->connection, - message, - handler->user_data); - if (handled) - break; - } - - return handled; -} - - -static DBusHandlerResult -nm_dbus_manager_signal_handler (DBusConnection *connection, - DBusMessage *message, - void *user_data) -{ - NMDBusManager * self = NM_DBUS_MANAGER (user_data); - gboolean handled; - - g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); - g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); - g_return_val_if_fail (self != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); - - handled = dispatch_signal (self, message); - - return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED); -} - char * nm_dbus_manager_get_name_owner (NMDBusManager *self, const char *name) @@ -483,11 +173,11 @@ nm_dbus_manager_get_name_owner (NMDBusManager *self, g_return_val_if_fail (name != NULL, NULL); if (!dbus_g_proxy_call (NM_DBUS_MANAGER_GET_PRIVATE (self)->proxy, - "GetNameOwner", &err, - G_TYPE_STRING, name, - G_TYPE_INVALID, - G_TYPE_STRING, &owner, - G_TYPE_INVALID)) { + "GetNameOwner", &err, + G_TYPE_STRING, name, + G_TYPE_INVALID, + G_TYPE_STRING, &owner, + G_TYPE_INVALID)) { nm_warning ("Error on GetNameOwner DBUS call: %s", err->message); g_error_free (err); } @@ -506,11 +196,11 @@ nm_dbus_manager_name_has_owner (NMDBusManager *self, g_return_val_if_fail (name != NULL, FALSE); if (!dbus_g_proxy_call (NM_DBUS_MANAGER_GET_PRIVATE (self)->proxy, - "NameHasOwner", &err, - G_TYPE_STRING, name, - G_TYPE_INVALID, - G_TYPE_BOOLEAN, &has_owner, - G_TYPE_INVALID)) { + "NameHasOwner", &err, + G_TYPE_STRING, name, + G_TYPE_INVALID, + G_TYPE_BOOLEAN, &has_owner, + G_TYPE_INVALID)) { nm_warning ("Error on NameHasOwner DBUS call: %s", err->message); g_error_free (err); } @@ -520,34 +210,15 @@ nm_dbus_manager_name_has_owner (NMDBusManager *self, static void proxy_name_owner_changed (DBusGProxy *proxy, - const char *name, - const char *old_owner, - const char *new_owner, - gpointer user_data) + const char *name, + const char *old_owner, + const char *new_owner, + gpointer user_data) { - NMDBusManager *self = NM_DBUS_MANAGER (user_data); - SignalMatch *match; - gboolean old_owner_good = (old_owner && strlen (old_owner)); - gboolean new_owner_good = (new_owner && strlen (new_owner)); - - match = find_signal_match (self, NULL, name); - - if (!old_owner_good && new_owner_good) { - /* Add any matches for this owner */ - if (match) { - signal_match_enable (self, match, new_owner); - } - } else if (old_owner_good && !new_owner_good) { - /* Mark any matches for services that have gone away as disabled. */ - if (match) { - signal_match_disable (match); - } - } - - g_signal_emit (self, - nm_dbus_manager_signals[NAME_OWNER_CHANGED], - 0, - name, old_owner, new_owner); + g_signal_emit (NM_DBUS_MANAGER (user_data), + nm_dbus_manager_signals[NAME_OWNER_CHANGED], + 0, + name, old_owner, new_owner); } static void @@ -562,8 +233,8 @@ destroy_cb (DBusGProxy *proxy, gpointer user_data) nm_dbus_manager_cleanup (self); g_signal_emit (G_OBJECT (self), - nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED], - 0, NULL); + nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED], + 0, NULL); start_reconnection_timeout (self); } @@ -573,8 +244,6 @@ nm_dbus_manager_init_bus (NMDBusManager *self) { NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE (self); GError *err = NULL; - DBusError error; - gboolean success = FALSE; if (priv->connection) { nm_warning ("DBus Manager already has a valid connection."); @@ -589,59 +258,29 @@ nm_dbus_manager_init_bus (NMDBusManager *self) "the message bus daemon is running! Message: %s", err->message); g_error_free (err); - goto out; + return FALSE; } priv->connection = dbus_g_connection_get_connection (priv->g_connection); dbus_connection_set_exit_on_disconnect (priv->connection, FALSE); priv->proxy = dbus_g_proxy_new_for_name (priv->g_connection, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus"); + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus"); g_signal_connect (priv->proxy, "destroy", - G_CALLBACK (destroy_cb), - self); + G_CALLBACK (destroy_cb), + self); dbus_g_proxy_add_signal (priv->proxy, "NameOwnerChanged", - G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, - G_TYPE_INVALID); + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, + G_TYPE_INVALID); dbus_g_proxy_connect_signal (priv->proxy, - "NameOwnerChanged", - G_CALLBACK (proxy_name_owner_changed), - self, NULL); - - if (!dbus_connection_add_filter (priv->connection, - nm_dbus_manager_signal_handler, - self, - NULL)) { - nm_warning ("Could not register a dbus message filter. The " - "NetworkManager dbus security policy may not be loaded. " - "Restart dbus?"); - goto out; - } - success = TRUE; - - /* Monitor DBus signals for service start/stop announcements */ - dbus_error_init (&error); - dbus_bus_add_match (priv->connection, - "type='signal'," - "interface='" DBUS_INTERFACE_DBUS "'," - "sender='" DBUS_SERVICE_DBUS "'", - &error); - if (dbus_error_is_set (&error)) { - nm_warning ("Could not monitor DBus signals. Message: %s", - error.message); - dbus_error_free (&error); - goto out; - } - success = TRUE; - -out: - if (!success) - nm_dbus_manager_cleanup (self); - return success; + "NameOwnerChanged", + G_CALLBACK (proxy_name_owner_changed), + self, NULL); + return TRUE; } /* Register our service on the bus; shouldn't be called until @@ -654,7 +293,6 @@ nm_dbus_manager_start_service (NMDBusManager *self) NMDBusManagerPrivate *priv; int flags; int request_name_result; - GSList *elt; GError *err = NULL; g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), FALSE); @@ -666,11 +304,6 @@ nm_dbus_manager_start_service (NMDBusManager *self) return FALSE; } - /* And our signal handlers */ - for (elt = priv->matches; elt; elt = g_slist_next (elt)) { - signal_match_enable (self, (SignalMatch *) elt->data, NULL); - } - #if (DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 60) flags = DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT; #else @@ -678,11 +311,11 @@ nm_dbus_manager_start_service (NMDBusManager *self) #endif if (!dbus_g_proxy_call (priv->proxy, "RequestName", &err, - G_TYPE_STRING, NM_DBUS_SERVICE, - G_TYPE_UINT, flags, - G_TYPE_INVALID, - G_TYPE_UINT, &request_name_result, - G_TYPE_INVALID)) { + G_TYPE_STRING, NM_DBUS_SERVICE, + G_TYPE_UINT, flags, + G_TYPE_INVALID, + G_TYPE_UINT, &request_name_result, + G_TYPE_INVALID)) { nm_warning ("Could not acquire the NetworkManager service.\n" " Message: '%s'", err->message); g_error_free (err); @@ -696,115 +329,13 @@ nm_dbus_manager_start_service (NMDBusManager *self) priv->started = TRUE; -out: + out: if (!priv->started) nm_dbus_manager_cleanup (self); return priv->started; } -static void -free_signal_handler_data (SignalHandlerData * data, - NMDBusManager * mgr) -{ - g_return_if_fail (mgr != NULL); - g_return_if_fail (data != NULL); - - if (data->match) - signal_match_unref (data->match, mgr); - - memset (data, 0, sizeof (SignalHandlerData)); - g_slice_free (SignalHandlerData, data); -} - -guint32 -nm_dbus_manager_register_signal_handler (NMDBusManager *self, - const char *interface, - const char *sender, - NMDBusSignalHandlerFunc callback, - gpointer user_data) -{ - NMDBusManagerPrivate *priv; - SignalHandlerData * sig_handler; - SignalMatch * match = NULL; - - g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), 0); - g_return_val_if_fail (callback != NULL, 0); - - /* One of interface or sender must be specified */ - g_return_val_if_fail (interface || sender, 0); - - priv = NM_DBUS_MANAGER_GET_PRIVATE (self); - - if (!(sig_handler = g_slice_new0 (SignalHandlerData))) { - nm_warning ("Not enough memory for new signal handler."); - return 0; - } - sig_handler->func = callback; - sig_handler->user_data = user_data; - - /* Find or create the DBus bus match */ - match = find_signal_match (self, interface, sender); - if (match != NULL) { - sig_handler->match = match; - signal_match_ref (match); - } else { - sig_handler->match = signal_match_new (interface, sender); - if (sig_handler->match == NULL) { - nm_warning ("Could not create new signal match."); - free_signal_handler_data (sig_handler, self); - return 0; - } - priv->matches = g_slist_append (priv->matches, - sig_handler->match); - } - - signal_match_enable (self, sig_handler->match, NULL); - - priv->sig_handler_id_counter++; - sig_handler->id = priv->sig_handler_id_counter; - - priv->signal_handlers = g_slist_append (priv->signal_handlers, - sig_handler); - - return sig_handler->id; -} - -void -nm_dbus_manager_remove_signal_handler (NMDBusManager *self, - guint32 id) -{ - NMDBusManagerPrivate *priv; - GSList * elt; - GSList * found_elt = NULL; - SignalHandlerData * sig_handler = NULL; - - g_return_if_fail (NM_IS_DBUS_MANAGER (self)); - g_return_if_fail (id > 0); - - priv = NM_DBUS_MANAGER_GET_PRIVATE (self); - - for (elt = priv->signal_handlers; elt; elt = g_slist_next (elt)) { - SignalHandlerData * handler = (SignalHandlerData *) elt->data; - - if (handler && (handler->id == id)) { - sig_handler = handler; - found_elt = elt; - break; - } - } - - /* Not found */ - if (!sig_handler || !found_elt) - return; - - /* Remove and free the signal handler */ - priv->signal_handlers = g_slist_remove_link (priv->signal_handlers, - found_elt); - free_signal_handler_data (sig_handler, self); - g_slist_free_1 (found_elt); -} - DBusConnection * nm_dbus_manager_get_dbus_connection (NMDBusManager *self) { diff --git a/src/nm-dbus-manager.h b/src/nm-dbus-manager.h index 415275c854..c190e4b33f 100644 --- a/src/nm-dbus-manager.h +++ b/src/nm-dbus-manager.h @@ -69,15 +69,6 @@ gboolean nm_dbus_manager_start_service (NMDBusManager *self); gboolean nm_dbus_manager_name_has_owner (NMDBusManager *self, const char *name); -guint32 nm_dbus_manager_register_signal_handler (NMDBusManager *self, - const char *interface, - const char *sender, - NMDBusSignalHandlerFunc callback, - gpointer user_data); - -void nm_dbus_manager_remove_signal_handler (NMDBusManager *self, - guint32 id); - DBusConnection * nm_dbus_manager_get_dbus_connection (NMDBusManager *self); DBusGConnection * nm_dbus_manager_get_connection (NMDBusManager *self); diff --git a/src/ppp-manager/Makefile.am b/src/ppp-manager/Makefile.am index cedab2a2a9..9413a214b1 100644 --- a/src/ppp-manager/Makefile.am +++ b/src/ppp-manager/Makefile.am @@ -40,5 +40,12 @@ nm_pppd_plugin_so_LDADD = \ $(GLIB_LIBS) \ $(top_builddir)/libnm-util/libnm-util.la +nm-pppd-plugin-glue.h: nm-pppd-plugin.xml + dbus-binding-tool --prefix=nm_pppd_plugin --mode=glib-server --output=nm-pppd-plugin-glue.h nm-pppd-plugin.xml + +BUILT_SOURCES = nm-pppd-plugin-glue.h + +EXTRA_DIST = nm-pppd-plugin.xml + CLEANFILES = $(BUILT_SOURCES) diff --git a/src/ppp-manager/nm-ppp-manager.c b/src/ppp-manager/nm-ppp-manager.c index 11435f1e76..73c8ff6c8f 100644 --- a/src/ppp-manager/nm-ppp-manager.c +++ b/src/ppp-manager/nm-ppp-manager.c @@ -9,7 +9,6 @@ #include "nm-ppp-manager.h" #include "nm-dbus-manager.h" #include "nm-utils.h" -#include "dbus-dict-helpers.h" #include "nm-marshal.h" #define NM_PPPD_PLUGIN LIBDIR "/nm-pppd-plugin.so" @@ -18,8 +17,8 @@ typedef struct { GPid pid; NMDBusManager *dbus_manager; + DBusGProxy *proxy; - guint32 signal_handler; guint32 ppp_timeout_handler; guint32 name_owner_changed_handler; } NMPPPManagerPrivate; @@ -156,7 +155,8 @@ nm_cmd_line_add_int (NMCmdLine *cmd, int i) /*******************************************/ -static inline const char *nm_find_pppd (void) +static inline const char * +nm_find_pppd (void) { static const char *pppd_binary_paths[] = { @@ -272,113 +272,6 @@ ppp_watch_cb (GPid pid, gint status, gpointer user_data) priv->pid = 0; } -#define HANDLE_DICT_ITEM(in_key, in_type, in_ary_type, op) \ - if (!strcmp (entry.key, in_key)) { \ - if (entry.type != in_type) { \ - nm_warning (in_key "had invalid type in PPP IP Config message."); \ - } else { \ - if (in_type == DBUS_TYPE_ARRAY && entry.array_type != in_ary_type) { \ - nm_warning (in_key "had invalid type in PPP IP Config message."); \ - } else { \ - op \ - } \ - } \ - goto next; \ - } - -static gboolean -parse_ip4_config (DBusMessage *message, char **interface, NMIP4Config **config) -{ - DBusMessageIter iter; - DBusMessageIter iter_dict; - NMUDictEntry entry; - gboolean success = FALSE; - - dbus_message_iter_init (message, &iter); - if (!nmu_dbus_dict_open_read (&iter, &iter_dict)) { - nm_warning ("Warning: couldn't get config dictionary from PPP IP Config message."); - goto out; - } - - *config = nm_ip4_config_new (); - - while (nmu_dbus_dict_has_dict_entry (&iter_dict)) { - int i; - - if (!nmu_dbus_dict_get_entry (&iter_dict, &entry)) { - nm_warning ("Error: couldn't read dict entryfrom PPP IP Config message."); - goto out; - } - - HANDLE_DICT_ITEM("interface", DBUS_TYPE_STRING, 0, - { if (strlen (entry.str_value)) *interface = g_strdup (entry.str_value); }); - /* IP specific options */ - HANDLE_DICT_ITEM("address", DBUS_TYPE_UINT32, 0, - { nm_ip4_config_set_address (*config, entry.uint32_value); }); - HANDLE_DICT_ITEM("netmask", DBUS_TYPE_UINT32, 0, - { nm_ip4_config_set_netmask (*config, entry.uint32_value ? entry.uint32_value : 0x00FF); }); - HANDLE_DICT_ITEM("gateway", DBUS_TYPE_UINT32, 0, - { nm_ip4_config_set_gateway (*config, entry.uint32_value); }); - - /* Multiple DNS servers are allowed */ - HANDLE_DICT_ITEM("dns_server", DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, - { - for (i = 0; i < entry.array_len; i++) - nm_ip4_config_add_nameserver (*config, entry.uint32array_value[i]); - }); - - /* FIXME: Ignoring WINS servers for now since IP4Config doesn't have a place for it */ - - next: - nmu_dbus_dict_entry_clear (&entry); - } - - success = TRUE; - - out: - if (!success && *config) { - g_object_unref (*config); - *config = NULL; - g_free (*interface); - *interface = NULL; - } - - return success; -} - -static gboolean -nm_ppp_manager_dbus_signal_handler (DBusConnection *connection, - DBusMessage *message, - gpointer user_data) -{ - NMPPPManager *manager = NM_PPP_MANAGER (user_data); - gboolean handled = FALSE; - - if (dbus_message_is_signal (message, NM_DBUS_INTERFACE_PPP, "Status")) { - guint32 state; - - if (dbus_message_get_args (message, NULL, - DBUS_TYPE_UINT32, &state, - DBUS_TYPE_INVALID)) { - - g_signal_emit (manager, signals[STATE_CHANGED], 0, state); - handled = TRUE; - } - } else if (dbus_message_is_signal (message, NM_DBUS_INTERFACE_PPP, "IP4Config")) { - char *iface = NULL; - NMIP4Config *config = NULL; - - if (parse_ip4_config (message, &iface, &config)) { - g_signal_emit (manager, signals[IP4_CONFIG], 0, iface, config); - g_free (iface); - g_object_unref (config); - handled = TRUE; - } - } - - return handled; -} - static gboolean pppd_timed_out (gpointer data) { @@ -390,6 +283,76 @@ pppd_timed_out (gpointer data) return FALSE; } +static void +ppp_status_changed (DBusGProxy *proxy, + guint32 status, + gpointer user_data) +{ + NMPPPManager *manager = NM_PPP_MANAGER (user_data); + + g_signal_emit (manager, signals[STATE_CHANGED], 0, status); +} + +static void +ip4_config_get (DBusGProxy *proxy, + GHashTable *config_hash, + gpointer user_data) +{ + NMPPPManager *manager = NM_PPP_MANAGER (user_data); + NMIP4Config *config; + GValue *val; + const char *iface; + int i; + + nm_info ("PPP manager(IP Config Get) reply received."); + + /* FIXME */ +/* g_source_remove (priv->ipconfig_timeout); */ +/* priv->ipconfig_timeout = 0; */ + + config = nm_ip4_config_new (); + nm_ip4_config_set_secondary (config, TRUE); + + val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_GATEWAY); + if (val) + nm_ip4_config_set_gateway (config, g_value_get_uint (val)); + + val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_ADDRESS); + if (val) + nm_ip4_config_set_address (config, g_value_get_uint (val)); + + val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_NETMASK); + if (val) + nm_ip4_config_set_netmask (config, g_value_get_uint (val)); + else + /* If no netmask, default to Class C address */ + nm_ip4_config_set_netmask (config, 0x00FF); + + val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_DNS); + if (val) { + GArray *dns = (GArray *) g_value_get_boxed (val); + + for (i = 0; i < dns->len; i++) + nm_ip4_config_add_nameserver (config, g_array_index (dns, guint, i)); + } + + /* FIXME: The plugin helpfully sends WINS servers as well + and we're insensitive clods and ignore them. */ + + val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_INTERFACE); + if (val) + iface = g_value_get_string (val); + else { + nm_warning ("No interface"); + goto out; + } + + g_signal_emit (manager, signals[IP4_CONFIG], 0, iface, config); + + out: + g_object_unref (config); +} + static void name_owner_changed (NMDBusManager *dbus_manager, const char *name, @@ -411,11 +374,27 @@ name_owner_changed (NMDBusManager *dbus_manager, priv->ppp_timeout_handler = 0; } - priv->signal_handler = nm_dbus_manager_register_signal_handler (priv->dbus_manager, - NM_DBUS_INTERFACE_PPP, - NM_DBUS_SERVICE_PPP, - nm_ppp_manager_dbus_signal_handler, - manager); + /* Work around the bug in dbus-glib where name-owner-changed signal is always emitted twice */ + if (!priv->proxy) { + priv->proxy = dbus_g_proxy_new_for_name (nm_dbus_manager_get_connection (priv->dbus_manager), + NM_DBUS_SERVICE_PPP, + NM_DBUS_PATH_PPP, + NM_DBUS_INTERFACE_PPP); + + dbus_g_proxy_add_signal (priv->proxy, "Status", G_TYPE_UINT, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (priv->proxy, "Status", + G_CALLBACK (ppp_status_changed), + manager, NULL); + + dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__BOXED, + G_TYPE_NONE, G_TYPE_VALUE, G_TYPE_INVALID); + dbus_g_proxy_add_signal (priv->proxy, "Ip4Config", + dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), + G_TYPE_INVALID); + dbus_g_proxy_connect_signal (priv->proxy, "Ip4Config", + G_CALLBACK (ip4_config_get), + manager, NULL); + } } else if (old_owner_good && !new_owner_good) { nm_ppp_manager_stop (manager); } @@ -548,7 +527,7 @@ nm_ppp_manager_start (NMPPPManager *manager, nm_debug ("Command line: %s", cmd_str); g_free (cmd_str); - priv->pid = 0; + priv->pid = 0; if (!g_spawn_async (NULL, (char **) ppp_cmd->array->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, pppd_child_setup, @@ -586,9 +565,9 @@ nm_ppp_manager_stop (NMPPPManager *manager) priv->ppp_timeout_handler = 0; } - if (priv->signal_handler) { - nm_dbus_manager_remove_signal_handler (priv->dbus_manager, priv->signal_handler); - priv->signal_handler = 0; + if (priv->proxy) { + g_object_unref (priv->proxy); + priv->proxy = NULL; } if (priv->dbus_manager) { diff --git a/src/ppp-manager/nm-pppd-plugin.c b/src/ppp-manager/nm-pppd-plugin.c index 013a5404d3..c4f24d3f19 100644 --- a/src/ppp-manager/nm-pppd-plugin.c +++ b/src/ppp-manager/nm-pppd-plugin.c @@ -6,18 +6,132 @@ #include #include #include -#include -#include +#include +#include +#include #include "nm-pppd-plugin.h" #include "nm-ppp-status.h" -#include "dbus-dict-helpers.h" +#include "nm-pppd-plugin-glue.h" char pppd_version[] = VERSION; +#define NM_TYPE_PPPD_PLUGIN (nm_pppd_plugin_get_type ()) +#define NM_PPPD_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_PPPD_PLUGIN, NMPppdPlugin)) +#define NM_PPPD_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_PPPD_PLUGIN, NMPppdPluginClass)) +#define NM_IS_PPPD_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_PPPD_PLUGIN)) +#define NM_IS_PPPD_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_PPPD_PLUGIN)) +#define NM_PPPD_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_PPPD_PLUGIN, NMPppdPluginClass)) + +typedef struct { + GObject parent; +} NMPppdPlugin; + +typedef struct { + GObjectClass parent; + + void (*state_changed) (NMPppdPlugin *plugin, + NMPPPStatus status); + void (*ip4_config) (NMPppdPlugin *plugin, + GHashTable *ip4_config); +} NMPppdPluginClass; + +G_DEFINE_TYPE (NMPppdPlugin, nm_pppd_plugin, G_TYPE_OBJECT) + +#define NM_PPPD_PLUGIN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_PPPD_PLUGIN, NMPppdPluginPrivate)) + +typedef struct { + DBusGConnection *bus; +} NMPppdPluginPrivate; + +enum { + STATE_CHANGED, + IP4_CONFIG, + + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + +static void +nm_pppd_plugin_init (NMPppdPlugin *plugin) +{ +} + +static void +finalize (GObject *object) +{ + dbus_g_connection_unref (NM_PPPD_PLUGIN_GET_PRIVATE (object)->bus); + + G_OBJECT_CLASS (nm_pppd_plugin_parent_class)->finalize (object); +} + +static void +nm_pppd_plugin_class_init (NMPppdPluginClass *plugin_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (plugin_class); + + g_type_class_add_private (object_class, sizeof (NMPppdPluginPrivate)); + dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (plugin_class), + &dbus_glib_nm_pppd_plugin_object_info); + + object_class->finalize = finalize; + + /* signals */ + signals[STATE_CHANGED] = + g_signal_new ("state-changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMPppdPluginClass, state_changed), + NULL, NULL, + g_cclosure_marshal_VOID__UINT, + G_TYPE_NONE, 1, + G_TYPE_UINT); + + signals[IP4_CONFIG] = + g_signal_new ("ip4-config", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMPppdPluginClass, ip4_config), + NULL, NULL, + g_cclosure_marshal_VOID__BOXED, + G_TYPE_NONE, 1, + dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)); +} + +static NMPppdPlugin * +nm_pppd_plugin_new (DBusGConnection *bus) +{ + GObject *obj; + + obj = g_object_new (NM_TYPE_PPPD_PLUGIN, NULL); + if (!obj) + return NULL; + + NM_PPPD_PLUGIN_GET_PRIVATE (obj)->bus = dbus_g_connection_ref (bus); + dbus_g_connection_register_g_object (bus, NM_DBUS_PATH_PPP, obj); + + return (NMPppdPlugin *) obj; +} + +static void +nm_pppd_plugin_state_changed (NMPppdPlugin *plugin, NMPPPStatus status) +{ + g_signal_emit (plugin, signals[STATE_CHANGED], 0, status); +} + +static void +nm_pppd_plugin_ip4_config (NMPppdPlugin *plugin, GHashTable *ip4_config) +{ + g_signal_emit (plugin, signals[IP4_CONFIG], 0, ip4_config); +} + +/*****************************************************************************/ + static void nm_phasechange (void *data, int arg) { + NMPppdPlugin *plugin = NM_PPPD_PLUGIN (data); NMPPPStatus status = NM_PPP_STATUS_UNKNOWN; char *phase; @@ -82,221 +196,157 @@ nm_phasechange (void *data, int arg) g_message ("pppd reported new phase: %s", phase); - if (status != NM_PPP_STATUS_UNKNOWN) { - DBusConnection *connection = (DBusConnection *) data; - DBusMessage *message; - - message = dbus_message_new_signal (NM_DBUS_PATH_PPP, - NM_DBUS_INTERFACE_PPP, - "Status"); - if (!message) { - g_warning ("Couldn't allocate the dbus message"); - return; - } - - if (!dbus_message_append_args (message, - DBUS_TYPE_UINT32, &status, - DBUS_TYPE_INVALID)) { - g_warning ("could not append message args"); - goto out; - } - - if (!dbus_connection_send (connection, message, NULL)) { - g_warning ("could not send dbus message"); - goto out; - } - - out: - dbus_message_unref (message); - } + if (status != NM_PPP_STATUS_UNKNOWN) + nm_pppd_plugin_state_changed (plugin, status); } -static const gchar * -ip4_address_as_string (guint32 ip) +static GValue * +str_to_gvalue (const char *str) { - struct in_addr tmp_addr; - gchar *ip_string; + GValue *val; - tmp_addr.s_addr = ip; - ip_string = inet_ntoa (tmp_addr); + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, str); - return ip_string; + return val; +} + +static GValue * +uint_to_gvalue (guint32 i) +{ + GValue *val; + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, i); + + return val; +} + +static void +value_destroy (gpointer data) +{ + GValue *val = (GValue *) data; + + g_value_unset (val); + g_slice_free (GValue, val); } static void nm_ip_up (void *data, int arg) { - DBusConnection *connection = (DBusConnection *) data; - DBusMessage *signal; - DBusMessageIter iter; - DBusMessageIter iter_dict; - guint32 ip4_address; - guint32 ip4_gateway; - guint32 ip4_dns_1; - guint32 ip4_dns_2; - guint32 ip4_wins_1; - guint32 ip4_wins_2; - guint32 ip4_netmask = 0xFFFFFFFF; /* Default mask of 255.255.255.255 */ + NMPppdPlugin *plugin = NM_PPPD_PLUGIN (data); + ipcp_options opts = ipcp_gotoptions[ifunit]; + GHashTable *hash; + GArray *array; + GValue *val; - ip4_address = ipcp_gotoptions[ifunit].ouraddr; - if (!ip4_address) { + if (!opts.ouraddr) { g_warning ("Didn't receive an internal IP from pppd"); return; } - ip4_gateway = ipcp_gotoptions[ifunit].hisaddr; + hash = g_hash_table_new_full (g_str_hash, g_str_equal, + NULL, value_destroy); - ip4_dns_1 = ipcp_gotoptions[ifunit].dnsaddr[0]; - ip4_dns_2 = ipcp_gotoptions[ifunit].dnsaddr[1]; - ip4_wins_1 = ipcp_gotoptions[ifunit].winsaddr[0]; - ip4_wins_2 = ipcp_gotoptions[ifunit].winsaddr[1]; + g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_INTERFACE, + str_to_gvalue (ifname)); + g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_ADDRESS, + uint_to_gvalue (opts.ouraddr)); + g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, + uint_to_gvalue (opts.hisaddr)); + g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_NETMASK, + uint_to_gvalue (0xFFFFFFFF)); - g_message ("Got ip configuration"); - g_message ("address: %s", ip4_address_as_string (ip4_address)); - g_message ("gateway: %s", ip4_address_as_string (ip4_gateway)); - g_message ("netmask: %s", ip4_address_as_string (ip4_netmask)); - g_message ("DNS1: %s", ip4_address_as_string (ip4_dns_1)); - g_message ("DNS2: %s", ip4_address_as_string (ip4_dns_2)); - g_message ("WINS1: %s", ip4_address_as_string (ip4_wins_1)); - g_message ("WINS2: %s", ip4_address_as_string (ip4_wins_2)); + if (opts.dnsaddr[0] || opts.dnsaddr[1]) { + array = g_array_new (FALSE, FALSE, sizeof (guint32)); - signal = dbus_message_new_signal (NM_DBUS_PATH_PPP, - NM_DBUS_INTERFACE_PPP, - "IP4Config"); - if (!signal) - goto out; + if (opts.dnsaddr[0]) + g_array_append_val (array, opts.dnsaddr[0]); + if (opts.dnsaddr[1]) + g_array_append_val (array, opts.dnsaddr[1]); - dbus_message_iter_init_append (signal, &iter); - if (!nmu_dbus_dict_open_write (&iter, &iter_dict)) { - g_warning ("dict open write failed!"); - goto out; + val = g_slice_new0 (GValue); + g_value_init (val, DBUS_TYPE_G_UINT_ARRAY); + g_value_set_boxed (val, array); + + g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_DNS, val); } - if (!nmu_dbus_dict_append_string (&iter_dict, "interface", ifname)) { - g_warning ("couldn't append interface to dict"); - goto out; + if (opts.winsaddr[0] || opts.winsaddr[1]) { + array = g_array_new (FALSE, FALSE, sizeof (guint32)); + + if (opts.winsaddr[0]) + g_array_append_val (array, opts.winsaddr[0]); + if (opts.winsaddr[1]) + g_array_append_val (array, opts.winsaddr[1]); + + val = g_slice_new0 (GValue); + g_value_init (val, DBUS_TYPE_G_UINT_ARRAY); + g_value_set_boxed (val, array); + + g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_WINS, val); } - if (!nmu_dbus_dict_append_uint32 (&iter_dict, "addres", ip4_address)) { - g_warning ("couldn't append address to dict"); - goto out; - } - - if (!nmu_dbus_dict_append_uint32 (&iter_dict, "netmask", ip4_netmask)) { - g_warning ("couldn't append netmask to dict"); - goto out; - } - - if (!nmu_dbus_dict_append_uint32 (&iter_dict, "gateway", ip4_gateway)) { - g_warning ("couldn't append gateway to dict"); - goto out; - } - - if (ip4_dns_1 || ip4_dns_2) { - guint32 ip4_dns[2]; - guint32 ip4_dns_len = 0; - - if (ip4_dns_1) - ip4_dns[ip4_dns_len++] = ip4_dns_1; - if (ip4_dns_2) - ip4_dns[ip4_dns_len++] = ip4_dns_2; - - if (!nmu_dbus_dict_append_uint32_array (&iter_dict, - "dns_server", - ip4_dns, - ip4_dns_len)) { - g_warning ("couldn't append dns_servers to dict"); - goto out; - } - } - - if (ip4_wins_1 || ip4_wins_2) { - guint32 ip4_wins[2]; - guint32 ip4_wins_len = 0; - - if (ip4_wins_1) - ip4_wins[ip4_wins_len++] = ip4_wins_1; - if (ip4_wins_2) - ip4_wins[ip4_wins_len++] = ip4_wins_2; - - if (!nmu_dbus_dict_append_uint32_array (&iter_dict, - "wins_server", - ip4_wins, - ip4_wins_len)) { - g_warning ("couldn't append wins_servers to dict"); - goto out; - } - } - - if (!nmu_dbus_dict_close_write (&iter, &iter_dict)) { - g_warning ("dict close write failed!"); - goto out; - } - - if (!dbus_connection_send (connection, signal, NULL)) { - g_warning ("could not send dbus message"); - goto out; - } - - out: - if (signal) - dbus_message_unref (signal); + nm_pppd_plugin_ip4_config (plugin, hash); + g_hash_table_destroy (hash); } static void nm_exit_notify (void *data, int arg) { - DBusConnection *connection = (DBusConnection *) data; + NMPppdPlugin *plugin = NM_PPPD_PLUGIN (data); g_message ("exiting"); - - if (connection) - dbus_connection_unref (connection); + g_object_unref (plugin); } -static DBusConnection * -nm_dbus_prepare_connection (void) -{ - DBusConnection *connection; - DBusError err; - - dbus_error_init (&err); - connection = dbus_bus_get (DBUS_BUS_SYSTEM, &err); - if (!connection || dbus_error_is_set (&err)) { - g_warning ("Could not get the system bus. Make sure the message bus daemon is running."); - goto out; - } - - dbus_connection_set_exit_on_disconnect (connection, FALSE); - - dbus_error_init (&err); - dbus_bus_request_name (connection, NM_DBUS_SERVICE_PPP, 0, &err); - if (dbus_error_is_set (&err)) { - g_warning ("Could not acquire the dbus service. dbus_bus_request_name() says: '%s'.", err.message); - goto out; - } - - out: - if (dbus_error_is_set (&err)) { - dbus_error_free (&err); - connection = NULL; - } - - return connection; -} - - int plugin_init (void) { - DBusConnection *connection; + DBusGConnection *bus; + DBusGProxy *bus_proxy; + NMPppdPlugin *plugin; + guint request_name_result; + GError *err = NULL; - connection = nm_dbus_prepare_connection (); - if (connection) { - add_notifier (&phasechange, nm_phasechange, connection); - add_notifier (&ip_up_notifier, nm_ip_up, connection); - add_notifier (&exitnotify, nm_exit_notify, connection); + g_type_init (); + + bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err); + if (!bus) { + g_warning ("Couldn't connect to system bus: %s", err->message); + g_error_free (err); + return -1; } - return connection ? 0 : -1; + bus_proxy = dbus_g_proxy_new_for_name (bus, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus"); + + if (!dbus_g_proxy_call (bus_proxy, "RequestName", &err, + G_TYPE_STRING, NM_DBUS_SERVICE_PPP, + G_TYPE_UINT, 0, + G_TYPE_INVALID, + G_TYPE_UINT, &request_name_result, + G_TYPE_INVALID)) { + g_warning ("Failed to acquire '" NM_DBUS_SERVICE_PPP "'"); + g_error_free (err); + dbus_g_connection_unref (bus); + g_object_unref (bus_proxy); + + return -1; + } + + g_object_unref (bus_proxy); + + plugin = nm_pppd_plugin_new (bus); + dbus_g_connection_unref (bus); + + add_notifier (&phasechange, nm_phasechange, plugin); + add_notifier (&ip_up_notifier, nm_ip_up, plugin); + add_notifier (&exitnotify, nm_exit_notify, plugin); + + return 0; } diff --git a/src/ppp-manager/nm-pppd-plugin.h b/src/ppp-manager/nm-pppd-plugin.h index 5c34906f93..a06b947ab9 100644 --- a/src/ppp-manager/nm-pppd-plugin.h +++ b/src/ppp-manager/nm-pppd-plugin.h @@ -1,3 +1,10 @@ #define NM_DBUS_SERVICE_PPP "org.freedesktop.NetworkManager.PPP" #define NM_DBUS_PATH_PPP "/org/freedesktop/NetworkManager/PPP" #define NM_DBUS_INTERFACE_PPP "org.freedesktop.NetworkManager.PPP" + +#define NM_PPP_IP4_CONFIG_INTERFACE "interface" +#define NM_PPP_IP4_CONFIG_ADDRESS "address" +#define NM_PPP_IP4_CONFIG_NETMASK "netmask" +#define NM_PPP_IP4_CONFIG_GATEWAY "gateway" +#define NM_PPP_IP4_CONFIG_DNS "dns" +#define NM_PPP_IP4_CONFIG_WINS "wins" diff --git a/src/ppp-manager/nm-pppd-plugin.xml b/src/ppp-manager/nm-pppd-plugin.xml new file mode 100644 index 0000000000..cbe6d9b9b8 --- /dev/null +++ b/src/ppp-manager/nm-pppd-plugin.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/supplicant-manager/nm-supplicant-config.c b/src/supplicant-manager/nm-supplicant-config.c index eb5fe8755a..948428ebe5 100644 --- a/src/supplicant-manager/nm-supplicant-config.c +++ b/src/supplicant-manager/nm-supplicant-config.c @@ -27,7 +27,6 @@ #include "nm-supplicant-config.h" #include "nm-supplicant-settings-verify.h" #include "nm-utils.h" -#include "dbus-dict-helpers.h" #include "nm-setting.h" #include "NetworkManagerUtils.h" diff --git a/src/supplicant-manager/nm-supplicant-interface.c b/src/supplicant-manager/nm-supplicant-interface.c index b18fe43e59..9a9c1a48a2 100644 --- a/src/supplicant-manager/nm-supplicant-interface.c +++ b/src/supplicant-manager/nm-supplicant-interface.c @@ -29,7 +29,6 @@ #include "nm-marshal.h" #include "nm-supplicant-config.h" #include "nm-dbus-manager.h" -#include "dbus-dict-helpers.h" #include "nm-call-store.h" #define WPAS_DBUS_IFACE_INTERFACE WPAS_DBUS_INTERFACE ".Interface" diff --git a/test/Makefile.am b/test/Makefile.am index 7759245def..e16e26fd7a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS=test-common libnm-util +SUBDIRS=test-common INCLUDES = -I${top_srcdir} \ -I${top_srcdir}/libnm-util \ diff --git a/test/libnm-util/.cvsignore b/test/libnm-util/.cvsignore deleted file mode 100644 index cc5c21d1a8..0000000000 --- a/test/libnm-util/.cvsignore +++ /dev/null @@ -1,5 +0,0 @@ -test-ciphers -Makefile.in -Makefile -test-dbus-helpers -test-dbus-dict-helpers diff --git a/test/libnm-util/Makefile.am b/test/libnm-util/Makefile.am deleted file mode 100644 index 4d552e635e..0000000000 --- a/test/libnm-util/Makefile.am +++ /dev/null @@ -1,20 +0,0 @@ -INCLUDES = -I${top_srcdir} \ - -I${top_srcdir}/include \ - -I${top_srcdir}/libnm-util \ - -I${top_srcdir}/test \ - -I${top_srcdir}/test/test-common - -noinst_PROGRAMS = test-dbus-dict-helpers - -test_dbus_dict_helpers_SOURCES = test-dbus-dict-helpers.c - -test_dbus_dict_helpers_CPPFLAGS = \ - $(DBUS_CFLAGS) \ - -DDBUS_API_SUBJECT_TO_CHANGE \ - -DBINDIR=\"$(bindir)\" \ - -DDATADIR=\"$(datadir)\" - -test_dbus_dict_helpers_LDADD = \ - $(DBUS_LIBS) \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/test/test-common/libtest-common.la diff --git a/test/libnm-util/test-dbus-dict-helpers.c b/test/libnm-util/test-dbus-dict-helpers.c deleted file mode 100644 index 05c506f06d..0000000000 --- a/test/libnm-util/test-dbus-dict-helpers.c +++ /dev/null @@ -1,355 +0,0 @@ -/* NetworkManager -- Forget about your network - * - * Dan Williams - * - * 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 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * (C) Copyright 2006 Red Hat, Inc. - */ - -#include -#include -#include -#include - -#include "test-common.h" -#include "dbus-dict-helpers.h" - -static char *progname = NULL; - - -#define DECLARE_ENTRY(name, val_type) \ -struct name { \ - const char *key; \ - val_type val; \ - dbus_bool_t found; \ - int type; \ -}; - -DECLARE_ENTRY(StringEntry, const char *) -DECLARE_ENTRY(ByteEntry, const char) -DECLARE_ENTRY(BoolEntry, dbus_bool_t) -DECLARE_ENTRY(Int16Entry, dbus_int16_t) -DECLARE_ENTRY(UInt16Entry, dbus_uint16_t) -DECLARE_ENTRY(Int32Entry, dbus_int32_t) -DECLARE_ENTRY(UInt32Entry, dbus_uint32_t) -DECLARE_ENTRY(Int64Entry, dbus_int64_t) -DECLARE_ENTRY(UInt64Entry, dbus_uint64_t) -DECLARE_ENTRY(DoubleEntry, double) -DECLARE_ENTRY(OPEntry, const char *) -DECLARE_ENTRY(ByteArrayEntry, const char *) -DECLARE_ENTRY(UInt32ArrayEntry, dbus_uint32_t *) -DECLARE_ENTRY(StringArrayEntry, char **) - -struct DictEntries { - struct StringEntry string; - struct ByteEntry byte; - struct BoolEntry bool; - struct Int16Entry int16; - struct UInt16Entry uint16; - struct Int32Entry int32; - struct UInt32Entry uint32; - struct Int64Entry int64; - struct UInt64Entry uint64; - struct DoubleEntry dbl; - struct OPEntry op; - struct ByteArrayEntry bytearr; - struct ByteArrayEntry zlbytearr; - struct UInt32ArrayEntry uint32arr; - struct UInt32ArrayEntry zluint32arr; - struct StringArrayEntry strarr; - struct StringArrayEntry zlstrarr; -}; - -#define TEST_KEY_STRING "String" -#define TEST_KEY_BYTE "Byte" -#define TEST_KEY_BOOL "Bool" -#define TEST_KEY_INT16 "Int16" -#define TEST_KEY_UINT16 "UInt16" -#define TEST_KEY_INT32 "Int32" -#define TEST_KEY_UINT32 "UInt32" -#define TEST_KEY_INT64 "Int64" -#define TEST_KEY_UINT64 "UInt64" -#define TEST_KEY_DOUBLE "Double" -#define TEST_KEY_OP "ObjectPath" -#define TEST_KEY_BYTEARR "ByteArray" -#define TEST_KEY_ZLBYTEARR "ZLByteArray" -#define UINT32ARR_LEN 4 -#define TEST_KEY_UINT32ARR "UInt32Array" -#define TEST_KEY_ZLUINT32ARR "ZLUInt32Array" -#define STRARR_LEN 2 -#define TEST_KEY_STRINGARR "StringArray" -#define TEST_KEY_ZLSTRINGARR "ZLStringArray" - -#define UINT32ARR_ELEM0 0xdeadbeef -#define UINT32ARR_ELEM1 0x1337bead -#define UINT32ARR_ELEM2 0x11111113 -#define UINT32ARR_ELEM3 0xbdefac44 - -struct DictEntries entries = { - { TEST_KEY_STRING, "foobar22", FALSE, DBUS_TYPE_STRING }, - { TEST_KEY_BYTE, 0x78, FALSE, DBUS_TYPE_BYTE }, - { TEST_KEY_BOOL, TRUE, FALSE, DBUS_TYPE_BOOLEAN }, - { TEST_KEY_INT16, -28567, FALSE, DBUS_TYPE_INT16 }, - { TEST_KEY_UINT16, 12345, FALSE, DBUS_TYPE_UINT16 }, - { TEST_KEY_INT32, -5987654, FALSE, DBUS_TYPE_INT32 }, - { TEST_KEY_UINT32, 45678912, FALSE, DBUS_TYPE_UINT32 }, - { TEST_KEY_INT64, -12491340761ll, FALSE, DBUS_TYPE_INT64 }, - { TEST_KEY_UINT64, 8899223582883ll, FALSE, DBUS_TYPE_UINT64 }, - { TEST_KEY_DOUBLE, 54.3355632f, FALSE, DBUS_TYPE_DOUBLE }, - { TEST_KEY_OP, "/com/it/foobar", FALSE, DBUS_TYPE_OBJECT_PATH }, - { TEST_KEY_BYTEARR, "qazwsxedcrfvtgb",FALSE, DBUS_TYPE_BYTE }, - { TEST_KEY_ZLBYTEARR,NULL, FALSE, DBUS_TYPE_BYTE }, - { TEST_KEY_UINT32ARR,NULL, FALSE, DBUS_TYPE_UINT32 }, - { TEST_KEY_ZLUINT32ARR,NULL, FALSE, DBUS_TYPE_UINT32 }, - { TEST_KEY_STRINGARR,NULL, FALSE, DBUS_TYPE_STRING }, - { TEST_KEY_ZLSTRINGARR,NULL, FALSE, DBUS_TYPE_STRING } -}; - - -static void -test_write_dict (DBusMessage *message) -{ - TestResult result = TEST_FAIL; - DBusMessageIter iter, iter_dict; - char * err_string = "failure"; - - fprintf (stdout, "\n\n---- START: WRITE DICT ---------------------------------------------\n"); - - dbus_message_iter_init_append (message, &iter); - if (!nmu_dbus_dict_open_write (&iter, &iter_dict)) { - err_string = "failed on open_write"; - goto done; - } - if (!nmu_dbus_dict_append_string (&iter_dict, entries.string.key, entries.string.val)) { - err_string = "failed to append string entry"; - goto done; - } - if (!nmu_dbus_dict_append_byte (&iter_dict, entries.byte.key, entries.byte.val)) { - err_string = "failed to append byte entry"; - goto done; - } - if (!nmu_dbus_dict_append_bool (&iter_dict, entries.bool.key, entries.bool.val)) { - err_string = "failed to append boolean entry"; - goto done; - } - if (!nmu_dbus_dict_append_int16 (&iter_dict, entries.int16.key, entries.int16.val)) { - err_string = "failed to append int16 entry"; - goto done; - } - if (!nmu_dbus_dict_append_uint16 (&iter_dict, entries.uint16.key, entries.uint16.val)) { - err_string = "failed to append uint16 entry"; - goto done; - } - if (!nmu_dbus_dict_append_int32 (&iter_dict, entries.int32.key, entries.int32.val)) { - err_string = "failed to append int32 entry"; - goto done; - } - if (!nmu_dbus_dict_append_uint32 (&iter_dict, entries.uint32.key, entries.uint32.val)) { - err_string = "failed to append uint32 entry"; - goto done; - } - if (!nmu_dbus_dict_append_int64 (&iter_dict, entries.int64.key, entries.int64.val)) { - err_string = "failed to append int64 entry"; - goto done; - } - if (!nmu_dbus_dict_append_uint64 (&iter_dict, entries.uint64.key, entries.uint64.val)) { - err_string = "failed to append uint64 entry"; - goto done; - } - if (!nmu_dbus_dict_append_double (&iter_dict, entries.dbl.key, entries.dbl.val)) { - err_string = "failed to append double entry"; - goto done; - } - if (!nmu_dbus_dict_append_object_path (&iter_dict, entries.op.key, entries.op.val)) { - err_string = "failed to append object path entry"; - goto done; - } - if (!nmu_dbus_dict_append_byte_array (&iter_dict, entries.bytearr.key, entries.bytearr.val, - strlen (entries.bytearr.val))) { - err_string = "failed to append byte array entry"; - goto done; - } - if (!nmu_dbus_dict_append_byte_array (&iter_dict, entries.zlbytearr.key, entries.zlbytearr.val, 0)) { - err_string = "failed to append zero-length byte array entry"; - goto done; - } - entries.uint32arr.val = malloc (sizeof (dbus_uint32_t) * UINT32ARR_LEN); - entries.uint32arr.val[0] = UINT32ARR_ELEM0; - entries.uint32arr.val[1] = UINT32ARR_ELEM1; - entries.uint32arr.val[2] = UINT32ARR_ELEM2; - entries.uint32arr.val[3] = UINT32ARR_ELEM3; - if (!nmu_dbus_dict_append_uint32_array (&iter_dict, entries.uint32arr.key, - (const dbus_uint32_t *) entries.uint32arr.val, UINT32ARR_LEN)) { - err_string = "failed to append uint32 array entry"; - goto done; - } - if (!nmu_dbus_dict_append_uint32_array (&iter_dict, entries.zluint32arr.key, - (const dbus_uint32_t *)entries.zluint32arr.val, 0)) { - err_string = "failed to append zero-length uint32 array entry"; - goto done; - } - entries.strarr.val = malloc (sizeof (char *) * STRARR_LEN); - entries.strarr.val[0] = "foo"; - entries.strarr.val[1] = "bar"; - if (!nmu_dbus_dict_append_string_array (&iter_dict, entries.strarr.key, - (const char **)entries.strarr.val, STRARR_LEN)) { - err_string = "failed to append string array entry"; - goto done; - } - if (!nmu_dbus_dict_append_string_array (&iter_dict, entries.zlstrarr.key, - (const char **)entries.zlstrarr.val, 0)) { - err_string = "failed to append zero-length string array entry"; - goto done; - } - if (!nmu_dbus_dict_close_write (&iter, &iter_dict)) { - err_string = "failed to close dictionary"; - goto done; - } - - result = TEST_SUCCEED; - err_string = "success"; - -done: - test_result (progname, "Dict Write", result, err_string); -} - -#define TEST_CASE(key_string, test_entry, comparison) \ - if (!strcmp (entry.key, test_entry.key)) { \ - fprintf (stderr, "Testing type " key_string ".\n"); \ - if (entry.type != test_entry.type) { \ - err_string = "Test item " key_string " was an unexpected type."; \ - goto done; \ - } \ - if (!(comparison)) { \ - err_string = "Test item " key_string " was unexpected value."; \ - goto done; \ - } \ - test_entry.found = TRUE; \ - goto next; \ - } - -#define TEST_CASE_ARRAY(key_string, test_entry, exp_len, comparison) \ - if (!strcmp (entry.key, test_entry.key)) { \ - fprintf (stderr, "Testing type " key_string ".\n"); \ - if (entry.type != DBUS_TYPE_ARRAY) { \ - err_string = "Test item " key_string " was an unexpected type."; \ - goto done; \ - } \ - if (entry.array_type != test_entry.type) { \ - err_string = "Test item " key_string " was an unexpected element type."; \ - goto done; \ - } \ - if (exp_len != entry.array_len) { \ - err_string = "Test item " key_string " had unexpected length!"; \ - goto done; \ - } \ - if (!(comparison)) { \ - err_string = "Test item " key_string " was unexpected value."; \ - goto done; \ - } \ - test_entry.found = TRUE; \ - goto next; \ - } - -static void -test_read_dict (DBusMessage *message) -{ - TestResult result = TEST_FAIL; - NMUDictEntry entry = { .type = DBUS_TYPE_STRING }; - DBusMessageIter iter, iter_dict; - char * err_string = "failure"; - - dbus_message_iter_init (message, &iter); - - if (!nmu_dbus_dict_open_read (&iter, &iter_dict)) { - err_string = "failure on open_read"; - goto done; - } - - while (nmu_dbus_dict_has_dict_entry (&iter_dict)) { - dbus_uint32_t bytearr_len = strlen (entries.bytearr.val); - - if (!nmu_dbus_dict_get_entry (&iter_dict, &entry)) { - err_string = "failure reading dict entry"; - goto done; - } - - TEST_CASE (TEST_KEY_STRING, entries.string, !strcmp (entry.str_value, entries.string.val)) - TEST_CASE (TEST_KEY_BYTE, entries.byte, entry.byte_value == entries.byte.val) - TEST_CASE (TEST_KEY_BOOL, entries.bool, entry.bool_value == entries.bool.val) - TEST_CASE (TEST_KEY_INT16, entries.int16, entry.int16_value == entries.int16.val) - TEST_CASE (TEST_KEY_UINT16, entries.uint16, entry.uint16_value == entries.uint16.val) - TEST_CASE (TEST_KEY_INT32, entries.int32, entry.int32_value == entries.int32.val) - TEST_CASE (TEST_KEY_UINT32, entries.uint32, entry.uint32_value == entries.uint32.val) - TEST_CASE (TEST_KEY_INT64, entries.int64, entry.int64_value == entries.int64.val) - TEST_CASE (TEST_KEY_UINT64, entries.uint64, entry.uint64_value == entries.uint64.val) - TEST_CASE (TEST_KEY_DOUBLE, entries.dbl, !memcmp (&entry.double_value, &entries.dbl.val, sizeof (double))) - TEST_CASE (TEST_KEY_OP, entries.op, !strcmp (entry.str_value, entries.op.val)) - TEST_CASE_ARRAY (TEST_KEY_BYTEARR, entries.bytearr, bytearr_len, - !memcmp (entry.bytearray_value, entries.bytearr.val, bytearr_len)) - TEST_CASE_ARRAY (TEST_KEY_ZLBYTEARR, entries.zlbytearr, 0, - entry.bytearray_value == entries.zlbytearr.val) - TEST_CASE_ARRAY (TEST_KEY_UINT32ARR, entries.uint32arr, UINT32ARR_LEN, - (entry.uint32array_value[0] == UINT32ARR_ELEM0 && - entry.uint32array_value[1] == UINT32ARR_ELEM1 && - entry.uint32array_value[2] == UINT32ARR_ELEM2 && - entry.uint32array_value[3] == UINT32ARR_ELEM3)) - TEST_CASE_ARRAY (TEST_KEY_ZLUINT32ARR, entries.zluint32arr, 0, - entry.uint32array_value == entries.zluint32arr.val) - TEST_CASE_ARRAY (TEST_KEY_STRINGARR, entries.strarr, STRARR_LEN, - (!strcmp (entry.strarray_value[0], "foo") && !strcmp (entry.strarray_value[1], "bar"))) - TEST_CASE_ARRAY (TEST_KEY_ZLSTRINGARR, entries.zlstrarr, 0, - entry.strarray_value == entries.zlstrarr.val) - - err_string = "Unknown dict entry encountered."; - goto done; - - next: - nmu_dbus_dict_entry_clear (&entry); - } - - if (!entries.string.found || !entries.byte.found || !entries.bool.found || !entries.int16.found - || !entries.uint16.found || !entries.int32.found || !entries.uint32.found - || !entries.int64.found || !entries.uint64.found || !entries.dbl.found - || !entries.op.found || !entries.bytearr.found || !entries.zlbytearr.found - || !entries.uint32arr.found || !entries.zluint32arr.found - || !entries.strarr.found || !entries.zlstrarr.found) { - err_string = "A required entry was not found in the dict."; - goto done; - } - - result = TEST_SUCCEED; - err_string = ""; - -done: - test_result (progname, "Dict Read", result, err_string); -} - - -int main (int argc, char **argv) -{ - DBusMessage * message; - progname = argv[0]; - - message = dbus_message_new_method_call ("com.it", "/com/it", - "com.it", "someMethod"); - test_write_dict (message); - test_read_dict (message); - - fprintf (stderr, "\n\n------ DONE\n"); - - return 0; -}