aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..ff9d56e
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,72 @@
+#include "config.h"
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#ifndef TUN_DEBUG
+# define TUN_DEBUG 0
+#endif
+
+#define numberof(ary) (sizeof(ary) / sizeof(ary[0]))
+
+#define print_msg(type) do { \
+ va_list args; \
+ va_start(args, format); \
+ fprintf(stderr, type": "); \
+ vfprintf(stderr, format, args); \
+ fprintf(stderr, "\n"); \
+ va_end(args); \
+} while (0)
+
+static inline _Noreturn void fatal(const char *format, ...)
+{
+ print_msg("fatal");
+ exit(1);
+}
+
+static inline void error(const char *format, ...)
+{
+ print_msg("error");
+}
+
+static inline void info(const char *format, ...)
+{
+ print_msg("info");
+}
+
+static inline void debug(const char *format, ...)
+{
+ print_msg("debug");
+}
+
+static inline uint16_t parse_u16(const char *str)
+{
+ long numl = strtol(str, NULL, 10);
+ if (numl <= 0 || numl > 65535)
+ fatal("could not parse port number: %s", str);
+ return (uint16_t)numl;
+}
+
+static inline struct in_addr parse_ipv4(const char *str)
+{
+ struct in_addr tmp;
+ int ret = inet_aton(str, &tmp);
+ if (ret == 0)
+ fatal("could not parse IPv4 address: %s", str);
+ return tmp;
+}
+
+static inline void dumpbin(const uint8_t *buf, size_t x)
+{
+ size_t i;
+ for (i = 0; i < x; i++)
+ {
+ if (i > 0) printf(":");
+ printf("%02X", buf[i]);
+ }
+ printf("\n");
+}