/* -*-c-*-
*
- * $Id: un.c,v 1.5 2002/02/22 23:43:32 mdw Exp $
+ * $Id: un.c,v 1.6 2003/11/25 14:08:23 mdw Exp $
*
* Protocol specific definitions for Unix-domain sockets
*
/*----- Revision history --------------------------------------------------*
*
* $Log: un.c,v $
+ * Revision 1.6 2003/11/25 14:08:23 mdw
+ * Debianization. Socket target options. Internet binding.
+ *
* Revision 1.5 2002/02/22 23:43:32 mdw
* Call @xfree@ rather than @free@.
*
#include "fw.h"
#include "reffd.h"
#include "scan.h"
+#include "socket.h"
#include "un.h"
/*----- Data structures ---------------------------------------------------*/
/* --- @option@ --- */
-static int un_option(scanner *sc, addr_opts *ao)
+static int srcopt(scanner *sc, addr_opts *ao)
{
un_opts *uo = (un_opts *)ao;
CONF_BEGIN(sc, "unix", "Unix domain socket")
CONF_END;
}
+static int un_option(scanner *sc, addr_opts *ao, unsigned type)
+{
+ CONF_BEGIN(sc, "unix", "Unix domain socket");
+ if (type != ADDR_DEST && srcopt(sc, ao))
+ CONF_ACCEPT;
+ CONF_END;
+}
+
/* --- @accept@ --- */
static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
DESTROY(uo);
}
-/* --- @bound@ --- */
+/* --- @bind@ --- */
-static void un_bound(addr *a, addr_opts *ao)
+static int un_bind(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);
- }
+ int fd;
+
+ if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
+ goto fail_0;
+ if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
+ goto fail_1;
+ if (fattr_apply(ua->sun.sun_path, &uo->f))
+ goto fail_1;
+ return (fd);
+
+fail_1:
+ close(fd);
+fail_0:
+ return (-1);
}
/* --- @unbind@ --- */
unlink(ua->sun.sun_path);
}
+/* --- @connect@ --- */
+
+static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
+{
+ un_addr *ua = (un_addr *)a;
+ int fd;
+
+ if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
+ goto fail_0;
+ return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
+ starget_connected, e));
+fail_0:
+ return (-1);
+}
+
/* --- Protocol definition --- */
addr_ops un_ops = {
- "unix", PF_UNIX,
+ "unix",
un_read, un_destroy, un_print,
- un_initopts, un_option, un_accept, un_freeopts, un_bound, un_unbind
+ un_initopts, un_option, un_freeopts, un_bind, un_unbind, un_accept,
+ 0, 0, un_connect
};
/*----- That's all, folks -------------------------------------------------*/