3 * $Id: un.c,v 1.1 1999/07/26 23:34:11 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.1 1999/07/26 23:34:11 mdw
33 * New socket address types.
37 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <arpa/inet.h>
57 #include <mLib/alloc.h>
58 #include <mLib/dstr.h>
59 #include <mLib/report.h>
70 /*----- Data structures ---------------------------------------------------*/
72 typedef struct un_addr
{
74 struct sockaddr_un sun
;
77 typedef struct un_opts
{
82 /*----- Protocol operations -----------------------------------------------*/
86 static addr
*un_read(scanner
*sc
, unsigned type
)
91 conf_name(sc
, '/', &d
);
92 ua
= xmalloc(sizeof(addr
) +
93 offsetof(struct sockaddr_un
, sun_path
) +
96 ua
->a
.sz
= offsetof(struct sockaddr_un
, sun_path
) + d
.len
+ 1;
97 ua
->sun
.sun_family
= AF_UNIX
;
98 memcpy(ua
->sun
.sun_path
, d
.buf
, d
.len
+ 1);
103 /* --- @destroy@ --- */
105 static void un_destroy(addr
*a
)
107 un_addr
*ua
= (un_addr
*)a
;
111 /* --- @print@ --- */
113 static void un_print(addr
*a
, unsigned type
, dstr
*d
)
115 un_addr
*ua
= (un_addr
*)a
;
116 dstr_puts(d
, "unix:");
117 dstr_puts(d
, ua
->sun
.sun_path
);
120 /* --- @initopts@ --- */
122 static addr_opts
*un_initopts(void)
124 un_opts
*uo
= CREATE(un_opts
);
125 uo
->f
= fattr_global
;
129 /* --- @option@ --- */
131 static int un_option(scanner
*sc
, addr_opts
*ao
)
133 un_opts
*uo
= (un_opts
*)ao
;
134 CONF_BEGIN(sc
, "unix", "Unix domain socket")
136 if (fattr_option(sc
, uo ?
&uo
->f
: &fattr_global
))
142 /* --- @accept@ --- */
144 static reffd
*un_accept(int fd
, addr_opts
*ao
, const char *desc
)
147 un_opts
*uo
= (un_opts
*)ao
;
149 /* --- Accept the new connection --- */
152 char buf
[PATH_MAX
+ sizeof(struct sockaddr
)];
153 struct sockaddr_un
*sun
= (struct sockaddr_un
*)buf
;
154 int sunsz
= sizeof(buf
);
156 if ((nfd
= accept(fd
, (struct sockaddr
*)sun
, &sunsz
)) < 0)
160 /* --- Log the connection --- *
162 * It'd be really nice if I could find out who the user is, but I can't in
163 * anything like a portable way.
166 if (!(uo
->ao
.f
& ADDRF_NOLOG
))
167 fw_log(-1, "[%s] accepted", desc
);
168 return (reffd_init(nfd
));
171 /* --- @freeopts@ --- */
173 static void un_freeopts(addr_opts
*ao
)
175 un_opts
*uo
= (un_opts
*)ao
;
179 /* --- @bound@ --- */
181 static void un_bound(addr
*a
, addr_opts
*ao
)
183 un_addr
*ua
= (un_addr
*)a
;
184 un_opts
*uo
= (un_opts
*)ao
;
185 if (fattr_apply(ua
->sun
.sun_path
, &uo
->f
)) {
187 un_print(a
, ADDR_SRC
, &d
);
188 fw_log(-1, "[%s] couldn't apply file attributes: %s",
189 d
.buf
, strerror(errno
));
194 /* --- @unbind@ --- */
196 static void un_unbind(addr
*a
)
198 un_addr
*ua
= (un_addr
*)a
;
199 unlink(ua
->sun
.sun_path
);
202 /* --- Protocol definition --- */
206 un_read
, un_destroy
, un_print
,
207 un_initopts
, un_option
, un_accept
, un_freeopts
, un_bound
, un_unbind
210 /*----- That's all, folks -------------------------------------------------*/