3 * $Id: un.c,v 1.2 1999/07/27 18:30:53 mdw Exp $
5 * Protocol specific definitions for Unix-domain sockets
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/07/27 18:30:53 mdw
33 * Various minor portability fixes.
35 * Revision 1.1 1999/07/26 23:34:11 mdw
36 * New socket address types.
40 /*----- Header files ------------------------------------------------------*/
43 #undef sun /* Cretins */
53 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <arpa/inet.h>
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
63 #include <mLib/report.h>
74 /*----- Data structures ---------------------------------------------------*/
76 typedef struct un_addr {
78 struct sockaddr_un sun;
81 typedef struct un_opts {
86 /*----- Protocol operations -----------------------------------------------*/
90 static addr *un_read(scanner *sc, unsigned type)
95 conf_name(sc, '/', &d);
96 ua = xmalloc(sizeof(addr) +
97 offsetof(struct sockaddr_un, sun_path) +
100 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
101 memset(&ua->sun, 0, ua->a.sz);
102 ua->sun.sun_family = AF_UNIX;
103 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
108 /* --- @destroy@ --- */
110 static void un_destroy(addr *a)
112 un_addr *ua = (un_addr *)a;
116 /* --- @print@ --- */
118 static void un_print(addr *a, unsigned type, dstr *d)
120 un_addr *ua = (un_addr *)a;
121 dstr_puts(d, "unix:");
122 dstr_puts(d, ua->sun.sun_path);
125 /* --- @initopts@ --- */
127 static addr_opts *un_initopts(void)
129 un_opts *uo = CREATE(un_opts);
130 uo->f = fattr_global;
134 /* --- @option@ --- */
136 static int un_option(scanner *sc, addr_opts *ao)
138 un_opts *uo = (un_opts *)ao;
139 CONF_BEGIN(sc, "unix", "Unix domain socket")
141 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
147 /* --- @accept@ --- */
149 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
152 un_opts *uo = (un_opts *)ao;
154 /* --- Accept the new connection --- */
157 char buf[PATH_MAX + sizeof(struct sockaddr)];
158 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
159 int sunsz = sizeof(buf);
161 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
165 /* --- Log the connection --- *
167 * It'd be really nice if I could find out who the user is, but I can't in
168 * anything like a portable way.
171 if (!(uo->ao.f & ADDRF_NOLOG))
172 fw_log(-1, "[%s] accepted", desc);
173 return (reffd_init(nfd));
176 /* --- @freeopts@ --- */
178 static void un_freeopts(addr_opts *ao)
180 un_opts *uo = (un_opts *)ao;
184 /* --- @bound@ --- */
186 static void un_bound(addr *a, addr_opts *ao)
188 un_addr *ua = (un_addr *)a;
189 un_opts *uo = (un_opts *)ao;
190 if (fattr_apply(ua->sun.sun_path, &uo->f)) {
192 un_print(a, ADDR_SRC, &d);
193 fw_log(-1, "[%s] couldn't apply file attributes: %s",
194 d.buf, strerror(errno));
199 /* --- @unbind@ --- */
201 static void un_unbind(addr *a)
203 un_addr *ua = (un_addr *)a;
204 unlink(ua->sun.sun_path);
207 /* --- Protocol definition --- */
211 un_read, un_destroy, un_print,
212 un_initopts, un_option, un_accept, un_freeopts, un_bound, un_unbind
215 /*----- That's all, folks -------------------------------------------------*/