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);
61 static int setup_hostname(void)
63 char name[HOST_NAME_MAX + 1];
65 memset(system_hostname, 0, sizeof(system_hostname));
69 #define WIFI_MAC "/opt/etc/.mac.info"
72 gchar* dev_id = "TIZEN";
73 char wifi_mac[HOST_NAME_MAX + 1];
75 fp = fopen(WIFI_MAC, "r");
77 connman_error("Failed to get current hostname");
78 strncpy(system_hostname, dev_id, strlen(dev_id));
82 rv = fgets(wifi_mac, HOST_NAME_MAX, fp);
84 connman_error("Failed to get current hostname");
85 strncpy(system_hostname, dev_id, strlen(dev_id));
90 dev_id = g_base64_encode((const guchar *)wifi_mac, strlen(wifi_mac));
91 g_sprintf(system_hostname, "TIZEN-%s", dev_id);
98 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
99 connman_error("Failed to get current hostname");
104 if (strlen(system_hostname) > 0 &&
105 strcmp(system_hostname, "(none)") != 0)
106 connman_info("System hostname is %s", system_hostname);
110 memset(name, 0, sizeof(name));
112 if (getdomainname(name, HOST_NAME_MAX) < 0) {
113 connman_error("Failed to get current domainname");
117 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
118 connman_info("System domainname is %s", name);
123 static bool valid_loopback(int sk, struct ifreq *ifr)
125 struct sockaddr_in *addr;
127 char buf[INET_ADDRSTRLEN];
129 /* It is possible to end up in situations in which the
130 * loopback interface is up but has no valid address. In that
131 * case, we expect EADDRNOTAVAIL and should return FALSE.
134 err = ioctl(sk, SIOCGIFADDR, ifr);
137 connman_error("Getting address failed (%s)", strerror(-err));
138 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
141 addr = (struct sockaddr_in *) &ifr->ifr_addr;
142 if (addr->sin_addr.s_addr != loopback_address) {
143 connman_warn("Invalid loopback address %s",
144 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
148 err = ioctl(sk, SIOCGIFNETMASK, ifr);
151 connman_error("Getting netmask failed (%s)", strerror(-err));
155 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
156 if (addr->sin_addr.s_addr != loopback_netmask) {
157 connman_warn("Invalid loopback netmask %s",
158 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
165 static int setup_loopback(void)
168 struct sockaddr_in addr;
171 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
175 memset(&ifr, 0, sizeof(ifr));
176 strcpy(ifr.ifr_name, "lo");
178 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
183 if (ifr.ifr_flags & IFF_UP) {
184 connman_info("Checking loopback interface settings");
185 if (valid_loopback(sk, &ifr)) {
190 connman_warn("Correcting wrong loopback settings");
193 memset(&addr, 0, sizeof(addr));
194 addr.sin_family = AF_INET;
195 addr.sin_addr.s_addr = loopback_address;
196 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
198 err = ioctl(sk, SIOCSIFADDR, &ifr);
201 connman_error("Setting address failed (%s)", strerror(-err));
205 memset(&addr, 0, sizeof(addr));
206 addr.sin_family = AF_INET;
207 addr.sin_addr.s_addr = loopback_netmask;
208 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
210 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
213 connman_error("Setting netmask failed (%s)", strerror(-err));
217 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
222 ifr.ifr_flags |= IFF_UP;
224 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
226 connman_error("Activating loopback interface failed (%s)",
237 static const char *loopback_get_hostname(void)
239 return system_hostname;
242 static int loopback_set_hostname(const char *hostname)
247 if (g_strcmp0(hostname, "<hostname>") == 0)
250 len = strlen(hostname);
252 if (!connman_inet_check_hostname(hostname, len))
255 if ((ptr = strstr(hostname, ".")))
256 len = ptr - hostname;
258 if (sethostname(hostname, len) < 0) {
260 connman_error("Failed to set hostname to %s", hostname);
264 connman_info("Setting hostname to %s", hostname);
269 static int loopback_set_domainname(const char *domainname)
273 len = strlen(domainname);
275 if (!connman_inet_check_hostname(domainname, len))
278 if (setdomainname(domainname, len) < 0) {
280 connman_error("Failed to set domainname to %s", domainname);
284 connman_info("Setting domainname to %s", domainname);
289 static struct connman_utsname_driver loopback_driver = {
291 .get_hostname = loopback_get_hostname,
292 .set_hostname = loopback_set_hostname,
293 .set_domainname = loopback_set_domainname,
296 static int loopback_init(void)
298 loopback_address = inet_addr("127.0.0.1");
299 loopback_netmask = inet_addr("255.0.0.0");
305 connman_utsname_driver_register(&loopback_driver);
310 static void loopback_exit(void)
312 connman_utsname_driver_unregister(&loopback_driver);
315 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
316 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)