5 * Copyright (C) 2007-2010 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>
43 static in_addr_t loopback_address;
44 static in_addr_t loopback_netmask;
46 static char system_hostname[HOST_NAME_MAX + 1];
48 static void create_hostname(void)
50 const char *name = "localhost";
52 if (sethostname(name, strlen(name)) < 0)
53 connman_error("Failed to set hostname to %s", name);
55 strncpy(system_hostname, name, HOST_NAME_MAX);
58 static int setup_hostname(void)
60 char name[HOST_NAME_MAX + 1];
62 memset(system_hostname, 0, sizeof(system_hostname));
64 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
65 connman_error("Failed to get current hostname");
69 if (strlen(system_hostname) > 0 &&
70 strcmp(system_hostname, "(none)") != 0)
71 connman_info("System hostname is %s", system_hostname);
75 memset(name, 0, sizeof(name));
77 if (getdomainname(name, HOST_NAME_MAX) < 0) {
78 connman_error("Failed to get current domainname");
82 if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
83 connman_info("System domainname is %s", name);
88 static gboolean valid_loopback(int sk, struct ifreq *ifr)
90 struct sockaddr_in *addr;
92 char buf[INET_ADDRSTRLEN];
94 /* It is possible to end up in situations in which the
95 * loopback interface is up but has no valid address. In that
96 * case, we expect EADDRNOTAVAIL and should return FALSE.
99 err = ioctl(sk, SIOCGIFADDR, ifr);
102 connman_error("Getting address failed (%s)", strerror(-err));
103 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
106 addr = (struct sockaddr_in *) &ifr->ifr_addr;
107 if (addr->sin_addr.s_addr != loopback_address) {
108 connman_warn("Invalid loopback address %s",
109 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
113 err = ioctl(sk, SIOCGIFNETMASK, ifr);
116 connman_error("Getting netmask failed (%s)", strerror(-err));
120 addr = (struct sockaddr_in *) &ifr->ifr_netmask;
121 if (addr->sin_addr.s_addr != loopback_netmask) {
122 connman_warn("Invalid loopback netmask %s",
123 inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
130 static int setup_loopback(void)
133 struct sockaddr_in addr;
136 sk = socket(PF_INET, SOCK_DGRAM, 0);
140 memset(&ifr, 0, sizeof(ifr));
141 strcpy(ifr.ifr_name, "lo");
143 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
148 if (ifr.ifr_flags & IFF_UP) {
149 connman_info("Checking loopback interface settings");
150 if (valid_loopback(sk, &ifr) == TRUE) {
155 connman_warn("Correcting wrong lookback settings");
158 memset(&addr, 0, sizeof(addr));
159 addr.sin_family = AF_INET;
160 addr.sin_addr.s_addr = loopback_address;
161 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
163 err = ioctl(sk, SIOCSIFADDR, &ifr);
166 connman_error("Setting address failed (%s)", strerror(-err));
170 memset(&addr, 0, sizeof(addr));
171 addr.sin_family = AF_INET;
172 addr.sin_addr.s_addr = loopback_netmask;
173 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
175 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
178 connman_error("Setting netmask failed (%s)", strerror(-err));
182 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
187 ifr.ifr_flags |= IFF_UP;
189 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
191 connman_error("Activating loopback interface failed (%s)",
202 static const char *loopback_get_hostname(void)
204 return system_hostname;
207 static int loopback_set_hostname(const char *hostname)
211 if (g_strcmp0(hostname, "<hostname>") == 0)
214 if (sethostname(hostname, strlen(hostname)) < 0) {
216 connman_error("Failed to set hostname to %s", hostname);
220 connman_info("Setting hostname to %s", hostname);
225 static int loopback_set_domainname(const char *domainname)
229 if (setdomainname(domainname, strlen(domainname)) < 0) {
231 connman_error("Failed to set domainname to %s", domainname);
235 connman_info("Setting domainname to %s", domainname);
240 static struct connman_utsname_driver loopback_driver = {
242 .get_hostname = loopback_get_hostname,
243 .set_hostname = loopback_set_hostname,
244 .set_domainname = loopback_set_domainname,
247 static int loopback_init(void)
249 loopback_address = inet_addr("127.0.0.1");
250 loopback_netmask = inet_addr("255.0.0.0");
256 connman_utsname_driver_register(&loopback_driver);
261 static void loopback_exit(void)
263 connman_utsname_driver_unregister(&loopback_driver);
266 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
267 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)