summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-06-04 10:46:38 +0200
committerDaniel Borkmann <dborkman@redhat.com>2013-06-04 10:47:28 +0200
commit23bd5ecbd11766088784507a56e32975b4c755a2 (patch)
tree86dc0078bac5d8a0d21cfd39ac72278e94e8173a
parentac5fd9168efaa4e149340340805b1be7425d8834 (diff)
xutils: move device management functions out of xutils
Move those functions out so that they can be more easily maintained in its separate file. Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
-rw-r--r--astraceroute.c1
-rw-r--r--astraceroute/Makefile1
-rw-r--r--curvetun/Makefile1
-rw-r--r--dev.c169
-rw-r--r--dev.h15
-rw-r--r--flowtop/Makefile1
-rw-r--r--ifpps.c1
-rw-r--r--ifpps/Makefile1
-rw-r--r--ioops.c1
-rw-r--r--mac80211.c1
-rw-r--r--netsniff-ng/Makefile1
-rw-r--r--promisc.c4
-rw-r--r--ring.h1
-rw-r--r--trafgen/Makefile1
-rw-r--r--xutils.c160
-rw-r--r--xutils.h7
16 files changed, 198 insertions, 168 deletions
diff --git a/astraceroute.c b/astraceroute.c
index c0f317f..89bcc04 100644
--- a/astraceroute.c
+++ b/astraceroute.c
@@ -33,6 +33,7 @@
#include "bpf.h"
#include "die.h"
+#include "dev.h"
#include "tprintf.h"
#include "pkt_buff.h"
#include "proto.h"
diff --git a/astraceroute/Makefile b/astraceroute/Makefile
index 82e5b5b..8add1cd 100644
--- a/astraceroute/Makefile
+++ b/astraceroute/Makefile
@@ -9,6 +9,7 @@ astraceroute-objs = xmalloc.o \
tprintf.o \
bpf.o \
str.o \
+ dev.o \
geoip.o \
ring_rx.o \
ring.o \
diff --git a/curvetun/Makefile b/curvetun/Makefile
index 289aa72..970bfa8 100644
--- a/curvetun/Makefile
+++ b/curvetun/Makefile
@@ -4,6 +4,7 @@ curvetun-libs = -lnacl \
curvetun-objs = xmalloc.o \
xutils.o \
str.o \
+ dev.o \
stun.o \
patricia.o \
corking.o \
diff --git a/dev.c b/dev.c
new file mode 100644
index 0000000..56a1a51
--- /dev/null
+++ b/dev.c
@@ -0,0 +1,169 @@
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <ifaddrs.h>
+
+#include "dev.h"
+#include "str.h"
+#include "die.h"
+#include "xutils.h"
+
+int device_ifindex(const char *ifname)
+{
+ int ret, sock, index;
+ struct ifreq ifr;
+
+ if (!strncmp("any", ifname, strlen("any")))
+ return 0;
+
+ sock = af_socket(AF_INET);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+ ret = ioctl(sock, SIOCGIFINDEX, &ifr);
+ if (!ret)
+ index = ifr.ifr_ifindex;
+ else
+ index = -1;
+
+ close(sock);
+
+ return index;
+}
+
+static int __device_address6(const char *ifname, struct sockaddr_storage *ss)
+{
+ int ret, family, found = -EINVAL;
+ struct ifaddrs *ifaddr, *ifa;
+
+ ret = getifaddrs(&ifaddr);
+ if (ret < 0)
+ panic("Cannot get device addresses for IPv6!\n");
+
+ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+ family = ifa->ifa_addr->sa_family;
+ if (family != AF_INET6)
+ continue;
+ if (strcmp(ifa->ifa_name, ifname))
+ continue;
+
+ memcpy(ss, ifa->ifa_addr, sizeof(*ss));
+ found = 0;
+ break;
+ }
+
+ freeifaddrs(ifaddr);
+ return found;
+}
+
+int device_address(const char *ifname, int af, struct sockaddr_storage *ss)
+{
+ int ret, sock;
+ struct ifreq ifr;
+
+ if (!ss)
+ return -EINVAL;
+ if (!strncmp("any", ifname, strlen("any")))
+ return -EINVAL;
+ if (af == AF_INET6)
+ return __device_address6(ifname, ss);
+
+ sock = af_socket(af);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+ ifr.ifr_addr.sa_family = af;
+
+ ret = ioctl(sock, SIOCGIFADDR, &ifr);
+ if (!ret)
+ memcpy(ss, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
+
+ close(sock);
+
+ return ret;
+}
+
+int device_mtu(const char *ifname)
+{
+ int ret, sock, mtu;
+ struct ifreq ifr;
+
+ sock = af_socket(AF_INET);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+ ret = ioctl(sock, SIOCGIFMTU, &ifr);
+ if (!ret)
+ mtu = ifr.ifr_mtu;
+ else
+ mtu = 0;
+
+ close(sock);
+
+ return mtu;
+}
+
+short device_get_flags(const char *ifname)
+{
+ short flags;
+ int ret, sock;
+ struct ifreq ifr;
+
+ sock = af_socket(AF_INET);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+ ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
+ if (!ret)
+ flags = ifr.ifr_flags;
+ else
+ flags = 0;
+
+ close(sock);
+
+ return flags;
+}
+
+void device_set_flags(const char *ifname, const short flags)
+{
+ int ret, sock;
+ struct ifreq ifr;
+
+ sock = af_socket(AF_INET);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+ ifr.ifr_flags = flags;
+
+ ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
+ if (ret < 0)
+ panic("Cannot set NIC flags!\n");
+
+ close(sock);
+}
+
+int device_up_and_running(char *ifname)
+{
+ if (!ifname)
+ return -EINVAL;
+ if (!strncmp("any", ifname, strlen("any")))
+ return 1;
+
+ return (device_get_flags(ifname) & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING);
+}
+
+u32 device_bitrate(const char *ifname)
+{
+ u32 speed_c, speed_w;
+
+ speed_c = ethtool_bitrate(ifname);
+ speed_w = wireless_bitrate(ifname);
+
+ return (speed_c == 0 ? speed_w : speed_c);
+}
diff --git a/dev.h b/dev.h
new file mode 100644
index 0000000..cebcee4
--- /dev/null
+++ b/dev.h
@@ -0,0 +1,15 @@
+#ifndef DEV_H
+#define DEV_H
+
+#include <sys/socket.h>
+#include "built_in.h"
+
+extern int device_mtu(const char *ifname);
+extern int device_address(const char *ifname, int af, struct sockaddr_storage *ss);
+extern int device_ifindex(const char *ifname);
+extern short device_get_flags(const char *ifname);
+extern void device_set_flags(const char *ifname, const short flags);
+extern int device_up_and_running(char *ifname);
+extern u32 device_bitrate(const char *ifname);
+
+#endif /* DEV_H */
diff --git a/flowtop/Makefile b/flowtop/Makefile
index 915301f..53ecd4e 100644
--- a/flowtop/Makefile
+++ b/flowtop/Makefile
@@ -10,6 +10,7 @@ flowtop-objs = xmalloc.o \
xutils.o \
oui.o \
str.o \
+ dev.o \
hash.o \
dissector_eth.o \
dissector_80211.o \
diff --git a/ifpps.c b/ifpps.c
index 340b1a3..2d0a5b0 100644
--- a/ifpps.c
+++ b/ifpps.c
@@ -18,6 +18,7 @@
#include <time.h>
#include "die.h"
+#include "dev.h"
#include "xmalloc.h"
#include "xutils.h"
#include "ioops.h"
diff --git a/ifpps/Makefile b/ifpps/Makefile
index 57816ac..49a50d0 100644
--- a/ifpps/Makefile
+++ b/ifpps/Makefile
@@ -5,4 +5,5 @@ ifpps-objs = xmalloc.o \
xutils.o \
promisc.o \
str.o \
+ dev.o \
ifpps.o
diff --git a/ioops.c b/ioops.c
index 1bcb2d7..091153b 100644
--- a/ioops.c
+++ b/ioops.c
@@ -9,6 +9,7 @@
#include <linux/if_tun.h>
#include "die.h"
+#include "dev.h"
#include "ioops.h"
#include "str.h"
#include "xutils.h"
diff --git a/mac80211.c b/mac80211.c
index 32f75f1..1572f73 100644
--- a/mac80211.c
+++ b/mac80211.c
@@ -24,6 +24,7 @@
#include "die.h"
#include "str.h"
+#include "dev.h"
#include "xutils.h"
#include "mac80211.h"
#include "xmalloc.h"
diff --git a/netsniff-ng/Makefile b/netsniff-ng/Makefile
index 39412a6..11467c9 100644
--- a/netsniff-ng/Makefile
+++ b/netsniff-ng/Makefile
@@ -33,6 +33,7 @@ netsniff-ng-objs = dissector.o \
proto_mpls_unicast.o \
proto_80211_mac_hdr.o \
promisc.o \
+ dev.o \
str.o \
irq.o \
iosched.o \
diff --git a/promisc.c b/promisc.c
index 721da18..32184b1 100644
--- a/promisc.c
+++ b/promisc.c
@@ -1,7 +1,9 @@
#include <string.h>
+#include <sys/socket.h>
+#include <linux/if.h>
#include "promisc.h"
-#include "xutils.h"
+#include "dev.h"
short enter_promiscuous_mode(char *ifname)
{
diff --git a/ring.h b/ring.h
index 8bfe1eb..9a2e05b 100644
--- a/ring.h
+++ b/ring.h
@@ -25,6 +25,7 @@
#include "xutils.h"
#include "built_in.h"
#include "die.h"
+#include "dev.h"
union tpacket_uhdr {
struct tpacket_hdr *h1;
diff --git a/trafgen/Makefile b/trafgen/Makefile
index 6942843..2bb1a2f 100644
--- a/trafgen/Makefile
+++ b/trafgen/Makefile
@@ -5,6 +5,7 @@ trafgen-libs = $(shell pkg-config --libs libnl-3.0) \
trafgen-objs = xmalloc.o \
ioops.o \
xutils.o \
+ dev.o \
irq.o \
str.o \
mac80211.o \
diff --git a/xutils.c b/xutils.c
index 991ff5b..28900cb 100644
--- a/xutils.c
+++ b/xutils.c
@@ -391,156 +391,6 @@ int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf)
return ret;
}
-u32 device_bitrate(const char *ifname)
-{
- u32 speed_c, speed_w;
-
- speed_c = ethtool_bitrate(ifname);
- speed_w = wireless_bitrate(ifname);
-
- return (speed_c == 0 ? speed_w : speed_c);
-}
-
-int device_ifindex(const char *ifname)
-{
- int ret, sock, index;
- struct ifreq ifr;
-
- if (!strncmp("any", ifname, strlen("any")))
- return 0;
-
- sock = af_socket(AF_INET);
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
- ret = ioctl(sock, SIOCGIFINDEX, &ifr);
- if (!ret)
- index = ifr.ifr_ifindex;
- else
- index = -1;
-
- close(sock);
-
- return index;
-}
-
-static int __device_address6(const char *ifname, struct sockaddr_storage *ss)
-{
- int ret, family, found = -EINVAL;
- struct ifaddrs *ifaddr, *ifa;
-
- ret = getifaddrs(&ifaddr);
- if (ret < 0)
- panic("Cannot get device addresses for IPv6!\n");
-
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
- family = ifa->ifa_addr->sa_family;
- if (family != AF_INET6)
- continue;
- if (strcmp(ifa->ifa_name, ifname))
- continue;
-
- memcpy(ss, ifa->ifa_addr, sizeof(*ss));
- found = 0;
- break;
- }
-
- freeifaddrs(ifaddr);
- return found;
-}
-
-int device_address(const char *ifname, int af, struct sockaddr_storage *ss)
-{
- int ret, sock;
- struct ifreq ifr;
-
- if (!ss)
- return -EINVAL;
- if (!strncmp("any", ifname, strlen("any")))
- return -EINVAL;
- if (af == AF_INET6)
- return __device_address6(ifname, ss);
-
- sock = af_socket(af);
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
- ifr.ifr_addr.sa_family = af;
-
- ret = ioctl(sock, SIOCGIFADDR, &ifr);
- if (!ret)
- memcpy(ss, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
-
- close(sock);
-
- return ret;
-}
-
-int device_mtu(const char *ifname)
-{
- int ret, sock, mtu;
- struct ifreq ifr;
-
- sock = af_socket(AF_INET);
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
- ret = ioctl(sock, SIOCGIFMTU, &ifr);
- if (!ret)
- mtu = ifr.ifr_mtu;
- else
- mtu = 0;
-
- close(sock);
-
- return mtu;
-}
-
-short device_get_flags(const char *ifname)
-{
- /* Really, it's short! Look at struct ifreq */
- short flags;
- int ret, sock;
- struct ifreq ifr;
-
- sock = af_socket(AF_INET);
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
- ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
- if (!ret)
- flags = ifr.ifr_flags;
- else
- flags = 0;
-
- close(sock);
-
- return flags;
-}
-
-void device_set_flags(const char *ifname, const short flags)
-{
- int ret, sock;
- struct ifreq ifr;
-
- sock = af_socket(AF_INET);
-
- memset(&ifr, 0, sizeof(ifr));
- strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
- ifr.ifr_flags = flags;
-
- ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
- if (ret < 0)
- panic("Cannot set NIC flags!\n");
-
- close(sock);
-}
-
void register_signal(int signal, void (*handler)(int))
{
sigset_t block_mask;
@@ -569,16 +419,6 @@ void register_signal_f(int signal, void (*handler)(int), int flags)
sigaction(signal, &saction, NULL);
}
-int device_up_and_running(char *ifname)
-{
- if (!ifname)
- return -EINVAL;
- if (!strncmp("any", ifname, strlen("any")))
- return 1;
-
- return (device_get_flags(ifname) & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING);
-}
-
void cpu_affinity(int cpu)
{
int ret;
diff --git a/xutils.h b/xutils.h
index 8cf1af2..7911110 100644
--- a/xutils.h
+++ b/xutils.h
@@ -31,14 +31,8 @@ extern int wireless_sigqual(const char *ifname, struct iw_statistics *stats);
extern int wireless_rangemax_sigqual(const char *ifname);
extern u32 wireless_bitrate(const char *ifname);
extern u32 ethtool_bitrate(const char *ifname);
-extern u32 device_bitrate(const char *ifname);
extern int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf);
extern int ethtool_link(const char *ifname);
-extern int device_mtu(const char *ifname);
-extern int device_address(const char *ifname, int af, struct sockaddr_storage *ss);
-extern int device_ifindex(const char *ifname);
-extern short device_get_flags(const char *ifname);
-extern void device_set_flags(const char *ifname, const short flags);
extern void drop_privileges(bool enforce, uid_t uid, gid_t gid);
extern void set_nonblocking(int fd);
extern int set_nonblocking_sloppy(int fd);
@@ -52,7 +46,6 @@ extern int get_system_socket_mem(int which);
extern void set_system_socket_mem(int which, int val);
extern void register_signal(int signal, void (*handler)(int));
extern void register_signal_f(int signal, void (*handler)(int), int flags);
-extern int device_up_and_running(char *ifname);
extern void set_epoll_descriptor(int fd_epoll, int action, int fd_toadd, int events);
extern int set_epoll_descriptor2(int fd_epoll, int action, int fd_toadd, int events);
extern void cpu_affinity(int cpu);