summaryrefslogtreecommitdiff
path: root/src/wifi-networkmanager.c
blob: cd1d70928f2cbe8ff0840e0f830e11841d58f853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include <wifi-plugin.h>
#include <wifi-networkmanager.h>

#define NETCONN_LOCAL_MAC "00:00:00:00:00:00" /**< So called, link local MAC address */
#define PROC_ARP_PATH "/proc/net/arp" /**< ARP table in linux */

connection_h h_conn = NULL;
nm_network_type network_type = NM_NETWORK_TYPE_UNKNOWN;
connection_profile_iterator_h profile_iter = NULL;

static char connected_bssid[MAX_SIZE_BUFFER];

bool __nm_init()
{
	FUNC_ENTER;
	int ret;

	if (h_conn != NULL) {
		UA_WIFI_ERR("h_conn was already created!!!");
		FUNC_EXIT;
		return false;
	}

	ret = connection_create(&h_conn);

	if (ret != CONNECTION_ERROR_NONE) {
		UA_WIFI_ERR("connection_create is fail");
		h_conn = NULL;
		FUNC_EXIT;
		return false;
	}

	__nm_update_network_type(true);

	FUNC_EXIT;
	return true;
}

void __nm_deinit()
{
	FUNC_ENTER;
	if (h_conn == NULL) {
		UA_WIFI_INFO("h_conn is NULL");
		return;
	}

	if (connection_destroy(h_conn) != CONNECTION_ERROR_NONE) {
		UA_WIFI_INFO("Failed to destroy connection");
		return;
	}

	UA_WIFI_INFO("Connection destroyed successfully");
	h_conn = NULL;
	FUNC_EXIT;
	return;

}

bool __nm_get_interface_name(char **iface_name)
{
	FUNC_ENTER;
	bool ret = true;

	if (network_type == NM_NETWORK_TYPE_WIFI) {
		UA_WIFI_INFO("currently network status WIFI");
		*iface_name = strdup("wlan0");
	} else if (network_type == NM_NETWORK_TYPE_ETHERNET) {
		UA_WIFI_INFO("currently network status Wired");
		*iface_name = strdup("eth0");
	} else {
		UA_WIFI_INFO("No Network or Network status not available");
		FUNC_EXIT;
		return false;
	}

	FUNC_EXIT;
	return ret;
}

connection_type_e __nm_get_connection_type()
{
	FUNC_ENTER;
	connection_type_e connection_type = CONNECTION_TYPE_DISCONNECTED;

	if (connection_get_type(h_conn, &connection_type) != CONNECTION_ERROR_NONE)
		UA_WIFI_ERR("connection_get_type error");

	UA_WIFI_INFO("connection get type %d", connection_type);
	FUNC_EXIT;
	return connection_type;
}

void __nm_get_connection_profile_handler(connection_profile_h *profile)
{
	FUNC_ENTER;

	/* TODO: Need to check if hconnection can be use instead of connection
	 * So, There should not be need of connection_create if m_hconnection is not NULL.
	 */
	connection_h connection = NULL;
	connection_profile_h profile_handlelocal;
	connection_profile_type_e profile_type;

	int ret = -1;

	ret = connection_create(&connection);

	UA_WIFI_INFO("ret: %d, m_Connection: %p", ret, connection);

	if (!connection) {
		UA_WIFI_INFO("connection is NULL");
		return;
	}

	ret = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED,
		&profile_iter);
	if (CONNECTION_ERROR_NONE != ret)
		UA_WIFI_INFO("Failed to get connection profile iterator");


	while (connection_profile_iterator_has_next(profile_iter)) {

		ret = connection_profile_iterator_next(profile_iter, &profile_handlelocal);
		if (CONNECTION_ERROR_NONE != ret) {
			UA_WIFI_INFO("failed to get connection profile iterator");
			break;
		}

		if (connection_profile_get_type(profile_handlelocal, &profile_type)) {
			UA_WIFI_INFO("failed to get profile get type");
			break;
		}

		UA_WIFI_INFO("Profile Type: [%d]", profile_type);

		if ((profile_type == CONNECTION_PROFILE_TYPE_ETHERNET) ||
			(profile_type == CONNECTION_PROFILE_TYPE_WIFI)) {
			*profile = profile_handlelocal;
			UA_WIFI_INFO("Successfully obtained connection profile handle");
			break;
		}
	}
	FUNC_EXIT;
}

void __nm_destroy_profile_iterator()
{
	FUNC_ENTER;

	if (profile_iter) {
		if (connection_destroy_profile_iterator(profile_iter) == CONNECTION_ERROR_NONE) {
			profile_iter = NULL;
			UA_WIFI_DBG("Destroyed profile iterator");
		} else
			UA_WIFI_DBG("Failed to Destroy profile iterator");
	}

	FUNC_EXIT;
}

bool __nm_get_gateway_address(char *gateway)
{
	FUNC_ENTER;
	int ret = -1;
	char *gateway_local = NULL;
	connection_profile_h profile;

	__nm_get_connection_profile_handler(&profile);

	if (profile) {
		ret = connection_profile_get_gateway_address(profile,
			CONNECTION_ADDRESS_FAMILY_IPV4, &gateway_local);

		if (ret == CONNECTION_ERROR_NONE) {
			g_strlcpy(gateway, gateway_local, IP_ADDRESS_STRING_SIZE);
			free(gateway_local);
		} else {
			UA_WIFI_ERR("get gateway failed");
			g_strlcpy(gateway, "0.0.0.0", IP_ADDRESS_STRING_SIZE);
		}
	} else {
		UA_WIFI_ERR("Failed to get profile handle !!!");
		g_strlcpy(gateway, "0.0.0.0", IP_ADDRESS_STRING_SIZE);
	}

	UA_WIFI_INFO("gateway is [%s]", gateway);
	__nm_destroy_profile_iterator();
	return true;
}

void __nm_get_linux_arp_table(char *ip_addr, char *mac_addr)
{
	FUNC_ENTER;

	FILE *fp = NULL;
	char data[1024];
	int matches;

	ret_if(NULL == ip_addr);
	ret_if(NULL == mac_addr);

	fp = fopen(PROC_ARP_PATH, "r");

	if (!fp) {
		UA_WIFI_ERR("fail to read %s", PROC_ARP_PATH);
		FUNC_EXIT;
		return;
	}

	if (!fgets(data, sizeof(data), fp)) {
		UA_WIFI_ERR("failed to read arp table");
		fclose(fp);
		FUNC_EXIT;
		return;
	}

	while (fgets(data, sizeof(data) - 1, fp)) {
		nm_linux_arp_entry entry;
		matches = sscanf(data, "%128s%128s%128s%128s%128s%128s",
				entry.ip_addr, entry.hw_type, entry.flags, entry.hw_addr,
				entry.mask, entry.device);

		if ((matches == 6) && !strcmp(ip_addr, entry.ip_addr)) {
			UA_WIFI_INFO("IP[%s] found in ARP table", ip_addr);
			/*
			 * It is gurranteed that mac_addr is same  to entry.hw_addr
			 * For instance,
			 *  char macaddr[MAX_SIZE_BUFFER];
			 *  typedef struct {
			 *     char ip_addr[MAX_SIZE_BUFFER];
			 *     char hw_type[MAX_SIZE_BUFFER];
			 *     char flags[MAX_SIZE_BUFFER];
			 *     char hw_addr[MAX_SIZE_BUFFER];
			 *     char mask[MAX_SIZE_BUFFER];
			 *     char device[MAX_SIZE_BUFFER];
			 * } nm_linux_arp_entry;
			 */
			g_strlcpy(mac_addr, entry.hw_addr, MAX_SIZE_BUFFER);
			fclose(fp);
			FUNC_EXIT;
			return;
		}
	}

	UA_WIFI_ERR("IP[%s] not found in ARP table", ip_addr);
	fclose(fp);
	FUNC_EXIT;
	return;
}

void __nm_get_ips_mac(char *ip_addr, char *mac_addr)
{
	FUNC_ENTER;

	UA_WIFI_INFO("IP address of GetIPsMAC: %s", ip_addr);

	ret_if(NULL == ip_addr);
	ret_if(NULL == mac_addr);

	/*
	 * It is gurranteed that mac_addr is greater than NETCONN_LOCAL_MAC
	 * For instance,
	 *  char macaddr[MAX_SIZE_BUFFER];
	 *  #define NETCONN_LOCAL_MAC "00:00:00:00:00:00"
	 */
	g_strlcpy(mac_addr, NETCONN_LOCAL_MAC, sizeof(NETCONN_LOCAL_MAC));

	/*! local IP has a wildcard MAC address, for now at least */
	if (!strcmp(ip_addr, "127.0.0.1"))
		UA_WIFI_INFO("local IP[%s]", ip_addr);
	else
		__nm_get_linux_arp_table(ip_addr, mac_addr);

	FUNC_EXIT;
	return;
}

void __nm_update_network_type(bool isConnect)
{
	FUNC_ENTER;

	if (!isConnect)
		network_type = NM_NETWORK_TYPE_DISCONNECTED;
	else {
		connection_type_e eType = __nm_get_connection_type();

		switch (eType) {
		case CONNECTION_TYPE_ETHERNET:
			network_type = NM_NETWORK_TYPE_ETHERNET;
			break;
		case CONNECTION_TYPE_WIFI:
			network_type = NM_NETWORK_TYPE_WIFI;
			break;
		default:
			network_type = NM_NETWORK_TYPE_UNKNOWN;
			break;
		}
	}

	UA_WIFI_INFO("isConnect : %d, network_type : %d", isConnect, network_type);

	if (isConnect) {
		UA_WIFI_INFO("update network status %s", isConnect ? "CONNECT" : "DISCONNECT");
		char gateway[IP_ADDRESS_STRING_SIZE];
		char macaddr[MAX_SIZE_BUFFER];

		memset(gateway, 0, IP_ADDRESS_STRING_SIZE);

		if (__nm_get_gateway_address(gateway) == true)
			UA_WIFI_INFO("currently gateway address : %s", gateway);


		__nm_get_ips_mac(gateway, macaddr);

		UA_WIFI_INFO("IPv4 mac address of the resource: %s", macaddr);

		g_strlcpy(connected_bssid, macaddr, MAX_SIZE_BUFFER);
	} else {
		UA_WIFI_INFO("update network status %s", isConnect ? "CONNECT" : "DISCONNECT");
		memset(connected_bssid, 0, sizeof(connected_bssid));
	}

	FUNC_EXIT;
}

bool __nm_get_mac_address(char **mac_addr)
{
	FUNC_ENTER;
	connection_type_e conn_type;

	if (!h_conn) {
		UA_WIFI_ERR("Connection is not created");
		return false;
	}

	if (network_type == NM_NETWORK_TYPE_WIFI) {
		UA_WIFI_INFO("Currently Network Status WIFI");
		conn_type = CONNECTION_TYPE_WIFI;
	} else if (network_type == NM_NETWORK_TYPE_ETHERNET) {
		UA_WIFI_INFO("Currently Network Status Wired");
		conn_type = CONNECTION_TYPE_ETHERNET;
	} else {
		UA_WIFI_INFO("No Network or Network status not available");
		FUNC_EXIT;
		return false;
	}

	if (connection_get_mac_address(h_conn, conn_type, mac_addr) != CONNECTION_ERROR_NONE) {
		UA_WIFI_ERR("Fail to get MAC address");
		FUNC_EXIT;
		return false;
	}

	FUNC_EXIT;
	return true;
}

char* __nm_get_ip_address(void)
{
	char *ip_addr = NULL;
	int ret = connection_get_ip_address(h_conn, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
	if ( CONNECTION_ERROR_NONE != ret) {
		UA_WIFI_ERR("Error in getting ip address");

		if (NULL != ip_addr) {
			free(ip_addr);
			ip_addr = NULL;
		}

		return NULL;
	}

//	UA_WIFI_DBG("Connected AP's  IP : %s", ip_addr);
	return ip_addr;
}

void __nm_get_connected_bssid(char** connect_bssid)
{
	if (connect_bssid == NULL)
		return;

	*connect_bssid = g_strdup(connected_bssid);
	return;
}