From ab9d73629872a474d9194177fa4c9c8cdabe9a44 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Tue, 4 Jun 2013 11:32:19 +0200 Subject: link: add link management functions Same here as usual, break out link functions from xutils. Signed-off-by: Daniel Borkmann --- astraceroute/Makefile | 1 + curvetun/Makefile | 1 + dev.c | 1 + flowtop/Makefile | 1 + ifpps.c | 1 + ifpps/Makefile | 3 +- link.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ link.h | 18 ++++++ netsniff-ng/Makefile | 1 + trafgen/Makefile | 1 + xutils.c | 158 ----------------------------------------------- xutils.h | 6 -- 12 files changed, 192 insertions(+), 166 deletions(-) create mode 100644 link.c create mode 100644 link.h diff --git a/astraceroute/Makefile b/astraceroute/Makefile index cc0a3ce..ba45395 100644 --- a/astraceroute/Makefile +++ b/astraceroute/Makefile @@ -10,6 +10,7 @@ astraceroute-objs = xmalloc.o \ bpf.o \ str.o \ sock.o \ + link.o \ dev.o \ geoip.o \ ring_rx.o \ diff --git a/curvetun/Makefile b/curvetun/Makefile index b2693ed..111664c 100644 --- a/curvetun/Makefile +++ b/curvetun/Makefile @@ -7,6 +7,7 @@ curvetun-objs = xmalloc.o \ dev.o \ stun.o \ sock.o \ + link.o \ patricia.o \ corking.o \ trie.o \ diff --git a/dev.c b/dev.c index 74e07a1..cd18d9b 100644 --- a/dev.c +++ b/dev.c @@ -8,6 +8,7 @@ #include "str.h" #include "sock.h" #include "die.h" +#include "link.h" #include "xutils.h" int device_ifindex(const char *ifname) diff --git a/flowtop/Makefile b/flowtop/Makefile index c909a40..ba22803 100644 --- a/flowtop/Makefile +++ b/flowtop/Makefile @@ -12,6 +12,7 @@ flowtop-objs = xmalloc.o \ str.o \ sock.o \ dev.o \ + link.o \ hash.o \ dissector_eth.o \ dissector_80211.o \ diff --git a/ifpps.c b/ifpps.c index 2d0a5b0..bdda39d 100644 --- a/ifpps.c +++ b/ifpps.c @@ -19,6 +19,7 @@ #include "die.h" #include "dev.h" +#include "link.h" #include "xmalloc.h" #include "xutils.h" #include "ioops.h" diff --git a/ifpps/Makefile b/ifpps/Makefile index 637db76..6088ec6 100644 --- a/ifpps/Makefile +++ b/ifpps/Makefile @@ -2,9 +2,8 @@ ifpps-libs = $(shell pkg-config --libs ncurses) ifpps-objs = xmalloc.o \ ioops.o \ - xutils.o \ promisc.o \ str.o \ - sock.o \ + link.o \ dev.o \ ifpps.o diff --git a/link.c b/link.c new file mode 100644 index 0000000..95f0816 --- /dev/null +++ b/link.c @@ -0,0 +1,166 @@ +#include +#include +#include +#include + +#include "link.h" +#include "sock.h" +#include "str.h" + +u32 wireless_bitrate(const char *ifname) +{ + int sock, ret, rate_in_mbit; + struct iwreq iwr; + + sock = af_socket(AF_INET); + + memset(&iwr, 0, sizeof(iwr)); + strlcpy(iwr.ifr_name, ifname, IFNAMSIZ); + + ret = ioctl(sock, SIOCGIWRATE, &iwr); + if (!ret) + rate_in_mbit = iwr.u.bitrate.value / 1000000; + else + rate_in_mbit = 0; + + close(sock); + + return rate_in_mbit; +} + +int wireless_sigqual(const char *ifname, struct iw_statistics *stats) +{ + int ret, sock; + struct iwreq iwr; + + sock = af_socket(AF_INET); + + memset(&iwr, 0, sizeof(iwr)); + strlcpy(iwr.ifr_name, ifname, IFNAMSIZ); + + iwr.u.data.pointer = (caddr_t) stats; + iwr.u.data.length = sizeof(*stats); + iwr.u.data.flags = 1; + + ret = ioctl(sock, SIOCGIWSTATS, &iwr); + + close(sock); + + return ret; +} + +int wireless_rangemax_sigqual(const char *ifname) +{ + int ret, sock, sigqual; + struct iwreq iwr; + struct iw_range iwrange; + + sock = af_socket(AF_INET); + + memset(&iwrange, 0, sizeof(iwrange)); + + memset(&iwr, 0, sizeof(iwr)); + strlcpy(iwr.ifr_name, ifname, IFNAMSIZ); + + iwr.u.data.pointer = (caddr_t) &iwrange; + iwr.u.data.length = sizeof(iwrange); + iwr.u.data.flags = 0; + + ret = ioctl(sock, SIOCGIWRANGE, &iwr); + if (!ret) + sigqual = iwrange.max_qual.qual; + else + sigqual = 0; + + close(sock); + + return sigqual; +} + +u32 ethtool_bitrate(const char *ifname) +{ + int ret, sock, bitrate; + struct ifreq ifr; + struct ethtool_cmd ecmd; + + sock = af_socket(AF_INET); + + memset(&ecmd, 0, sizeof(ecmd)); + + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); + + ecmd.cmd = ETHTOOL_GSET; + ifr.ifr_data = (char *) &ecmd; + + ret = ioctl(sock, SIOCETHTOOL, &ifr); + if (ret) { + bitrate = 0; + goto out; + } + + switch (ecmd.speed) { + case SPEED_10: + case SPEED_100: + case SPEED_1000: + case SPEED_2500: + case SPEED_10000: + bitrate = ecmd.speed; + break; + default: + bitrate = 0; + break; + }; +out: + close(sock); + + return bitrate; +} + +int ethtool_link(const char *ifname) +{ + int ret, sock; + struct ifreq ifr; + struct ethtool_value ecmd; + + sock = af_socket(AF_INET); + + memset(&ecmd, 0, sizeof(ecmd)); + + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); + + ecmd.cmd = ETHTOOL_GLINK; + ifr.ifr_data = (char *) &ecmd; + + ret = ioctl(sock, SIOCETHTOOL, &ifr); + if (ret) + ret = -EINVAL; + else + ret = !!ecmd.data; + + close(sock); + return ret; +} + +int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf) +{ + int ret, sock; + struct ifreq ifr; + + sock = af_socket(AF_INET); + + memset(drvinf, 0, sizeof(*drvinf)); + + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); + + drvinf->cmd = ETHTOOL_GDRVINFO; + ifr.ifr_data = (char *) drvinf; + + ret = ioctl(sock, SIOCETHTOOL, &ifr); + + close(sock); + + return ret; +} diff --git a/link.h b/link.h new file mode 100644 index 0000000..67ec433 --- /dev/null +++ b/link.h @@ -0,0 +1,18 @@ +#ifndef LINK_H +#define LINK_H + +#include +#include +#include +#include + +#include "built_in.h" + +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 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf); +extern int ethtool_link(const char *ifname); + +#endif /* LINK_H */ diff --git a/netsniff-ng/Makefile b/netsniff-ng/Makefile index 9dfa8d2..02c6120 100644 --- a/netsniff-ng/Makefile +++ b/netsniff-ng/Makefile @@ -41,6 +41,7 @@ netsniff-ng-objs = dissector.o \ irq.o \ iosched.o \ ioops.o \ + link.o \ xutils.o \ xmalloc.o \ hash.o \ diff --git a/trafgen/Makefile b/trafgen/Makefile index 0ba1953..69db433 100644 --- a/trafgen/Makefile +++ b/trafgen/Makefile @@ -9,6 +9,7 @@ trafgen-objs = xmalloc.o \ proc.o \ dev.o \ irq.o \ + link.o \ str.o \ sock.o \ mac80211.o \ diff --git a/xutils.c b/xutils.c index e4cc1a4..d53a3e7 100644 --- a/xutils.c +++ b/xutils.c @@ -72,164 +72,6 @@ int set_epoll_descriptor2(int fd_epoll, int action, int fd_toadd, int events) return epoll_ctl(fd_epoll, action, fd_toadd, &ev); } -u32 wireless_bitrate(const char *ifname) -{ - int sock, ret, rate_in_mbit; - struct iwreq iwr; - - sock = af_socket(AF_INET); - - memset(&iwr, 0, sizeof(iwr)); - strlcpy(iwr.ifr_name, ifname, IFNAMSIZ); - - ret = ioctl(sock, SIOCGIWRATE, &iwr); - if (!ret) - rate_in_mbit = iwr.u.bitrate.value / 1000000; - else - rate_in_mbit = 0; - - close(sock); - - return rate_in_mbit; -} - -int wireless_sigqual(const char *ifname, struct iw_statistics *stats) -{ - int ret, sock; - struct iwreq iwr; - - sock = af_socket(AF_INET); - - memset(&iwr, 0, sizeof(iwr)); - strlcpy(iwr.ifr_name, ifname, IFNAMSIZ); - - iwr.u.data.pointer = (caddr_t) stats; - iwr.u.data.length = sizeof(*stats); - iwr.u.data.flags = 1; - - ret = ioctl(sock, SIOCGIWSTATS, &iwr); - - close(sock); - - return ret; -} - -int wireless_rangemax_sigqual(const char *ifname) -{ - int ret, sock, sigqual; - struct iwreq iwr; - struct iw_range iwrange; - - sock = af_socket(AF_INET); - - memset(&iwrange, 0, sizeof(iwrange)); - - memset(&iwr, 0, sizeof(iwr)); - strlcpy(iwr.ifr_name, ifname, IFNAMSIZ); - - iwr.u.data.pointer = (caddr_t) &iwrange; - iwr.u.data.length = sizeof(iwrange); - iwr.u.data.flags = 0; - - ret = ioctl(sock, SIOCGIWRANGE, &iwr); - if (!ret) - sigqual = iwrange.max_qual.qual; - else - sigqual = 0; - - close(sock); - - return sigqual; -} - -u32 ethtool_bitrate(const char *ifname) -{ - int ret, sock, bitrate; - struct ifreq ifr; - struct ethtool_cmd ecmd; - - sock = af_socket(AF_INET); - - memset(&ecmd, 0, sizeof(ecmd)); - - memset(&ifr, 0, sizeof(ifr)); - strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); - - ecmd.cmd = ETHTOOL_GSET; - ifr.ifr_data = (char *) &ecmd; - - ret = ioctl(sock, SIOCETHTOOL, &ifr); - if (ret) { - bitrate = 0; - goto out; - } - - switch (ecmd.speed) { - case SPEED_10: - case SPEED_100: - case SPEED_1000: - case SPEED_2500: - case SPEED_10000: - bitrate = ecmd.speed; - break; - default: - bitrate = 0; - break; - }; -out: - close(sock); - - return bitrate; -} - -int ethtool_link(const char *ifname) -{ - int ret, sock; - struct ifreq ifr; - struct ethtool_value ecmd; - - sock = af_socket(AF_INET); - - memset(&ecmd, 0, sizeof(ecmd)); - - memset(&ifr, 0, sizeof(ifr)); - strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); - - ecmd.cmd = ETHTOOL_GLINK; - ifr.ifr_data = (char *) &ecmd; - - ret = ioctl(sock, SIOCETHTOOL, &ifr); - if (ret) - ret = -EINVAL; - else - ret = !!ecmd.data; - - close(sock); - return ret; -} - -int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf) -{ - int ret, sock; - struct ifreq ifr; - - sock = af_socket(AF_INET); - - memset(drvinf, 0, sizeof(*drvinf)); - - memset(&ifr, 0, sizeof(ifr)); - strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); - - drvinf->cmd = ETHTOOL_GDRVINFO; - ifr.ifr_data = (char *) drvinf; - - ret = ioctl(sock, SIOCETHTOOL, &ifr); - - close(sock); - - return ret; -} - void register_signal(int signal, void (*handler)(int)) { sigset_t block_mask; diff --git a/xutils.h b/xutils.h index bfb24f1..b2287bb 100644 --- a/xutils.h +++ b/xutils.h @@ -25,12 +25,6 @@ #include "built_in.h" -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 int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf); -extern int ethtool_link(const char *ifname); extern void register_signal(int signal, void (*handler)(int)); extern void register_signal_f(int signal, void (*handler)(int), int flags); extern void set_epoll_descriptor(int fd_epoll, int action, int fd_toadd, int events); -- cgit v1.2.3-54-g00ecf