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