5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <sys/inotify.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
39 #include <glib/gprintf.h>
42 #define CONNMAN_API_SUBJECT_TO_CHANGE
43 #include <connman/plugin.h>
44 #include <connman/utsname.h>
45 #include <connman/log.h>
46 #include <connman/inet.h>
48 static in_addr_t loopback_address;
49 static in_addr_t loopback_netmask;
51 static char system_hostname[HOST_NAME_MAX + 1];
53 static void create_hostname(void)
55 const char *name = "localhost";
57 if (sethostname(name, strlen(name)) < 0)
58 connman_error("Failed to set hostname to %s", name);
60 strncpy(system_hostname, name, HOST_NAME_MAX);
63 static int setup_hostname(void)
65 char name[HOST_NAME_MAX + 1];
67 memset(system_hostname, 0, sizeof(system_hostname));
72 #if defined TIZEN_WEARABLE
73 #define BT_MAC "/csa/bluetooth/.bd_addr"
75 gchar* dev_id = "GearS2";
76 char bt_mac[HOST_NAME_MAX + 1];
79 fp = fopen(BT_MAC, "r");
81 connman_error("Failed to get current hostname");
82 strncpy(system_hostname, dev_id, strlen(dev_id));
86 // get the last line's address
87 while (fgets(bt_mac, HOST_NAME_MAX, fp)) {}
89 if (strlen(bt_mac) == 6) {
94 g_sprintf(system_hostname, "%s-%s", dev_id, addr);
96 strncpy(system_hostname, dev_id, strlen(dev_id));
101 #define WIFI_MAC "/opt/etc/.mac.info"
104 gchar* dev_id = "TIZEN";
105 char wifi_mac[HOST_NAME_MAX + 1];
107 fp = fopen(WIFI_MAC, "r");
109 connman_error("Failed to get current hostname");
110 strncpy(system_hostname, dev_id, strlen(dev_id));
114 rv = fgets(wifi_mac, HOST_NAME_MAX, fp);
116 connman_error("Failed to get current hostname");
117 strncpy(system_hostname, dev_id, strlen(dev_id));
122 dev_id = g_base64_encode((const guchar *)wifi_mac, strlen(wifi_mac));
123 g_sprintf(system_hostname, "TIZEN-%s", dev_id);
131 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
132 connman_error("Failed to get current hostname");
136 if (strlen(system_hostname) > 0 &&
137 strcmp(system_hostname, "(none)") != 0)
138 connman_info("System hostname is %s", system_hostname);
142 memset(name, 0, sizeof(name));
144 if (getdomainname(name, HOST_NAME_MAX) < 0) {
145 connman_error("Failed to get current domainname");
149 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
150 connman_info("System domainname is %s", name);
155 static bool valid_loopback(int sk, struct ifreq *ifr)
157 struct sockaddr_in *addr;
159 char buf[INET_ADDRSTRLEN];
161 /* It is possible to end up in situations in which the
162 * loopback interface is up but has no valid address. In that
163 * case, we expect EADDRNOTAVAIL and should return FALSE.
166 err = ioctl(sk, SIOCGIFADDR, ifr);
169 connman_error("Getting address failed (%s)", strerror(-err));
170 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
173 addr = (struct sockaddr_in *) &ifr->ifr_addr;
174 if (addr->sin_addr.s_addr != loopback_address) {
175 connman_warn("Invalid loopback address %s",
176 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
180 err = ioctl(sk, SIOCGIFNETMASK, ifr);
183 connman_error("Getting netmask failed (%s)", strerror(-err));
187 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
188 if (addr->sin_addr.s_addr != loopback_netmask) {
189 connman_warn("Invalid loopback netmask %s",
190 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
197 static int setup_loopback(void)
200 struct sockaddr_in addr;
203 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
207 memset(&ifr, 0, sizeof(ifr));
208 strcpy(ifr.ifr_name, "lo");
210 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
215 if (ifr.ifr_flags & IFF_UP) {
216 connman_info("Checking loopback interface settings");
217 if (valid_loopback(sk, &ifr)) {
222 connman_warn("Correcting wrong loopback settings");
225 memset(&addr, 0, sizeof(addr));
226 addr.sin_family = AF_INET;
227 addr.sin_addr.s_addr = loopback_address;
228 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
230 err = ioctl(sk, SIOCSIFADDR, &ifr);
233 connman_error("Setting address failed (%s)", strerror(-err));
237 memset(&addr, 0, sizeof(addr));
238 addr.sin_family = AF_INET;
239 addr.sin_addr.s_addr = loopback_netmask;
240 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
242 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
245 connman_error("Setting netmask failed (%s)", strerror(-err));
249 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
254 ifr.ifr_flags |= IFF_UP;
256 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
258 connman_error("Activating loopback interface failed (%s)",
269 static const char *loopback_get_hostname(void)
271 return system_hostname;
274 static int loopback_set_hostname(const char *hostname)
279 if (g_strcmp0(hostname, "<hostname>") == 0)
282 len = strlen(hostname);
284 if (!connman_inet_check_hostname(hostname, len))
287 if ((ptr = strstr(hostname, ".")))
288 len = ptr - hostname;
290 if (sethostname(hostname, len) < 0) {
292 connman_error("Failed to set hostname to %s", hostname);
296 connman_info("Setting hostname to %s", hostname);
301 static int loopback_set_domainname(const char *domainname)
305 len = strlen(domainname);
307 if (!connman_inet_check_hostname(domainname, len))
310 if (setdomainname(domainname, len) < 0) {
312 connman_error("Failed to set domainname to %s", domainname);
316 connman_info("Setting domainname to %s", domainname);
321 static struct connman_utsname_driver loopback_driver = {
323 .get_hostname = loopback_get_hostname,
324 .set_hostname = loopback_set_hostname,
325 .set_domainname = loopback_set_domainname,
328 static int loopback_init(void)
330 loopback_address = inet_addr("127.0.0.1");
331 loopback_netmask = inet_addr("255.0.0.0");
337 connman_utsname_driver_register(&loopback_driver);
342 static void loopback_exit(void)
344 connman_utsname_driver_unregister(&loopback_driver);
347 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
348 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)