Fix whitespace throughout.
[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 *
10 * This file is part of the `fw' port forwarder.
11 *
12 * `fw' 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.
206212ca 16 *
aa1f699e 17 * `fw' 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.
206212ca 21 *
aa1f699e 22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
aa1f699e 27/*----- Header files ------------------------------------------------------*/
28
29#include "config.h"
e0ce9d38 30#undef sun /* Cretins */
aa1f699e 31
aa1f699e 32#include <errno.h>
33#include <limits.h>
34#include <stddef.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <sys/types.h>
40#include <unistd.h>
41
42#include <sys/socket.h>
43#include <sys/un.h>
44#include <arpa/inet.h>
45#include <netdb.h>
46
47#include <mLib/alloc.h>
48#include <mLib/dstr.h>
49#include <mLib/report.h>
50#include <mLib/sub.h>
51
52#include "addr.h"
53#include "conf.h"
54#include "fattr.h"
55#include "fw.h"
56#include "reffd.h"
57#include "scan.h"
0ac54f22 58#include "socket.h"
aa1f699e 59#include "un.h"
60
61/*----- Data structures ---------------------------------------------------*/
62
63typedef struct un_addr {
64 addr a;
65 struct sockaddr_un sun;
66} un_addr;
67
68typedef struct un_opts {
69 addr_opts ao;
70 fattr f;
71} un_opts;
72
73/*----- Protocol operations -----------------------------------------------*/
74
75/* --- @read@ --- */
76
77static addr *un_read(scanner *sc, unsigned type)
78{
79 dstr d = DSTR_INIT;
80 un_addr *ua;
81
82 conf_name(sc, '/', &d);
83 ua = xmalloc(sizeof(addr) +
84 offsetof(struct sockaddr_un, sun_path) +
85 d.len + 1);
86 ua->a.ops = &un_ops;
87 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
e0ce9d38 88 memset(&ua->sun, 0, ua->a.sz);
aa1f699e 89 ua->sun.sun_family = AF_UNIX;
90 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
91 dstr_destroy(&d);
92 return (&ua->a);
93}
94
95/* --- @destroy@ --- */
96
97static void un_destroy(addr *a)
98{
99 un_addr *ua = (un_addr *)a;
b0805b27 100 xfree(ua);
aa1f699e 101}
102
103/* --- @print@ --- */
104
105static void un_print(addr *a, unsigned type, dstr *d)
106{
107 un_addr *ua = (un_addr *)a;
108 dstr_puts(d, "unix:");
109 dstr_puts(d, ua->sun.sun_path);
110}
111
112/* --- @initopts@ --- */
113
114static addr_opts *un_initopts(void)
115{
116 un_opts *uo = CREATE(un_opts);
117 uo->f = fattr_global;
118 return (&uo->ao);
119}
120
121/* --- @option@ --- */
122
0ac54f22 123static int srcopt(scanner *sc, addr_opts *ao)
aa1f699e 124{
125 un_opts *uo = (un_opts *)ao;
ad7fbf45 126 CONF_BEGIN(sc, "source", "Unix domain socket source")
aa1f699e 127
128 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
129 CONF_ACCEPT;
130
131 CONF_END;
132}
133
0ac54f22 134static int un_option(scanner *sc, addr_opts *ao, unsigned type)
135{
136 CONF_BEGIN(sc, "unix", "Unix domain socket");
137 if (type != ADDR_DEST && srcopt(sc, ao))
138 CONF_ACCEPT;
139 CONF_END;
140}
141
aa1f699e 142/* --- @accept@ --- */
143
144static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
145{
146 int nfd;
147 un_opts *uo = (un_opts *)ao;
148
149 /* --- Accept the new connection --- */
150
151 {
152 char buf[PATH_MAX + sizeof(struct sockaddr)];
153 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
bc241e98 154 size_t sunsz = sizeof(buf);
206212ca 155
aa1f699e 156 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
157 return (0);
158 }
159
160 /* --- Log the connection --- *
161 *
162 * It'd be really nice if I could find out who the user is, but I can't in
163 * anything like a portable way.
164 */
165
166 if (!(uo->ao.f & ADDRF_NOLOG))
167 fw_log(-1, "[%s] accepted", desc);
168 return (reffd_init(nfd));
169}
170
171/* --- @freeopts@ --- */
172
173static void un_freeopts(addr_opts *ao)
174{
175 un_opts *uo = (un_opts *)ao;
176 DESTROY(uo);
177}
178
0ac54f22 179/* --- @bind@ --- */
aa1f699e 180
0ac54f22 181static int un_bind(addr *a, addr_opts *ao)
aa1f699e 182{
183 un_addr *ua = (un_addr *)a;
184 un_opts *uo = (un_opts *)ao;
0ac54f22 185 int fd;
186
187 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
188 goto fail_0;
189 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
190 goto fail_1;
191 if (fattr_apply(ua->sun.sun_path, &uo->f))
192 goto fail_1;
193 return (fd);
194
195fail_1:
196 close(fd);
197fail_0:
198 return (-1);
aa1f699e 199}
200
201/* --- @unbind@ --- */
202
203static void un_unbind(addr *a)
204{
205 un_addr *ua = (un_addr *)a;
206 unlink(ua->sun.sun_path);
207}
208
0ac54f22 209/* --- @connect@ --- */
210
211static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
212{
213 un_addr *ua = (un_addr *)a;
214 int fd;
215
216 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
217 goto fail_0;
218 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
219 starget_connected, e));
220fail_0:
221 return (-1);
222}
223
aa1f699e 224/* --- Protocol definition --- */
225
226addr_ops un_ops = {
0ac54f22 227 "unix",
aa1f699e 228 un_read, un_destroy, un_print,
ee599f55 229 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
0ac54f22 230 0, 0, un_connect
aa1f699e 231};
232
233/*----- That's all, folks -------------------------------------------------*/