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
30 #include <sys/ioctl.h>
31 #include <sys/inotify.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
38 #include <glib/gprintf.h>
40 #define CONNMAN_API_SUBJECT_TO_CHANGE
41 #include <connman/plugin.h>
42 #include <connman/utsname.h>
43 #include <connman/log.h>
44 #include <connman/inet.h>
46 static in_addr_t loopback_address;
47 static in_addr_t loopback_netmask;
49 static char system_hostname[HOST_NAME_MAX + 1];
51 static void create_hostname(void)
53 const char *name = "localhost";
55 if (sethostname(name, strlen(name)) < 0)
56 connman_error("Failed to set hostname to %s", name);
58 strncpy(system_hostname, name, HOST_NAME_MAX);
62 static void _create_hostname(void)
65 #define WIFI_MAC "/opt/etc/.mac.info"
67 memset(system_hostname, 0, sizeof(system_hostname));
70 gchar* dev_id = "TIZEN";
71 char wifi_mac[HOST_NAME_MAX + 1];
73 fp = fopen(WIFI_MAC, "r");
75 connman_error("Failed to get current hostname");
76 strncpy(system_hostname, dev_id, strlen(dev_id));
80 rv = fgets(wifi_mac, HOST_NAME_MAX, fp);
82 connman_error("Failed to get current hostname");
83 strncpy(system_hostname, dev_id, strlen(dev_id));
88 dev_id = g_base64_encode((const guchar *)wifi_mac, strlen(wifi_mac));
89 g_sprintf(system_hostname, "TIZEN-%s", dev_id);
95 static int setup_hostname(void)
97 char name[HOST_NAME_MAX + 1];
99 memset(system_hostname, 0, sizeof(system_hostname));
101 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
102 connman_error("Failed to get current hostname");
105 #if defined TIZEN_EXT
106 if (strlen(system_hostname) > 0 &&
107 strcmp(system_hostname, "(none)") != 0 &&
108 strcmp(system_hostname, "localhost") != 0)
109 connman_info("System hostname is %s", system_hostname);
113 if (strlen(system_hostname) > 0 &&
114 strcmp(system_hostname, "(none)") != 0)
115 connman_info("System hostname is %s", system_hostname);
120 memset(name, 0, sizeof(name));
122 if (getdomainname(name, HOST_NAME_MAX) < 0) {
123 connman_error("Failed to get current domainname");
127 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
128 connman_info("System domainname is %s", name);
133 static bool valid_loopback(int sk, struct ifreq *ifr)
135 struct sockaddr_in *addr;
137 char buf[INET_ADDRSTRLEN];
139 /* It is possible to end up in situations in which the
140 * loopback interface is up but has no valid address. In that
141 * case, we expect EADDRNOTAVAIL and should return FALSE.
144 err = ioctl(sk, SIOCGIFADDR, ifr);
147 connman_error("Getting address failed (%s)", strerror(-err));
148 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
151 addr = (struct sockaddr_in *) &ifr->ifr_addr;
152 if (addr->sin_addr.s_addr != loopback_address) {
153 connman_warn("Invalid loopback address %s",
154 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
158 err = ioctl(sk, SIOCGIFNETMASK, ifr);
161 connman_error("Getting netmask failed (%s)", strerror(-err));
165 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
166 if (addr->sin_addr.s_addr != loopback_netmask) {
167 connman_warn("Invalid loopback netmask %s",
168 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
175 static int setup_loopback(void)
178 struct sockaddr_in addr;
181 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
185 memset(&ifr, 0, sizeof(ifr));
186 strcpy(ifr.ifr_name, "lo");
188 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
193 if (ifr.ifr_flags & IFF_UP) {
194 connman_info("Checking loopback interface settings");
195 if (valid_loopback(sk, &ifr)) {
200 connman_warn("Correcting wrong loopback settings");
203 memset(&addr, 0, sizeof(addr));
204 addr.sin_family = AF_INET;
205 addr.sin_addr.s_addr = loopback_address;
206 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
208 err = ioctl(sk, SIOCSIFADDR, &ifr);
211 connman_error("Setting address failed (%s)", strerror(-err));
215 memset(&addr, 0, sizeof(addr));
216 addr.sin_family = AF_INET;
217 addr.sin_addr.s_addr = loopback_netmask;
218 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
220 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
223 connman_error("Setting netmask failed (%s)", strerror(-err));
227 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
232 ifr.ifr_flags |= IFF_UP;
234 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
236 connman_error("Activating loopback interface failed (%s)",
247 static const char *loopback_get_hostname(void)
249 return system_hostname;
252 static int loopback_set_hostname(const char *hostname)
257 if (g_strcmp0(hostname, "<hostname>") == 0)
260 len = strlen(hostname);
262 if (!connman_inet_check_hostname(hostname, len))
265 if ((ptr = strstr(hostname, ".")))
266 len = ptr - hostname;
268 if (sethostname(hostname, len) < 0) {
270 connman_error("Failed to set hostname to %s", hostname);
274 connman_info("Setting hostname to %s", hostname);
279 static int loopback_set_domainname(const char *domainname)
283 len = strlen(domainname);
285 if (!connman_inet_check_hostname(domainname, len))
288 if (setdomainname(domainname, len) < 0) {
290 connman_error("Failed to set domainname to %s", domainname);
294 connman_info("Setting domainname to %s", domainname);
299 static struct connman_utsname_driver loopback_driver = {
301 .get_hostname = loopback_get_hostname,
302 .set_hostname = loopback_set_hostname,
303 .set_domainname = loopback_set_domainname,
306 static int loopback_init(void)
308 loopback_address = inet_addr("127.0.0.1");
309 loopback_netmask = inet_addr("255.0.0.0");
315 connman_utsname_driver_register(&loopback_driver);
320 static void loopback_exit(void)
322 connman_utsname_driver_unregister(&loopback_driver);
325 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
326 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)