network: add support for configuring extra peers via a separate json file
[project/unetd.git] / utils.h
diff --git a/utils.h b/utils.h
index b0243743e8db6c8e5fb3d253fab9d38d2c615577..d79d96c1e9a7c1dcfa8f20845c4390ea5dc00652 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -1,11 +1,15 @@
-// SPDX-License-Identifier: GPL-2.0+
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  */
 #ifndef __UNETD_UTILS_H
 #define __UNETD_UTILS_H
 
+#include <string.h>
 #include <netinet/in.h>
+#include <libubox/utils.h>
+
+struct nl_msg;
 
 union network_addr {
        struct {
@@ -55,6 +59,8 @@ int network_get_subnet(int af, union network_addr *addr, int *mask,
                       const char *str);
 int network_get_local_addr(void *local, const union network_endpoint *target);
 
+void *unet_read_file(const char *name, size_t *len);
+
 #define DIV_ROUND_UP(n, d)     (((n) + (d) - 1) / (d))
 
 #define bitmask_size(len)      (4 * DIV_ROUND_UP(len, 32))
@@ -82,4 +88,41 @@ static inline void bitmask_set_val(uint32_t *mask, unsigned int i, bool val)
                bitmask_clear(mask, i);
 }
 
+static inline uint16_t get_unaligned_be16(const uint8_t *p)
+{
+       return p[1] | p[0] << 8;
+}
+
+static inline uint32_t get_unaligned_be32(const uint8_t *p)
+{
+       return p[3] | p[2] << 8 | p[1] << 16 | p[0] << 24;
+}
+
+static inline uint64_t get_unaligned_be64(const uint8_t *p)
+{
+       return (uint64_t)get_unaligned_be32(p) << 32 |
+              get_unaligned_be32(p + 4);
+}
+
+static inline uint16_t get_unaligned_le16(const uint8_t *p)
+{
+       return p[0] | p[1] << 8;
+}
+
+static inline uint32_t get_unaligned_le32(const uint8_t *p)
+{
+       return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
+}
+
+static inline uint64_t get_unaligned_le64(const uint8_t *p)
+{
+       return (uint64_t)get_unaligned_le32(p + 4) << 32 |
+              get_unaligned_le32(p);
+}
+
+int rtnl_init(void);
+int rtnl_call(struct nl_msg *msg);
+
+uint64_t unet_gettime(void);
+
 #endif