2 * Network Client Library
4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.tizenopensource.org/license
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #endif /* __cplusplus */
26 /*****************************************************************************
28 *****************************************************************************/
35 #include <dbus/dbus.h>
37 #include <sys/types.h>
41 #include <sys/ioctl.h>
43 #include <net/if_arp.h>
44 #include <linux/if_ether.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 #include "network-internal.h"
51 #include "network-dbus-request.h"
53 /*****************************************************************************
55 *****************************************************************************/
57 /*****************************************************************************
58 * Local Functions Declaration
59 *****************************************************************************/
61 /*****************************************************************************
63 *****************************************************************************/
65 /*****************************************************************************
66 * Extern Global Variables
67 *****************************************************************************/
68 extern network_info_t NetworkInfo;
70 /*****************************************************************************
72 *****************************************************************************/
74 /** set all request to FALSE (0) */
75 network_request_table_t request_table[NETWORK_REQUEST_TYPE_MAX] = {{0, }, };
78 pthread_mutex_t callback_mutex;
79 pthread_mutex_t wifi_state_mutex;
82 /*****************************************************************************
83 * Local Functions Definition
84 *****************************************************************************/
86 char *__convert_eap_type_to_string(gchar eap_type)
89 case WLAN_SEC_EAP_TYPE_PEAP:
92 case WLAN_SEC_EAP_TYPE_TLS:
95 case WLAN_SEC_EAP_TYPE_TTLS:
98 case WLAN_SEC_EAP_TYPE_SIM:
101 case WLAN_SEC_EAP_TYPE_AKA:
109 char *__convert_eap_auth_to_string(gchar eap_auth)
112 case WLAN_SEC_EAP_AUTH_NONE:
115 case WLAN_SEC_EAP_AUTH_PAP:
118 case WLAN_SEC_EAP_AUTH_MSCHAP:
121 case WLAN_SEC_EAP_AUTH_MSCHAPV2:
124 case WLAN_SEC_EAP_AUTH_GTC:
127 case WLAN_SEC_EAP_AUTH_MD5:
135 /*****************************************************************************
136 * Global Functions Definition
137 *****************************************************************************/
139 char* _net_print_error(net_err_t error)
144 return "NET_ERR_NONE";
146 /* Common Error value */
149 case NET_ERR_UNKNOWN:
150 return "NET_ERR_UNKNOWN";
152 /* Client Register related Errors used in API return */
154 /** Application is already registered */
155 case NET_ERR_APP_ALREADY_REGISTERED:
156 return "NET_ERR_APP_ALREADY_REGISTERED";
157 /** Application is not registered */
158 case NET_ERR_APP_NOT_REGISTERED:
159 return "NET_ERR_APP_NOT_REGISTERED";
161 /* Connection Related Error */
163 /** No active connection exists for the given profile name */
164 case NET_ERR_NO_ACTIVE_CONNECTIONS:
165 return "NET_ERR_NO_ACTIVE_CONNECTIONS";
166 /** Active connection already exists for the given profile name */
167 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
168 return "NET_ERR_ACTIVE_CONNECTION_EXISTS";
170 /** Connection failure : out of range */
171 case NET_ERR_CONNECTION_OUT_OF_RANGE:
172 return "NET_ERR_CONNECTION_OUT_OF_RANGE";
173 /** Connection failure : pin missing */
174 case NET_ERR_CONNECTION_PIN_MISSING:
175 return "NET_ERR_CONNECTION_PIN_MISSING";
176 /** Connection failure : dhcp failed */
177 case NET_ERR_CONNECTION_DHCP_FAILED:
178 return "NET_ERR_CONNECTION_DHCP_FAILED";
179 /** Connection failure */
180 case NET_ERR_CONNECTION_CONNECT_FAILED:
181 return "NET_ERR_CONNECTION_CONNECT_FAILED";
182 /** Connection failure : login failed */
183 case NET_ERR_CONNECTION_LOGIN_FAILED:
184 return "NET_ERR_CONNECTION_LOGIN_FAILED";
185 /** Connection failure : authentication failed */
186 case NET_ERR_CONNECTION_AUTH_FAILED:
187 return "NET_ERR_CONNECTION_AUTH_FAILED";
188 /** Connection failure : invalid key */
189 case NET_ERR_CONNECTION_INVALID_KEY:
190 return "NET_ERR_CONNECTION_INVALID_KEY";
194 /** Access is denied */
195 case NET_ERR_ACCESS_DENIED:
196 return "NET_ERR_ACCESS_DENIED";
197 /** Operation is in progress */
198 case NET_ERR_IN_PROGRESS:
199 return "NET_ERR_IN_PROGRESS";
200 /** Operation was aborted by client or network*/
201 case NET_ERR_OPERATION_ABORTED:
202 return "NET_ERR_OPERATION_ABORTED";
203 /** Invalid value of API parameter */
204 case NET_ERR_INVALID_PARAM:
205 return "NET_ERR_INVALID_PARAM";
206 /** invalid operation depending on current state */
207 case NET_ERR_INVALID_OPERATION:
208 return "NET_ERR_INVALID_OPERATION";
210 /** Feature not supported */
211 case NET_ERR_NOT_SUPPORTED:
212 return "NET_ERR_NOT_SUPPORTED";
214 case NET_ERR_TIME_OUT:
215 return "NET_ERR_TIME_OUT";
216 /** Network service is not available*/
217 case NET_ERR_NO_SERVICE:
218 return "NET_ERR_NO_SERVICE";
219 /** DBus can't find appropriate method */
220 case NET_ERR_UNKNOWN_METHOD:
221 return "NET_ERR_UNKNOWN_METHOD";
227 net_device_t _net_get_tech_type_from_path(const char *profile_name)
229 __NETWORK_FUNC_ENTER__;
231 net_device_t device_type = NET_DEVICE_UNKNOWN;
233 if (g_str_has_prefix(profile_name, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE)
234 device_type = NET_DEVICE_WIFI;
235 else if (g_str_has_prefix(profile_name, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE)
236 device_type = NET_DEVICE_CELLULAR;
237 else if (g_str_has_prefix(profile_name, CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX) == TRUE)
238 device_type = NET_DEVICE_ETHERNET;
240 __NETWORK_FUNC_EXIT__;
244 char* _net_get_string(DBusMessage* msg)
246 __NETWORK_FUNC_ENTER__;
248 DBusMessageIter args;
249 char* sigvalue = NULL;
251 if (!dbus_message_iter_init(msg, &args)) {
252 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
253 } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
254 NETWORK_LOG(NETWORK_LOW, "Argument is not string\n");
256 dbus_message_iter_get_basic(&args, &sigvalue);
259 __NETWORK_FUNC_EXIT__;
263 unsigned long long _net_get_uint64(DBusMessage* msg)
265 DBusMessageIter args;
266 unsigned long long sigvalue = 0;
268 if (!dbus_message_iter_init(msg, &args)) {
269 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
270 } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_UINT64) {
271 NETWORK_LOG(NETWORK_LOW, "Argument is not uint64\n");
273 dbus_message_iter_get_basic(&args, &sigvalue);
279 char* _net_get_object(DBusMessage* msg)
281 __NETWORK_FUNC_ENTER__;
283 DBusMessageIter args;
284 char* sigvalue = NULL;
286 if (!dbus_message_iter_init(msg, &args)) {
287 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
288 } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
289 NETWORK_LOG(NETWORK_LOW, "Argument is not string\n");
291 dbus_message_iter_get_basic(&args, &sigvalue);
294 __NETWORK_FUNC_EXIT__;
298 int _net_get_boolean(DBusMessage* msg)
300 __NETWORK_FUNC_ENTER__;
302 DBusMessageIter args;
303 dbus_bool_t val = FALSE;
304 int retvalue = FALSE;
306 if (!dbus_message_iter_init(msg, &args)) {
307 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
308 } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_BOOLEAN) {
309 NETWORK_LOG(NETWORK_LOW, "Argument is not boolean\n");
311 dbus_message_iter_get_basic(&args, &val);
319 __NETWORK_FUNC_EXIT__;
323 int _net_get_path(DBusMessage *msg, char *profile_name)
325 __NETWORK_FUNC_ENTER__;
327 char* ProfileName = NULL;
329 ProfileName = (char*)dbus_message_get_path(msg);
330 snprintf(profile_name, strlen(ProfileName) + 1, "%s", ProfileName);
332 __NETWORK_FUNC_EXIT__;
337 int _net_get_tech_state(DBusMessage* msg, network_get_tech_state_info_t* tech_state)
339 __NETWORK_FUNC_ENTER__;
341 net_err_t Error = NET_ERR_NONE;
342 DBusMessageIter args, dict;
344 if (!dbus_message_iter_init(msg, &args)) {
345 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
346 Error = NET_ERR_UNKNOWN;
350 dbus_message_iter_recurse(&args, &dict);
352 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
353 DBusMessageIter key_iter, sub_iter1, sub_iter2;
354 const char *key = NULL;
355 const char *tech_name = NULL;
357 dbus_message_iter_recurse(&dict, &key_iter);
358 dbus_message_iter_get_basic(&key_iter, &key);
360 if (strcmp(key, "AvailableTechnologies") == 0 ||
361 strcmp(key, "EnabledTechnologies") == 0 ||
362 strcmp(key, "ConnectedTechnologies") == 0) {
363 dbus_message_iter_next(&key_iter);
364 dbus_message_iter_recurse(&key_iter, &sub_iter1);
366 if (dbus_message_iter_get_arg_type(&sub_iter1) == DBUS_TYPE_ARRAY)
367 dbus_message_iter_recurse(&sub_iter1, &sub_iter2);
371 while (dbus_message_iter_get_arg_type(&sub_iter2) == DBUS_TYPE_STRING) {
372 dbus_message_iter_get_basic(&sub_iter2, &tech_name);
373 if (tech_name != NULL &&
374 strcmp(tech_name, tech_state->technology) == 0) {
375 if (strcmp(key, "AvailableTechnologies") == 0)
376 tech_state->AvailableTechnology = TRUE;
377 else if (strcmp(key, "EnabledTechnologies") == 0)
378 tech_state->EnabledTechnology = TRUE;
380 tech_state->ConnectedTechnology = TRUE;
383 dbus_message_iter_next(&sub_iter2);
385 } else if (strcmp(key, "DefaultTechnology") == 0) {
386 dbus_message_iter_next(&key_iter);
387 dbus_message_iter_recurse(&key_iter, &sub_iter1);
389 if (dbus_message_iter_get_arg_type(&sub_iter1) == DBUS_TYPE_STRING) {
390 dbus_message_iter_get_basic(&sub_iter1, &tech_name);
391 if (tech_name != NULL && strcmp(tech_name, tech_state->technology) == 0)
392 tech_state->DefaultTechnology = TRUE;
396 dbus_message_iter_next(&dict);
400 __NETWORK_FUNC_EXIT__;
404 /** This function is used only to open Wi-Fi connection with hidden APs */
405 int _net_open_connection_with_wifi_info(const net_wifi_connection_info_t* wifi_info)
407 __NETWORK_FUNC_ENTER__;
409 net_err_t Error = NET_ERR_NONE;
411 net_wifi_connect_service_info_t wifi_connection_info;
412 memset(&wifi_connection_info, 0, sizeof(net_wifi_connect_service_info_t));
414 wifi_connection_info.type = g_strdup("wifi");
416 if (wifi_info->wlan_mode == NETPM_WLAN_CONNMODE_ADHOC)
417 wifi_connection_info.mode = g_strdup("adhoc");
419 wifi_connection_info.mode = g_strdup("managed");
421 wifi_connection_info.ssid = g_strdup(wifi_info->essid);
423 switch (wifi_info->security_info.sec_mode) {
424 case WLAN_SEC_MODE_NONE:
425 wifi_connection_info.security = g_strdup("none");
428 case WLAN_SEC_MODE_WEP:
429 wifi_connection_info.security = g_strdup("wep");
430 wifi_connection_info.passphrase = g_strdup(wifi_info->security_info.authentication.wep.wepKey);
433 /** WPA-PSK(equivalent to WPA-NONE in case of Ad-Hoc) */
434 case WLAN_SEC_MODE_WPA_PSK:
435 wifi_connection_info.security = g_strdup("psk");
436 wifi_connection_info.passphrase = g_strdup(wifi_info->security_info.authentication.psk.pskKey);
440 /** WPA-PSK / WPA2-PSK supported */
441 case WLAN_SEC_MODE_WPA2_PSK:
442 wifi_connection_info.security = g_strdup("rsn");
443 wifi_connection_info.passphrase = g_strdup(wifi_info->security_info.authentication.psk.pskKey);
446 case WLAN_SEC_MODE_IEEE8021X:
447 wifi_connection_info.security = g_strdup("ieee8021x");
449 wifi_connection_info.eap_type = g_strdup(
450 __convert_eap_type_to_string(wifi_info->security_info.authentication.eap.eap_type));
451 wifi_connection_info.eap_auth = g_strdup(
452 __convert_eap_auth_to_string(wifi_info->security_info.authentication.eap.eap_auth));
454 if (wifi_info->security_info.authentication.eap.username != NULL)
455 if (strlen(wifi_info->security_info.authentication.eap.username) > 0)
456 wifi_connection_info.identity = g_strdup(wifi_info->security_info.authentication.eap.username);
458 if (wifi_info->security_info.authentication.eap.password != NULL)
459 if (strlen(wifi_info->security_info.authentication.eap.password) > 0)
460 wifi_connection_info.password = g_strdup(wifi_info->security_info.authentication.eap.password);
462 if (wifi_info->security_info.authentication.eap.ca_cert_filename != NULL)
463 if (strlen(wifi_info->security_info.authentication.eap.ca_cert_filename) > 0)
464 wifi_connection_info.ca_cert_file = g_strdup(wifi_info->security_info.authentication.eap.ca_cert_filename);
466 if (wifi_info->security_info.authentication.eap.client_cert_filename != NULL)
467 if (strlen(wifi_info->security_info.authentication.eap.client_cert_filename) > 0)
468 wifi_connection_info.client_cert_file = g_strdup(wifi_info->security_info.authentication.eap.client_cert_filename);
470 if (wifi_info->security_info.authentication.eap.private_key_filename != NULL)
471 if (strlen(wifi_info->security_info.authentication.eap.private_key_filename) > 0)
472 wifi_connection_info.private_key_file = g_strdup(wifi_info->security_info.authentication.eap.private_key_filename);
474 if (wifi_info->security_info.authentication.eap.private_key_passwd != NULL)
475 if (strlen(wifi_info->security_info.authentication.eap.private_key_passwd) > 0)
476 wifi_connection_info.private_key_password = g_strdup(wifi_info->security_info.authentication.eap.private_key_passwd);
481 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid security type\n");
482 __NETWORK_FUNC_EXIT__;
483 return NET_ERR_INVALID_PARAM;
486 NETWORK_LOG(NETWORK_HIGH,
487 "Parameters: type:\t%s\nmode:\t%s\nssid:\t%s\nsecurity:\t%s\npassphrase:\t%s\n",
488 wifi_connection_info.type, wifi_connection_info.mode,
489 wifi_connection_info.ssid, wifi_connection_info.security,
490 wifi_connection_info.passphrase);
492 if (wifi_info->security_info.sec_mode == WLAN_SEC_MODE_IEEE8021X) {
493 NETWORK_LOG(NETWORK_HIGH,
494 "Wi-Fi Enterprise type:\t%s\nauth:\t%s\nidentity:\t%s\npassword:\t%s\n",
495 wifi_connection_info.eap_type, wifi_connection_info.eap_auth,
496 wifi_connection_info.identity, wifi_connection_info.password);
497 NETWORK_LOG(NETWORK_HIGH,
498 "CA cert:\t%s\nClient cert:\t%s\nPrivate key:\t%s\nPrivate key password:\t%s\n",
499 wifi_connection_info.ca_cert_file, wifi_connection_info.client_cert_file,
500 wifi_connection_info.private_key_file, wifi_connection_info.private_key_password);
503 if ((Error = _net_dbus_connect_service(&wifi_connection_info)) != NET_ERR_NONE)
504 NETWORK_LOG(NETWORK_EXCEPTION, "Failed to request connect service. Error [%s]\n",
505 _net_print_error(Error));
507 NETWORK_LOG(NETWORK_HIGH, "Successfully requested to connect service\n");
509 g_free(wifi_connection_info.type);
510 g_free(wifi_connection_info.mode);
511 g_free(wifi_connection_info.ssid);
512 g_free(wifi_connection_info.security);
513 g_free(wifi_connection_info.passphrase);
514 g_free(wifi_connection_info.eap_type);
515 g_free(wifi_connection_info.eap_auth);
516 g_free(wifi_connection_info.identity);
517 g_free(wifi_connection_info.password);
518 g_free(wifi_connection_info.ca_cert_file);
519 g_free(wifi_connection_info.client_cert_file);
520 g_free(wifi_connection_info.private_key_file);
521 g_free(wifi_connection_info.private_key_password);
523 __NETWORK_FUNC_EXIT__;
527 int _net_mutex_init(void)
529 __NETWORK_FUNC_ENTER__;
531 if (pthread_mutex_init(&networkinfo_mutex.callback_mutex, NULL) != 0) {
532 NETWORK_LOG(NETWORK_ERROR, "Mutex for callback initialization failed!\n");
533 __NETWORK_FUNC_EXIT__;
534 return NET_ERR_UNKNOWN;
537 if (pthread_mutex_init(&networkinfo_mutex.wifi_state_mutex, NULL) != 0) {
538 NETWORK_LOG(NETWORK_ERROR, "Mutex for wifi state initialization failed!\n");
539 pthread_mutex_destroy(&networkinfo_mutex.callback_mutex);
540 __NETWORK_FUNC_EXIT__;
541 return NET_ERR_UNKNOWN;
544 __NETWORK_FUNC_EXIT__;
548 void _net_mutex_destroy(void)
550 __NETWORK_FUNC_ENTER__;
552 pthread_mutex_destroy(&networkinfo_mutex.callback_mutex);
553 pthread_mutex_destroy(&networkinfo_mutex.wifi_state_mutex);
555 __NETWORK_FUNC_EXIT__;
558 void _net_client_callback(net_event_info_t *event_data)
560 pthread_mutex_lock(&networkinfo_mutex.callback_mutex);
561 __NETWORK_FUNC_ENTER__;
563 if (NetworkInfo.ClientEventCb != NULL)
564 NetworkInfo.ClientEventCb(event_data, NetworkInfo.user_data);
566 if (NetworkInfo.ClientEventCb_conn != NULL)
567 NetworkInfo.ClientEventCb_conn(event_data, NetworkInfo.user_data_conn);
569 if (NetworkInfo.ClientEventCb_wifi != NULL)
570 NetworkInfo.ClientEventCb_wifi(event_data, NetworkInfo.user_data_wifi);
572 __NETWORK_FUNC_EXIT__;
573 pthread_mutex_unlock(&networkinfo_mutex.callback_mutex);
576 net_wifi_state_t _net_get_wifi_state(void)
578 pthread_mutex_lock(&networkinfo_mutex.wifi_state_mutex);
579 __NETWORK_FUNC_ENTER__;
581 net_err_t Error = NET_ERR_NONE;
582 network_get_tech_state_info_t tech_state = {{0,},};
583 net_wifi_state_t wifi_state = WIFI_UNKNOWN;
585 snprintf(tech_state.technology, NET_TECH_LENGTH_MAX, "%s", "wifi");
586 Error = _net_dbus_get_technology_state(&tech_state);
587 if (Error != NET_ERR_NONE) {
588 NETWORK_LOG(NETWORK_ERROR,
589 "Error!!! _net_dbus_get_technology_state() failed. Error [%s]\n",
590 _net_print_error(Error));
594 if (tech_state.EnabledTechnology == TRUE &&
595 tech_state.AvailableTechnology == TRUE)
596 wifi_state = WIFI_ON;
598 wifi_state = WIFI_OFF;
601 __NETWORK_FUNC_EXIT__;
602 pthread_mutex_unlock(&networkinfo_mutex.wifi_state_mutex);
606 void _net_clear_request_table(void)
608 __NETWORK_FUNC_ENTER__;
612 for (i = 0;i < NETWORK_REQUEST_TYPE_MAX;i++)
613 memset(&request_table[i], 0, sizeof(network_request_table_t));
615 __NETWORK_FUNC_EXIT__;
620 #endif /* __cplusplus */