3 * $Id: un.c,v 1.3 2000/08/01 17:58:32 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.3 2000/08/01 17:58:32 mdw
33 * Remove unnecessary <ctype.h> header.
35 * Revision 1.2 1999/07/27 18:30:53 mdw
36 * Various minor portability fixes.
38 * Revision 1.1 1999/07/26 23:34:11 mdw
39 * New socket address types.
43 /*----- Header files ------------------------------------------------------*/
46 #undef sun /* Cretins */
55 #include <sys/types.h>
58 #include <sys/socket.h>
60 #include <arpa/inet.h>
63 #include <mLib/alloc.h>
64 #include <mLib/dstr.h>
65 #include <mLib/report.h>
76 /*----- Data structures ---------------------------------------------------*/
78 typedef struct un_addr {
80 struct sockaddr_un sun;
83 typedef struct un_opts {
88 /*----- Protocol operations -----------------------------------------------*/
92 static addr *un_read(scanner *sc, unsigned type)
97 conf_name(sc, '/', &d);
98 ua = xmalloc(sizeof(addr) +
99 offsetof(struct sockaddr_un, sun_path) +
102 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
103 memset(&ua->sun, 0, ua->a.sz);
104 ua->sun.sun_family = AF_UNIX;
105 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
110 /* --- @destroy@ --- */
112 static void un_destroy(addr *a)
114 un_addr *ua = (un_addr *)a;
118 /* --- @print@ --- */
120 static void un_print(addr *a, unsigned type, dstr *d)
122 un_addr *ua = (un_addr *)a;
123 dstr_puts(d, "unix:");
124 dstr_puts(d, ua->sun.sun_path);
127 /* --- @initopts@ --- */
129 static addr_opts *un_initopts(void)
131 un_opts *uo = CREATE(un_opts);
132 uo->f = fattr_global;
136 /* --- @option@ --- */
138 static int un_option(scanner *sc, addr_opts *ao)
140 un_opts *uo = (un_opts *)ao;
141 CONF_BEGIN(sc, "unix", "Unix domain socket")
143 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
149 /* --- @accept@ --- */
151 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
154 un_opts *uo = (un_opts *)ao;
156 /* --- Accept the new connection --- */
159 char buf[PATH_MAX + sizeof(struct sockaddr)];
160 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
161 int sunsz = sizeof(buf);
163 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
167 /* --- Log the connection --- *
169 * It'd be really nice if I could find out who the user is, but I can't in
170 * anything like a portable way.
173 if (!(uo->ao.f & ADDRF_NOLOG))
174 fw_log(-1, "[%s] accepted", desc);
175 return (reffd_init(nfd));
178 /* --- @freeopts@ --- */
180 static void un_freeopts(addr_opts *ao)
182 un_opts *uo = (un_opts *)ao;
186 /* --- @bound@ --- */
188 static void un_bound(addr *a, addr_opts *ao)
190 un_addr *ua = (un_addr *)a;
191 un_opts *uo = (un_opts *)ao;
192 if (fattr_apply(ua->sun.sun_path, &uo->f)) {
194 un_print(a, ADDR_SRC, &d);
195 fw_log(-1, "[%s] couldn't apply file attributes: %s",
196 d.buf, strerror(errno));
201 /* --- @unbind@ --- */
203 static void un_unbind(addr *a)
205 un_addr *ua = (un_addr *)a;
206 unlink(ua->sun.sun_path);
209 /* --- Protocol definition --- */
213 un_read, un_destroy, un_print,
214 un_initopts, un_option, un_accept, un_freeopts, un_bound, un_unbind
217 /*----- That's all, folks -------------------------------------------------*/