From 6751366d75406726fb533f842fd59b69feb95292 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 6 Dec 2008 00:27:49 +0100 Subject: [PATCH] Set loopback interface parameters during setup --- plugins/loopback.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/plugins/loopback.c b/plugins/loopback.c index 255d2bf..184ea73 100644 --- a/plugins/loopback.c +++ b/plugins/loopback.c @@ -23,11 +23,81 @@ #include #endif +#include +#include +#include +#include +#include +#include +#include + #include +#include 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) -- 2.7.4