summaryrefslogtreecommitdiff
path: root/mcserverproxy/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'mcserverproxy/utils.h')
-rw-r--r--mcserverproxy/utils.h245
1 files changed, 245 insertions, 0 deletions
diff --git a/mcserverproxy/utils.h b/mcserverproxy/utils.h
new file mode 100644
index 0000000..c36a36c
--- /dev/null
+++ b/mcserverproxy/utils.h
@@ -0,0 +1,245 @@
+#ifndef fooutilshfoo
+#define fooutilshfoo
+
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <linux/if_packet.h>
+
+struct list_head {
+ struct list_head *next;
+ struct list_head *prev;
+};
+
+#define zmalloc(s) __zmalloc(__func__, __LINE__, s)
+void *__zmalloc(const char *fn, int line, size_t s);
+
+#define xstrdup(s) __xstrdup(__func__, __LINE__, s)
+char *__xstrdup(const char *fn, int line, const char *s);
+
+#define xstrndup(s, n) __xstrndup(__func__, __LINE__, s, n)
+char *__xstrndup(const char *fn, int line, const char *s, size_t n);
+
+#define xfree(s) __xfree(__func__, __LINE__, s)
+void __xfree(const char *fn, int line, void *ptr);
+
+void debug_resource_usage();
+
+/* Length of longest DNS name = 253 + trailing dot */
+#define FQDN_STR_LEN 254
+
+/* Length of longest port string = strlen("65535") */
+#define PORT_STR_LEN 5
+
+/* Length of longest address family string = strlen("AF_INETX") */
+#define AF_STR_LEN 8
+
+/* Length of longest addrstr, format = "AF_INETX <IPADDR> <PORT> */
+#define ADDRSTRLEN (AF_STR_LEN + 1 + INET6_ADDRSTRLEN + 1 + PORT_STR_LEN + 1)
+struct saddr {
+ union {
+ struct sockaddr_storage storage;
+ struct sockaddr_in in4;
+ struct sockaddr_in6 in6;
+ struct sockaddr_ll ll;
+ };
+ socklen_t addrlen;
+ char addrstr[ADDRSTRLEN];
+ struct list_head list;
+};
+
+struct connection;
+
+typedef void(*connection_cb_t)(struct connection *, bool);
+
+struct connection {
+ struct saddr remote;
+ struct saddr local;
+
+ struct list_head *addrs;
+ unsigned next_addr;
+
+ connection_cb_t cb;
+};
+
+struct uring_task;
+
+void socket_set_low_latency(int sfd);
+
+void connection_set_local(struct connection *conn, int fd);
+
+void connection_set_remote(struct connection *conn, struct saddr *remote);
+
+void connect_any(struct uring_task *task,
+ struct list_head *addrs, struct connection *conn,
+ connection_cb_t cb);
+
+char *saddr_addr(struct saddr *saddr, char *buf, size_t len);
+
+uint16_t saddr_port(struct saddr *saddr);
+
+void saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port);
+
+void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port);
+
+void saddr_set_addrstr(struct saddr *saddr);
+
+int strtou16_strict(const char *str, uint16_t *result);
+
+static inline bool empty_str(const char *str)
+{
+ if (!str || str[0] == '\0')
+ return true;
+ else
+ return false;
+}
+
+static inline bool streq(const char *a, const char *b)
+{
+ return strcmp(a, b) == 0;
+}
+
+static inline bool strcaseeq(const char *a, const char *b)
+{
+ return strcasecmp(a, b) == 0;
+}
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define chtobe32(x) __bswap_constant_32(x)
+#else
+#define chtobe32(x) (x)
+#endif
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define cinet_addr(a,b,c,d) ((uint32_t)((a)<<0|(b)<<8|(c)<<16|(d)<<24))
+#else
+#define cinet_addr(a,b,c,d) ((uint32_t)((a)<<24|(b)<<16|(c)<<8|(d)<<0))
+#endif
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
+
+static inline void list_init(struct list_head *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+static inline void list_del(struct list_head *list)
+{
+ list->next->prev = list->prev;
+ list->prev->next = list->next;
+}
+
+static inline void list_add(struct list_head *new, struct list_head *list)
+{
+ list->next->prev = new;
+ new->next = list->next;
+ new->prev = list;
+ list->next = new;
+}
+
+static inline void list_replace(struct list_head *old, struct list_head *new)
+{
+ old->prev->next = new;
+ old->next->prev = new;
+ new->next = old->next;
+ new->prev = old->prev;
+ old->next = old->prev = NULL;
+}
+
+static inline bool list_empty(struct list_head *list)
+{
+ return list->next == list;
+}
+
+#define PIPE_RD 0
+#define PIPE_WR 1
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
+
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+
+#define container_of(ptr, type, member) ({ \
+ const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+ (type *)( (char *)__mptr - offsetof(type,member) );})
+
+#define list_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+
+#define list_next_entry(pos, member) \
+ list_entry((pos)->member.next, typeof(*(pos)), member)
+
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+#define list_for_each_entry(pos, head, member) \
+ for (pos = list_first_entry(head, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_next_entry(pos, member))
+
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/*
+#define _cleanup_(x) __attribute__((cleanup(x)))
+
+#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
+ static inline void func##p(type *p) { \
+ if (*p) \
+ func(*p); \
+ } \
+ struct __useless_struct_to_allow_trailing_semicolon__
+
+static inline unsigned
+strv_length(char **strv)
+{
+ unsigned len;
+
+ for (len = 0; strv && *strv; strv++)
+ len++;
+
+ return len;
+}
+
+static inline void strv_free(char **l) {
+ char **k;
+ if (l) {
+ for (k = l; *k; k++)
+ free(k);
+ free(l);
+ }
+}
+DEFINE_TRIVIAL_CLEANUP_FUNC(char **, strv_free);
+#define _cleanup_strv_free_ _cleanup_(strv_freep)
+
+static inline void freep(void *p) {
+ free(*(void**) p);
+}
+#define _cleanup_free_ _cleanup_(freep)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(int, close);
+#define _cleanup_close_ _cleanup_(closep)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(FILE *, fclose);
+#define _cleanup_fclose_ _cleanup_(fclosep)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(DIR *, closedir);
+#define _cleanup_closedir_ _cleanup_(closedirp)
+
+*/
+
+#endif
+