3 * $Id: un.c,v 1.5 2002/02/22 23:43: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.5 2002/02/22 23:43:32 mdw
33 * Call @xfree@ rather than @free@.
35 * Revision 1.4 2000/08/01 17:59:56 mdw
36 * Switch over to using `size_t' for socket address lengths.
38 * Revision 1.3 2000/08/01 17:58:32 mdw
39 * Remove unnecessary <ctype.h> header.
41 * Revision 1.2 1999/07/27 18:30:53 mdw
42 * Various minor portability fixes.
44 * Revision 1.1 1999/07/26 23:34:11 mdw
45 * New socket address types.
49 /*----- Header files ------------------------------------------------------*/
52 #undef sun /* Cretins */
61 #include <sys/types.h>
64 #include <sys/socket.h>
66 #include <arpa/inet.h>
69 #include <mLib/alloc.h>
70 #include <mLib/dstr.h>
71 #include <mLib/report.h>
82 /*----- Data structures ---------------------------------------------------*/
84 typedef struct un_addr
{
86 struct sockaddr_un sun
;
89 typedef struct un_opts
{
94 /*----- Protocol operations -----------------------------------------------*/
98 static addr
*un_read(scanner
*sc
, unsigned type
)
103 conf_name(sc
, '/', &d
);
104 ua
= xmalloc(sizeof(addr
) +
105 offsetof(struct sockaddr_un
, sun_path
) +
108 ua
->a
.sz
= offsetof(struct sockaddr_un
, sun_path
) + d
.len
+ 1;
109 memset(&ua
->sun
, 0, ua
->a
.sz
);
110 ua
->sun
.sun_family
= AF_UNIX
;
111 memcpy(ua
->sun
.sun_path
, d
.buf
, d
.len
+ 1);
116 /* --- @destroy@ --- */
118 static void un_destroy(addr
*a
)
120 un_addr
*ua
= (un_addr
*)a
;
124 /* --- @print@ --- */
126 static void un_print(addr
*a
, unsigned type
, dstr
*d
)
128 un_addr
*ua
= (un_addr
*)a
;
129 dstr_puts(d
, "unix:");
130 dstr_puts(d
, ua
->sun
.sun_path
);
133 /* --- @initopts@ --- */
135 static addr_opts
*un_initopts(void)
137 un_opts
*uo
= CREATE(un_opts
);
138 uo
->f
= fattr_global
;
142 /* --- @option@ --- */
144 static int un_option(scanner
*sc
, addr_opts
*ao
)
146 un_opts
*uo
= (un_opts
*)ao
;
147 CONF_BEGIN(sc
, "unix", "Unix domain socket")
149 if (fattr_option(sc
, uo ?
&uo
->f
: &fattr_global
))
155 /* --- @accept@ --- */
157 static reffd
*un_accept(int fd
, addr_opts
*ao
, const char *desc
)
160 un_opts
*uo
= (un_opts
*)ao
;
162 /* --- Accept the new connection --- */
165 char buf
[PATH_MAX
+ sizeof(struct sockaddr
)];
166 struct sockaddr_un
*sun
= (struct sockaddr_un
*)buf
;
167 size_t sunsz
= sizeof(buf
);
169 if ((nfd
= accept(fd
, (struct sockaddr
*)sun
, &sunsz
)) < 0)
173 /* --- Log the connection --- *
175 * It'd be really nice if I could find out who the user is, but I can't in
176 * anything like a portable way.
179 if (!(uo
->ao
.f
& ADDRF_NOLOG
))
180 fw_log(-1, "[%s] accepted", desc
);
181 return (reffd_init(nfd
));
184 /* --- @freeopts@ --- */
186 static void un_freeopts(addr_opts
*ao
)
188 un_opts
*uo
= (un_opts
*)ao
;
192 /* --- @bound@ --- */
194 static void un_bound(addr
*a
, addr_opts
*ao
)
196 un_addr
*ua
= (un_addr
*)a
;
197 un_opts
*uo
= (un_opts
*)ao
;
198 if (fattr_apply(ua
->sun
.sun_path
, &uo
->f
)) {
200 un_print(a
, ADDR_SRC
, &d
);
201 fw_log(-1, "[%s] couldn't apply file attributes: %s",
202 d
.buf
, strerror(errno
));
207 /* --- @unbind@ --- */
209 static void un_unbind(addr
*a
)
211 un_addr
*ua
= (un_addr
*)a
;
212 unlink(ua
->sun
.sun_path
);
215 /* --- Protocol definition --- */
219 un_read
, un_destroy
, un_print
,
220 un_initopts
, un_option
, un_accept
, un_freeopts
, un_bound
, un_unbind
223 /*----- That's all, folks -------------------------------------------------*/