3 * $Id: un.c,v 1.4 2000/08/01 17:59:56 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.4 2000/08/01 17:59:56 mdw
33 * Switch over to using `size_t' for socket address lengths.
35 * Revision 1.3 2000/08/01 17:58:32 mdw
36 * Remove unnecessary <ctype.h> header.
38 * Revision 1.2 1999/07/27 18:30:53 mdw
39 * Various minor portability fixes.
41 * Revision 1.1 1999/07/26 23:34:11 mdw
42 * New socket address types.
46 /*----- Header files ------------------------------------------------------*/
49 #undef sun /* Cretins */
58 #include <sys/types.h>
61 #include <sys/socket.h>
63 #include <arpa/inet.h>
66 #include <mLib/alloc.h>
67 #include <mLib/dstr.h>
68 #include <mLib/report.h>
79 /*----- Data structures ---------------------------------------------------*/
81 typedef struct un_addr {
83 struct sockaddr_un sun;
86 typedef struct un_opts {
91 /*----- Protocol operations -----------------------------------------------*/
95 static addr *un_read(scanner *sc, unsigned type)
100 conf_name(sc, '/', &d);
101 ua = xmalloc(sizeof(addr) +
102 offsetof(struct sockaddr_un, sun_path) +
105 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
106 memset(&ua->sun, 0, ua->a.sz);
107 ua->sun.sun_family = AF_UNIX;
108 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
113 /* --- @destroy@ --- */
115 static void un_destroy(addr *a)
117 un_addr *ua = (un_addr *)a;
121 /* --- @print@ --- */
123 static void un_print(addr *a, unsigned type, dstr *d)
125 un_addr *ua = (un_addr *)a;
126 dstr_puts(d, "unix:");
127 dstr_puts(d, ua->sun.sun_path);
130 /* --- @initopts@ --- */
132 static addr_opts *un_initopts(void)
134 un_opts *uo = CREATE(un_opts);
135 uo->f = fattr_global;
139 /* --- @option@ --- */
141 static int un_option(scanner *sc, addr_opts *ao)
143 un_opts *uo = (un_opts *)ao;
144 CONF_BEGIN(sc, "unix", "Unix domain socket")
146 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
152 /* --- @accept@ --- */
154 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
157 un_opts *uo = (un_opts *)ao;
159 /* --- Accept the new connection --- */
162 char buf[PATH_MAX + sizeof(struct sockaddr)];
163 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
164 size_t sunsz = sizeof(buf);
166 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
170 /* --- Log the connection --- *
172 * It'd be really nice if I could find out who the user is, but I can't in
173 * anything like a portable way.
176 if (!(uo->ao.f & ADDRF_NOLOG))
177 fw_log(-1, "[%s] accepted", desc);
178 return (reffd_init(nfd));
181 /* --- @freeopts@ --- */
183 static void un_freeopts(addr_opts *ao)
185 un_opts *uo = (un_opts *)ao;
189 /* --- @bound@ --- */
191 static void un_bound(addr *a, addr_opts *ao)
193 un_addr *ua = (un_addr *)a;
194 un_opts *uo = (un_opts *)ao;
195 if (fattr_apply(ua->sun.sun_path, &uo->f)) {
197 un_print(a, ADDR_SRC, &d);
198 fw_log(-1, "[%s] couldn't apply file attributes: %s",
199 d.buf, strerror(errno));
204 /* --- @unbind@ --- */
206 static void un_unbind(addr *a)
208 un_addr *ua = (un_addr *)a;
209 unlink(ua->sun.sun_path);
212 /* --- Protocol definition --- */
216 un_read, un_destroy, un_print,
217 un_initopts, un_option, un_accept, un_freeopts, un_bound, un_unbind
220 /*----- That's all, folks -------------------------------------------------*/