3 * Protocol specific definitions for Unix-domain sockets
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fwd' port forwarder.
12 * `fwd' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fwd' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fwd'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Data structures ---------------------------------------------------*/
31 typedef struct un_addr
{
33 struct sockaddr_un sun
;
36 typedef struct un_opts
{
41 /*----- Protocol operations -----------------------------------------------*/
45 static addr
*un_read(scanner
*sc
, unsigned type
)
51 ua
= xmalloc(sizeof(addr
) +
52 offsetof(struct sockaddr_un
, sun_path
) +
55 ua
->a
.sz
= offsetof(struct sockaddr_un
, sun_path
) + d
.len
+ 1;
56 memset(&ua
->sun
, 0, ua
->a
.sz
);
57 ua
->sun
.sun_family
= AF_UNIX
;
58 memcpy(ua
->sun
.sun_path
, d
.buf
, d
.len
+ 1);
63 /* --- @destroy@ --- */
65 static void un_destroy(addr
*a
)
67 un_addr
*ua
= (un_addr
*)a
;
73 static void un_print(addr
*a
, unsigned type
, dstr
*d
)
75 un_addr
*ua
= (un_addr
*)a
;
76 dstr_puts(d
, "unix:");
77 dstr_puts(d
, ua
->sun
.sun_path
);
80 /* --- @initopts@ --- */
82 static addr_opts
*un_initopts(void)
84 un_opts
*uo
= CREATE(un_opts
);
89 /* --- @option@ --- */
91 static int srcopt(scanner
*sc
, addr_opts
*ao
)
93 un_opts
*uo
= (un_opts
*)ao
;
94 CONF_BEGIN(sc
, "source", "Unix domain socket source")
96 if (fattr_option(sc
, uo ?
&uo
->f
: &fattr_global
))
102 static int un_option(scanner
*sc
, addr_opts
*ao
, unsigned type
)
104 CONF_BEGIN(sc
, "unix", "Unix domain socket");
105 if (type
!= ADDR_DEST
&& srcopt(sc
, ao
))
110 /* --- @accept@ --- */
112 static reffd
*un_accept(int fd
, addr_opts
*ao
, const char *desc
)
115 un_opts
*uo
= (un_opts
*)ao
;
117 /* --- Accept the new connection --- */
120 char buf
[PATH_MAX
+ sizeof(struct sockaddr
)];
121 struct sockaddr_un
*sun
= (struct sockaddr_un
*)buf
;
122 size_t sunsz
= sizeof(buf
);
124 if ((nfd
= accept(fd
, (struct sockaddr
*)sun
, &sunsz
)) < 0)
128 /* --- Log the connection --- *
130 * It'd be really nice if I could find out who the user is, but I can't in
131 * anything like a portable way.
134 if (!(uo
->ao
.f
& ADDRF_NOLOG
))
135 fw_log(-1, "[%s] accepted", desc
);
136 return (reffd_init(nfd
));
139 /* --- @freeopts@ --- */
141 static void un_freeopts(addr_opts
*ao
)
143 un_opts
*uo
= (un_opts
*)ao
;
149 static int un_bind(addr
*a
, addr_opts
*ao
)
151 un_addr
*ua
= (un_addr
*)a
;
152 un_opts
*uo
= (un_opts
*)ao
;
155 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
157 if (bind(fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
)))
159 if (fattr_apply(ua
->sun
.sun_path
, &uo
->f
))
169 /* --- @unbind@ --- */
171 static void un_unbind(addr
*a
)
173 un_addr
*ua
= (un_addr
*)a
;
174 unlink(ua
->sun
.sun_path
);
177 /* --- @connect@ --- */
179 static int un_connect(addr
*a
, addr_opts
*ao
, conn
*c
, endpt
*e
)
181 un_addr
*ua
= (un_addr
*)a
;
184 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
186 return (conn_init(c
, sel
, fd
, (struct sockaddr
*)&ua
->sun
, sizeof(ua
->sun
),
187 starget_connected
, e
));
192 /* --- Protocol definition --- */
196 un_read
, un_destroy
, un_print
,
197 un_initopts
, un_option
, 0, un_freeopts
, un_bind
, un_unbind
, un_accept
,
201 /*----- That's all, folks -------------------------------------------------*/