From a66fe185ab6a3eb7a483fd9888b54ee43328836e Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Tue, 4 Jun 2013 11:21:48 +0200 Subject: sock: add socket management functions Remove them from xutils, and add them to socket management. Signed-off-by: Daniel Borkmann --- astraceroute.c | 1 + astraceroute/Makefile | 1 + bpfc/Makefile | 1 + ct_client.c | 1 + ct_server.c | 1 + curvetun/Makefile | 1 + dev.c | 1 + flowtop/Makefile | 1 + ifpps/Makefile | 1 + mausezahn/Makefile | 1 + netsniff-ng.c | 1 + netsniff-ng/Makefile | 1 + sock.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++ sock.h | 17 +++++ stun.c | 1 + trafgen.c | 1 + trafgen/Makefile | 1 + xutils.c | 173 +------------------------------------------------ xutils.h | 14 ---- 19 files changed, 209 insertions(+), 186 deletions(-) create mode 100644 sock.c create mode 100644 sock.h 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 \ diff --git a/dev.c b/dev.c index 56a1a51..74e07a1 100644 --- a/dev.c +++ b/dev.c @@ -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 \ diff --git a/sock.c b/sock.c new file mode 100644 index 0000000..40e4504 --- /dev/null +++ b/sock.c @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include + +#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]); +} diff --git a/sock.h b/sock.h new file mode 100644 index 0000000..007bf2a --- /dev/null +++ b/sock.h @@ -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 */ diff --git a/stun.c b/stun.c index 5892ad5..4b8dd62 100644 --- a/stun.c +++ b/stun.c @@ -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); diff --git a/trafgen.c b/trafgen.c index e725af8..c36a2d1 100644 --- a/trafgen.c +++ b/trafgen.c @@ -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 \ diff --git a/xutils.c b/xutils.c index e07ce9e..e4cc1a4 100644 --- a/xutils.c +++ b/xutils.c @@ -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) { diff --git a/xutils.h b/xutils.h index 83d710a..bfb24f1 100644 --- a/xutils.h +++ b/xutils.h @@ -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); -- cgit v1.2.3-54-g00ecf