Sync with Tizen 2.4(v1.1.38)
[platform/core/connectivity/net-config.git] / src / utils / emulator.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <vconf.h>
21 #include <net/if.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/ioctl.h>
25 #include <arpa/inet.h>
26 #include <vconf-keys.h>
27 #include <system_info.h>
28
29 #include "log.h"
30 #include "emulator.h"
31 #include "util.h"
32
33 static gboolean netconfig_is_emulated = FALSE;
34
35 static gboolean __netconfig_emulator_test_emulation_env(void)
36 {
37         int ret;
38         char *model = NULL;
39
40         DBG("Test emulation environment");
41
42         ret = system_info_get_platform_string("tizen.org/system/model_name", &model);
43         if (ret != SYSTEM_INFO_ERROR_NONE) {
44                 ERR("Failed to get system information(%d)", ret);
45                 return FALSE;
46         }
47
48         if (model && strncmp(model, "Emulator", strlen("Emulator")) == 0) {
49                 free(model);
50                 return TRUE;
51         }
52
53         if (model)
54                 free(model);
55
56         return FALSE;
57 }
58
59 static void __netconfig_emulator_set_ip(void)
60 {
61         const char EMUL_IFNAME[] = "eth0";
62         char ip[30] = { 0, };
63         int sockfd = 0;
64         struct ifreq ifr;
65
66         sockfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
67         if (sockfd < 0) {
68                 ERR("Failed to open socket");
69                 return;
70         }
71
72         memset(&ifr, 0, sizeof(struct ifreq));
73         g_strlcpy((char *)ifr.ifr_name, EMUL_IFNAME, 16);
74
75         if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
76                 ERR("Failed to get IP address");
77
78                 close(sockfd);
79                 return;
80         }
81
82         close(sockfd);
83
84         g_strlcpy(ip,
85                         inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), 30);
86
87         vconf_set_str(VCONFKEY_NETWORK_IP, ip);
88 }
89
90 static void __netconfig_emulator_set_proxy(void)
91 {
92         const char HTTP_PROXY[] = "http_proxy";
93         char *proxy = NULL;
94
95         proxy = netconfig_get_env(HTTP_PROXY);
96         DBG("Get system proxy: %s", proxy);
97
98         if (proxy != NULL){
99                 vconf_set_str(VCONFKEY_NETWORK_PROXY, proxy);
100                 free(proxy);
101         }
102 }
103
104 static void __netconfig_emulator_set_network_state(void)
105 {
106         vconf_set_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
107         vconf_set_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
108         vconf_set_int(VCONFKEY_DNET_STATE, VCONFKEY_DNET_NORMAL_CONNECTED);
109 }
110
111 static void __netconfig_emulator_config_emul_env(void)
112 {
113         __netconfig_emulator_set_ip();
114         __netconfig_emulator_set_proxy();
115         __netconfig_emulator_set_network_state();
116 }
117
118 gboolean emulator_is_emulated(void)
119 {
120         return netconfig_is_emulated;
121 }
122
123 void emulator_test_and_start(void)
124 {
125         netconfig_is_emulated = __netconfig_emulator_test_emulation_env();
126
127         DBG("Emulation environment tested: %s", netconfig_is_emulated ? "It's emulated" : "Not emulated");
128
129         if (netconfig_is_emulated == TRUE)
130                 __netconfig_emulator_config_emul_env();
131 }