diff options
-rw-r--r-- | astraceroute.c | 1 | ||||
-rw-r--r-- | astraceroute/Makefile | 1 | ||||
-rw-r--r-- | curvetun/Makefile | 1 | ||||
-rw-r--r-- | dev.c | 169 | ||||
-rw-r--r-- | dev.h | 15 | ||||
-rw-r--r-- | flowtop/Makefile | 1 | ||||
-rw-r--r-- | ifpps.c | 1 | ||||
-rw-r--r-- | ifpps/Makefile | 1 | ||||
-rw-r--r-- | ioops.c | 1 | ||||
-rw-r--r-- | mac80211.c | 1 | ||||
-rw-r--r-- | netsniff-ng/Makefile | 1 | ||||
-rw-r--r-- | promisc.c | 4 | ||||
-rw-r--r-- | ring.h | 1 | ||||
-rw-r--r-- | trafgen/Makefile | 1 | ||||
-rw-r--r-- | xutils.c | 160 | ||||
-rw-r--r-- | xutils.h | 7 |
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 \ @@ -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); +} @@ -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 \ @@ -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 @@ -9,6 +9,7 @@ #include <linux/if_tun.h> #include "die.h" +#include "dev.h" #include "ioops.h" #include "str.h" #include "xutils.h" @@ -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 \ @@ -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) { @@ -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 \ @@ -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; @@ -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); |