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