1    	/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2    	/* NetworkManager -- Network link manager
3    	 *
4    	 * This program is free software; you can redistribute it and/or modify
5    	 * it under the terms of the GNU General Public License as published by
6    	 * the Free Software Foundation; either version 2 of the License, or
7    	 * (at your option) any later version.
8    	 *
9    	 * This program is distributed in the hope that it will be useful,
10   	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   	 * GNU General Public License for more details.
13   	 *
14   	 * You should have received a copy of the GNU General Public License along
15   	 * with this program; if not, write to the Free Software Foundation, Inc.,
16   	 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17   	 *
18   	 * Copyright (C) 2005 - 2011 Red Hat, Inc.
19   	 * Copyright (C) 2006 - 2008 Novell, Inc.
20   	 */
21   	
22   	#include <config.h>
23   	#include <sys/stat.h>
24   	#include <stdio.h>
25   	#include <string.h>
26   	#include <glib.h>
27   	
28   	#include "wifi-utils.h"
29   	#include "wifi-utils-private.h"
30   	#include "wifi-utils-nl80211.h"
31   	#if HAVE_WEXT
32   	#include "wifi-utils-wext.h"
33   	#endif
34   	
35   	gpointer
36   	wifi_data_new (const char *iface, int ifindex, gsize len)
37   	{
38   		WifiData *data;
39   	
40   		data = g_malloc0 (len);
41   		data->iface = g_strdup (iface);
42   		data->ifindex = ifindex;
43   		return data;
44   	}
45   	
46   	void
47   	wifi_data_free (WifiData *data)
48   	{
49   		g_free (data->iface);
50   		memset (data, 0, sizeof (*data));
51   		g_free (data);
52   	}
53   	
54   	/***************************************************************/
55   	
56   	WifiData *
57   	wifi_utils_init (const char *iface, int ifindex, gboolean check_scan)
58   	{
59   		WifiData *ret;
60   	
61   		g_return_val_if_fail (iface != NULL, NULL);
62   		g_return_val_if_fail (ifindex > 0, NULL);
63   	
64   		ret = wifi_nl80211_init (iface, ifindex);
65   		if (ret == NULL) {
66   	#if HAVE_WEXT
67   			ret = wifi_wext_init (iface, ifindex, check_scan);
68   	#endif
69   		}
70   		return ret;
71   	}
72   	
73   	NMDeviceWifiCapabilities
74   	wifi_utils_get_caps (WifiData *data)
75   	{
76   		g_return_val_if_fail (data != NULL, NM_WIFI_DEVICE_CAP_NONE);
77   	
78   		return data->caps;	
79   	}
80   	
81   	gboolean
82   	wifi_utils_can_scan_ssid (WifiData *data)
83   	{
84   		g_return_val_if_fail (data != NULL, FALSE);
85   		return data->can_scan_ssid;
86   	}
87   	
88   	NM80211Mode
89   	wifi_utils_get_mode (WifiData *data)
90   	{
91   		g_return_val_if_fail (data != NULL, NM_802_11_MODE_UNKNOWN);
92   		return data->get_mode (data);
93   	}
94   	
95   	gboolean
96   	wifi_utils_set_mode (WifiData *data, const NM80211Mode mode)
97   	{
98   		g_return_val_if_fail (data != NULL, FALSE);
99   		g_return_val_if_fail (   (mode == NM_802_11_MODE_INFRA)
100  		                      || (mode == NM_802_11_MODE_AP)
101  		                      || (mode == NM_802_11_MODE_ADHOC), FALSE);
102  	
103  		/* nl80211 probably doesn't need this */
104  		return data->set_mode ? data->set_mode (data, mode) : TRUE;
105  	}
106  	
107  	guint32
108  	wifi_utils_get_freq (WifiData *data)
109  	{
110  		g_return_val_if_fail (data != NULL, 0);
111  		return data->get_freq (data);
112  	}
113  	
114  	guint32
115  	wifi_utils_find_freq (WifiData *data, const guint32 *freqs)
116  	{
117  		g_return_val_if_fail (data != NULL, 0);
118  		g_return_val_if_fail (freqs != NULL, 0);
119  		return data->find_freq (data, freqs);
120  	}
121  	
122  	GByteArray *
123  	wifi_utils_get_ssid (WifiData *data)
124  	{
125  		g_return_val_if_fail (data != NULL, NULL);
126  		return data->get_ssid (data);
127  	}
128  	
129  	gboolean
130  	wifi_utils_get_bssid (WifiData *data, struct ether_addr *out_bssid)
131  	{
132  		g_return_val_if_fail (data != NULL, FALSE);
133  		g_return_val_if_fail (out_bssid != NULL, FALSE);
134  	
135  		memset (out_bssid, 0, sizeof (*out_bssid));
136  		return data->get_bssid (data, out_bssid);
137  	}
138  	
139  	guint32
140  	wifi_utils_get_rate (WifiData *data)
141  	{
142  		g_return_val_if_fail (data != NULL, 0);
143  		return data->get_rate (data);
144  	}
145  	
146  	int
147  	wifi_utils_get_qual (WifiData *data)
148  	{
149  		g_return_val_if_fail (data != NULL, 0);
150  		return data->get_qual (data);
151  	}
152  	
153  	void
154  	wifi_utils_deinit (WifiData *data)
155  	{
156  		g_return_if_fail (data != NULL);
157  		data->deinit (data);
158  		wifi_data_free (data);
159  	}
160  	
161  	gboolean
162  	wifi_utils_is_wifi (const char *iface, const char *sysfs_path)
163  	{
164  		char phy80211_path[255];
165  		struct stat s;
166  	
167  		g_return_val_if_fail (iface != NULL, FALSE);
168  	
169  		if (sysfs_path) {
170  			/* Check for nl80211 sysfs paths */
171  			snprintf (phy80211_path, sizeof (phy80211_path), "%s/phy80211", sysfs_path);
172  			if ((stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR)))
173  				return TRUE;
174  		}
175  	
176  		if (wifi_nl80211_is_wifi (iface))
177  			return TRUE;
178  	
179  	#if HAVE_WEXT
180  		if (wifi_wext_is_wifi (iface))
181  			return TRUE;
182  	#endif
183  	
184  		return FALSE;
185  	}
186  	
187  	
188  	/* OLPC Mesh-only functions */
189  	
190  	guint32
191  	wifi_utils_get_mesh_channel (WifiData *data)
192  	{
193  		g_return_val_if_fail (data != NULL, FALSE);
194  		g_return_val_if_fail (data->get_mesh_channel != NULL, FALSE);
195  		return data->get_mesh_channel (data);
196  	}
197  	
198  	gboolean
199  	wifi_utils_set_mesh_channel (WifiData *data, guint32 channel)
200  	{
201  		g_return_val_if_fail (data != NULL, FALSE);
(1) Event unsigned_compare: This greater-than-or-equal-to-zero comparison of an unsigned value is always true. "channel >= 0U".
202  		g_return_val_if_fail (channel >= 0, FALSE);
203  		g_return_val_if_fail (channel <= 13, FALSE);
204  		g_return_val_if_fail (data->set_mesh_channel != NULL, FALSE);
205  		return data->set_mesh_channel (data, channel);
206  	}
207  	
208  	gboolean
209  	wifi_utils_set_mesh_ssid (WifiData *data, const GByteArray *ssid)
210  	{
211  		g_return_val_if_fail (data != NULL, FALSE);
212  		g_return_val_if_fail (data->set_mesh_ssid != NULL, FALSE);
213  		return data->set_mesh_ssid (data, ssid);
214  	}
215  	
216