Tizen 2.1 base
[platform/core/connectivity/net-config.git] / src / utils / emulator.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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 <stdlib.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <arpa/inet.h>
24 #include <net/if.h>
25 #include <sys/utsname.h>
26 #include <vconf.h>
27 #include <vconf-keys.h>
28
29 #include "log.h"
30 #include "emulator.h"
31
32 static gboolean netconfig_is_emulated = FALSE;
33
34 static gboolean __netconfig_emulator_test_emulation_env(void)
35 {
36         struct utsname buf;
37         const char *EMUL_UTSNAME_MACHINE_SUFFIX = "emulated";
38
39         DBG("Test emulation environment");
40
41         uname(&buf);
42
43         if (g_str_has_suffix(buf.machine, EMUL_UTSNAME_MACHINE_SUFFIX) == TRUE)
44                 return TRUE;
45
46         return FALSE;
47 }
48
49 static void __netconfig_emulator_set_ip(void)
50 {
51         const int BUF_LEN_MAX = 255;
52         const char EMUL_IFNAME[] = "eth0";
53         char ip[BUF_LEN_MAX];
54         int sockfd = 0;
55         struct ifreq ifr;
56
57         if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
58                 ERR("Failed to open socket");
59                 return;
60         }
61
62         memset(&ifr, 0, sizeof(struct ifreq));
63         g_strlcpy((char*)&ifr.ifr_name, EMUL_IFNAME, sizeof(EMUL_IFNAME));
64
65         if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
66                 ERR("Error getting IP address");
67
68                 close(sockfd);
69                 return;
70         }
71
72         g_strlcpy(ip, (char*)inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr), BUF_LEN_MAX);
73
74         vconf_set_str(VCONFKEY_NETWORK_IP, ip);
75
76         close(sockfd);
77 }
78
79 static void __netconfig_emulator_set_proxy(void)
80 {
81         const char HTTP_PROXY[] = "http_proxy";
82         char *proxy = NULL;
83
84         proxy = getenv(HTTP_PROXY);
85         DBG("Get system proxy: %s", proxy);
86
87         if(proxy != NULL)
88                 vconf_set_str(VCONFKEY_NETWORK_PROXY, proxy);
89 }
90
91 static void __netconfig_emulator_set_network_state(void)
92 {
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);
96 }
97
98 static void __netconfig_emulator_config_emul_env(void)
99 {
100         __netconfig_emulator_set_ip();
101         __netconfig_emulator_set_proxy();
102         __netconfig_emulator_set_network_state();
103 }
104
105 gboolean netconfig_emulator_is_emulated(void)
106 {
107         return netconfig_is_emulated;
108 }
109
110 void netconfig_emulator_test_and_start(void)
111 {
112         netconfig_is_emulated = __netconfig_emulator_test_emulation_env();
113
114         DBG("Emulation environment tested: %s", netconfig_is_emulated ? "It's emulated" : "Not emulated");
115
116         if (netconfig_is_emulated == TRUE)
117                 __netconfig_emulator_config_emul_env();
118 }