nm-utils.h 6.66 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 3 4
/* NetworkManager -- Network link manager
 *
 * Ray Strode <rstrode@redhat.com>
5 6
 * Dan Williams <dcbw@redhat.com>
 * Tambet Ingo <tambet@gmail.com>
7
 *
8 9 10 11
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
17
 *
18 19 20 21
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
22
 *
23
 * (C) Copyright 2005 - 2010 Red Hat, Inc.
24 25 26 27 28 29 30 31
 */

#ifndef NM_UTILS_H
#define NM_UTILS_H

#include <glib.h>
#include <execinfo.h>

32 33
#include "nm-connection.h"

34 35
G_BEGIN_DECLS

36 37 38 39
/*********************************************************/
/* The API defined here is _NOT_ guaranteed in any way!! */
/*********************************************************/

Dan Williams's avatar
Dan Williams committed
40 41 42 43 44
/**
 * nm_print_backtrace:
 *
 * Prints a backtrace of the calling process to the logging location.
 */
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#define nm_print_backtrace()						\
G_STMT_START								\
{									\
	void *_call_stack[512];						\
	int  _call_stack_size;						\
	char **_symbols;						\
	_call_stack_size = backtrace (_call_stack,			\
				      G_N_ELEMENTS (_call_stack));	\
	_symbols = backtrace_symbols (_call_stack, _call_stack_size);	\
	if (_symbols != NULL)						\
	{								\
		int _i;							\
		_i = 0;							\
		g_critical ("traceback:\n");				\
		while (_i < _call_stack_size)				\
		{							\
			g_critical ("\t%s\n", _symbols[_i]);		\
			_i++;						\
		}							\
		free (_symbols);					\
	}								\
}									\
G_STMT_END

Dan Williams's avatar
Dan Williams committed
69 70 71 72 73 74
/**
 * nm_get_timestamp:
 * @timestamp: location in which to place the current timestamp
 *
 * For debugging only.
 */
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#define nm_get_timestamp(timestamp)					\
G_STMT_START								\
{									\
	GTimeVal _tv;							\
	g_get_current_time (&_tv);					\
	*timestamp = (_tv.tv_sec * (1.0 * G_USEC_PER_SEC) +		\
		      _tv.tv_usec) / G_USEC_PER_SEC;			\
}									\
G_STMT_END

#define nm_info(fmt, args...)						\
G_STMT_START								\
{									\
	g_message ("<info>  " fmt "\n", ##args);			\
} G_STMT_END

#define nm_info_str(fmt_str, args...)						\
G_STMT_START								\
{									\
	g_message ("<info>  %s\n", fmt_str, ##args);			\
} G_STMT_END

#define nm_debug(fmt, args...)						\
G_STMT_START								\
{									\
	gdouble _timestamp;						\
	nm_get_timestamp (&_timestamp);					\
	g_debug ("<debug> [%f] %s(): " fmt "\n", _timestamp,	\
		 G_STRFUNC, ##args);				\
} G_STMT_END

#define nm_debug_str(fmt_str, args...)						\
G_STMT_START								\
{									\
	gdouble _timestamp;						\
	nm_get_timestamp (&_timestamp);					\
	g_debug ("<debug> [%f] %s(): %s\n", _timestamp,	\
		 G_STRFUNC, fmt_str, ##args);				\
} G_STMT_END

#define nm_warning(fmt, args...)					\
G_STMT_START								\
{									\
	g_warning ("<WARN>  %s(): " fmt "\n", 			\
		   G_STRFUNC, ##args);			\
} G_STMT_END

#define nm_warning_str(fmt_str, args...)					\
G_STMT_START								\
{									\
	g_warning ("<WARN>  %s(): %s\n", 			\
		   G_STRFUNC, fmt_str, ##args);			\
} G_STMT_END

#define nm_error(fmt, args...)						\
G_STMT_START								\
{									\
	gdouble _timestamp;						\
	nm_get_timestamp (&_timestamp);					\
	g_critical ("<ERROR>\t[%f] %s (): " fmt "\n", _timestamp,	\
		    G_STRFUNC, ##args);			\
	nm_print_backtrace ();						\
	G_BREAKPOINT ();						\
} G_STMT_END

#define nm_error_str(fmt_str, args...)						\
G_STMT_START								\
{									\
	gdouble _timestamp;						\
	nm_get_timestamp (&_timestamp);					\
	g_critical ("<ERROR>\t[%f] %s (): %s\n", _timestamp,	\
		    G_STRFUNC, fmt_str, ##args);			\
	nm_print_backtrace ();						\
	G_BREAKPOINT ();						\
} G_STMT_END

151 152 153 154
/* init, deinit nm_utils */
gboolean nm_utils_init (GError **error);
void     nm_utils_deinit (void);

155
/* SSID helpers */
156
gboolean    nm_utils_is_empty_ssid    (const guint8 * ssid, int len);
157 158 159 160
const char *nm_utils_escape_ssid      (const guint8 *ssid, guint32 len);
gboolean    nm_utils_same_ssid        (const GByteArray * ssid1,
							    const GByteArray * ssid2,
							    gboolean ignore_trailing_null);
161

162
char *nm_utils_ssid_to_utf8 (const GByteArray *ssid);
163

164
GHashTable *nm_utils_gvalue_hash_dup  (GHashTable *hash);
165

166 167 168
void        nm_utils_slist_free       (GSList *list,
							    GDestroyNotify elem_destroy_fn);

169 170 171 172 173 174 175 176 177
typedef enum {
	NMU_SEC_INVALID = 0,
	NMU_SEC_NONE,
	NMU_SEC_STATIC_WEP,
	NMU_SEC_LEAP,
	NMU_SEC_DYNAMIC_WEP,
	NMU_SEC_WPA_PSK,
	NMU_SEC_WPA_ENTERPRISE,
	NMU_SEC_WPA2_PSK,
178
	NMU_SEC_WPA2_ENTERPRISE
179 180 181
} NMUtilsSecurityType;

gboolean nm_utils_security_valid (NMUtilsSecurityType type,
182
                                  guint32 wifi_caps,
183
                                  gboolean have_ap,
184
                                  gboolean adhoc,
185 186 187 188
                                  guint32 ap_flags,
                                  guint32 ap_wpa,
                                  guint32 ap_rsn);

189 190 191
GSList *nm_utils_ip4_addresses_from_gvalue (const GValue *value);
void nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value);

192 193 194
GSList *nm_utils_ip4_routes_from_gvalue (const GValue *value);
void nm_utils_ip4_routes_to_gvalue (GSList *list, GValue *value);

Dan Williams's avatar
Dan Williams committed
195 196
guint32 nm_utils_ip4_netmask_to_prefix (guint32 netmask);
guint32 nm_utils_ip4_prefix_to_netmask (guint32 prefix);
197
guint32 nm_utils_ip4_get_default_prefix (guint32 ip);
198

199 200 201 202 203 204 205 206 207
GSList *nm_utils_ip6_addresses_from_gvalue (const GValue *value);
void nm_utils_ip6_addresses_to_gvalue (GSList *list, GValue *value);

GSList *nm_utils_ip6_routes_from_gvalue (const GValue *value);
void nm_utils_ip6_routes_to_gvalue (GSList *list, GValue *value);

GSList *nm_utils_ip6_dns_from_gvalue (const GValue *value);
void nm_utils_ip6_dns_to_gvalue (GSList *list, GValue *value);

208 209 210
char *nm_utils_uuid_generate (void);
char *nm_utils_uuid_generate_from_string (const char *s);

211 212 213 214 215
GByteArray *nm_utils_rsa_key_encrypt (const GByteArray *data,
                                      const char *in_password,
                                      char **out_password,
                                      GError **error);

216 217
G_END_DECLS

218 219 220 221 222
guint32 nm_utils_wifi_freq_to_channel (guint32 freq);
guint32 nm_utils_wifi_channel_to_freq (guint32 channel, const char *band);
guint32 nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band);
gboolean nm_utils_wifi_is_channel_valid (guint32 channel, const char *band);

223
#endif /* NM_UTILS_H */