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