X-Git-Url: https://git.distorted.org.uk/~mdw/fwd/blobdiff_plain/08cb0dd89765683f47206905c9116b73949fee63..0ac54f22a766f068db98e1caecbc913cb0cfd191:/un.c diff --git a/un.c b/un.c index e120f95..e415256 100644 --- a/un.c +++ b/un.c @@ -1,6 +1,6 @@ /* -*-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 * @@ -29,6 +29,9 @@ /*----- 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@. * @@ -77,6 +80,7 @@ #include "fw.h" #include "reffd.h" #include "scan.h" +#include "socket.h" #include "un.h" /*----- Data structures ---------------------------------------------------*/ @@ -141,7 +145,7 @@ static addr_opts *un_initopts(void) /* --- @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") @@ -152,6 +156,14 @@ static int un_option(scanner *sc, addr_opts *ao) 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) @@ -189,19 +201,26 @@ static void un_freeopts(addr_opts *ao) 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@ --- */ @@ -212,12 +231,28 @@ static void un_unbind(addr *a) 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 -------------------------------------------------*/