From 9e4cf71371187927a6eb7cc4fe32b67d58a93010 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Tue, 4 Jun 2013 00:04:15 +0200 Subject: xutils: break out string handling and locking Break out all string handling functions and lockme stuff in order to further eliminate the big code blob in xutils, so that it can be easier maintained. Signed-off-by: Daniel Borkmann --- astraceroute/Makefile | 1 + bpf.c | 1 + bpf_parser.y | 1 + bpfc/Makefile | 1 + ct_client.c | 1 + ct_servmgmt.c | 1 + ct_usermgmt.c | 1 + curve.c | 1 + curvetun.c | 1 + curvetun/Makefile | 1 + dissector_eth.c | 1 + flowtop.c | 1 + flowtop/Makefile | 1 + geoip.c | 1 + ifpps/Makefile | 1 + irq.c | 1 + lockme.h | 19 +++++++++++ mac80211.c | 1 + mausezahn/Makefile | 1 + netsniff-ng.c | 2 ++ netsniff-ng/Makefile | 1 + oui.c | 1 + str.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ str.h | 12 +++++++ trafgen.c | 2 ++ trafgen/Makefile | 1 + trafgen_parser.y | 1 + tstamping.c | 1 + xio.c | 1 + xmalloc.c | 1 + xutils.c | 89 +-------------------------------------------------- xutils.h | 7 ---- 32 files changed, 149 insertions(+), 95 deletions(-) create mode 100644 lockme.h create mode 100644 str.c create mode 100644 str.h diff --git a/astraceroute/Makefile b/astraceroute/Makefile index 36e37f0..4cf9a3e 100644 --- a/astraceroute/Makefile +++ b/astraceroute/Makefile @@ -8,6 +8,7 @@ astraceroute-objs = xmalloc.o \ proto_none.o \ tprintf.o \ bpf.o \ + str.o \ geoip.o \ ring_rx.o \ ring.o \ diff --git a/bpf.c b/bpf.c index 3809444..b64b909 100644 --- a/bpf.c +++ b/bpf.c @@ -18,6 +18,7 @@ #include "xmalloc.h" #include "xutils.h" #include "die.h" +#include "str.h" #define EXTRACT_SHORT(packet) \ ((unsigned short) ntohs(*(unsigned short *) packet)) diff --git a/bpf_parser.y b/bpf_parser.y index 7734fb3..307066e 100644 --- a/bpf_parser.y +++ b/bpf_parser.y @@ -19,6 +19,7 @@ #include #include "bpf.h" +#include "str.h" #include "xmalloc.h" #include "bpf_parser.tab.h" #include "built_in.h" diff --git a/bpfc/Makefile b/bpfc/Makefile index 4451f64..efebcc9 100644 --- a/bpfc/Makefile +++ b/bpfc/Makefile @@ -2,6 +2,7 @@ bpfc-libs = bpfc-objs = xmalloc.o \ xutils.o \ + str.o \ bpf.o \ bpf_lexer.yy.o \ bpf_parser.tab.o \ diff --git a/ct_client.c b/ct_client.c index dbf07c5..d99e7bb 100644 --- a/ct_client.c +++ b/ct_client.c @@ -27,6 +27,7 @@ #include "built_in.h" #include "die.h" +#include "str.h" #include "xio.h" #include "xutils.h" #include "curve.h" diff --git a/ct_servmgmt.c b/ct_servmgmt.c index a63eb57..0d7da78 100644 --- a/ct_servmgmt.c +++ b/ct_servmgmt.c @@ -12,6 +12,7 @@ #include #include "die.h" +#include "str.h" #include "built_in.h" #include "locking.h" #include "xmalloc.h" diff --git a/ct_usermgmt.c b/ct_usermgmt.c index c728801..6dc7f35 100644 --- a/ct_usermgmt.c +++ b/ct_usermgmt.c @@ -19,6 +19,7 @@ #include "locking.h" #include "xmalloc.h" #include "xio.h" +#include "str.h" #include "curvetun.h" #include "xutils.h" #include "curve.h" diff --git a/curve.c b/curve.c index 229cd76..1b05f0c 100644 --- a/curve.c +++ b/curve.c @@ -22,6 +22,7 @@ #include "xutils.h" #include "xio.h" #include "die.h" +#include "str.h" #include "curvetun.h" #include "locking.h" #include "crypto_verify_32.h" diff --git a/curvetun.c b/curvetun.c index 29bee4a..b2cc9b5 100644 --- a/curvetun.c +++ b/curvetun.c @@ -27,6 +27,7 @@ #include "xutils.h" #include "die.h" +#include "str.h" #include "xmalloc.h" #include "curvetun.h" #include "curve.h" diff --git a/curvetun/Makefile b/curvetun/Makefile index 954a865..c54f8e2 100644 --- a/curvetun/Makefile +++ b/curvetun/Makefile @@ -4,6 +4,7 @@ curvetun-libs = -lnacl \ curvetun-objs = xmalloc.o \ xio.o \ xutils.o \ + str.o \ stun.o \ patricia.o \ trie.o \ diff --git a/dissector_eth.c b/dissector_eth.c index 0ac88af..b8166e4 100644 --- a/dissector_eth.c +++ b/dissector_eth.c @@ -8,6 +8,7 @@ #include "hash.h" #include "oui.h" +#include "str.h" #include "protos.h" #include "pkt_buff.h" #include "dissector.h" diff --git a/flowtop.c b/flowtop.c index 134be9f..1c51486 100644 --- a/flowtop.c +++ b/flowtop.c @@ -30,6 +30,7 @@ #include "die.h" #include "xmalloc.h" #include "xio.h" +#include "str.h" #include "geoip.h" #include "xutils.h" #include "built_in.h" diff --git a/flowtop/Makefile b/flowtop/Makefile index 29a58ce..2446832 100644 --- a/flowtop/Makefile +++ b/flowtop/Makefile @@ -9,6 +9,7 @@ flowtop-objs = xmalloc.o \ xio.o \ xutils.o \ oui.o \ + str.o \ hash.o \ dissector_eth.o \ dissector_80211.o \ diff --git a/geoip.c b/geoip.c index b2121e7..85c425c 100644 --- a/geoip.c +++ b/geoip.c @@ -20,6 +20,7 @@ #include "die.h" #include "xutils.h" #include "xio.h" +#include "str.h" #include "xmalloc.h" #include "zlib.h" #include "geoip.h" diff --git a/ifpps/Makefile b/ifpps/Makefile index a86e88c..ef26a71 100644 --- a/ifpps/Makefile +++ b/ifpps/Makefile @@ -3,4 +3,5 @@ ifpps-libs = $(shell pkg-config --libs ncurses) ifpps-objs = xmalloc.o \ xio.o \ xutils.o \ + str.o \ ifpps.o diff --git a/irq.c b/irq.c index fbd1cbc..fc2427b 100644 --- a/irq.c +++ b/irq.c @@ -14,6 +14,7 @@ #include #include "irq.h" +#include "str.h" #include "die.h" #include "xutils.h" diff --git a/lockme.h b/lockme.h new file mode 100644 index 0000000..7cce97b --- /dev/null +++ b/lockme.h @@ -0,0 +1,19 @@ +#ifndef LOCKME_H +#define LOCKME_H + +#include + +#include "die.h" + +static inline void xlockme(void) +{ + if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) + panic("Cannot lock pages!\n"); +} + +static inline void xunlockme(void) +{ + munlockall(); +} + +#endif /* LOCKME_H */ diff --git a/mac80211.c b/mac80211.c index 0b46679..32f75f1 100644 --- a/mac80211.c +++ b/mac80211.c @@ -23,6 +23,7 @@ #include #include "die.h" +#include "str.h" #include "xutils.h" #include "mac80211.h" #include "xmalloc.h" diff --git a/mausezahn/Makefile b/mausezahn/Makefile index ea5d195..cecd139 100644 --- a/mausezahn/Makefile +++ b/mausezahn/Makefile @@ -6,6 +6,7 @@ mausezahn-libs = -lcli \ -lm mausezahn-objs = xutils.o \ + str.o \ staging/layer1.o \ staging/layer2.o \ staging/layer3.o \ diff --git a/netsniff-ng.c b/netsniff-ng.c index b61dec9..ed3d659 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -33,7 +33,9 @@ #include "xio.h" #include "die.h" #include "irq.h" +#include "str.h" #include "geoip.h" +#include "lockme.h" #include "tprintf.h" #include "dissector.h" #include "xmalloc.h" diff --git a/netsniff-ng/Makefile b/netsniff-ng/Makefile index 53f1fea..f397b92 100644 --- a/netsniff-ng/Makefile +++ b/netsniff-ng/Makefile @@ -32,6 +32,7 @@ netsniff-ng-objs = dissector.o \ proto_vlan_q_in_q.o \ proto_mpls_unicast.o \ proto_80211_mac_hdr.o \ + str.o \ irq.o \ iosched.o \ xio.o \ diff --git a/oui.c b/oui.c index e735c15..2fa716a 100644 --- a/oui.c +++ b/oui.c @@ -11,6 +11,7 @@ #include "xmalloc.h" #include "xutils.h" #include "oui.h" +#include "str.h" static struct hash_table oui; diff --git a/str.c b/str.c new file mode 100644 index 0000000..7a5cb49 --- /dev/null +++ b/str.c @@ -0,0 +1,88 @@ +/* + * netsniff-ng - the packet sniffing beast + * Copyright 2009, 2010 Daniel Borkmann. + * Subject to the GPL, version 2. + */ + +#include +#include +#include + +#include "str.h" + +size_t strlcpy(char *dest, const char *src, size_t size) +{ + size_t ret = strlen(src); + + if (size) { + size_t len = (ret >= size) ? size - 1 : ret; + + memcpy(dest, src, len); + dest[len] = '\0'; + } + + return ret; +} + +static inline int vslprintf(char *dst, size_t size, const char *fmt, va_list ap) +{ + int ret; + + ret = vsnprintf(dst, size, fmt, ap); + dst[size - 1] = '\0'; + + return ret; +} + +int slprintf(char *dst, size_t size, const char *fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = vslprintf(dst, size, fmt, ap); + va_end(ap); + + return ret; +} + +int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = vslprintf(dst, size, fmt, ap); + va_end(ap); + + return ret; +} + +noinline void *xmemset(void *s, int c, size_t n) +{ + size_t i; + uint8_t *ptr = s; + + for (i = 0; i < n; ++i) + ptr[i] = (uint8_t) c; + + return ptr; +} + +char *strtrim_right(char *p, char c) +{ + char *end; + size_t len; + + len = strlen(p); + while (*p && len) { + end = p + len - 1; + if (c == *end) + *end = 0; + else + break; + len = strlen(p); + } + + return p; +} diff --git a/str.h b/str.h new file mode 100644 index 0000000..7b8916c --- /dev/null +++ b/str.h @@ -0,0 +1,12 @@ +#ifndef STR_H +#define STR_H + +#include "built_in.h" + +extern size_t strlcpy(char *dest, const char *src, size_t size); +extern int slprintf(char *dst, size_t size, const char *fmt, ...) __check_format_printf(3, 4); +extern int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...); +extern char *strtrim_right(char *p, char c); +extern noinline void *xmemset(void *s, int c, size_t n); + +#endif /* STR_H */ diff --git a/trafgen.c b/trafgen.c index d493614..158b96e 100644 --- a/trafgen.c +++ b/trafgen.c @@ -34,6 +34,8 @@ #include "xmalloc.h" #include "die.h" +#include "str.h" +#include "lockme.h" #include "mac80211.h" #include "xutils.h" #include "xio.h" diff --git a/trafgen/Makefile b/trafgen/Makefile index 914b61c..75a9c09 100644 --- a/trafgen/Makefile +++ b/trafgen/Makefile @@ -6,6 +6,7 @@ trafgen-objs = xmalloc.o \ xio.o \ xutils.o \ irq.o \ + str.o \ mac80211.o \ ring_tx.o \ ring.o \ diff --git a/trafgen_parser.y b/trafgen_parser.y index 1112f8a..85d7b01 100644 --- a/trafgen_parser.y +++ b/trafgen_parser.y @@ -23,6 +23,7 @@ #include "trafgen_conf.h" #include "built_in.h" #include "die.h" +#include "str.h" #include "csum.h" #include "xutils.h" diff --git a/tstamping.c b/tstamping.c index 4de455d..9ef1a4a 100644 --- a/tstamping.c +++ b/tstamping.c @@ -18,6 +18,7 @@ extern int set_sockopt_hwtimestamp(int sock, const char *dev); #include #include "xutils.h" +#include "str.h" int set_sockopt_hwtimestamp(int sock, const char *dev) { diff --git a/xio.c b/xio.c index d2c6181..991ff7f 100644 --- a/xio.c +++ b/xio.c @@ -23,6 +23,7 @@ #include "die.h" #include "xio.h" +#include "str.h" #include "xutils.h" int open_or_die(const char *file, int flags) diff --git a/xmalloc.c b/xmalloc.c index 246be07..b40f14c 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -18,6 +18,7 @@ #include "xutils.h" #include "built_in.h" #include "die.h" +#include "str.h" void *xmalloc(size_t size) { diff --git a/xutils.c b/xutils.c index 8856b3a..b539d8c 100644 --- a/xutils.c +++ b/xutils.c @@ -41,6 +41,7 @@ #include #include "die.h" +#include "str.h" #include "xutils.h" #include "ring.h" #include "built_in.h" @@ -707,94 +708,6 @@ int set_sched_status(int policy, int priority) return 0; } -void xlockme(void) -{ - if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) - panic("Cannot lock pages!\n"); -} - -void xunlockme(void) -{ - munlockall(); -} - -size_t strlcpy(char *dest, const char *src, size_t size) -{ - size_t ret = strlen(src); - - if (size) { - size_t len = (ret >= size) ? size - 1 : ret; - - memcpy(dest, src, len); - dest[len] = '\0'; - } - - return ret; -} - -static inline int vslprintf(char *dst, size_t size, const char *fmt, va_list ap) -{ - int ret; - - ret = vsnprintf(dst, size, fmt, ap); - dst[size - 1] = '\0'; - - return ret; -} - -int slprintf(char *dst, size_t size, const char *fmt, ...) -{ - int ret; - va_list ap; - - va_start(ap, fmt); - ret = vslprintf(dst, size, fmt, ap); - va_end(ap); - - return ret; -} - -int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...) -{ - int ret; - va_list ap; - - va_start(ap, fmt); - ret = vslprintf(dst, size, fmt, ap); - va_end(ap); - - return ret; -} - -noinline void *xmemset(void *s, int c, size_t n) -{ - size_t i; - uint8_t *ptr = s; - - for (i = 0; i < n; ++i) - ptr[i] = (uint8_t) c; - - return ptr; -} - -char *strtrim_right(char *p, char c) -{ - char *end; - size_t len; - - len = strlen(p); - while (*p && len) { - end = p + len - 1; - if (c == *end) - *end = 0; - else - break; - len = strlen(p); - } - - return p; -} - int get_default_sched_policy(void) { return SCHED_FIFO; diff --git a/xutils.h b/xutils.h index ca1a8c5..e435c43 100644 --- a/xutils.h +++ b/xutils.h @@ -40,8 +40,6 @@ 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 xlockme(void); -extern void xunlockme(void); extern void set_nonblocking(int fd); extern int set_nonblocking_sloppy(int fd); extern int set_reuseaddr(int fd); @@ -69,11 +67,6 @@ extern void cpu_affinity(int cpu); extern int set_cpu_affinity(char *str, int inverted); extern int set_proc_prio(int prio); extern int set_sched_status(int policy, int priority); -extern size_t strlcpy(char *dest, const char *src, size_t size); -extern int slprintf(char *dst, size_t size, const char *fmt, ...) __check_format_printf(3, 4); -extern int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...); -extern noinline void *xmemset(void *s, int c, size_t n); -extern char *strtrim_right(char *p, char c); extern int get_default_sched_policy(void); extern int get_default_sched_prio(void); extern int get_number_cpus(void); -- cgit v1.2.3-54-g00ecf