New socket address types.
[fwd] / un.c
diff --git a/un.c b/un.c
new file mode 100644 (file)
index 0000000..c0a2cc9
--- /dev/null
+++ b/un.c
@@ -0,0 +1,210 @@
+/* -*-c-*-
+ *
+ * $Id: un.c,v 1.1 1999/07/26 23:34:11 mdw Exp $
+ *
+ * Protocol specific definitions for Unix-domain sockets
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the `fw' port forwarder.
+ *
+ * `fw' is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * `fw' is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with `fw'; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: un.c,v $
+ * Revision 1.1  1999/07/26 23:34:11  mdw
+ * New socket address types.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "config.h"
+
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include <mLib/alloc.h>
+#include <mLib/dstr.h>
+#include <mLib/report.h>
+#include <mLib/sub.h>
+
+#include "addr.h"
+#include "conf.h"
+#include "fattr.h"
+#include "fw.h"
+#include "reffd.h"
+#include "scan.h"
+#include "un.h"
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct un_addr {
+  addr a;
+  struct sockaddr_un sun;
+} un_addr;
+
+typedef struct un_opts {
+  addr_opts ao;
+  fattr f;
+} un_opts;
+
+/*----- Protocol operations -----------------------------------------------*/
+
+/* --- @read@ --- */
+
+static addr *un_read(scanner *sc, unsigned type)
+{
+  dstr d = DSTR_INIT;
+  un_addr *ua;
+
+  conf_name(sc, '/', &d);
+  ua = xmalloc(sizeof(addr) +
+              offsetof(struct sockaddr_un, sun_path) +
+              d.len + 1);
+  ua->a.ops = &un_ops;
+  ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
+  ua->sun.sun_family = AF_UNIX;
+  memcpy(ua->sun.sun_path, d.buf, d.len + 1);
+  dstr_destroy(&d);
+  return (&ua->a);
+}
+
+/* --- @destroy@ --- */
+
+static void un_destroy(addr *a)
+{
+  un_addr *ua = (un_addr *)a;
+  free(ua);
+}
+
+/* --- @print@ --- */
+
+static void un_print(addr *a, unsigned type, dstr *d)
+{
+  un_addr *ua = (un_addr *)a;
+  dstr_puts(d, "unix:");
+  dstr_puts(d, ua->sun.sun_path);
+}
+
+/* --- @initopts@ --- */
+
+static addr_opts *un_initopts(void)
+{
+  un_opts *uo = CREATE(un_opts);
+  uo->f = fattr_global;
+  return (&uo->ao);
+}
+
+/* --- @option@ --- */
+
+static int un_option(scanner *sc, addr_opts *ao)
+{
+  un_opts *uo = (un_opts *)ao;
+  CONF_BEGIN(sc, "unix", "Unix domain socket")
+
+  if (fattr_option(sc, uo ? &uo->f : &fattr_global))
+    CONF_ACCEPT;
+
+  CONF_END;
+}
+
+/* --- @accept@ --- */
+
+static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
+{
+  int nfd;
+  un_opts *uo = (un_opts *)ao;
+
+  /* --- Accept the new connection --- */
+
+  {
+    char buf[PATH_MAX + sizeof(struct sockaddr)];
+    struct sockaddr_un *sun = (struct sockaddr_un *)buf;
+    int sunsz = sizeof(buf);
+    if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
+      return (0);
+  }
+
+  /* --- Log the connection --- *
+   *
+   * It'd be really nice if I could find out who the user is, but I can't in
+   * anything like a portable way.
+   */
+
+  if (!(uo->ao.f & ADDRF_NOLOG))
+    fw_log(-1, "[%s] accepted", desc);
+  return (reffd_init(nfd));
+}
+
+/* --- @freeopts@ --- */
+
+static void un_freeopts(addr_opts *ao)
+{
+  un_opts *uo = (un_opts *)ao;
+  DESTROY(uo);
+}
+
+/* --- @bound@ --- */
+
+static void un_bound(addr *a, addr_opts *ao)
+{
+  un_addr *ua = (un_addr *)a;
+  un_opts *uo = (un_opts *)ao;
+  if (fattr_apply(ua->sun.sun_path, &uo->f)) {
+    dstr d = DSTR_INIT;
+    un_print(a, ADDR_SRC, &d);
+    fw_log(-1, "[%s] couldn't apply file attributes: %s",
+          d.buf, strerror(errno));
+    dstr_destroy(&d);
+  }
+}
+
+/* --- @unbind@ --- */
+
+static void un_unbind(addr *a)
+{
+  un_addr *ua = (un_addr *)a;
+  unlink(ua->sun.sun_path);
+}
+
+/* --- Protocol definition --- */
+
+addr_ops un_ops = {
+  "unix", PF_UNIX,
+  un_read, un_destroy, un_print,
+  un_initopts, un_option, un_accept, un_freeopts, un_bound, un_unbind
+};
+
+/*----- That's all, folks -------------------------------------------------*/