Whitespace fixing.
[fwd] / un.c
CommitLineData
aa1f699e 1/* -*-c-*-
2 *
aa1f699e 3 * Protocol specific definitions for Unix-domain sockets
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
206212ca 8/*----- Licensing notice --------------------------------------------------*
aa1f699e 9 *
9155ea97 10 * This file is part of the `fwd' port forwarder.
aa1f699e 11 *
9155ea97 12 * `fwd' is free software; you can redistribute it and/or modify
aa1f699e 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.
206212ca 16 *
9155ea97 17 * `fwd' is distributed in the hope that it will be useful,
aa1f699e 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.
206212ca 21 *
aa1f699e 22 * You should have received a copy of the GNU General Public License
9155ea97 23 * along with `fwd'; if not, write to the Free Software Foundation,
aa1f699e 24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
9155ea97 27#include "fwd.h"
aa1f699e 28
29/*----- Data structures ---------------------------------------------------*/
30
31typedef struct un_addr {
32 addr a;
33 struct sockaddr_un sun;
34} un_addr;
35
36typedef struct un_opts {
37 addr_opts ao;
38 fattr f;
39} un_opts;
40
41/*----- Protocol operations -----------------------------------------------*/
42
43/* --- @read@ --- */
44
45static addr *un_read(scanner *sc, unsigned type)
46{
47 dstr d = DSTR_INIT;
48 un_addr *ua;
49
35a142ca 50 conf_fname(sc, &d);
aa1f699e 51 ua = xmalloc(sizeof(addr) +
52 offsetof(struct sockaddr_un, sun_path) +
53 d.len + 1);
54 ua->a.ops = &un_ops;
55 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
e0ce9d38 56 memset(&ua->sun, 0, ua->a.sz);
aa1f699e 57 ua->sun.sun_family = AF_UNIX;
58 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
59 dstr_destroy(&d);
60 return (&ua->a);
61}
62
63/* --- @destroy@ --- */
64
65static void un_destroy(addr *a)
66{
67 un_addr *ua = (un_addr *)a;
b0805b27 68 xfree(ua);
aa1f699e 69}
70
71/* --- @print@ --- */
72
73static void un_print(addr *a, unsigned type, dstr *d)
74{
75 un_addr *ua = (un_addr *)a;
76 dstr_puts(d, "unix:");
77 dstr_puts(d, ua->sun.sun_path);
78}
79
80/* --- @initopts@ --- */
81
82static addr_opts *un_initopts(void)
83{
84 un_opts *uo = CREATE(un_opts);
85 uo->f = fattr_global;
86 return (&uo->ao);
87}
88
89/* --- @option@ --- */
90
0ac54f22 91static int srcopt(scanner *sc, addr_opts *ao)
aa1f699e 92{
93 un_opts *uo = (un_opts *)ao;
ad7fbf45 94 CONF_BEGIN(sc, "source", "Unix domain socket source")
aa1f699e 95
96 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
97 CONF_ACCEPT;
98
99 CONF_END;
100}
101
0ac54f22 102static int un_option(scanner *sc, addr_opts *ao, unsigned type)
103{
104 CONF_BEGIN(sc, "unix", "Unix domain socket");
105 if (type != ADDR_DEST && srcopt(sc, ao))
106 CONF_ACCEPT;
107 CONF_END;
108}
109
aa1f699e 110/* --- @accept@ --- */
111
112static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
113{
114 int nfd;
115 un_opts *uo = (un_opts *)ao;
116
117 /* --- Accept the new connection --- */
118
119 {
120 char buf[PATH_MAX + sizeof(struct sockaddr)];
121 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
bc241e98 122 size_t sunsz = sizeof(buf);
206212ca 123
aa1f699e 124 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
125 return (0);
126 }
127
128 /* --- Log the connection --- *
129 *
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.
132 */
133
134 if (!(uo->ao.f & ADDRF_NOLOG))
135 fw_log(-1, "[%s] accepted", desc);
136 return (reffd_init(nfd));
137}
138
139/* --- @freeopts@ --- */
140
141static void un_freeopts(addr_opts *ao)
142{
143 un_opts *uo = (un_opts *)ao;
144 DESTROY(uo);
145}
146
0ac54f22 147/* --- @bind@ --- */
aa1f699e 148
0ac54f22 149static int un_bind(addr *a, addr_opts *ao)
aa1f699e 150{
151 un_addr *ua = (un_addr *)a;
152 un_opts *uo = (un_opts *)ao;
0ac54f22 153 int fd;
154
155 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
156 goto fail_0;
157 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
158 goto fail_1;
159 if (fattr_apply(ua->sun.sun_path, &uo->f))
160 goto fail_1;
161 return (fd);
162
163fail_1:
164 close(fd);
165fail_0:
166 return (-1);
aa1f699e 167}
168
169/* --- @unbind@ --- */
170
171static void un_unbind(addr *a)
172{
173 un_addr *ua = (un_addr *)a;
174 unlink(ua->sun.sun_path);
175}
176
0ac54f22 177/* --- @connect@ --- */
178
179static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
180{
181 un_addr *ua = (un_addr *)a;
182 int fd;
183
184 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
185 goto fail_0;
186 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
187 starget_connected, e));
188fail_0:
189 return (-1);
190}
191
aa1f699e 192/* --- Protocol definition --- */
193
194addr_ops un_ops = {
0ac54f22 195 "unix",
aa1f699e 196 un_read, un_destroy, un_print,
ee599f55 197 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
0ac54f22 198 0, 0, un_connect
aa1f699e 199};
200
201/*----- That's all, folks -------------------------------------------------*/