94059e05efa84c14e8594aa28ee9a452848769da
[platform/upstream/connman.git] / plugins / loopback.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/inotify.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <net/if.h>
35
36 #include <glib.h>
37
38 #define CONNMAN_API_SUBJECT_TO_CHANGE
39 #include <connman/plugin.h>
40 #include <connman/utsname.h>
41 #include <connman/log.h>
42
43 static in_addr_t loopback_address;
44 static in_addr_t loopback_netmask;
45
46 #if 0
47 static GIOChannel *inotify_channel = NULL;
48
49 static int hostname_descriptor = -1;
50
51 static gboolean inotify_event(GIOChannel *channel,
52                                         GIOCondition condition, gpointer data)
53 {
54         unsigned char buf[129], *ptr = buf;
55         gsize len;
56         GIOError err;
57
58         if (condition & (G_IO_HUP | G_IO_ERR))
59                 return FALSE;
60
61         memset(buf, 0, sizeof(buf));
62
63         err = g_io_channel_read(channel, (gchar *) buf, sizeof(buf) - 1, &len);
64         if (err != G_IO_ERROR_NONE) {
65                 if (err == G_IO_ERROR_AGAIN)
66                         return TRUE;
67                 connman_error("Reading from inotify channel failed");
68                 return FALSE;
69         }
70
71         while (len >= sizeof(struct inotify_event)) {
72                 struct inotify_event *evt = (struct inotify_event *) ptr;
73
74                 if (evt->wd == hostname_descriptor) {
75                         if (evt->mask & (IN_CREATE | IN_MOVED_TO))
76                                 connman_info("create hostname file");
77
78                         if (evt->mask & (IN_DELETE | IN_MOVED_FROM))
79                                 connman_info("delete hostname file");
80
81                         if (evt->mask & (IN_MODIFY | IN_MOVE_SELF))
82                                 connman_info("modify hostname file");
83                 }
84
85                 len -= sizeof(struct inotify_event) + evt->len;
86                 ptr += sizeof(struct inotify_event) + evt->len;
87         }
88
89         return TRUE;
90 }
91
92 static int create_watch(void)
93 {
94         int fd;
95
96         fd = inotify_init();
97         if (fd < 0) {
98                 connman_error("Creation of inotify context failed");
99                 return -EIO;
100         }
101
102         inotify_channel = g_io_channel_unix_new(fd);
103         if (inotify_channel == NULL) {
104                 connman_error("Creation of inotify channel failed");
105                 close(fd);
106                 return -EIO;
107         }
108
109         hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
110                                 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
111         if (hostname_descriptor < 0) {
112                 connman_error("Creation of hostname watch failed");
113                 g_io_channel_unref(inotify_channel);
114                 inotify_channel = NULL;
115                 close(fd);
116                 return -EIO;
117         }
118
119         g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
120                                                         inotify_event, NULL);
121
122         return 0;
123 }
124
125 static void remove_watch(void)
126 {
127         int fd;
128
129         if (inotify_channel == NULL)
130                 return;
131
132         fd = g_io_channel_unix_get_fd(inotify_channel);
133
134         if (hostname_descriptor >= 0)
135                 inotify_rm_watch(fd, hostname_descriptor);
136
137         g_io_channel_unref(inotify_channel);
138
139         close(fd);
140 }
141 #endif
142
143 static void create_hostname(void)
144 {
145         const char *name = "localhost";
146
147         if (sethostname(name, strlen(name)) < 0)
148                 connman_error("Failed to set hostname to %s", name);
149 }
150
151 static int setup_hostname(void)
152 {
153         char name[HOST_NAME_MAX + 1];
154
155         memset(name, 0, sizeof(name));
156
157         if (gethostname(name, HOST_NAME_MAX) < 0) {
158                 connman_error("Failed to get current hostname");
159                 return -EIO;
160         }
161
162         if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
163                 connman_info("System hostname is %s", name);
164         else
165                 create_hostname();
166
167         memset(name, 0, sizeof(name));
168
169         if (getdomainname(name, HOST_NAME_MAX) < 0) {
170                 connman_error("Failed to get current domainname");
171                 return -EIO;
172         }
173
174         if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
175                 connman_info("System domainname is %s", name);
176
177         return 0;
178 }
179
180 static gboolean valid_loopback(int sk, struct ifreq *ifr)
181 {
182         struct sockaddr_in *addr;
183         int err;
184
185         err = ioctl(sk, SIOCGIFADDR, ifr);
186         if (err < 0) {
187                 err = -errno;
188                 connman_error("Getting address failed (%s)", strerror(-err));
189                 return TRUE;
190         }
191
192         addr = (struct sockaddr_in *) &ifr->ifr_addr;
193         if (addr->sin_addr.s_addr != loopback_address)
194                 return FALSE;
195
196         err = ioctl(sk, SIOCGIFNETMASK, ifr);
197         if (err < 0) {
198                 err = -errno;
199                 connman_error("Getting netmask failed (%s)", strerror(-err));
200                 return TRUE;
201         }
202
203         addr = (struct sockaddr_in *) &ifr->ifr_netmask;
204         if (addr->sin_addr.s_addr != loopback_netmask)
205                 return FALSE;
206
207         return TRUE;
208 }
209
210 static int setup_loopback(void)
211 {
212         struct ifreq ifr;
213         struct sockaddr_in addr;
214         int sk, err;
215
216         sk = socket(PF_INET, SOCK_DGRAM, 0);
217         if (sk < 0)
218                 return -1;
219
220         memset(&ifr, 0, sizeof(ifr));
221         strcpy(ifr.ifr_name, "lo");
222
223         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
224                 err = -errno;
225                 goto done;
226         }
227
228         if (ifr.ifr_flags & IFF_UP) {
229                 connman_info("Checking loopback interface settings");
230                 if (valid_loopback(sk, &ifr) == TRUE) {
231                         err = -EALREADY;
232                         goto done;
233                 }
234
235                 connman_warn("Correcting wrong lookback settings");
236         }
237
238         memset(&addr, 0, sizeof(addr));
239         addr.sin_family = AF_INET;
240         addr.sin_addr.s_addr = inet_addr("127.0.0.1");
241         memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
242
243         err = ioctl(sk, SIOCSIFADDR, &ifr);
244         if (err < 0) {
245                 err = -errno;
246                 connman_error("Setting address failed (%s)", strerror(-err));
247                 goto done;
248         }
249
250         memset(&addr, 0, sizeof(addr));
251         addr.sin_family = AF_INET;
252         addr.sin_addr.s_addr = inet_addr("255.0.0.0");
253         memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
254
255         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
256         if (err < 0) {
257                 err = -errno;
258                 connman_error("Setting netmask failed (%s)", strerror(-err));
259                 goto done;
260         }
261
262         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
263                 err = -errno;
264                 goto done;
265         }
266
267         ifr.ifr_flags |= IFF_UP;
268
269         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
270                 err = -errno;
271                 connman_error("Activating loopback interface failed (%s)",
272                                                         strerror(-err));
273                 goto done;
274         }
275
276 done:
277         close(sk);
278
279         return err;
280 }
281
282 static int loopback_set_hostname(const char *hostname)
283 {
284         int err;
285
286         if (g_strcmp0(hostname, "<hostname>") == 0)
287                 return 0;
288
289         if (sethostname(hostname, strlen(hostname)) < 0) {
290                 err = -errno;
291                 connman_error("Failed to set hostname to %s", hostname);
292                 return err;
293         }
294
295         connman_info("Setting hostname to %s", hostname);
296
297         return 0;
298 }
299
300 static int loopback_set_domainname(const char *domainname)
301 {
302         int err;
303
304         if (setdomainname(domainname, strlen(domainname)) < 0) {
305                 err = -errno;
306                 connman_error("Failed to set domainname to %s", domainname);
307                 return err;
308         }
309
310         connman_info("Setting domainname to %s", domainname);
311
312         return 0;
313 }
314
315 static struct connman_utsname_driver loopback_driver = {
316         .name           = "loopback",
317         .set_hostname   = loopback_set_hostname,
318         .set_domainname = loopback_set_domainname,
319 };
320
321 static int loopback_init(void)
322 {
323         loopback_address = inet_addr("127.0.0.1");
324         loopback_netmask = inet_addr("255.0.0.0");
325
326         setup_loopback();
327
328         setup_hostname();
329
330         //create_watch();
331
332         connman_utsname_driver_register(&loopback_driver);
333
334         return 0;
335 }
336
337 static void loopback_exit(void)
338 {
339         connman_utsname_driver_unregister(&loopback_driver);
340
341         //remove_watch();
342 }
343
344 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
345                 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)