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