Merge "Add dbus method for getting wifi passphrase" into tizen
[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 #include "netdbus.h"
33
34 static gboolean netconfig_is_emulated = FALSE;
35
36 static gboolean __netconfig_emulator_test_emulation_env(void)
37 {
38         int ret;
39         char *model = NULL;
40
41         DBG("Test emulation environment");
42
43         ret = system_info_get_platform_string("tizen.org/system/model_name", &model);
44         if (ret != SYSTEM_INFO_ERROR_NONE) {
45                 ERR("Failed to get system information(%d)", ret);
46                 return FALSE;
47         }
48
49         if (model && strncmp(model, "Emulator", strlen("Emulator")) == 0) {
50                 free(model);
51                 return TRUE;
52         }
53
54         if (model)
55                 free(model);
56
57         return FALSE;
58 }
59
60 static void __netconfig_emulator_set_ip(void)
61 {
62         const char EMUL_IFNAME[] = "eth0";
63         char ip[30] = { 0, };
64         int sockfd = 0;
65         struct ifreq ifr;
66         GVariantBuilder *builder;
67         GVariant *params;
68
69         sockfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
70         if (sockfd < 0) {
71                 ERR("Failed to open socket");
72                 return;
73         }
74
75         memset(&ifr, 0, sizeof(struct ifreq));
76         g_strlcpy((char *)ifr.ifr_name, EMUL_IFNAME, 16);
77
78         if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
79                 ERR("Failed to get IP address");
80
81                 close(sockfd);
82                 return;
83         }
84
85         close(sockfd);
86
87         g_strlcpy(ip,
88                         inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), 30);
89
90         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip, TRUE);
91
92         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
93         g_variant_builder_add(builder, "{sv}", "IPv4Address",
94                                                   g_variant_new_string(ip));
95
96         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
97         g_variant_builder_unref(builder);
98
99         netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
100                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged", params);
101 }
102
103 static void __netconfig_emulator_set_proxy(void)
104 {
105         const char HTTP_PROXY[] = "http_proxy";
106         char *proxy = NULL;
107         GVariantBuilder *builder;
108         GVariant *params;
109
110         proxy = netconfig_get_env(HTTP_PROXY);
111         DBG("Get system proxy: %s", proxy);
112
113         if (proxy != NULL) {
114                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy, TRUE);
115
116                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
117                 g_variant_builder_add(builder, "{sv}", "ProxyAddress",
118                                                           g_variant_new_string(proxy));
119
120                 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
121                 g_variant_builder_unref(builder);
122
123                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
124                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
125                                                    params);
126
127                 free(proxy);
128         }
129 }
130
131 static void __netconfig_emulator_set_network_state(void)
132 {
133         GVariantBuilder *builder;
134         GVariant *params;
135
136         netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1, TRUE);
137         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET, TRUE);
138         netconfig_set_vconf_int(VCONFKEY_DNET_STATE, VCONFKEY_DNET_NORMAL_CONNECTED, TRUE);
139
140         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
141
142         g_variant_builder_add(builder, "{sv}", "NetworkStatus",
143                                           g_variant_new_int32(VCONFKEY_NETWORK_ETHERNET));
144
145         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
146         g_variant_builder_unref(builder);
147
148         netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
149                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged", params);
150 }
151
152 static void __netconfig_emulator_config_emul_env(void)
153 {
154         __netconfig_emulator_set_ip();
155         __netconfig_emulator_set_proxy();
156         __netconfig_emulator_set_network_state();
157 }
158
159 gboolean emulator_is_emulated(void)
160 {
161         return netconfig_is_emulated;
162 }
163
164 void emulator_test_and_start(void)
165 {
166         netconfig_is_emulated = __netconfig_emulator_test_emulation_env();
167
168         DBG("Emulation environment tested: %s", netconfig_is_emulated ? "It's emulated" : "Not emulated");
169
170         if (netconfig_is_emulated == TRUE)
171                 __netconfig_emulator_config_emul_env();
172 }