5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/log.h>
39 static int setup_hostname(void)
41 char name[HOST_NAME_MAX + 1];
43 memset(name, 0, sizeof(name));
45 if (gethostname(name, HOST_NAME_MAX) < 0) {
46 connman_error("Failed to get current hostname");
50 connman_info("System hostname is %s", name);
55 static int loopback_init(void)
58 struct sockaddr_in *addr;
61 sk = socket(PF_INET, SOCK_DGRAM, 0);
65 memset(&ifr, 0, sizeof(ifr));
66 strcpy(ifr.ifr_name, "lo");
68 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
73 if (ifr.ifr_flags & IFF_UP) {
75 connman_info("The loopback interface is already up");
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");
83 err = ioctl(sk, SIOCSIFADDR, &ifr);
86 connman_error("Setting address failed (%s)", strerror(-err));
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");
94 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
97 connman_error("Setting netmask failed (%s)", strerror(-err));
101 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
106 ifr.ifr_flags |= IFF_UP;
108 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
110 connman_error("Activating loopback interface failed (%s)",
123 static void loopback_exit(void)
127 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
128 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)