3 * $Id: un.c,v 1.9 2004/04/08 01:36:25 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 /*----- Header files ------------------------------------------------------*/
32 #undef sun /* Cretins */
41 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <arpa/inet.h>
49 #include <mLib/alloc.h>
50 #include <mLib/dstr.h>
51 #include <mLib/report.h>
63 /*----- Data structures ---------------------------------------------------*/
65 typedef struct un_addr
{
67 struct sockaddr_un sun
;
70 typedef struct un_opts
{
75 /*----- Protocol operations -----------------------------------------------*/
79 static addr
*un_read(scanner
*sc
, unsigned type
)
84 conf_name(sc
, '/', &d
);
85 ua
= xmalloc(sizeof(addr
) +
86 offsetof(struct sockaddr_un
, sun_path
) +
89 ua
->a
.sz
= offsetof(struct sockaddr_un
, sun_path
) + d
.len
+ 1;
90 memset(&ua
->sun
, 0, ua
->a
.sz
);
91 ua
->sun
.sun_family
= AF_UNIX
;
92 memcpy(ua
->sun
.sun_path
, d
.buf
, d
.len
+ 1);
97 /* --- @destroy@ --- */
99 static void un_destroy(addr
*a
)
101 un_addr
*ua
= (un_addr
*)a
;
105 /* --- @print@ --- */
107 static void un_print(addr
*a
, unsigned type
, dstr
*d
)
109 un_addr
*ua
= (un_addr
*)a
;
110 dstr_puts(d
, "unix:");
111 dstr_puts(d
, ua
->sun
.sun_path
);
114 /* --- @initopts@ --- */
116 static addr_opts
*un_initopts(void)
118 un_opts
*uo
= CREATE(un_opts
);
119 uo
->f
= fattr_global
;
123 /* --- @option@ --- */
125 static int srcopt(scanner
*sc
, addr_opts
*ao
)
127 un_opts
*uo
= (un_opts
*)ao
;
128 CONF_BEGIN(sc
, "source", "Unix domain socket source")
130 if (fattr_option(sc
, uo ?
&uo
->f
: &fattr_global
))
136 static int un_option(scanner
*sc
, addr_opts
*ao
, unsigned type
)
138 CONF_BEGIN(sc
, "unix", "Unix domain socket");
139 if (type
!= ADDR_DEST
&& srcopt(sc
, ao
))
144 /* --- @accept@ --- */
146 static reffd
*un_accept(int fd
, addr_opts
*ao
, const char *desc
)
149 un_opts
*uo
= (un_opts
*)ao
;
151 /* --- Accept the new connection --- */
154 char buf
[PATH_MAX
+ sizeof(struct sockaddr
)];
155 struct sockaddr_un
*sun
= (struct sockaddr_un
*)buf
;
156 size_t sunsz
= sizeof(buf
);
158 if ((nfd
= accept(fd
, (struct sockaddr
*)sun
, &sunsz
)) < 0)
162 /* --- Log the connection --- *
164 * It'd be really nice if I could find out who the user is, but I can't in
165 * anything like a portable way.
168 if (!(uo
->ao
.f
& ADDRF_NOLOG
))
169 fw_log(-1, "[%s] accepted", desc
);
170 return (reffd_init(nfd
));
173 /* --- @freeopts@ --- */
175 static void un_freeopts(addr_opts
*ao
)
177 un_opts
*uo
= (un_opts
*)ao
;
183 static int un_bind(addr
*a
, addr_opts
*ao
)
185 un_addr
*ua
= (un_addr
*)a
;
186 un_opts
*uo
= (un_opts
*)ao
;
189 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
191 if (bind(fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
)))
193 if (fattr_apply(ua
->sun
.sun_path
, &uo
->f
))
203 /* --- @unbind@ --- */
205 static void un_unbind(addr
*a
)
207 un_addr
*ua
= (un_addr
*)a
;
208 unlink(ua
->sun
.sun_path
);
211 /* --- @connect@ --- */
213 static int un_connect(addr
*a
, addr_opts
*ao
, conn
*c
, endpt
*e
)
215 un_addr
*ua
= (un_addr
*)a
;
218 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
220 return (conn_init(c
, sel
, fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
),
221 starget_connected
, e
));
226 /* --- Protocol definition --- */
230 un_read
, un_destroy
, un_print
,
231 un_initopts
, un_option
, 0, un_freeopts
, un_bind
, un_unbind
, un_accept
,
235 /*----- That's all, folks -------------------------------------------------*/