Dispatch to methods handling address-family specifics.
[yaid] / yaid.h
diff --git a/yaid.h b/yaid.h
index e0d13c1..85ad397 100644 (file)
--- a/yaid.h
+++ b/yaid.h
@@ -33,6 +33,8 @@
 
 /*----- Header files ------------------------------------------------------*/
 
+#include "config.h"
+
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-
 #include <syslog.h>
 
 #include <mLib/bits.h>
 #include <mLib/conn.h>
 #include <mLib/darray.h>
 #include <mLib/dstr.h>
+#include <mLib/fdflags.h>
 #include <mLib/fwatch.h>
 #include <mLib/quis.h>
 #include <mLib/report.h>
 #include <mLib/sel.h>
 #include <mLib/selbuf.h>
 
+/*----- System specifics --------------------------------------------------*/
+
+#define SYS_UNDEF 0
+#define SYS_LINUX 1
+
+#if SYS == SYS_LINUX
+#  include <linux/netlink.h>
+#  include <linux/rtnetlink.h>
+#else
+#  error "Unsupported operating system: sorry.  Patches welcome!"
+#endif
+
 /*----- Data structures ---------------------------------------------------*/
 
 #define ADDRLEN 64
@@ -81,6 +93,50 @@ struct socket {
   unsigned port;
 };
 
+struct addrpat {
+  unsigned len;
+  union addr addr;
+};
+
+struct portpat {
+  unsigned lo, hi;
+};
+
+struct sockpat {
+  struct addrpat addr;
+  struct portpat port;
+};
+
+#define ADDRTYPES(_)                                                   \
+  _(ipv4, IPV4, INET, "IPv4", 32)                                      \
+  _(ipv6, IPV6, INET6, "IPv6", 128)
+
+struct addrops {
+  int af;
+  const char *name;
+  unsigned len;
+  const union addr *any;
+  const struct addrops_sys *sys;
+  int (*addreq)(const union addr *, const union addr *);
+  int (*match_addrpat)(const struct addrpat *, const union addr *);
+  void (*socket_to_sockaddr)(const struct socket *s, void *, size_t *);
+  void (*sockaddr_to_addr)(const void *, union addr *);
+  int (*init_listen_socket)(int);
+};
+
+enum {
+#define DEFADDR(ty, TY, af, name, len) ADDR_##TY,
+  ADDRTYPES(DEFADDR)
+#undef DEFADDR
+  ADDR_LIMIT
+};
+
+extern const struct addrops addroptab[];
+#define OPS_SYS(ty, TY, af, name, len)                                 \
+  extern const struct addrops_sys addrops_sys_##ty;
+ADDRTYPES(OPS_SYS)
+#undef OPS_SYS
+
 enum { L, R, NDIR };
 
 #define RESPONSE(_)                                                    \
@@ -110,7 +166,7 @@ enum {
 };
 
 struct query {
-  int af;
+  const struct addrops *ao;
   struct socket s[NDIR];
   unsigned resp;
   union {
@@ -131,20 +187,6 @@ enum {
   T_ERROR
 };
 
-struct addrpat {
-  unsigned len;
-  union addr addr;
-};
-
-struct portpat {
-  unsigned lo, hi;
-};
-
-struct sockpat {
-  struct addrpat addr;
-  struct portpat port;
-};
-
 #define ACTIONS(_)                                                     \
   _(USER, "user")                                                      \
   _(TOKEN, "token")                                                    \
@@ -169,7 +211,7 @@ struct action {
 };
 
 struct policy {
-  int af;
+  const struct addrops *ao;
   struct sockpat sp[NDIR];
   struct action act;
 };
@@ -189,6 +231,10 @@ DA_DECL(policy_v, struct policy);
 
 /*----- Functions provided ------------------------------------------------*/
 
+int sockeq(const struct addrops *ao,
+          const struct socket *sa, const struct socket *sb);
+void dputsock(dstr *d, const struct addrops *ao, const struct socket *s);
+
 void logmsg(const struct query *q, int prio, const char *msg, ...);
 
 void identify(struct query *q);