3 * Protocol specific definitions for Unix-domain sockets
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
30 #undef sun /* Cretins */
39 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <arpa/inet.h>
47 #include <mLib/alloc.h>
48 #include <mLib/dstr.h>
49 #include <mLib/report.h>
61 /*----- Data structures ---------------------------------------------------*/
63 typedef struct un_addr {
65 struct sockaddr_un sun;
68 typedef struct un_opts {
73 /*----- Protocol operations -----------------------------------------------*/
77 static addr *un_read(scanner *sc, unsigned type)
82 conf_name(sc, '/', &d);
83 ua = xmalloc(sizeof(addr) +
84 offsetof(struct sockaddr_un, sun_path) +
87 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
88 memset(&ua->sun, 0, ua->a.sz);
89 ua->sun.sun_family = AF_UNIX;
90 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
95 /* --- @destroy@ --- */
97 static void un_destroy(addr *a)
99 un_addr *ua = (un_addr *)a;
103 /* --- @print@ --- */
105 static void un_print(addr *a, unsigned type, dstr *d)
107 un_addr *ua = (un_addr *)a;
108 dstr_puts(d, "unix:");
109 dstr_puts(d, ua->sun.sun_path);
112 /* --- @initopts@ --- */
114 static addr_opts *un_initopts(void)
116 un_opts *uo = CREATE(un_opts);
117 uo->f = fattr_global;
121 /* --- @option@ --- */
123 static int srcopt(scanner *sc, addr_opts *ao)
125 un_opts *uo = (un_opts *)ao;
126 CONF_BEGIN(sc, "source", "Unix domain socket source")
128 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
134 static int un_option(scanner *sc, addr_opts *ao, unsigned type)
136 CONF_BEGIN(sc, "unix", "Unix domain socket");
137 if (type != ADDR_DEST && srcopt(sc, ao))
142 /* --- @accept@ --- */
144 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
147 un_opts *uo = (un_opts *)ao;
149 /* --- Accept the new connection --- */
152 char buf[PATH_MAX + sizeof(struct sockaddr)];
153 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
154 size_t sunsz = sizeof(buf);
156 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
160 /* --- Log the connection --- *
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.
166 if (!(uo->ao.f & ADDRF_NOLOG))
167 fw_log(-1, "[%s] accepted", desc);
168 return (reffd_init(nfd));
171 /* --- @freeopts@ --- */
173 static void un_freeopts(addr_opts *ao)
175 un_opts *uo = (un_opts *)ao;
181 static int un_bind(addr *a, addr_opts *ao)
183 un_addr *ua = (un_addr *)a;
184 un_opts *uo = (un_opts *)ao;
187 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
189 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
191 if (fattr_apply(ua->sun.sun_path, &uo->f))
201 /* --- @unbind@ --- */
203 static void un_unbind(addr *a)
205 un_addr *ua = (un_addr *)a;
206 unlink(ua->sun.sun_path);
209 /* --- @connect@ --- */
211 static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
213 un_addr *ua = (un_addr *)a;
216 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
218 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
219 starget_connected, e));
224 /* --- Protocol definition --- */
228 un_read, un_destroy, un_print,
229 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
233 /*----- That's all, folks -------------------------------------------------*/