Update docco for new options.
[fwd] / un.c
1 /* -*-c-*-
2 *
3 * $Id: un.c,v 1.6 2003/11/25 14:08:23 mdw Exp $
4 *
5 * Protocol specific definitions for Unix-domain sockets
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
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.
18 *
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.
23 *
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.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: un.c,v $
32 * Revision 1.6 2003/11/25 14:08:23 mdw
33 * Debianization. Socket target options. Internet binding.
34 *
35 * Revision 1.5 2002/02/22 23:43:32 mdw
36 * Call @xfree@ rather than @free@.
37 *
38 * Revision 1.4 2000/08/01 17:59:56 mdw
39 * Switch over to using `size_t' for socket address lengths.
40 *
41 * Revision 1.3 2000/08/01 17:58:32 mdw
42 * Remove unnecessary <ctype.h> header.
43 *
44 * Revision 1.2 1999/07/27 18:30:53 mdw
45 * Various minor portability fixes.
46 *
47 * Revision 1.1 1999/07/26 23:34:11 mdw
48 * New socket address types.
49 *
50 */
51
52 /*----- Header files ------------------------------------------------------*/
53
54 #include "config.h"
55 #undef sun /* Cretins */
56
57 #include <errno.h>
58 #include <limits.h>
59 #include <stddef.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63
64 #include <sys/types.h>
65 #include <unistd.h>
66
67 #include <sys/socket.h>
68 #include <sys/un.h>
69 #include <arpa/inet.h>
70 #include <netdb.h>
71
72 #include <mLib/alloc.h>
73 #include <mLib/dstr.h>
74 #include <mLib/report.h>
75 #include <mLib/sub.h>
76
77 #include "addr.h"
78 #include "conf.h"
79 #include "fattr.h"
80 #include "fw.h"
81 #include "reffd.h"
82 #include "scan.h"
83 #include "socket.h"
84 #include "un.h"
85
86 /*----- Data structures ---------------------------------------------------*/
87
88 typedef struct un_addr {
89 addr a;
90 struct sockaddr_un sun;
91 } un_addr;
92
93 typedef struct un_opts {
94 addr_opts ao;
95 fattr f;
96 } un_opts;
97
98 /*----- Protocol operations -----------------------------------------------*/
99
100 /* --- @read@ --- */
101
102 static addr *un_read(scanner *sc, unsigned type)
103 {
104 dstr d = DSTR_INIT;
105 un_addr *ua;
106
107 conf_name(sc, '/', &d);
108 ua = xmalloc(sizeof(addr) +
109 offsetof(struct sockaddr_un, sun_path) +
110 d.len + 1);
111 ua->a.ops = &un_ops;
112 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
113 memset(&ua->sun, 0, ua->a.sz);
114 ua->sun.sun_family = AF_UNIX;
115 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
116 dstr_destroy(&d);
117 return (&ua->a);
118 }
119
120 /* --- @destroy@ --- */
121
122 static void un_destroy(addr *a)
123 {
124 un_addr *ua = (un_addr *)a;
125 xfree(ua);
126 }
127
128 /* --- @print@ --- */
129
130 static void un_print(addr *a, unsigned type, dstr *d)
131 {
132 un_addr *ua = (un_addr *)a;
133 dstr_puts(d, "unix:");
134 dstr_puts(d, ua->sun.sun_path);
135 }
136
137 /* --- @initopts@ --- */
138
139 static addr_opts *un_initopts(void)
140 {
141 un_opts *uo = CREATE(un_opts);
142 uo->f = fattr_global;
143 return (&uo->ao);
144 }
145
146 /* --- @option@ --- */
147
148 static int srcopt(scanner *sc, addr_opts *ao)
149 {
150 un_opts *uo = (un_opts *)ao;
151 CONF_BEGIN(sc, "unix", "Unix domain socket")
152
153 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
154 CONF_ACCEPT;
155
156 CONF_END;
157 }
158
159 static int un_option(scanner *sc, addr_opts *ao, unsigned type)
160 {
161 CONF_BEGIN(sc, "unix", "Unix domain socket");
162 if (type != ADDR_DEST && srcopt(sc, ao))
163 CONF_ACCEPT;
164 CONF_END;
165 }
166
167 /* --- @accept@ --- */
168
169 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
170 {
171 int nfd;
172 un_opts *uo = (un_opts *)ao;
173
174 /* --- Accept the new connection --- */
175
176 {
177 char buf[PATH_MAX + sizeof(struct sockaddr)];
178 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
179 size_t sunsz = sizeof(buf);
180
181 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
182 return (0);
183 }
184
185 /* --- Log the connection --- *
186 *
187 * It'd be really nice if I could find out who the user is, but I can't in
188 * anything like a portable way.
189 */
190
191 if (!(uo->ao.f & ADDRF_NOLOG))
192 fw_log(-1, "[%s] accepted", desc);
193 return (reffd_init(nfd));
194 }
195
196 /* --- @freeopts@ --- */
197
198 static void un_freeopts(addr_opts *ao)
199 {
200 un_opts *uo = (un_opts *)ao;
201 DESTROY(uo);
202 }
203
204 /* --- @bind@ --- */
205
206 static int un_bind(addr *a, addr_opts *ao)
207 {
208 un_addr *ua = (un_addr *)a;
209 un_opts *uo = (un_opts *)ao;
210 int fd;
211
212 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
213 goto fail_0;
214 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
215 goto fail_1;
216 if (fattr_apply(ua->sun.sun_path, &uo->f))
217 goto fail_1;
218 return (fd);
219
220 fail_1:
221 close(fd);
222 fail_0:
223 return (-1);
224 }
225
226 /* --- @unbind@ --- */
227
228 static void un_unbind(addr *a)
229 {
230 un_addr *ua = (un_addr *)a;
231 unlink(ua->sun.sun_path);
232 }
233
234 /* --- @connect@ --- */
235
236 static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
237 {
238 un_addr *ua = (un_addr *)a;
239 int fd;
240
241 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
242 goto fail_0;
243 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
244 starget_connected, e));
245 fail_0:
246 return (-1);
247 }
248
249 /* --- Protocol definition --- */
250
251 addr_ops un_ops = {
252 "unix",
253 un_read, un_destroy, un_print,
254 un_initopts, un_option, un_freeopts, un_bind, un_unbind, un_accept,
255 0, 0, un_connect
256 };
257
258 /*----- That's all, folks -------------------------------------------------*/