2 * Network Configuration Module
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/ioctl.h>
23 #include <arpa/inet.h>
25 #include <sys/utsname.h>
27 #include <vconf-keys.h>
32 static gboolean netconfig_is_emulated = FALSE;
34 static gboolean __netconfig_emulator_test_emulation_env(void)
37 const char *EMUL_UTSNAME_MACHINE_SUFFIX = "emulated";
39 DBG("Test emulation environment");
43 if (g_str_has_suffix(buf.machine, EMUL_UTSNAME_MACHINE_SUFFIX) == TRUE)
49 static void __netconfig_emulator_set_ip(void)
51 const int BUF_LEN_MAX = 255;
52 const char EMUL_IFNAME[] = "eth0";
57 if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
58 ERR("Failed to open socket");
62 memset(&ifr, 0, sizeof(struct ifreq));
63 g_strlcpy((char*)&ifr.ifr_name, EMUL_IFNAME, sizeof(EMUL_IFNAME));
65 if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
66 ERR("Error getting IP address");
72 g_strlcpy(ip, (char*)inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr), BUF_LEN_MAX);
74 vconf_set_str(VCONFKEY_NETWORK_IP, ip);
79 static void __netconfig_emulator_set_proxy(void)
81 const char HTTP_PROXY[] = "http_proxy";
84 proxy = getenv(HTTP_PROXY);
85 DBG("Get system proxy: %s", proxy);
88 vconf_set_str(VCONFKEY_NETWORK_PROXY, proxy);
91 static void __netconfig_emulator_set_network_state(void)
93 vconf_set_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
94 vconf_set_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
95 vconf_set_int(VCONFKEY_DNET_STATE, VCONFKEY_DNET_OFF);
98 static void __netconfig_emulator_config_emul_env(void)
100 __netconfig_emulator_set_ip();
101 __netconfig_emulator_set_proxy();
102 __netconfig_emulator_set_network_state();
105 gboolean netconfig_emulator_is_emulated(void)
107 return netconfig_is_emulated;
110 void netconfig_emulator_test_and_start(void)
112 netconfig_is_emulated = __netconfig_emulator_test_emulation_env();
114 DBG("Emulation environment tested: %s", netconfig_is_emulated ? "It's emulated" : "Not emulated");
116 if (netconfig_is_emulated == TRUE)
117 __netconfig_emulator_config_emul_env();