diff options
author | Daniel Borkmann <dborkman@redhat.com> | 2013-06-04 11:32:19 +0200 |
---|---|---|
committer | Daniel Borkmann <dborkman@redhat.com> | 2013-06-04 11:32:19 +0200 |
commit | ab9d73629872a474d9194177fa4c9c8cdabe9a44 (patch) | |
tree | be238632f09ab058370c086dfb6feee32006b7ff | |
parent | a66fe185ab6a3eb7a483fd9888b54ee43328836e (diff) |
link: add link management functions
Same here as usual, break out link functions from xutils.
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
-rw-r--r-- | astraceroute/Makefile | 1 | ||||
-rw-r--r-- | curvetun/Makefile | 1 | ||||
-rw-r--r-- | dev.c | 1 | ||||
-rw-r--r-- | flowtop/Makefile | 1 | ||||
-rw-r--r-- | ifpps.c | 1 | ||||
-rw-r--r-- | ifpps/Makefile | 3 | ||||
-rw-r--r-- | link.c | 166 | ||||
-rw-r--r-- | link.h | 18 | ||||
-rw-r--r-- | netsniff-ng/Makefile | 1 | ||||
-rw-r--r-- | trafgen/Makefile | 1 | ||||
-rw-r--r-- | xutils.c | 158 | ||||
-rw-r--r-- | xutils.h | 6 |
12 files changed, 192 insertions, 166 deletions
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 \ @@ -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 \ @@ -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 @@ -0,0 +1,166 @@ +#include <sys/ioctl.h> +#include <linux/sockios.h> +#include <unistd.h> +#include <errno.h> + +#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; +} @@ -0,0 +1,18 @@ +#ifndef LINK_H +#define LINK_H + +#include <stdint.h> +#include <sys/socket.h> +#include <linux/ethtool.h> +#include <linux/wireless.h> + +#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 \ @@ -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; @@ -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); |