3 * $Id: un.c,v 1.6 2003/11/25 14:08:23 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.6 2003/11/25 14:08:23 mdw
33 * Debianization. Socket target options. Internet binding.
35 * Revision 1.5 2002/02/22 23:43:32 mdw
36 * Call @xfree@ rather than @free@.
38 * Revision 1.4 2000/08/01 17:59:56 mdw
39 * Switch over to using `size_t' for socket address lengths.
41 * Revision 1.3 2000/08/01 17:58:32 mdw
42 * Remove unnecessary <ctype.h> header.
44 * Revision 1.2 1999/07/27 18:30:53 mdw
45 * Various minor portability fixes.
47 * Revision 1.1 1999/07/26 23:34:11 mdw
48 * New socket address types.
52 /*----- Header files ------------------------------------------------------*/
55 #undef sun /* Cretins */
64 #include <sys/types.h>
67 #include <sys/socket.h>
69 #include <arpa/inet.h>
72 #include <mLib/alloc.h>
73 #include <mLib/dstr.h>
74 #include <mLib/report.h>
86 /*----- Data structures ---------------------------------------------------*/
88 typedef struct un_addr
{
90 struct sockaddr_un sun
;
93 typedef struct un_opts
{
98 /*----- Protocol operations -----------------------------------------------*/
102 static addr
*un_read(scanner
*sc
, unsigned type
)
107 conf_name(sc
, '/', &d
);
108 ua
= xmalloc(sizeof(addr
) +
109 offsetof(struct sockaddr_un
, sun_path
) +
112 ua
->a
.sz
= offsetof(struct sockaddr_un
, sun_path
) + d
.len
+ 1;
113 memset(&ua
->sun
, 0, ua
->a
.sz
);
114 ua
->sun
.sun_family
= AF_UNIX
;
115 memcpy(ua
->sun
.sun_path
, d
.buf
, d
.len
+ 1);
120 /* --- @destroy@ --- */
122 static void un_destroy(addr
*a
)
124 un_addr
*ua
= (un_addr
*)a
;
128 /* --- @print@ --- */
130 static void un_print(addr
*a
, unsigned type
, dstr
*d
)
132 un_addr
*ua
= (un_addr
*)a
;
133 dstr_puts(d
, "unix:");
134 dstr_puts(d
, ua
->sun
.sun_path
);
137 /* --- @initopts@ --- */
139 static addr_opts
*un_initopts(void)
141 un_opts
*uo
= CREATE(un_opts
);
142 uo
->f
= fattr_global
;
146 /* --- @option@ --- */
148 static int srcopt(scanner
*sc
, addr_opts
*ao
)
150 un_opts
*uo
= (un_opts
*)ao
;
151 CONF_BEGIN(sc
, "unix", "Unix domain socket")
153 if (fattr_option(sc
, uo ?
&uo
->f
: &fattr_global
))
159 static int un_option(scanner
*sc
, addr_opts
*ao
, unsigned type
)
161 CONF_BEGIN(sc
, "unix", "Unix domain socket");
162 if (type
!= ADDR_DEST
&& srcopt(sc
, ao
))
167 /* --- @accept@ --- */
169 static reffd
*un_accept(int fd
, addr_opts
*ao
, const char *desc
)
172 un_opts
*uo
= (un_opts
*)ao
;
174 /* --- Accept the new connection --- */
177 char buf
[PATH_MAX
+ sizeof(struct sockaddr
)];
178 struct sockaddr_un
*sun
= (struct sockaddr_un
*)buf
;
179 size_t sunsz
= sizeof(buf
);
181 if ((nfd
= accept(fd
, (struct sockaddr
*)sun
, &sunsz
)) < 0)
185 /* --- Log the connection --- *
187 * It'd be really nice if I could find out who the user is, but I can't in
188 * anything like a portable way.
191 if (!(uo
->ao
.f
& ADDRF_NOLOG
))
192 fw_log(-1, "[%s] accepted", desc
);
193 return (reffd_init(nfd
));
196 /* --- @freeopts@ --- */
198 static void un_freeopts(addr_opts
*ao
)
200 un_opts
*uo
= (un_opts
*)ao
;
206 static int un_bind(addr
*a
, addr_opts
*ao
)
208 un_addr
*ua
= (un_addr
*)a
;
209 un_opts
*uo
= (un_opts
*)ao
;
212 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
214 if (bind(fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
)))
216 if (fattr_apply(ua
->sun
.sun_path
, &uo
->f
))
226 /* --- @unbind@ --- */
228 static void un_unbind(addr
*a
)
230 un_addr
*ua
= (un_addr
*)a
;
231 unlink(ua
->sun
.sun_path
);
234 /* --- @connect@ --- */
236 static int un_connect(addr
*a
, addr_opts
*ao
, conn
*c
, endpt
*e
)
238 un_addr
*ua
= (un_addr
*)a
;
241 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
243 return (conn_init(c
, sel
, fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
),
244 starget_connected
, e
));
249 /* --- Protocol definition --- */
253 un_read
, un_destroy
, un_print
,
254 un_initopts
, un_option
, un_freeopts
, un_bind
, un_unbind
, un_accept
,
258 /*----- That's all, folks -------------------------------------------------*/