chiark / gitweb /
server/tripe.c: Formalize the main loop machinery.
[tripe] / server / tripe.h
index a2907be1a6bff9a030d9dd4879c8eed1e96e14e6..bd70bb84b4751476f578fa28ac685a84ab1eca2a 100644 (file)
 #include <pwd.h>
 #include <grp.h>
 
+#ifdef HAVE_LIBADNS
+#  define ADNS_FEATURE_MANYAF
+#  include <adns.h>
+#endif
+
 #include <mLib/alloc.h>
 #include <mLib/arena.h>
 #include <mLib/base64.h>
-#include <mLib/bres.h>
+#ifndef HAVE_LIBADNS
+#  include <mLib/bres.h>
+#endif
 #include <mLib/codec.h>
 #include <mLib/daemonize.h>
 #include <mLib/dstr.h>
@@ -428,6 +435,27 @@ extern const bulkops bulktab[];
 
 /*----- Data structures ---------------------------------------------------*/
 
+/* --- The address-family table --- */
+
+#define ADDRFAM(_)                                                     \
+  _(INET,      want_ipv4)                                              \
+  _(INET6,     want_ipv6)
+
+enum {
+#define ENUM(af, qf) AFIX_##af,
+  ADDRFAM(ENUM)
+#undef ENUM
+  NADDRFAM
+};
+
+extern const struct addrfam {
+  int af;
+  const char *name;
+#ifdef HAVE_LIBADNS
+  adns_queryflags qf;
+#endif
+} aftab[NADDRFAM];
+
 /* --- Socket addresses --- *
  *
  * A magic union of supported socket addresses.
@@ -436,6 +464,7 @@ extern const bulkops bulktab[];
 typedef union addr {
   struct sockaddr sa;
   struct sockaddr_in sin;
+  struct sockaddr_in6 sin6;
 } addr;
 
 /* --- Mapping keyed on addresses --- */
@@ -635,6 +664,7 @@ typedef struct peer {
   peer_byaddr *byaddr;                 /* Lookup-by-address block */
   struct ping *pings;                  /* Pings we're waiting for */
   peerspec spec;                       /* Specifications for this peer */
+  int afix;                            /* Index of address family */
   tunnel *t;                           /* Tunnel for local packets */
   char *ifname;                                /* Interface name for tunnel */
   keyset *ks;                          /* List head for keysets */
@@ -646,6 +676,11 @@ typedef struct peer {
 
 typedef struct peer_iter { sym_iter i; } peer_iter;
 
+typedef struct udpsocket {
+  sel_file sf;                         /* Selector for the socket */
+  unsigned port;                       /* Chosen port number */
+} udpsocket;
+
 typedef struct ping {
   struct ping *next, *prev;            /* Links to next and previous */
   peer *p;                             /* Peer so we can free it */
@@ -691,9 +726,14 @@ typedef struct admin_bgop {
 typedef struct admin_resop {
   admin_bgop bg;                       /* Background operation header */
   char *addr;                          /* Hostname to be resolved */
+#ifdef HAVE_LIBADNS
+  adns_query q;
+#else
   bres_client r;                       /* Background resolver task */
+#endif
   sel_timer t;                         /* Timer for resolver */
   addr sa;                             /* Socket address */
+  unsigned port;                       /* Port number chosen */
   size_t sasz;                         /* Socket address size */
   void (*func)(struct admin_resop *, int); /* Handler */
 } admin_resop;
@@ -777,6 +817,7 @@ extern sel_state sel;                       /* Global I/O event state */
 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
 extern const tunnel_ops *tunnels[];    /* Table of tunnels (0-term) */
 extern const tunnel_ops *tun_default;  /* Default tunnel to use */
+extern udpsocket udpsock[NADDRFAM];    /* The master UDP sockets */
 extern kdata *master;                  /* Default private key */
 extern const char *tag_priv;           /* Default private key tag */
 
@@ -1192,7 +1233,9 @@ extern int c_check(const void */*m*/, size_t /*msz*/, buf */*b*/);
  *
  *               * "?PEER" PEER -- peer's name
  *
- *               * "?ERRNO" ERRNO -- system error code
+ *               * "?ERR" CODE -- system error code
+ *
+ *               * "?ERRNO" -- system error code from @errno@
  *
  *               * "[!]..." ... -- @dstr_putf@-like string as single token
  */
@@ -1256,7 +1299,10 @@ extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...);
  *
  * Returns:    ---
  *
- * Use:                Creates a new admin connection.
+ * Use:                Creates a new admin connection.  It's safe to call this
+ *             before @a_init@ -- and, indeed, this makes sense if you also
+ *             call @a_switcherr@ to report initialization errors through
+ *             the administration machinery.
  */
 
 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
@@ -1296,6 +1342,50 @@ extern void a_preselect(void);
 
 extern void a_daemon(void);
 
+/* --- @a_listen@ --- *
+ *
+ * Arguments:  @const char *name@ = socket name to create
+ *             @uid_t u@ = user to own the socket
+ *             @gid_t g@ = group to own the socket
+ *             @mode_t m@ = permissions to set on the socket
+ *
+ * Returns:    ---
+ *
+ * Use:                Creates the admin listening socket.
+ */
+
+extern void a_listen(const char */*sock*/,
+                    uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
+
+/* --- @a_switcherr@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Arrange to report warnings, trace messages, etc. to
+ *             administration clients rather than the standard-error stream.
+ *
+ *             Obviously this makes no sense unless there is at least one
+ *             client established.  Calling @a_listen@ won't help with this,
+ *             because the earliest a new client can connect is during the
+ *             first select-loop iteration, which is too late: some initial
+ *             client must have been added manually using @a_create@.
+ */
+
+extern void a_switcherr(void);
+
+/* --- @a_signals@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Establishes handlers for the obvious signals.
+ */
+
+extern void a_signals(void);
+
 /* --- @a_init@ --- *
  *
  * Arguments:  @const char *sock@ = socket name to create
@@ -1308,8 +1398,7 @@ extern void a_daemon(void);
  * Use:                Creates the admin listening socket.
  */
 
-extern void a_init(const char */*sock*/,
-                  uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
+extern void a_init(void);
 
 /*----- Mapping with addresses as keys ------------------------------------*/
 
@@ -1598,26 +1687,27 @@ extern void p_setifname(peer */*p*/, const char */*name*/);
 
 extern const addr *p_addr(peer */*p*/);
 
-/* --- @p_init@ --- *
+/* --- @p_bind@ --- *
  *
- * Arguments:  @struct in_addr addr@ = address to bind to
- *             @unsigned port@ = port number to listen to
+ * Arguments:  @struct addrinfo *ailist@ = addresses to bind to
  *
  * Returns:    ---
  *
- * Use:                Initializes the peer system; creates the socket.
+ * Use:                Binds to the main UDP sockets.
  */
 
-extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
+extern void p_bind(struct addrinfo */*ailist*/);
 
-/* --- @p_port@ --- *
+/* --- @p_init@ --- *
  *
  * Arguments:  ---
  *
- * Returns:    Port number used for socket.
+ * Returns:    ---
+ *
+ * Use:                Initializes the peer system.
  */
 
-unsigned p_port(void);
+extern void p_init(void);
 
 /* --- @p_create@ --- *
  *
@@ -1739,6 +1829,57 @@ extern void p_mkiter(peer_iter */*i*/);
 
 extern peer *p_next(peer_iter */*i*/);
 
+/*----- The interval timer ------------------------------------------------*/
+
+/* --- @iv_addreason@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Adds an `interval timer reason'; if there are no others, the
+ *             interval timer is engaged.
+ */
+
+extern void iv_addreason(void);
+
+/* --- @iv_rmreason@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Removes an interval timer reason; if there are none left, the
+ *             interval timer is disengaged.
+ */
+
+extern void iv_rmreason(void);
+
+/*----- The main loop -----------------------------------------------------*/
+
+/* --- @lp_init@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes the main loop.  Most importantly, this sets up
+ *             the select multiplexor that everything else hooks onto.
+ */
+
+extern void lp_init(void);
+
+/* --- @lp_run@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    Zero on successful termination; @-1@ if things went wrong.
+ *
+ * Use:                Cranks the main loop until it should be cranked no more.
+ */
+
+extern int lp_run(void);
+
 /*----- Tunnel drivers ----------------------------------------------------*/
 
 #ifdef TUN_LINUX
@@ -1778,6 +1919,15 @@ extern const char *timestr(time_t /*t*/);
 
 extern int mystrieq(const char */*x*/, const char */*y*/);
 
+/* --- @afix@ --- *
+ *
+ * Arguments:  @int af@ = an address family code
+ *
+ * Returns:    The index of the address family's record in @aftab@, or @-1@.
+ */
+
+extern int afix(int af);
+
 /* --- @addrsz@ --- *
  *
  * Arguments:  @const addr *a@ = a network address
@@ -1787,6 +1937,19 @@ extern int mystrieq(const char */*x*/, const char */*y*/);
 
 extern socklen_t addrsz(const addr */*a*/);
 
+/* --- @getport@, @setport@ --- *
+ *
+ * Arguments:  @addr *a@ = a network address
+ *             @unsigned port@ = port number to set
+ *
+ * Returns:    ---
+ *
+ * Use:                Retrieves or sets the port number in an address structure.
+ */
+
+extern unsigned getport(addr */*a*/);
+extern void setport(addr */*a*/, unsigned /*port*/);
+
 /* --- @seq_reset@ --- *
  *
  * Arguments:  @seqwin *s@ = sequence-checking window