diff options
author | Daniel Borkmann <dborkman@redhat.com> | 2013-06-04 11:21:48 +0200 |
---|---|---|
committer | Daniel Borkmann <dborkman@redhat.com> | 2013-06-04 11:22:25 +0200 |
commit | a66fe185ab6a3eb7a483fd9888b54ee43328836e (patch) | |
tree | 527c53135af3c6d2c9b27e6de628c1ac5e2e2754 | |
parent | 0b65b09f15783b067343f7a2dcf67ae1f1e27dc7 (diff) |
sock: add socket management functions
Remove them from xutils, and add them to socket management.
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
-rw-r--r-- | astraceroute.c | 1 | ||||
-rw-r--r-- | astraceroute/Makefile | 1 | ||||
-rw-r--r-- | bpfc/Makefile | 1 | ||||
-rw-r--r-- | ct_client.c | 1 | ||||
-rw-r--r-- | ct_server.c | 1 | ||||
-rw-r--r-- | curvetun/Makefile | 1 | ||||
-rw-r--r-- | dev.c | 1 | ||||
-rw-r--r-- | flowtop/Makefile | 1 | ||||
-rw-r--r-- | ifpps/Makefile | 1 | ||||
-rw-r--r-- | mausezahn/Makefile | 1 | ||||
-rw-r--r-- | netsniff-ng.c | 1 | ||||
-rw-r--r-- | netsniff-ng/Makefile | 1 | ||||
-rw-r--r-- | sock.c | 176 | ||||
-rw-r--r-- | sock.h | 17 | ||||
-rw-r--r-- | stun.c | 1 | ||||
-rw-r--r-- | trafgen.c | 1 | ||||
-rw-r--r-- | trafgen/Makefile | 1 | ||||
-rw-r--r-- | xutils.c | 173 | ||||
-rw-r--r-- | xutils.h | 14 |
19 files changed, 209 insertions, 186 deletions
diff --git a/astraceroute.c b/astraceroute.c index 89bcc04..1469f8f 100644 --- a/astraceroute.c +++ b/astraceroute.c @@ -40,6 +40,7 @@ #include "xmalloc.h" #include "ioops.h" #include "csum.h" +#include "sock.h" #include "geoip.h" #include "xutils.h" #include "ring_rx.h" diff --git a/astraceroute/Makefile b/astraceroute/Makefile index 8add1cd..cc0a3ce 100644 --- a/astraceroute/Makefile +++ b/astraceroute/Makefile @@ -9,6 +9,7 @@ astraceroute-objs = xmalloc.o \ tprintf.o \ bpf.o \ str.o \ + sock.o \ dev.o \ geoip.o \ ring_rx.o \ diff --git a/bpfc/Makefile b/bpfc/Makefile index efebcc9..61107bf 100644 --- a/bpfc/Makefile +++ b/bpfc/Makefile @@ -3,6 +3,7 @@ bpfc-libs = bpfc-objs = xmalloc.o \ xutils.o \ str.o \ + sock.o \ bpf.o \ bpf_lexer.yy.o \ bpf_parser.tab.o \ diff --git a/ct_client.c b/ct_client.c index 1b3057b..50f5df4 100644 --- a/ct_client.c +++ b/ct_client.c @@ -28,6 +28,7 @@ #include "built_in.h" #include "die.h" #include "str.h" +#include "sock.h" #include "ioops.h" #include "xutils.h" #include "curve.h" diff --git a/ct_server.c b/ct_server.c index 565eb1f..89ed815 100644 --- a/ct_server.c +++ b/ct_server.c @@ -37,6 +37,7 @@ #include "ioexact.h" #include "corking.h" #include "cpus.h" +#include "sock.h" #include "built_in.h" #include "ct_usermgmt.h" #include "cpusched.h" diff --git a/curvetun/Makefile b/curvetun/Makefile index 970bfa8..b2693ed 100644 --- a/curvetun/Makefile +++ b/curvetun/Makefile @@ -6,6 +6,7 @@ curvetun-objs = xmalloc.o \ str.o \ dev.o \ stun.o \ + sock.o \ patricia.o \ corking.o \ trie.o \ @@ -6,6 +6,7 @@ #include "dev.h" #include "str.h" +#include "sock.h" #include "die.h" #include "xutils.h" diff --git a/flowtop/Makefile b/flowtop/Makefile index 53ecd4e..c909a40 100644 --- a/flowtop/Makefile +++ b/flowtop/Makefile @@ -10,6 +10,7 @@ flowtop-objs = xmalloc.o \ xutils.o \ oui.o \ str.o \ + sock.o \ dev.o \ hash.o \ dissector_eth.o \ diff --git a/ifpps/Makefile b/ifpps/Makefile index 49a50d0..637db76 100644 --- a/ifpps/Makefile +++ b/ifpps/Makefile @@ -5,5 +5,6 @@ ifpps-objs = xmalloc.o \ xutils.o \ promisc.o \ str.o \ + sock.o \ dev.o \ ifpps.o diff --git a/mausezahn/Makefile b/mausezahn/Makefile index cecd139..2e9e417 100644 --- a/mausezahn/Makefile +++ b/mausezahn/Makefile @@ -7,6 +7,7 @@ mausezahn-libs = -lcli \ mausezahn-objs = xutils.o \ str.o \ + sock.o \ staging/layer1.o \ staging/layer2.o \ staging/layer3.o \ diff --git a/netsniff-ng.c b/netsniff-ng.c index 234d781..9eb482d 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -37,6 +37,7 @@ #include "die.h" #include "irq.h" #include "str.h" +#include "sock.h" #include "geoip.h" #include "lockme.h" #include "tprintf.h" diff --git a/netsniff-ng/Makefile b/netsniff-ng/Makefile index 104a42f..9dfa8d2 100644 --- a/netsniff-ng/Makefile +++ b/netsniff-ng/Makefile @@ -37,6 +37,7 @@ netsniff-ng-objs = dissector.o \ proc.o \ dev.o \ str.o \ + sock.o \ irq.o \ iosched.o \ ioops.o \ @@ -0,0 +1,176 @@ +#include <sys/types.h> +#include <sys/socket.h> +#include <fcntl.h> +#include <arpa/inet.h> +#include <linux/if_ether.h> +#include <linux/tcp.h> + +#include "sock.h" +#include "die.h" +#include "str.h" +#include "built_in.h" + +int af_socket(int af) +{ + int sock; + + if (unlikely(af != AF_INET && af != AF_INET6)) + panic("Wrong AF socket type!\n"); + + sock = socket(af, SOCK_DGRAM, 0); + if (unlikely(sock < 0)) + panic("Creation AF socket failed!\n"); + + return sock; +} + +int pf_socket(void) +{ + int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); + if (unlikely(sock < 0)) + panic("Creation of PF socket failed!\n"); + + return sock; +} + +void set_sock_prio(int fd, int prio) +{ + int ret, val = prio; + + ret = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &val, sizeof(val)); + if (unlikely(ret)) + panic("Cannot set socket priority!\n"); +} + +void set_nonblocking(int fd) +{ + int ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK); + if (unlikely(ret < 0)) + panic("Cannot fcntl!\n"); +} + +int set_nonblocking_sloppy(int fd) +{ + return fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK); +} + +void set_socket_keepalive(int fd) +{ + int ret, one = 1; + + ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); + if (unlikely(ret)) + panic("Cannot set TCP keepalive!\n"); +} + +void set_tcp_nodelay(int fd) +{ + int one = 1; + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); +} + +int set_ipv6_only(int fd) +{ + int one = 1; + return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); +} + +int set_reuseaddr(int fd) +{ + int ret, one = 1; + + ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + if (unlikely(ret < 0)) + panic("Cannot reuse addr!\n"); + + return 0; +} + +void set_mtu_disc_dont(int fd) +{ + int mtu = IP_PMTUDISC_DONT, ret; + + ret = setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu)); + if (unlikely(ret)) + panic("Cannot set MTU discovery options!\n"); +} + +enum { + sock_rmem_max = 0, + sock_rmem_def, + sock_wmem_max, + sock_wmem_def, +}; + +#define SMEM_SUG_MAX 104857600 +#define SMEM_SUG_DEF 4194304 + +static const char *const sock_mem[] = { + [sock_rmem_max] = "/proc/sys/net/core/rmem_max", + [sock_rmem_def] = "/proc/sys/net/core/rmem_default", + [sock_wmem_max] = "/proc/sys/net/core/wmem_max", + [sock_wmem_def] = "/proc/sys/net/core/wmem_default", +}; + +static int get_system_socket_mem(int which) +{ + int fd, val = -1; + ssize_t ret; + const char *file = sock_mem[which]; + char buff[64]; + + fd = open(file, O_RDONLY); + if (fd < 0) + return val; + + ret = read(fd, buff, sizeof(buff)); + if (ret > 0) + val = atoi(buff); + + close(fd); + return val; +} + +static void set_system_socket_mem(int which, int val) +{ + int fd; + const char *file = sock_mem[which]; + ssize_t ret; + char buff[64]; + + fd = open(file, O_WRONLY); + if (fd < 0) + return; + + memset(buff, 0, sizeof(buff)); + slprintf(buff, sizeof(buff), "%d", val); + + ret = write(fd, buff, strlen(buff)); + ret = ret; + + close(fd); +} + +void set_system_socket_memory(int *vals, size_t len) +{ + bug_on(len != 4); + + if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) + set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); + if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) + set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); + if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) + set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); + if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) + set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); +} + +void reset_system_socket_memory(int *vals, size_t len) +{ + bug_on(len != 4); + + set_system_socket_mem(sock_rmem_max, vals[0]); + set_system_socket_mem(sock_rmem_def, vals[1]); + set_system_socket_mem(sock_wmem_max, vals[2]); + set_system_socket_mem(sock_wmem_def, vals[3]); +} @@ -0,0 +1,17 @@ +#ifndef SOCK_H +#define SOCK_H + +extern int af_socket(int af); +extern int pf_socket(void); +extern void set_nonblocking(int fd); +extern int set_nonblocking_sloppy(int fd); +extern int set_reuseaddr(int fd); +extern void set_sock_prio(int fd, int prio); +extern void set_tcp_nodelay(int fd); +extern void set_socket_keepalive(int fd); +extern int set_ipv6_only(int fd); +extern void set_mtu_disc_dont(int fd); +extern void set_system_socket_memory(int *vals, size_t len); +extern void reset_system_socket_memory(int *vals, size_t len); + +#endif /* SOCK_H */ @@ -21,6 +21,7 @@ #include "xmalloc.h" #include "xutils.h" #include "die.h" +#include "sock.h" extern int print_stun_probe(char *server, int sport, int tport); @@ -35,6 +35,7 @@ #include "xmalloc.h" #include "die.h" #include "str.h" +#include "sock.h" #include "cpus.h" #include "lockme.h" #include "privs.h" diff --git a/trafgen/Makefile b/trafgen/Makefile index c6e8591..0ba1953 100644 --- a/trafgen/Makefile +++ b/trafgen/Makefile @@ -10,6 +10,7 @@ trafgen-objs = xmalloc.o \ dev.o \ irq.o \ str.o \ + sock.o \ mac80211.o \ ring_tx.o \ ring.o \ @@ -44,117 +44,9 @@ #include "str.h" #include "xutils.h" #include "ring.h" +#include "sock.h" #include "built_in.h" -enum { - sock_rmem_max = 0, - sock_rmem_def, - sock_wmem_max, - sock_wmem_def, -}; - -#define SMEM_SUG_MAX 104857600 -#define SMEM_SUG_DEF 4194304 - -static const char *const to_prio[] = { - "none", - "realtime", - "best-effort", - "idle", -}; - -static const char *const sock_mem[] = { - "/proc/sys/net/core/rmem_max", - "/proc/sys/net/core/rmem_default", - "/proc/sys/net/core/wmem_max", - "/proc/sys/net/core/wmem_default", -}; - -int af_socket(int af) -{ - int sock; - - if (unlikely(af != AF_INET && af != AF_INET6)) - panic("Wrong AF socket type!\n"); - - sock = socket(af, SOCK_DGRAM, 0); - if (unlikely(sock < 0)) - panic("Creation AF socket failed!\n"); - - return sock; -} - -int pf_socket(void) -{ - int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); - if (unlikely(sock < 0)) - panic("Creation of PF socket failed!\n"); - - return sock; -} - -void set_sock_prio(int fd, int prio) -{ - int ret, val = prio; - - ret = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &val, sizeof(val)); - if (unlikely(ret)) - panic("Cannot set socket priority!\n"); -} - -void set_nonblocking(int fd) -{ - int ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK); - if (unlikely(ret < 0)) - panic("Cannot fcntl!\n"); -} - -int set_nonblocking_sloppy(int fd) -{ - return fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK); -} - -void set_socket_keepalive(int fd) -{ - int ret, one = 1; - - ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); - if (unlikely(ret)) - panic("Cannot set TCP keepalive!\n"); -} - -void set_tcp_nodelay(int fd) -{ - int one = 1; - setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); -} - -int set_ipv6_only(int fd) -{ - int one = 1; - return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); -} - -int set_reuseaddr(int fd) -{ - int ret, one = 1; - - ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); - if (unlikely(ret < 0)) - panic("Cannot reuse addr!\n"); - - return 0; -} - -void set_mtu_disc_dont(int fd) -{ - int mtu = IP_PMTUDISC_DONT, ret; - - ret = setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu)); - if (unlikely(ret)) - panic("Cannot set MTU discovery options!\n"); -} - void set_epoll_descriptor(int fd_epoll, int action, int fd_toadd, int events) { int ret; @@ -201,45 +93,6 @@ u32 wireless_bitrate(const char *ifname) return rate_in_mbit; } -int get_system_socket_mem(int which) -{ - int fd, val = -1; - ssize_t ret; - const char *file = sock_mem[which]; - char buff[64]; - - fd = open(file, O_RDONLY); - if (fd < 0) - return val; - - ret = read(fd, buff, sizeof(buff)); - if (ret > 0) - val = atoi(buff); - - close(fd); - return val; -} - -void set_system_socket_mem(int which, int val) -{ - int fd; - const char *file = sock_mem[which]; - ssize_t ret; - char buff[64]; - - fd = open(file, O_WRONLY); - if (fd < 0) - return; - - memset(buff, 0, sizeof(buff)); - slprintf(buff, sizeof(buff), "%d", val); - - ret = write(fd, buff, strlen(buff)); - ret = ret; - - close(fd); -} - int wireless_sigqual(const char *ifname, struct iw_statistics *stats) { int ret, sock; @@ -405,30 +258,6 @@ void register_signal_f(int signal, void (*handler)(int), int flags) sigaction(signal, &saction, NULL); } -void set_system_socket_memory(int *vals, size_t len) -{ - bug_on(len != 4); - - if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) - set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); - if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) - set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); - if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) - set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); - if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) - set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); -} - -void reset_system_socket_memory(int *vals, size_t len) -{ - bug_on(len != 4); - - set_system_socket_mem(sock_rmem_max, vals[0]); - set_system_socket_mem(sock_rmem_def, vals[1]); - set_system_socket_mem(sock_wmem_max, vals[2]); - set_system_socket_mem(sock_wmem_def, vals[3]); -} - void set_itimer_interval_value(struct itimerval *itimer, unsigned long sec, unsigned long usec) { @@ -25,30 +25,16 @@ #include "built_in.h" -extern int af_socket(int af); -extern int pf_socket(void); 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 set_nonblocking(int fd); -extern int set_nonblocking_sloppy(int fd); -extern int set_reuseaddr(int fd); -extern void set_sock_prio(int fd, int prio); -extern void set_tcp_nodelay(int fd); -extern void set_socket_keepalive(int fd); -extern int set_ipv6_only(int fd); -extern void set_mtu_disc_dont(int fd); -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 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 set_system_socket_memory(int *vals, size_t len); -extern void reset_system_socket_memory(int *vals, size_t len); extern void set_itimer_interval_value(struct itimerval *itimer, unsigned long sec, unsigned long usec); |