nm-utils-private.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
  2. /*
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the
  15. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA.
  17. *
  18. * Copyright 2005 - 2017 Red Hat, Inc.
  19. */
  20. #ifndef __NM_UTILS_PRIVATE_H__
  21. #define __NM_UTILS_PRIVATE_H__
  22. #if !((NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_PRIVATE)
  23. #error Cannot use this header.
  24. #endif
  25. #include "nm-setting-private.h"
  26. #include "nm-setting-ip-config.h"
  27. struct _NMVariantAttributeSpec {
  28. char *name;
  29. const GVariantType *type;
  30. bool v4:1;
  31. bool v6:1;
  32. bool no_value:1;
  33. bool consumes_rest:1;
  34. char str_type;
  35. };
  36. gboolean _nm_utils_string_slist_validate (GSList *list,
  37. const char **valid_values);
  38. /* D-Bus transform funcs */
  39. GVariant *_nm_utils_hwaddr_cloned_get (NMSetting *setting,
  40. const char *property);
  41. gboolean _nm_utils_hwaddr_cloned_set (NMSetting *setting,
  42. GVariant *connection_dict,
  43. const char *property,
  44. GVariant *value,
  45. NMSettingParseFlags parse_flags,
  46. GError **error);
  47. gboolean _nm_utils_hwaddr_cloned_not_set (NMSetting *setting,
  48. GVariant *connection_dict,
  49. const char *property,
  50. NMSettingParseFlags parse_flags,
  51. GError **error);
  52. GVariant * _nm_utils_hwaddr_cloned_data_synth (NMSetting *setting,
  53. NMConnection *connection,
  54. const char *property);
  55. gboolean _nm_utils_hwaddr_cloned_data_set (NMSetting *setting,
  56. GVariant *connection_dict,
  57. const char *property,
  58. GVariant *value,
  59. NMSettingParseFlags parse_flags,
  60. GError **error);
  61. GVariant * _nm_utils_hwaddr_to_dbus (const GValue *prop_value);
  62. void _nm_utils_hwaddr_from_dbus (GVariant *dbus_value,
  63. GValue *prop_value);
  64. GVariant * _nm_utils_strdict_to_dbus (const GValue *prop_value);
  65. void _nm_utils_strdict_from_dbus (GVariant *dbus_value,
  66. GValue *prop_value);
  67. void _nm_utils_bytes_from_dbus (GVariant *dbus_value,
  68. GValue *prop_value);
  69. char * _nm_utils_hwaddr_canonical_or_invalid (const char *mac, gssize length);
  70. GPtrArray * _nm_utils_team_link_watchers_from_variant (GVariant *value);
  71. GVariant * _nm_utils_team_link_watchers_to_variant (GPtrArray *link_watchers);
  72. void _nm_utils_format_variant_attributes_full (GString *str,
  73. const NMUtilsNamedValue *values,
  74. guint num_values,
  75. char attr_separator,
  76. char key_value_separator);
  77. gboolean _nm_sriov_vf_parse_vlans (NMSriovVF *vf, const char *str, GError **error);
  78. /* JSON to GValue conversion macros */
  79. typedef struct {
  80. const char *key1;
  81. const char *key2;
  82. const char *key3;
  83. union {
  84. int default_int;
  85. gboolean default_bool;
  86. const char *default_str;
  87. };
  88. } _NMUtilsTeamPropertyKeys;
  89. static inline int
  90. _nm_utils_json_extract_int (char *conf,
  91. _NMUtilsTeamPropertyKeys key,
  92. gboolean is_port)
  93. {
  94. gs_free GValue *t_value = NULL;
  95. int ret;
  96. t_value = _nm_utils_team_config_get (conf, key.key1, key.key2, key.key3, is_port);
  97. if (!t_value)
  98. return key.default_int;
  99. ret = g_value_get_int (t_value);
  100. g_value_unset (t_value);
  101. return ret;
  102. }
  103. static inline gboolean
  104. _nm_utils_json_extract_boolean (char *conf,
  105. _NMUtilsTeamPropertyKeys key,
  106. gboolean is_port)
  107. {
  108. gs_free GValue *t_value = NULL;
  109. gboolean ret;
  110. t_value = _nm_utils_team_config_get (conf, key.key1, key.key2, key.key3, is_port);
  111. if (!t_value)
  112. return key.default_bool;
  113. ret = g_value_get_boolean (t_value);
  114. g_value_unset (t_value);
  115. return ret;
  116. }
  117. static inline char *
  118. _nm_utils_json_extract_string (char *conf,
  119. _NMUtilsTeamPropertyKeys key,
  120. gboolean is_port)
  121. {
  122. gs_free GValue *t_value = NULL;
  123. char *ret;
  124. t_value = _nm_utils_team_config_get (conf, key.key1, key.key2, key.key3, is_port);
  125. if (!t_value)
  126. return g_strdup (key.default_str);
  127. ret = g_value_dup_string (t_value);
  128. g_value_unset (t_value);
  129. return ret;
  130. }
  131. static inline char **
  132. _nm_utils_json_extract_strv (char *conf,
  133. _NMUtilsTeamPropertyKeys key,
  134. gboolean is_port)
  135. {
  136. gs_free GValue *t_value = NULL;
  137. char **ret;
  138. t_value = _nm_utils_team_config_get (conf, key.key1, key.key2, key.key3, is_port);
  139. if (!t_value)
  140. return NULL;
  141. ret = g_strdupv (g_value_get_boxed (t_value));
  142. g_value_unset (t_value);
  143. return ret;
  144. }
  145. static inline GPtrArray *
  146. _nm_utils_json_extract_ptr_array (char *conf,
  147. _NMUtilsTeamPropertyKeys key,
  148. gboolean is_port)
  149. {
  150. gs_free GValue *t_value = NULL;
  151. GPtrArray *data, *ret;
  152. guint i;
  153. ret = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_team_link_watcher_unref);
  154. t_value = _nm_utils_team_config_get (conf, key.key1, key.key2, key.key3, is_port);
  155. if (!t_value)
  156. return ret;
  157. data = g_value_get_boxed (t_value);
  158. if (!data)
  159. return ret;
  160. for (i = 0; i < data->len; i++)
  161. g_ptr_array_add (ret, nm_team_link_watcher_dup (data->pdata[i]));
  162. g_value_unset (t_value);
  163. return ret;
  164. }
  165. static inline void
  166. _nm_utils_json_append_gvalue (char **conf,
  167. _NMUtilsTeamPropertyKeys key,
  168. const GValue *val)
  169. {
  170. _nm_utils_team_config_set (conf, key.key1, key.key2, key.key3, val);
  171. }
  172. #endif