1 /* $NetBSD: channel.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */
4 * Copyright (c) 2008 Iain Hibbert
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: channel.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
41 static struct chlist channel_list;
42 static int channel_count;
43 static int channel_tick;
45 static void channel_start(int, short, void *);
46 static void channel_read(int, short, void *);
47 static void channel_dispatch(packet_t *);
48 static void channel_watchdog(int, short, void *);
54 LIST_INIT(&channel_list);
62 chan = malloc(sizeof(channel_t));
64 log_err("%s() failed: %m", __func__);
68 memset(chan, 0, sizeof(channel_t));
69 STAILQ_INIT(&chan->pktlist);
70 chan->state = CHANNEL_CLOSED;
71 LIST_INSERT_HEAD(&channel_list, chan, next);
73 server_update(++channel_count);
79 channel_open(channel_t *chan, int fd)
83 assert(chan->refcnt == 0);
84 assert(chan->state != CHANNEL_CLOSED);
87 chan->sendbuf = malloc(chan->mtu);
88 if (chan->sendbuf == NULL) {
89 log_err("Could not malloc channel sendbuf: %m");
95 if (ioctl(fd, FIONBIO, &n) == -1) {
96 log_err("Could not set non-blocking IO: %m");
100 event_set(&chan->rd_ev, fd, EV_READ | EV_PERSIST, channel_read, chan);
101 if (event_add(&chan->rd_ev, NULL) == -1) {
102 log_err("Could not add channel read event: %m");
106 event_set(&chan->wr_ev, fd, EV_WRITE, channel_start, chan);
111 log_debug("(fd#%d)", chan->fd);
117 channel_close(channel_t *chan)
121 assert(chan->state != CHANNEL_CLOSED);
123 log_debug("(fd#%d)", chan->fd);
125 chan->state = CHANNEL_CLOSED;
126 event_del(&chan->rd_ev);
127 event_del(&chan->wr_ev);
132 while ((ph = STAILQ_FIRST(&chan->pktlist)) != NULL) {
133 STAILQ_REMOVE_HEAD(&chan->pktlist, next);
138 if (chan->pfh != NULL) {
139 pidfile_remove(chan->pfh);
143 if (chan->refcnt == 0)
148 channel_free(channel_t *chan)
151 assert(chan->refcnt == 0);
152 assert(chan->state == CHANNEL_CLOSED);
153 assert(chan->qlen == 0);
154 assert(STAILQ_EMPTY(&chan->pktlist));
156 LIST_REMOVE(chan, next);
162 server_update(--channel_count);
164 if (server_limit == 0) {
165 log_info("connection closed, exiting");
171 channel_start(int fd, short ev, void *arg)
173 channel_t *chan = arg;
176 chan->oactive = true;
178 while (chan->qlen > 0) {
179 ph = STAILQ_FIRST(&chan->pktlist);
181 channel_timeout(chan, 10);
182 if (chan->send(chan, ph->data) == false) {
183 if (event_add(&chan->wr_ev, NULL) == -1) {
184 log_err("Could not add channel write event: %m");
190 STAILQ_REMOVE_HEAD(&chan->pktlist, next);
195 channel_timeout(chan, 0);
196 chan->oactive = false;
200 channel_read(int fd, short ev, void *arg)
202 channel_t *chan = arg;
206 pkt = packet_alloc(chan);
212 nr = read(fd, pkt->buf, chan->mru);
214 log_err("channel read error: %m");
219 if (nr == 0) { /* EOF */
220 log_debug("(fd#%d) EOF", fd);
227 if (chan->recv(pkt) == true)
228 channel_dispatch(pkt);
234 channel_dispatch(packet_t *pkt)
239 * This is simple routing. I'm not sure if its allowed by
240 * the PAN or BNEP specifications, but it seems logical
241 * to send unicast packets to connected destinations where
244 if (!ETHER_IS_MULTICAST(pkt->dst)) {
245 LIST_FOREACH(chan, &channel_list, next) {
246 if (chan == pkt->chan
247 || chan->state != CHANNEL_OPEN)
250 if (memcmp(pkt->dst, chan->raddr, ETHER_ADDR_LEN) == 0) {
251 if (chan->qlen > CHANNEL_MAXQLEN)
252 log_notice("Queue overflow");
254 channel_put(chan, pkt);
261 LIST_FOREACH(chan, &channel_list, next) {
262 if (chan == pkt->chan
263 || chan->state != CHANNEL_OPEN)
266 if (chan->qlen > CHANNEL_MAXQLEN) {
267 log_notice("Queue overflow");
271 channel_put(chan, pkt);
276 channel_put(channel_t *chan, packet_t *pkt)
280 ph = pkthdr_alloc(pkt);
285 STAILQ_INSERT_TAIL(&chan->pktlist, ph, next);
288 channel_start(chan->fd, EV_WRITE, chan);
292 * Simple watchdog timer, only ticks when it is required and
293 * closes the channel down if it times out.
296 channel_timeout(channel_t *chan, int to)
298 static struct event ev;
303 chan->tick = (channel_tick + to) % 60;
305 if (channel_tick == 0) {
306 evtimer_set(&ev, channel_watchdog, &ev);
307 channel_watchdog(0, 0, &ev);
312 channel_watchdog(int fd, short ev, void *arg)
314 static struct timeval tv = { .tv_sec = 1 };
315 channel_t *chan, *next;
318 tick = (channel_tick % 60) + 1;
321 next = LIST_FIRST(&channel_list);
322 while ((chan = next) != NULL) {
323 next = LIST_NEXT(chan, next);
325 if (chan->tick == tick)
327 else if (chan->tick != 0)
331 if (channel_tick != 0 && evtimer_add(arg, &tv) < 0) {
332 log_err("Could not add watchdog event: %m");