3 * $Id: un.c,v 1.8 2003/11/30 00:25:27 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.8 2003/11/30 00:25:27 mdw
33 * Fix source option prefix.
35 * Revision 1.7 2003/11/29 20:36:07 mdw
36 * Privileged outgoing connections.
38 * Revision 1.6 2003/11/25 14:08:23 mdw
39 * Debianization. Socket target options. Internet binding.
41 * Revision 1.5 2002/02/22 23:43:32 mdw
42 * Call @xfree@ rather than @free@.
44 * Revision 1.4 2000/08/01 17:59:56 mdw
45 * Switch over to using `size_t' for socket address lengths.
47 * Revision 1.3 2000/08/01 17:58:32 mdw
48 * Remove unnecessary <ctype.h> header.
50 * Revision 1.2 1999/07/27 18:30:53 mdw
51 * Various minor portability fixes.
53 * Revision 1.1 1999/07/26 23:34:11 mdw
54 * New socket address types.
58 /*----- Header files ------------------------------------------------------*/
61 #undef sun /* Cretins */
70 #include <sys/types.h>
73 #include <sys/socket.h>
75 #include <arpa/inet.h>
78 #include <mLib/alloc.h>
79 #include <mLib/dstr.h>
80 #include <mLib/report.h>
92 /*----- Data structures ---------------------------------------------------*/
94 typedef struct un_addr
{
96 struct sockaddr_un sun
;
99 typedef struct un_opts
{
104 /*----- Protocol operations -----------------------------------------------*/
108 static addr
*un_read(scanner
*sc
, unsigned type
)
113 conf_name(sc
, '/', &d
);
114 ua
= xmalloc(sizeof(addr
) +
115 offsetof(struct sockaddr_un
, sun_path
) +
118 ua
->a
.sz
= offsetof(struct sockaddr_un
, sun_path
) + d
.len
+ 1;
119 memset(&ua
->sun
, 0, ua
->a
.sz
);
120 ua
->sun
.sun_family
= AF_UNIX
;
121 memcpy(ua
->sun
.sun_path
, d
.buf
, d
.len
+ 1);
126 /* --- @destroy@ --- */
128 static void un_destroy(addr
*a
)
130 un_addr
*ua
= (un_addr
*)a
;
134 /* --- @print@ --- */
136 static void un_print(addr
*a
, unsigned type
, dstr
*d
)
138 un_addr
*ua
= (un_addr
*)a
;
139 dstr_puts(d
, "unix:");
140 dstr_puts(d
, ua
->sun
.sun_path
);
143 /* --- @initopts@ --- */
145 static addr_opts
*un_initopts(void)
147 un_opts
*uo
= CREATE(un_opts
);
148 uo
->f
= fattr_global
;
152 /* --- @option@ --- */
154 static int srcopt(scanner
*sc
, addr_opts
*ao
)
156 un_opts
*uo
= (un_opts
*)ao
;
157 CONF_BEGIN(sc
, "source", "Unix domain socket source")
159 if (fattr_option(sc
, uo ?
&uo
->f
: &fattr_global
))
165 static int un_option(scanner
*sc
, addr_opts
*ao
, unsigned type
)
167 CONF_BEGIN(sc
, "unix", "Unix domain socket");
168 if (type
!= ADDR_DEST
&& srcopt(sc
, ao
))
173 /* --- @accept@ --- */
175 static reffd
*un_accept(int fd
, addr_opts
*ao
, const char *desc
)
178 un_opts
*uo
= (un_opts
*)ao
;
180 /* --- Accept the new connection --- */
183 char buf
[PATH_MAX
+ sizeof(struct sockaddr
)];
184 struct sockaddr_un
*sun
= (struct sockaddr_un
*)buf
;
185 size_t sunsz
= sizeof(buf
);
187 if ((nfd
= accept(fd
, (struct sockaddr
*)sun
, &sunsz
)) < 0)
191 /* --- Log the connection --- *
193 * It'd be really nice if I could find out who the user is, but I can't in
194 * anything like a portable way.
197 if (!(uo
->ao
.f
& ADDRF_NOLOG
))
198 fw_log(-1, "[%s] accepted", desc
);
199 return (reffd_init(nfd
));
202 /* --- @freeopts@ --- */
204 static void un_freeopts(addr_opts
*ao
)
206 un_opts
*uo
= (un_opts
*)ao
;
212 static int un_bind(addr
*a
, addr_opts
*ao
)
214 un_addr
*ua
= (un_addr
*)a
;
215 un_opts
*uo
= (un_opts
*)ao
;
218 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
220 if (bind(fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
)))
222 if (fattr_apply(ua
->sun
.sun_path
, &uo
->f
))
232 /* --- @unbind@ --- */
234 static void un_unbind(addr
*a
)
236 un_addr
*ua
= (un_addr
*)a
;
237 unlink(ua
->sun
.sun_path
);
240 /* --- @connect@ --- */
242 static int un_connect(addr
*a
, addr_opts
*ao
, conn
*c
, endpt
*e
)
244 un_addr
*ua
= (un_addr
*)a
;
247 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
249 return (conn_init(c
, sel
, fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
),
250 starget_connected
, e
));
255 /* --- Protocol definition --- */
259 un_read
, un_destroy
, un_print
,
260 un_initopts
, un_option
, 0, un_freeopts
, un_bind
, un_unbind
, un_accept
,
264 /*----- That's all, folks -------------------------------------------------*/