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 #define CONNMAN_API_SUBJECT_TO_CHANGE
39 #include <connman/plugin.h>
40 #include <connman/utsname.h>
41 #include <connman/log.h>
42 #include <connman/inet.h>
44 static in_addr_t loopback_address;
45 static in_addr_t loopback_netmask;
47 static char system_hostname[HOST_NAME_MAX + 1];
49 static void create_hostname(void)
51 const char *name = "localhost";
53 if (sethostname(name, strlen(name)) < 0)
54 connman_error("Failed to set hostname to %s", name);
56 strncpy(system_hostname, name, HOST_NAME_MAX);
59 static int setup_hostname(void)
61 char name[HOST_NAME_MAX + 1];
63 memset(system_hostname, 0, sizeof(system_hostname));
65 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
66 connman_error("Failed to get current hostname");
70 if (strlen(system_hostname) > 0 &&
71 strcmp(system_hostname, "(none)") != 0)
72 connman_info("System hostname is %s", system_hostname);
76 memset(name, 0, sizeof(name));
78 if (getdomainname(name, HOST_NAME_MAX) < 0) {
79 connman_error("Failed to get current domainname");
83 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
84 connman_info("System domainname is %s", name);
89 static bool valid_loopback(int sk, struct ifreq *ifr)
91 struct sockaddr_in *addr;
93 char buf[INET_ADDRSTRLEN];
95 /* It is possible to end up in situations in which the
96 * loopback interface is up but has no valid address. In that
97 * case, we expect EADDRNOTAVAIL and should return FALSE.
100 err = ioctl(sk, SIOCGIFADDR, ifr);
103 connman_error("Getting address failed (%s)", strerror(-err));
104 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
107 addr = (struct sockaddr_in *) &ifr->ifr_addr;
108 if (addr->sin_addr.s_addr != loopback_address) {
109 connman_warn("Invalid loopback address %s",
110 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
114 err = ioctl(sk, SIOCGIFNETMASK, ifr);
117 connman_error("Getting netmask failed (%s)", strerror(-err));
121 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
122 if (addr->sin_addr.s_addr != loopback_netmask) {
123 connman_warn("Invalid loopback netmask %s",
124 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
131 static int setup_loopback(void)
134 struct sockaddr_in addr;
137 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
141 memset(&ifr, 0, sizeof(ifr));
142 strcpy(ifr.ifr_name, "lo");
144 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
149 if (ifr.ifr_flags & IFF_UP) {
150 connman_info("Checking loopback interface settings");
151 if (valid_loopback(sk, &ifr)) {
156 connman_warn("Correcting wrong loopback settings");
159 memset(&addr, 0, sizeof(addr));
160 addr.sin_family = AF_INET;
161 addr.sin_addr.s_addr = loopback_address;
162 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
164 err = ioctl(sk, SIOCSIFADDR, &ifr);
167 connman_error("Setting address failed (%s)", strerror(-err));
171 memset(&addr, 0, sizeof(addr));
172 addr.sin_family = AF_INET;
173 addr.sin_addr.s_addr = loopback_netmask;
174 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
176 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
179 connman_error("Setting netmask failed (%s)", strerror(-err));
183 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
188 ifr.ifr_flags |= IFF_UP;
190 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
192 connman_error("Activating loopback interface failed (%s)",
203 static const char *loopback_get_hostname(void)
205 return system_hostname;
208 static int loopback_set_hostname(const char *hostname)
213 if (g_strcmp0(hostname, "<hostname>") == 0)
216 len = strlen(hostname);
218 if (!connman_inet_check_hostname(hostname, len))
221 if ((ptr = strstr(hostname, ".")))
222 len = ptr - hostname;
224 if (sethostname(hostname, len) < 0) {
226 connman_error("Failed to set hostname to %s", hostname);
230 connman_info("Setting hostname to %s", hostname);
235 static int loopback_set_domainname(const char *domainname)
239 len = strlen(domainname);
241 if (!connman_inet_check_hostname(domainname, len))
244 if (setdomainname(domainname, len) < 0) {
246 connman_error("Failed to set domainname to %s", domainname);
250 connman_info("Setting domainname to %s", domainname);
255 static struct connman_utsname_driver loopback_driver = {
257 .get_hostname = loopback_get_hostname,
258 .set_hostname = loopback_set_hostname,
259 .set_domainname = loopback_set_domainname,
262 static int loopback_init(void)
264 loopback_address = inet_addr("127.0.0.1");
265 loopback_netmask = inet_addr("255.0.0.0");
271 connman_utsname_driver_register(&loopback_driver);
276 static void loopback_exit(void)
278 connman_utsname_driver_unregister(&loopback_driver);
281 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
282 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)