device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / 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 - 2013 Red Hat, Inc.
19  */
20
21 #ifndef NM_UTILS_H
22 #define NM_UTILS_H
23
24 #include <glib.h>
25
26 #include "nm-connection.h"
27 #include "nm-utils-enum-types.h"
28
29 G_BEGIN_DECLS
30
31 /* init, deinit nm_utils */
32 gboolean nm_utils_init (GError **error);
33 void     nm_utils_deinit (void);
34
35 /* SSID helpers */
36 gboolean    nm_utils_is_empty_ssid (const guint8 *ssid, int len);
37 const char *nm_utils_escape_ssid   (const guint8 *ssid, guint32 len);
38 gboolean    nm_utils_same_ssid     (const GByteArray *ssid1,
39                                     const GByteArray *ssid2,
40                                     gboolean ignore_trailing_null);
41 char *      nm_utils_ssid_to_utf8  (const GByteArray *ssid);
42
43 GHashTable *nm_utils_gvalue_hash_dup  (GHashTable *hash);
44
45 NM_DEPRECATED_IN_0_9_10
46 void        nm_utils_slist_free    (GSList *list, GDestroyNotify elem_destroy_fn);
47
48 /**
49  * NMUtilsSecurityType:
50  * @NMU_SEC_INVALID: unknown or invalid security, placeholder and not used
51  * @NMU_SEC_NONE: unencrypted and open
52  * @NMU_SEC_STATIC_WEP: static WEP keys are used for encryption
53  * @NMU_SEC_LEAP: Cisco LEAP is used for authentication and for generating the
54  * dynamic WEP keys automatically
55  * @NMU_SEC_DYNAMIC_WEP: standard 802.1x is used for authentication and
56  * generating the dynamic WEP keys automatically
57  * @NMU_SEC_WPA_PSK: WPA1 is used with Pre-Shared Keys (PSK)
58  * @NMU_SEC_WPA_ENTERPRISE: WPA1 is used with 802.1x authentication
59  * @NMU_SEC_WPA2_PSK: WPA2/RSN is used with Pre-Shared Keys (PSK)
60  * @NMU_SEC_WPA2_ENTERPRISE: WPA2 is used with 802.1x authentication
61  *
62  * Describes generic security mechanisms that 802.11 access points may offer.
63  * Used with nm_utils_security_valid() for checking whether a given access
64  * point is compatible with a network device.
65  **/
66 typedef enum {
67         NMU_SEC_INVALID = 0,
68         NMU_SEC_NONE,
69         NMU_SEC_STATIC_WEP,
70         NMU_SEC_LEAP,
71         NMU_SEC_DYNAMIC_WEP,
72         NMU_SEC_WPA_PSK,
73         NMU_SEC_WPA_ENTERPRISE,
74         NMU_SEC_WPA2_PSK,
75         NMU_SEC_WPA2_ENTERPRISE
76 } NMUtilsSecurityType;
77
78 gboolean nm_utils_security_valid (NMUtilsSecurityType type,
79                                   NMDeviceWifiCapabilities wifi_caps,
80                                   gboolean have_ap,
81                                   gboolean adhoc,
82                                   NM80211ApFlags ap_flags,
83                                   NM80211ApSecurityFlags ap_wpa,
84                                   NM80211ApSecurityFlags ap_rsn);
85
86 gboolean nm_utils_ap_mode_security_valid (NMUtilsSecurityType type,
87                                           NMDeviceWifiCapabilities wifi_caps);
88
89 gboolean nm_utils_wep_key_valid (const char *key, NMWepKeyType wep_type);
90 gboolean nm_utils_wpa_psk_valid (const char *psk);
91
92 GSList *nm_utils_ip4_addresses_from_gvalue (const GValue *value);
93 void nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value);
94
95 GSList *nm_utils_ip4_routes_from_gvalue (const GValue *value);
96 void nm_utils_ip4_routes_to_gvalue (GSList *list, GValue *value);
97
98 guint32 nm_utils_ip4_netmask_to_prefix (guint32 netmask);
99 guint32 nm_utils_ip4_prefix_to_netmask (guint32 prefix);
100 guint32 nm_utils_ip4_get_default_prefix (guint32 ip);
101
102 GSList *nm_utils_ip6_addresses_from_gvalue (const GValue *value);
103 void nm_utils_ip6_addresses_to_gvalue (GSList *list, GValue *value);
104
105 GSList *nm_utils_ip6_routes_from_gvalue (const GValue *value);
106 void nm_utils_ip6_routes_to_gvalue (GSList *list, GValue *value);
107
108 GSList *nm_utils_ip6_dns_from_gvalue (const GValue *value);
109 void nm_utils_ip6_dns_to_gvalue (GSList *list, GValue *value);
110
111 char *nm_utils_uuid_generate (void);
112 char *nm_utils_uuid_generate_from_string (const char *s);
113
114 GByteArray *nm_utils_rsa_key_encrypt (const GByteArray *data,
115                                       const char *in_password,
116                                       char **out_password,
117                                       GError **error);
118 GByteArray *nm_utils_rsa_key_encrypt_aes (const GByteArray *data,
119                                           const char *in_password,
120                                           char **out_password,
121                                           GError **error);
122 gboolean nm_utils_file_is_pkcs12 (const char *filename);
123
124 typedef gboolean (*NMUtilsFileSearchInPathsPredicate) (const char *filename, gpointer user_data);
125
126 const char *nm_utils_file_search_in_paths (const char *progname,
127                                            const char *try_first,
128                                            const char *const *paths,
129                                            GFileTest file_test_flags,
130                                            NMUtilsFileSearchInPathsPredicate predicate,
131                                            gpointer user_data,
132                                            GError **error);
133
134 guint32 nm_utils_wifi_freq_to_channel (guint32 freq);
135 guint32 nm_utils_wifi_channel_to_freq (guint32 channel, const char *band);
136 guint32 nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band);
137 gboolean nm_utils_wifi_is_channel_valid (guint32 channel, const char *band);
138
139 /**
140  * NM_UTILS_HWADDR_LEN_MAX:
141  *
142  * The maximum length of hardware addresses handled by NetworkManager itself,
143  * nm_utils_hwaddr_len(), and nm_utils_hwaddr_aton().
144  */
145 #define NM_UTILS_HWADDR_LEN_MAX 20 /* INFINIBAND_ALEN */
146
147 int         nm_utils_hwaddr_len   (int type) G_GNUC_PURE;
148 NM_DEPRECATED_IN_0_9_10
149 int         nm_utils_hwaddr_type  (int len) G_GNUC_PURE;
150 char       *nm_utils_hwaddr_ntoa  (gconstpointer addr, int type);
151 GByteArray *nm_utils_hwaddr_atoba (const char *asc, int type);
152 guint8     *nm_utils_hwaddr_aton  (const char *asc, int type, gpointer buffer);
153
154 NM_AVAILABLE_IN_0_9_10
155 char       *nm_utils_hwaddr_ntoa_len  (gconstpointer addr, gsize length);
156 NM_AVAILABLE_IN_0_9_10
157 guint8     *nm_utils_hwaddr_aton_len  (const char *asc, gpointer buffer, gsize length);
158
159 NM_AVAILABLE_IN_0_9_10
160 gboolean    nm_utils_hwaddr_valid (const char *asc);
161
162 NM_AVAILABLE_IN_0_9_10
163 char *nm_utils_bin2hexstr (const char *bytes, int len, int final_len);
164 NM_AVAILABLE_IN_0_9_10
165 int   nm_utils_hex2byte   (const char *hex);
166 NM_AVAILABLE_IN_0_9_10
167 char *nm_utils_hexstr2bin (const char *hex, size_t len);
168
169 gboolean    nm_utils_iface_valid_name(const char *name);
170
171 gboolean nm_utils_is_uuid (const char *str);
172
173 /**
174  * NM_UTILS_INET_ADDRSTRLEN:
175  *
176  * Defines the minimal length for a char buffer that is suitable as @dst argument
177  * for both nm_utils_inet4_ntop() and nm_utils_inet6_ntop().
178  **/
179 #define NM_UTILS_INET_ADDRSTRLEN     INET6_ADDRSTRLEN
180 NM_AVAILABLE_IN_0_9_10
181 const char *nm_utils_inet4_ntop (in_addr_t inaddr, char *dst);
182 NM_AVAILABLE_IN_0_9_10
183 const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst);
184
185 NM_AVAILABLE_IN_0_9_10
186 gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type);
187
188 G_END_DECLS
189
190 #endif /* NM_UTILS_H */