Add support for sending flight mode indication
[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 <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <net/if.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/log.h>
37
38 static int loopback_init(void)
39 {
40         struct ifreq ifr;
41         struct sockaddr_in *addr;
42         int sk, err;
43
44         sk = socket(PF_INET, SOCK_DGRAM, 0);
45         if (sk < 0)
46                 return -1;
47
48         memset(&ifr, 0, sizeof(ifr));
49         strcpy(ifr.ifr_name, "lo");
50
51         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
52                 err = -errno;
53                 goto done;
54         }
55
56         if (ifr.ifr_flags & IFF_UP) {
57                 err = -EALREADY;
58                 connman_info("The loopback interface is already up");
59                 goto done;
60         }
61
62         addr = (struct sockaddr_in *) &ifr.ifr_addr;
63         addr->sin_family = AF_INET;
64         addr->sin_addr.s_addr = inet_addr("127.0.0.0");
65
66         err = ioctl(sk, SIOCSIFADDR, &ifr);
67         if (err < 0) {
68                 err = -errno;
69                 connman_error("Setting address failed (%s)", strerror(-err));
70                 goto done;
71         }
72
73         addr = (struct sockaddr_in *) &ifr.ifr_netmask;
74         addr->sin_family = AF_INET;
75         addr->sin_addr.s_addr = inet_addr("255.0.0.0");
76
77         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
78         if (err < 0) {
79                 err = -errno;
80                 connman_error("Setting netmask failed (%s)", strerror(-err));
81                 goto done;
82         }
83
84         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
85                 err = -errno;
86                 goto done;
87         }
88
89         ifr.ifr_flags |= IFF_UP;
90
91         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
92                 err = -errno;
93                 connman_error("Activating loopback interface failed (%s)",
94                                                         strerror(-err));
95                 goto done;
96         }
97
98 done:
99         close(sk);
100
101         return err;
102 }
103
104 static void loopback_exit(void)
105 {
106 }
107
108 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
109                 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)