nm-utils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_H__
  21. #define __NM_UTILS_H__
  22. #if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION)
  23. #error "Only <NetworkManager.h> can be included directly."
  24. #endif
  25. #include <glib.h>
  26. #include <netinet/in.h>
  27. /* For ETH_ALEN and INFINIBAND_ALEN */
  28. #include <linux/if_ether.h>
  29. #include <linux/if_infiniband.h>
  30. #include "nm-core-enum-types.h"
  31. #include "nm-setting-sriov.h"
  32. #include "nm-setting-tc-config.h"
  33. #include "nm-setting-wireless-security.h"
  34. G_BEGIN_DECLS
  35. typedef struct _NMVariantAttributeSpec NMVariantAttributeSpec;
  36. /* SSID helpers */
  37. gboolean nm_utils_is_empty_ssid (const guint8 *ssid, gsize len);
  38. const char *nm_utils_escape_ssid (const guint8 *ssid, gsize len);
  39. gboolean nm_utils_same_ssid (const guint8 *ssid1, gsize len1,
  40. const guint8 *ssid2, gsize len2,
  41. gboolean ignore_trailing_null);
  42. char * nm_utils_ssid_to_utf8 (const guint8 *ssid, gsize len);
  43. /**
  44. * NMUtilsSecurityType:
  45. * @NMU_SEC_INVALID: unknown or invalid security, placeholder and not used
  46. * @NMU_SEC_NONE: unencrypted and open
  47. * @NMU_SEC_STATIC_WEP: static WEP keys are used for encryption
  48. * @NMU_SEC_LEAP: Cisco LEAP is used for authentication and for generating the
  49. * dynamic WEP keys automatically
  50. * @NMU_SEC_DYNAMIC_WEP: standard 802.1x is used for authentication and
  51. * generating the dynamic WEP keys automatically
  52. * @NMU_SEC_WPA_PSK: WPA1 is used with Pre-Shared Keys (PSK)
  53. * @NMU_SEC_WPA_ENTERPRISE: WPA1 is used with 802.1x authentication
  54. * @NMU_SEC_WPA2_PSK: WPA2/RSN is used with Pre-Shared Keys (PSK)
  55. * @NMU_SEC_WPA2_ENTERPRISE: WPA2 is used with 802.1x authentication
  56. *
  57. * Describes generic security mechanisms that 802.11 access points may offer.
  58. * Used with nm_utils_security_valid() for checking whether a given access
  59. * point is compatible with a network device.
  60. **/
  61. typedef enum {
  62. NMU_SEC_INVALID = 0,
  63. NMU_SEC_NONE,
  64. NMU_SEC_STATIC_WEP,
  65. NMU_SEC_LEAP,
  66. NMU_SEC_DYNAMIC_WEP,
  67. NMU_SEC_WPA_PSK,
  68. NMU_SEC_WPA_ENTERPRISE,
  69. NMU_SEC_WPA2_PSK,
  70. NMU_SEC_WPA2_ENTERPRISE
  71. } NMUtilsSecurityType;
  72. gboolean nm_utils_security_valid (NMUtilsSecurityType type,
  73. NMDeviceWifiCapabilities wifi_caps,
  74. gboolean have_ap,
  75. gboolean adhoc,
  76. NM80211ApFlags ap_flags,
  77. NM80211ApSecurityFlags ap_wpa,
  78. NM80211ApSecurityFlags ap_rsn);
  79. gboolean nm_utils_ap_mode_security_valid (NMUtilsSecurityType type,
  80. NMDeviceWifiCapabilities wifi_caps);
  81. gboolean nm_utils_wep_key_valid (const char *key, NMWepKeyType wep_type);
  82. gboolean nm_utils_wpa_psk_valid (const char *psk);
  83. NM_AVAILABLE_IN_1_6
  84. gboolean nm_utils_is_json_object (const char *str, GError **error);
  85. GVariant *nm_utils_ip4_dns_to_variant (char **dns);
  86. char **nm_utils_ip4_dns_from_variant (GVariant *value);
  87. GVariant *nm_utils_ip4_addresses_to_variant (GPtrArray *addresses,
  88. const char *gateway);
  89. GPtrArray *nm_utils_ip4_addresses_from_variant (GVariant *value,
  90. char **out_gateway);
  91. GVariant *nm_utils_ip4_routes_to_variant (GPtrArray *routes);
  92. GPtrArray *nm_utils_ip4_routes_from_variant (GVariant *value);
  93. guint32 nm_utils_ip4_netmask_to_prefix (guint32 netmask);
  94. guint32 nm_utils_ip4_prefix_to_netmask (guint32 prefix);
  95. guint32 nm_utils_ip4_get_default_prefix (guint32 ip);
  96. GVariant *nm_utils_ip6_dns_to_variant (char **dns);
  97. char **nm_utils_ip6_dns_from_variant (GVariant *value);
  98. GVariant *nm_utils_ip6_addresses_to_variant (GPtrArray *addresses,
  99. const char *gateway);
  100. GPtrArray *nm_utils_ip6_addresses_from_variant (GVariant *value,
  101. char **out_gateway);
  102. GVariant *nm_utils_ip6_routes_to_variant (GPtrArray *routes);
  103. GPtrArray *nm_utils_ip6_routes_from_variant (GVariant *value);
  104. GVariant *nm_utils_ip_addresses_to_variant (GPtrArray *addresses);
  105. GPtrArray *nm_utils_ip_addresses_from_variant (GVariant *value,
  106. int family);
  107. GVariant *nm_utils_ip_routes_to_variant (GPtrArray *routes);
  108. GPtrArray *nm_utils_ip_routes_from_variant (GVariant *value,
  109. int family);
  110. char *nm_utils_uuid_generate (void);
  111. gboolean nm_utils_file_is_certificate (const char *filename);
  112. gboolean nm_utils_file_is_private_key (const char *filename, gboolean *out_encrypted);
  113. gboolean nm_utils_file_is_pkcs12 (const char *filename);
  114. typedef gboolean (*NMUtilsFileSearchInPathsPredicate) (const char *filename, gpointer user_data);
  115. struct stat;
  116. typedef gboolean (*NMUtilsCheckFilePredicate) (const char *filename, const struct stat *stat, gpointer user_data, GError **error);
  117. const char *nm_utils_file_search_in_paths (const char *progname,
  118. const char *try_first,
  119. const char *const *paths,
  120. GFileTest file_test_flags,
  121. NMUtilsFileSearchInPathsPredicate predicate,
  122. gpointer user_data,
  123. GError **error);
  124. guint32 nm_utils_wifi_freq_to_channel (guint32 freq);
  125. guint32 nm_utils_wifi_channel_to_freq (guint32 channel, const char *band);
  126. guint32 nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band);
  127. gboolean nm_utils_wifi_is_channel_valid (guint32 channel, const char *band);
  128. NM_AVAILABLE_IN_1_2
  129. const guint *nm_utils_wifi_2ghz_freqs (void);
  130. NM_AVAILABLE_IN_1_2
  131. const guint *nm_utils_wifi_5ghz_freqs (void);
  132. const char *nm_utils_wifi_strength_bars (guint8 strength);
  133. /**
  134. * NM_UTILS_HWADDR_LEN_MAX:
  135. *
  136. * The maximum length of hardware addresses handled by NetworkManager itself,
  137. * nm_utils_hwaddr_len(), and nm_utils_hwaddr_aton().
  138. */
  139. #define NM_UTILS_HWADDR_LEN_MAX 20 /* INFINIBAND_ALEN */
  140. gsize nm_utils_hwaddr_len (int type) G_GNUC_PURE;
  141. char *nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length);
  142. GByteArray *nm_utils_hwaddr_atoba (const char *asc, gsize length);
  143. guint8 *nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length);
  144. gboolean nm_utils_hwaddr_valid (const char *asc, gssize length);
  145. char *nm_utils_hwaddr_canonical (const char *asc, gssize length);
  146. gboolean nm_utils_hwaddr_matches (gconstpointer hwaddr1,
  147. gssize hwaddr1_len,
  148. gconstpointer hwaddr2,
  149. gssize hwaddr2_len);
  150. char *nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len);
  151. GBytes *nm_utils_hexstr2bin (const char *hex);
  152. NM_DEPRECATED_IN_1_6_FOR(nm_utils_is_valid_iface_name)
  153. gboolean nm_utils_iface_valid_name (const char *name);
  154. NM_AVAILABLE_IN_1_6
  155. gboolean nm_utils_is_valid_iface_name (const char *name, GError **error);
  156. gboolean nm_utils_is_uuid (const char *str);
  157. /**
  158. * NM_UTILS_INET_ADDRSTRLEN:
  159. *
  160. * Defines the minimal length for a char buffer that is suitable as @dst argument
  161. * for both nm_utils_inet4_ntop() and nm_utils_inet6_ntop().
  162. **/
  163. #define NM_UTILS_INET_ADDRSTRLEN INET6_ADDRSTRLEN
  164. const char *nm_utils_inet4_ntop (in_addr_t inaddr, char *dst);
  165. const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst);
  166. gboolean nm_utils_ipaddr_valid (int family, const char *ip);
  167. gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type);
  168. NM_AVAILABLE_IN_1_2
  169. int nm_utils_bond_mode_string_to_int (const char *mode);
  170. NM_AVAILABLE_IN_1_2
  171. const char *nm_utils_bond_mode_int_to_string (int mode);
  172. NM_AVAILABLE_IN_1_2
  173. char *nm_utils_enum_to_str (GType type, int value);
  174. NM_AVAILABLE_IN_1_2
  175. gboolean nm_utils_enum_from_str (GType type, const char *str, int *out_value, char **err_token);
  176. NM_AVAILABLE_IN_1_2
  177. const char **nm_utils_enum_get_values (GType type, int from, int to);
  178. NM_AVAILABLE_IN_1_6
  179. guint nm_utils_version (void);
  180. NM_AVAILABLE_IN_1_8
  181. GHashTable * nm_utils_parse_variant_attributes (const char *string,
  182. char attr_separator,
  183. char key_value_separator,
  184. gboolean ignore_unknown,
  185. const NMVariantAttributeSpec *const *spec,
  186. GError **error);
  187. NM_AVAILABLE_IN_1_8
  188. char * nm_utils_format_variant_attributes (GHashTable *attributes,
  189. char attr_separator,
  190. char key_value_separator);
  191. /*****************************************************************************/
  192. NM_AVAILABLE_IN_1_12
  193. NMTCQdisc *nm_utils_tc_qdisc_from_str (const char *str, GError **error);
  194. NM_AVAILABLE_IN_1_12
  195. char *nm_utils_tc_qdisc_to_str (NMTCQdisc *qdisc, GError **error);
  196. NM_AVAILABLE_IN_1_12
  197. NMTCAction *nm_utils_tc_action_from_str (const char *str, GError **error);
  198. NM_AVAILABLE_IN_1_12
  199. char *nm_utils_tc_action_to_str (NMTCAction *action, GError **error);
  200. NM_AVAILABLE_IN_1_12
  201. NMTCTfilter *nm_utils_tc_tfilter_from_str (const char *str, GError **error);
  202. NM_AVAILABLE_IN_1_12
  203. char *nm_utils_tc_tfilter_to_str (NMTCTfilter *tfilter, GError **error);
  204. /*****************************************************************************/
  205. NM_AVAILABLE_IN_1_14
  206. char *nm_utils_sriov_vf_to_str (const NMSriovVF *vf, gboolean omit_index, GError **error);
  207. NM_AVAILABLE_IN_1_14
  208. NMSriovVF *nm_utils_sriov_vf_from_str (const char *str, GError **error);
  209. /*****************************************************************************/
  210. NM_AVAILABLE_IN_1_12
  211. gint64 nm_utils_get_timestamp_msec (void);
  212. G_END_DECLS
  213. #endif /* __NM_UTILS_H__ */