summaryrefslogtreecommitdiffstats
path: root/net.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-07-09 06:50:31 +0000
committerRoy Marples <roy@marples.name>2008-07-09 06:50:31 +0000
commitd31af5110f301c20859d2572850e4e5b7227e532 (patch)
tree0093e29914eec151e8bd223658b06b1723ca8f31 /net.c
parenta30b3a9fbd6ad8eed29de9408f81a469e6f04e4f (diff)
downloaddhcpcd-d31af5110f301c20859d2572850e4e5b7227e532.tar.xz
Add more error checking if the link was taken down or ip addresses removed - if this happens we reset to the init state.
Diffstat (limited to 'net.c')
-rw-r--r--net.c49
1 files changed, 33 insertions, 16 deletions
diff --git a/net.c b/net.c
index 74dace15..fa1b7c92 100644
--- a/net.c
+++ b/net.c
@@ -278,6 +278,38 @@ do_interface(const char *ifname,
return retval;
}
+int
+up_interface(const char *ifname)
+{
+ int s;
+ struct ifreq ifr;
+ int retval = -1;
+#ifdef __linux__
+ char *p;
+#endif
+
+ if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ return -1;
+ memset(&ifr, 0, sizeof(ifr));
+ strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+#ifdef __linux__
+ /* We can only bring the real interface up */
+ if ((p = strchr(ifr.ifr_name, ':')))
+ *p = '\0';
+#endif
+ if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
+ if ((ifr.ifr_flags & IFF_UP))
+ retval = 0;
+ else {
+ ifr.ifr_flags |= IFF_UP;
+ if (ioctl(s, SIOCSIFFLAGS, &ifr) == 0)
+ retval = 0;
+ }
+ }
+ close(s);
+ return retval;
+}
+
struct interface *
read_interface(const char *ifname, _unused int metric)
{
@@ -287,9 +319,6 @@ read_interface(const char *ifname, _unused int metric)
unsigned char *hwaddr = NULL;
size_t hwlen = 0;
sa_family_t family = 0;
-#ifdef __linux__
- char *p;
-#endif
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
@@ -342,20 +371,8 @@ read_interface(const char *ifname, _unused int metric)
goto eexit;
}
- /* Bring the interface up if it's down */
- strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
-#ifdef __linux__
- /* We can only bring the real interface up */
- if ((p = strchr(ifr.ifr_name, ':')))
- *p = '\0';
-#endif
- if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
+ if (up_interface(ifname) != 0)
goto eexit;
- if (!(ifr.ifr_flags & IFF_UP)) {
- ifr.ifr_flags |= IFF_UP;
- if (ioctl(s, SIOCSIFFLAGS, &ifr) != 0)
- goto eexit;
- }
iface = xzalloc(sizeof(*iface));
strlcpy(iface->name, ifname, IF_NAMESIZE);