From ec347c080dfd6422d7ff882e9d69123a374b4f63 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Fri, 9 Dec 2011 15:37:58 +0100 Subject: [PATCH] Removed files, reorganized code i.e. renamed write_or_die.c to xutils.c --- src/alsa.c | 2 +- src/cli.c | 4 +- src/{cli_cmds.h => clicmds.h} | 6 +-- src/curve.c | 2 +- src/mtrand.c | 2 +- src/strlcpy.c | 75 -------------------------------- src/strlcpy.h | 16 ------- src/transsip/Makefile | 6 +-- src/xmalloc.c | 2 +- src/{write_or_die.c => xutils.c} | 93 +++++++++++++++++++++++++++------------- src/{write_or_die.h => xutils.h} | 12 ++++-- 11 files changed, 83 insertions(+), 137 deletions(-) rename src/{cli_cmds.h => clicmds.h} (96%) delete mode 100644 src/strlcpy.c delete mode 100644 src/strlcpy.h rename src/{write_or_die.c => xutils.c} (69%) rename src/{write_or_die.h => xutils.h} (74%) diff --git a/src/alsa.c b/src/alsa.c index df24a82..8ff0d86 100644 --- a/src/alsa.c +++ b/src/alsa.c @@ -40,7 +40,7 @@ #include "die.h" #include "alsa.h" #include "xmalloc.h" -#include "strlcpy.h" +#include "xutils.h" #define PERIODS 3 diff --git a/src/cli.c b/src/cli.c index 3e8dd81..017c7fd 100644 --- a/src/cli.c +++ b/src/cli.c @@ -18,10 +18,10 @@ #include "tty.h" #include "conf.h" #include "cli.h" -#include "cli_cmds.h" -#include "strlcpy.h" +#include "clicmds.h" #include "version.h" #include "xmalloc.h" +#include "xutils.h" #include "signals.h" #include "compiler.h" #include "stun.h" diff --git a/src/cli_cmds.h b/src/clicmds.h similarity index 96% rename from src/cli_cmds.h rename to src/clicmds.h index 374b7e4..33f5ccb 100644 --- a/src/cli_cmds.h +++ b/src/clicmds.h @@ -6,8 +6,8 @@ * Subject to the GPL, version 2. */ -#ifndef CLI_CMDS_H -#define CLI_CMDS_H +#ifndef CLICMDS_H +#define CLICMDS_H #include @@ -57,5 +57,5 @@ static struct shell_cmd cmd_tree[] = { { NULL, NULL, NULL, NULL, }, }; -#endif /* CLI_CMDS_H */ +#endif /* CLICMDS_H */ diff --git a/src/curve.c b/src/curve.c index 73dad48..6cd6c28 100644 --- a/src/curve.c +++ b/src/curve.c @@ -20,8 +20,8 @@ #include "compiler.h" #include "xmalloc.h" +#include "xutils.h" #include "curve.h" -#include "strlcpy.h" #include "die.h" #include "mtrand.h" #include "conf.h" diff --git a/src/mtrand.c b/src/mtrand.c index 3758561..8756716 100644 --- a/src/mtrand.c +++ b/src/mtrand.c @@ -55,7 +55,7 @@ #include #include "mtrand.h" -#include "write_or_die.h" +#include "xutils.h" #define N 624 #define M 397 diff --git a/src/strlcpy.c b/src/strlcpy.c deleted file mode 100644 index f7e4617..0000000 --- a/src/strlcpy.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * transsip - the telephony network - * By Daniel Borkmann - * Copyright 2011 Daniel Borkmann , - * Swiss federal institute of technology (ETH Zurich) - * Subject to the GPL, version 2. - * strlcpy: Copyright (C) 1991, 1992 Linus Torvalds, GPL, version 2 - */ - -#define _BSD_SOURCE -#include -#include -#include - -#include "strlcpy.h" -#include "xmalloc.h" -#include "die.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; -} - -int slprintf(char *dst, size_t size, const char *fmt, ...) -{ - int ret; - va_list ap; - va_start(ap, fmt); - ret = vsnprintf(dst, size, fmt, ap); - dst[size - 1] = '\0'; - va_end(ap); - return ret; -} - -char **strntoargv(char *str, size_t len, int *argc) -{ - int done = 0; - char **argv = NULL; - if (argc == NULL) - panic("argc is null!\n"); - *argc = 0; - if (len <= 1) /* '\0' */ - goto out; - while (!done) { - while (len > 0 && *str == ' ') { - len--; - str++; - } - if (len > 0 && *str != '\0') { - (*argc)++; - argv = xrealloc(argv, 1, sizeof(char *) * (*argc)); - argv[(*argc) - 1] = str; - while (len > 0 && *str != ' ') { - len--; - str++; - } - if (len > 0 && *str == ' ') { - len--; - *str = '\0'; - str++; - } - } else { - done = 1; - } - } -out: - return argv; -} - diff --git a/src/strlcpy.h b/src/strlcpy.h deleted file mode 100644 index f6bac2e..0000000 --- a/src/strlcpy.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * transsip - the telephony network - * By Daniel Borkmann - * Copyright 2011 Daniel Borkmann , - * Swiss federal institute of technology (ETH Zurich) - * Subject to the GPL, version 2. - */ - -#ifndef STRLCPY_H -#define STRLCPY_H - -extern size_t strlcpy(char *dest, const char *src, size_t size); -extern int slprintf(char *dst, size_t size, const char *fmt, ...); -extern char **strntoargv(char *str, size_t len, int *argc); - -#endif /* STRLCPY_H */ diff --git a/src/transsip/Makefile b/src/transsip/Makefile index 9f92102..ef494aa 100644 --- a/src/transsip/Makefile +++ b/src/transsip/Makefile @@ -10,13 +10,13 @@ LIBS = ./nacl/libnacl.a -lspeexdsp -lasound -lcelt0 -lm -lreadline -lpthr core-objs = transsip.o lib-objs = xmalloc.o \ + xutils.o \ stun.o \ alsa.o \ cli.o \ curve.o \ - mtrand.o \ - write_or_die.o \ - strlcpy.o + mtrand.o + # dht.o \ target = transsip diff --git a/src/xmalloc.c b/src/xmalloc.c index 92eca4e..d627aa0 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -19,7 +19,7 @@ #include "compiler.h" #include "xmalloc.h" -#include "strlcpy.h" +#include "xutils.h" #include "die.h" __hidden void *xmalloc(size_t size) diff --git a/src/write_or_die.c b/src/xutils.c similarity index 69% rename from src/write_or_die.c rename to src/xutils.c index 02ddfc2..a9e6448 100644 --- a/src/write_or_die.c +++ b/src/xutils.c @@ -3,8 +3,10 @@ * By Daniel Borkmann * Copyright 2011 Daniel Borkmann. * Subject to the GPL, version 2. + * strlcpy: Copyright (C) 1991, 1992 Linus Torvalds, GPL, version 2 */ +#define _BSD_SOURCE #include #include #include @@ -12,17 +14,16 @@ #include #include #include +#include #include #include #include #include #include -#include -#include -#include "write_or_die.h" +#include "xutils.h" +#include "xmalloc.h" #include "die.h" -#include "strlcpy.h" extern sig_atomic_t quit; @@ -48,32 +49,6 @@ int open_or_die_m(const char *file, int flags, mode_t mode) return ret; } -int tun_open_or_die(char *name) -{ - int fd, ret; - struct ifreq ifr; - - fd = open("/dev/net/tun", O_RDWR); - if (fd < 0) - panic("Cannot open /dev/net/tun!\n"); - - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - - if (name) - strlcpy(ifr.ifr_name, name, IFNAMSIZ); - - ret = ioctl(fd, TUNSETIFF, &ifr); - if (ret < 0) - panic("ioctl screwed up!\n"); - - ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); - if (ret < 0) - panic("fctnl screwed up!\n"); - - return fd; -} - ssize_t read_or_die(int fd, void *buf, size_t len) { ssize_t ret = read(fd, buf, len); @@ -173,3 +148,61 @@ ssize_t write_or_whine(int fd, const void *buf, size_t len, return ret; } + +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; +} + +int slprintf(char *dst, size_t size, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsnprintf(dst, size, fmt, ap); + dst[size - 1] = '\0'; + va_end(ap); + return ret; +} + +char **strntoargv(char *str, size_t len, int *argc) +{ + int done = 0; + char **argv = NULL; + if (argc == NULL) + panic("argc is null!\n"); + *argc = 0; + if (len <= 1) /* '\0' */ + goto out; + while (!done) { + while (len > 0 && *str == ' ') { + len--; + str++; + } + if (len > 0 && *str != '\0') { + (*argc)++; + argv = xrealloc(argv, 1, sizeof(char *) * (*argc)); + argv[(*argc) - 1] = str; + while (len > 0 && *str != ' ') { + len--; + str++; + } + if (len > 0 && *str == ' ') { + len--; + *str = '\0'; + str++; + } + } else { + done = 1; + } + } +out: + return argv; +} + diff --git a/src/write_or_die.h b/src/xutils.h similarity index 74% rename from src/write_or_die.h rename to src/xutils.h index 745f6f9..21584a8 100644 --- a/src/write_or_die.h +++ b/src/xutils.h @@ -5,13 +5,14 @@ * Subject to the GPL, version 2. */ -#ifndef WRITE_OR_DIE_H -#define WRITE_OR_DIE_H +#ifndef XUTILS_H +#define XUTILS_H + +#include extern void fsync_or_die(int fd, const char *msg); extern int open_or_die(const char *file, int flags); extern int open_or_die_m(const char *file, int flags, mode_t mode); -extern int tun_open_or_die(char *name); extern ssize_t read_or_die(int fd, void *buf, size_t count); extern ssize_t read_exact(int fd, void *buf, size_t len, int mayexit); extern ssize_t write_exact(int fd, void *buf, size_t len, int mayexit); @@ -20,5 +21,8 @@ extern ssize_t write_or_whine_pipe(int fd, const void *buf, size_t len, const char *msg); extern ssize_t write_or_whine(int fd, const void *buf, size_t len, const char *msg); +extern size_t strlcpy(char *dest, const char *src, size_t size); +extern int slprintf(char *dst, size_t size, const char *fmt, ...); +extern char **strntoargv(char *str, size_t len, int *argc); -#endif /* WRITE_OR_DIE_H */ +#endif /* XUTILS_H */ -- 2.11.4.GIT