#include <config.h>
#endif
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+
#include <connman/plugin.h>
+#include <connman/log.h>
static int loopback_init(void)
{
- return 0;
+ struct ifreq ifr;
+ struct sockaddr_in *addr;
+ int sk, err;
+
+ sk = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sk < 0)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strcpy(ifr.ifr_name, "lo");
+
+ if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
+ err = -errno;
+ goto done;
+ }
+
+ if (ifr.ifr_flags & IFF_UP) {
+ err = -EALREADY;
+ connman_info("The loopback interface is already up");
+ goto done;
+ }
+
+ addr = (struct sockaddr_in *) &ifr.ifr_addr;
+ addr->sin_family = AF_INET;
+ addr->sin_addr.s_addr = inet_addr("127.0.0.0");
+
+ err = ioctl(sk, SIOCSIFADDR, &ifr);
+ if (err < 0) {
+ err = -errno;
+ connman_error("Setting address failed (%s)", strerror(-err));
+ goto done;
+ }
+
+ addr = (struct sockaddr_in *) &ifr.ifr_netmask;
+ addr->sin_family = AF_INET;
+ addr->sin_addr.s_addr = inet_addr("255.0.0.0");
+
+ err = ioctl(sk, SIOCSIFNETMASK, &ifr);
+ if (err < 0) {
+ err = -errno;
+ connman_error("Setting netmask failed (%s)", strerror(-err));
+ goto done;
+ }
+
+ if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
+ err = -errno;
+ goto done;
+ }
+
+ ifr.ifr_flags |= IFF_UP;
+
+ if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
+ err = -errno;
+ connman_error("Activating loopback interface failed (%s)",
+ strerror(-err));
+ goto done;
+ }
+
+done:
+ close(sk);
+
+ return err;
}
static void loopback_exit(void)