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));
71 #define WIFI_MAC "/opt/etc/.mac.info"
74 gchar* dev_id = "TIZEN";
75 char wifi_mac[HOST_NAME_MAX + 1];
77 fp = fopen(WIFI_MAC, "r");
79 connman_error("Failed to get current hostname");
80 strncpy(system_hostname, dev_id, strlen(dev_id));
84 rv = fgets(wifi_mac, HOST_NAME_MAX, fp);
86 connman_error("Failed to get current hostname");
87 strncpy(system_hostname, dev_id, strlen(dev_id));
92 dev_id = g_base64_encode((const guchar *)wifi_mac, strlen(wifi_mac));
93 g_sprintf(system_hostname, "TIZEN-%s", dev_id);
100 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
101 connman_error("Failed to get current hostname");
105 if (strlen(system_hostname) > 0 &&
106 strcmp(system_hostname, "(none)") != 0)
107 connman_info("System hostname is %s", system_hostname);
111 memset(name, 0, sizeof(name));
113 if (getdomainname(name, HOST_NAME_MAX) < 0) {
114 connman_error("Failed to get current domainname");
118 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
119 connman_info("System domainname is %s", name);
124 static bool valid_loopback(int sk, struct ifreq *ifr)
126 struct sockaddr_in *addr;
128 char buf[INET_ADDRSTRLEN];
130 /* It is possible to end up in situations in which the
131 * loopback interface is up but has no valid address. In that
132 * case, we expect EADDRNOTAVAIL and should return FALSE.
135 err = ioctl(sk, SIOCGIFADDR, ifr);
138 connman_error("Getting address failed (%s)", strerror(-err));
139 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
142 addr = (struct sockaddr_in *) &ifr->ifr_addr;
143 if (addr->sin_addr.s_addr != loopback_address) {
144 connman_warn("Invalid loopback address %s",
145 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
149 err = ioctl(sk, SIOCGIFNETMASK, ifr);
152 connman_error("Getting netmask failed (%s)", strerror(-err));
156 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
157 if (addr->sin_addr.s_addr != loopback_netmask) {
158 connman_warn("Invalid loopback netmask %s",
159 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
166 static int setup_loopback(void)
169 struct sockaddr_in addr;
172 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
176 memset(&ifr, 0, sizeof(ifr));
177 strcpy(ifr.ifr_name, "lo");
179 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
184 if (ifr.ifr_flags & IFF_UP) {
185 connman_info("Checking loopback interface settings");
186 if (valid_loopback(sk, &ifr)) {
191 connman_warn("Correcting wrong loopback settings");
194 memset(&addr, 0, sizeof(addr));
195 addr.sin_family = AF_INET;
196 addr.sin_addr.s_addr = loopback_address;
197 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
199 err = ioctl(sk, SIOCSIFADDR, &ifr);
202 connman_error("Setting address failed (%s)", strerror(-err));
206 memset(&addr, 0, sizeof(addr));
207 addr.sin_family = AF_INET;
208 addr.sin_addr.s_addr = loopback_netmask;
209 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
211 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
214 connman_error("Setting netmask failed (%s)", strerror(-err));
218 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
223 ifr.ifr_flags |= IFF_UP;
225 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
227 connman_error("Activating loopback interface failed (%s)",
238 static const char *loopback_get_hostname(void)
240 return system_hostname;
243 static int loopback_set_hostname(const char *hostname)
248 if (g_strcmp0(hostname, "<hostname>") == 0)
251 len = strlen(hostname);
253 if (!connman_inet_check_hostname(hostname, len))
256 if ((ptr = strstr(hostname, ".")))
257 len = ptr - hostname;
259 if (sethostname(hostname, len) < 0) {
261 connman_error("Failed to set hostname to %s", hostname);
265 connman_info("Setting hostname to %s", hostname);
270 static int loopback_set_domainname(const char *domainname)
274 len = strlen(domainname);
276 if (!connman_inet_check_hostname(domainname, len))
279 if (setdomainname(domainname, len) < 0) {
281 connman_error("Failed to set domainname to %s", domainname);
285 connman_info("Setting domainname to %s", domainname);
290 static struct connman_utsname_driver loopback_driver = {
292 .get_hostname = loopback_get_hostname,
293 .set_hostname = loopback_set_hostname,
294 .set_domainname = loopback_set_domainname,
297 static int loopback_init(void)
299 loopback_address = inet_addr("127.0.0.1");
300 loopback_netmask = inet_addr("255.0.0.0");
306 connman_utsname_driver_register(&loopback_driver);
311 static void loopback_exit(void)
313 connman_utsname_driver_unregister(&loopback_driver);
316 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
317 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)