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