Whitespace fixing.
[fwd] / un.c
1 /* -*-c-*-
2 *
3 * Protocol specific definitions for Unix-domain sockets
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the `fwd' port forwarder.
11 *
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.
16 *
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.
21 *
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.
25 */
26
27 #include "fwd.h"
28
29 /*----- Data structures ---------------------------------------------------*/
30
31 typedef struct un_addr {
32 addr a;
33 struct sockaddr_un sun;
34 } un_addr;
35
36 typedef struct un_opts {
37 addr_opts ao;
38 fattr f;
39 } un_opts;
40
41 /*----- Protocol operations -----------------------------------------------*/
42
43 /* --- @read@ --- */
44
45 static addr *un_read(scanner *sc, unsigned type)
46 {
47 dstr d = DSTR_INIT;
48 un_addr *ua;
49
50 conf_fname(sc, &d);
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;
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);
59 dstr_destroy(&d);
60 return (&ua->a);
61 }
62
63 /* --- @destroy@ --- */
64
65 static void un_destroy(addr *a)
66 {
67 un_addr *ua = (un_addr *)a;
68 xfree(ua);
69 }
70
71 /* --- @print@ --- */
72
73 static 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
82 static 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
91 static int srcopt(scanner *sc, addr_opts *ao)
92 {
93 un_opts *uo = (un_opts *)ao;
94 CONF_BEGIN(sc, "source", "Unix domain socket source")
95
96 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
97 CONF_ACCEPT;
98
99 CONF_END;
100 }
101
102 static 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
110 /* --- @accept@ --- */
111
112 static 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;
122 size_t sunsz = sizeof(buf);
123
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
141 static void un_freeopts(addr_opts *ao)
142 {
143 un_opts *uo = (un_opts *)ao;
144 DESTROY(uo);
145 }
146
147 /* --- @bind@ --- */
148
149 static int un_bind(addr *a, addr_opts *ao)
150 {
151 un_addr *ua = (un_addr *)a;
152 un_opts *uo = (un_opts *)ao;
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
163 fail_1:
164 close(fd);
165 fail_0:
166 return (-1);
167 }
168
169 /* --- @unbind@ --- */
170
171 static void un_unbind(addr *a)
172 {
173 un_addr *ua = (un_addr *)a;
174 unlink(ua->sun.sun_path);
175 }
176
177 /* --- @connect@ --- */
178
179 static 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));
188 fail_0:
189 return (-1);
190 }
191
192 /* --- Protocol definition --- */
193
194 addr_ops un_ops = {
195 "unix",
196 un_read, un_destroy, un_print,
197 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
198 0, 0, un_connect
199 };
200
201 /*----- That's all, folks -------------------------------------------------*/