Check return value of uname()
[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         if (uname(&buf) != 0)
42                 return FALSE;
43
44         if (g_str_has_suffix(buf.machine, EMUL_UTSNAME_MACHINE_SUFFIX) == TRUE)
45                 return TRUE;
46
47         return FALSE;
48 }
49
50 static void __netconfig_emulator_set_ip(void)
51 {
52         const int BUF_LEN_MAX = 255;
53         const char EMUL_IFNAME[] = "eth0";
54         char ip[BUF_LEN_MAX];
55         int sockfd = 0;
56         struct ifreq ifr;
57
58         if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
59                 ERR("Failed to open socket");
60                 return;
61         }
62
63         memset(&ifr, 0, sizeof(struct ifreq));
64         g_strlcpy((char*)&ifr.ifr_name, EMUL_IFNAME, sizeof(EMUL_IFNAME));
65
66         if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
67                 ERR("Error getting IP address");
68
69                 close(sockfd);
70                 return;
71         }
72
73         g_strlcpy(ip, (char*)inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr), BUF_LEN_MAX);
74
75         vconf_set_str(VCONFKEY_NETWORK_IP, ip);
76
77         close(sockfd);
78 }
79
80 static void __netconfig_emulator_set_proxy(void)
81 {
82         const char HTTP_PROXY[] = "http_proxy";
83         char *proxy = NULL;
84
85         proxy = getenv(HTTP_PROXY);
86         DBG("Get system proxy: %s", proxy);
87
88         if(proxy != NULL)
89                 vconf_set_str(VCONFKEY_NETWORK_PROXY, proxy);
90 }
91
92 static void __netconfig_emulator_set_network_state(void)
93 {
94         vconf_set_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
95         vconf_set_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
96         vconf_set_int(VCONFKEY_DNET_STATE, VCONFKEY_DNET_OFF);
97 }
98
99 static void __netconfig_emulator_config_emul_env(void)
100 {
101         __netconfig_emulator_set_ip();
102         __netconfig_emulator_set_proxy();
103         __netconfig_emulator_set_network_state();
104 }
105
106 gboolean netconfig_emulator_is_emulated(void)
107 {
108         return netconfig_is_emulated;
109 }
110
111 void netconfig_emulator_test_and_start(void)
112 {
113         netconfig_is_emulated = __netconfig_emulator_test_emulation_env();
114
115         DBG("Emulation environment tested: %s", netconfig_is_emulated ? "It's emulated" : "Not emulated");
116
117         if (netconfig_is_emulated == TRUE)
118                 __netconfig_emulator_config_emul_env();
119 }