summaryrefslogtreecommitdiff
path: root/gdhcp/common.c
diff options
context:
space:
mode:
authorMartin Xu <martin.xu@intel.com>2010-11-12 17:07:31 +0100
committerSamuel Ortiz <sameo@linux.intel.com>2010-11-13 23:35:38 +0100
commit5dcabcf4a70d58a862990ba3235882c4e47ebcc4 (patch)
tree16d9240c1af02db72ac28fc9eb370d742302ebe8 /gdhcp/common.c
parent18151aa9ff937fe04abea3af39fbbe442565dc31 (diff)
downloadconnman-5dcabcf4a70d58a862990ba3235882c4e47ebcc4.tar.gz
connman-5dcabcf4a70d58a862990ba3235882c4e47ebcc4.tar.bz2
connman-5dcabcf4a70d58a862990ba3235882c4e47ebcc4.zip
gdhcp: Move get_interface_name and interface_is_up to common.c
This is needed for the dhcp server implementation.
Diffstat (limited to 'gdhcp/common.c')
-rw-r--r--gdhcp/common.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/gdhcp/common.c b/gdhcp/common.c
index fc95881e..00817af8 100644
--- a/gdhcp/common.c
+++ b/gdhcp/common.c
@@ -22,11 +22,15 @@
#include <config.h>
#endif
+#include <stdio.h>
#include <errno.h>
#include <unistd.h>
+#include <sys/ioctl.h>
#include <stdint.h>
#include <string.h>
#include <endian.h>
+#include <net/if_arp.h>
+#include <linux/if.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
@@ -417,3 +421,68 @@ int dhcp_l3_socket(int port, const char *interface)
return fd;
}
+
+char *get_interface_name(int index)
+{
+ struct ifreq ifr;
+ int sk, err;
+
+ if (index < 0)
+ return NULL;
+
+ sk = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sk < 0) {
+ perror("Open socket error");
+ return NULL;
+ }
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_ifindex = index;
+
+ err = ioctl(sk, SIOCGIFNAME, &ifr);
+ if (err < 0) {
+ perror("Get interface name error");
+ close(sk);
+ return NULL;
+ }
+
+ close(sk);
+
+ return g_strdup(ifr.ifr_name);
+}
+
+gboolean interface_is_up(int index)
+{
+ int sk, err;
+ struct ifreq ifr;
+ gboolean ret = FALSE;
+
+ sk = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sk < 0) {
+ perror("Open socket error");
+ return FALSE;
+ }
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_ifindex = index;
+
+ err = ioctl(sk, SIOCGIFNAME, &ifr);
+ if (err < 0) {
+ perror("Get interface name error");
+ goto done;
+ }
+
+ err = ioctl(sk, SIOCGIFFLAGS, &ifr);
+ if (err < 0) {
+ perror("Get interface flags error");
+ goto done;
+ }
+
+ if (ifr.ifr_flags & IFF_UP)
+ ret = TRUE;
+
+done:
+ close(sk);
+
+ return ret;
+}