Read hostname after setting up loopback interface
[framework/connectivity/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/socket.h>
32 #include <arpa/inet.h>
33 #include <net/if.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38
39 static int setup_hostname(void)
40 {
41         char name[HOST_NAME_MAX + 1];
42
43         memset(name, 0, sizeof(name));
44
45         if (gethostname(name, HOST_NAME_MAX) < 0) {
46                 connman_error("Failed to get current hostname");
47                 return -EIO;
48         }
49
50         connman_info("System hostname is %s", name);
51
52         return 0;
53 }
54
55 static int loopback_init(void)
56 {
57         struct ifreq ifr;
58         struct sockaddr_in *addr;
59         int sk, err;
60
61         sk = socket(PF_INET, SOCK_DGRAM, 0);
62         if (sk < 0)
63                 return -1;
64
65         memset(&ifr, 0, sizeof(ifr));
66         strcpy(ifr.ifr_name, "lo");
67
68         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
69                 err = -errno;
70                 goto done;
71         }
72
73         if (ifr.ifr_flags & IFF_UP) {
74                 err = -EALREADY;
75                 connman_info("The loopback interface is already up");
76                 goto done;
77         }
78
79         addr = (struct sockaddr_in *) &ifr.ifr_addr;
80         addr->sin_family = AF_INET;
81         addr->sin_addr.s_addr = inet_addr("127.0.0.1");
82
83         err = ioctl(sk, SIOCSIFADDR, &ifr);
84         if (err < 0) {
85                 err = -errno;
86                 connman_error("Setting address failed (%s)", strerror(-err));
87                 goto done;
88         }
89
90         addr = (struct sockaddr_in *) &ifr.ifr_netmask;
91         addr->sin_family = AF_INET;
92         addr->sin_addr.s_addr = inet_addr("255.0.0.0");
93
94         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
95         if (err < 0) {
96                 err = -errno;
97                 connman_error("Setting netmask failed (%s)", strerror(-err));
98                 goto done;
99         }
100
101         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
102                 err = -errno;
103                 goto done;
104         }
105
106         ifr.ifr_flags |= IFF_UP;
107
108         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
109                 err = -errno;
110                 connman_error("Activating loopback interface failed (%s)",
111                                                         strerror(-err));
112                 goto done;
113         }
114
115 done:
116         close(sk);
117
118         setup_hostname();
119
120         return err;
121 }
122
123 static void loopback_exit(void)
124 {
125 }
126
127 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
128                 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)