No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / btpand / channel.c
blobd9352e12c3cf8116ff1c1512ee27934bbc5b8320
1 /* $NetBSD: channel.c,v 1.2 2009/05/02 20:07:51 plunky Exp $ */
3 /*-
4 * Copyright (c) 2008 Iain Hibbert
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: channel.c,v 1.2 2009/05/02 20:07:51 plunky Exp $");
31 #include <sys/ioctl.h>
33 #include <unistd.h>
35 #include "btpand.h"
37 static struct chlist channel_list;
38 static int channel_tick;
40 static void channel_start(int, short, void *);
41 static void channel_read(int, short, void *);
42 static void channel_dispatch(packet_t *);
43 static void channel_watchdog(int, short, void *);
45 void
46 channel_init(void)
49 LIST_INIT(&channel_list);
52 channel_t *
53 channel_alloc(void)
55 channel_t *chan;
57 chan = malloc(sizeof(channel_t));
58 if (chan == NULL) {
59 log_err("%s() failed: %m", __func__);
60 return NULL;
63 memset(chan, 0, sizeof(channel_t));
64 STAILQ_INIT(&chan->pktlist);
65 chan->state = CHANNEL_CLOSED;
66 LIST_INSERT_HEAD(&channel_list, chan, next);
68 return chan;
71 bool
72 channel_open(channel_t *chan, int fd)
74 int n;
76 assert(chan->refcnt == 0);
77 assert(chan->state != CHANNEL_CLOSED);
78 assert(chan->send != NULL);
79 assert(chan->recv != NULL);
80 assert(chan->down != NULL);
82 if (chan->mtu > 0) {
83 chan->sendbuf = malloc(chan->mtu);
84 if (chan->sendbuf == NULL) {
85 log_err("Could not malloc channel sendbuf: %m");
86 return false;
90 n = 1;
91 if (ioctl(fd, FIONBIO, &n) == -1) {
92 log_err("Could not set non-blocking IO: %m");
93 return false;
96 event_set(&chan->rd_ev, fd, EV_READ | EV_PERSIST, channel_read, chan);
97 if (event_add(&chan->rd_ev, NULL) == -1) {
98 log_err("Could not add channel read event: %m");
99 return false;
102 event_set(&chan->wr_ev, fd, EV_WRITE, channel_start, chan);
104 chan->refcnt++;
105 chan->fd = fd;
107 log_debug("(fd#%d)", chan->fd);
109 return true;
112 void
113 channel_close(channel_t *chan)
115 pkthdr_t *ph;
117 assert(chan->state != CHANNEL_CLOSED);
119 log_debug("(fd#%d)", chan->fd);
121 chan->state = CHANNEL_CLOSED;
122 event_del(&chan->rd_ev);
123 event_del(&chan->wr_ev);
124 close(chan->fd);
125 chan->refcnt--;
126 chan->tick = 0;
128 while ((ph = STAILQ_FIRST(&chan->pktlist)) != NULL) {
129 STAILQ_REMOVE_HEAD(&chan->pktlist, next);
130 pkthdr_free(ph);
131 chan->qlen--;
134 if (chan->refcnt == 0)
135 channel_free(chan);
138 void
139 channel_free(channel_t *chan)
142 assert(chan->refcnt == 0);
143 assert(chan->state == CHANNEL_CLOSED);
144 assert(chan->qlen == 0);
145 assert(STAILQ_EMPTY(&chan->pktlist));
147 LIST_REMOVE(chan, next);
148 free(chan->pfilter);
149 free(chan->mfilter);
150 free(chan->sendbuf);
151 free(chan);
154 static void
155 channel_start(int fd, short ev, void *arg)
157 channel_t *chan = arg;
158 pkthdr_t *ph;
160 chan->oactive = true;
162 while (chan->qlen > 0) {
163 ph = STAILQ_FIRST(&chan->pktlist);
165 channel_timeout(chan, 10);
166 if (chan->send(chan, ph->data) == false) {
167 if (event_add(&chan->wr_ev, NULL) == -1) {
168 log_err("Could not add channel write event: %m");
169 chan->down(chan);
171 return;
174 STAILQ_REMOVE_HEAD(&chan->pktlist, next);
175 pkthdr_free(ph);
176 chan->qlen--;
179 channel_timeout(chan, 0);
180 chan->oactive = false;
183 static void
184 channel_read(int fd, short ev, void *arg)
186 channel_t *chan = arg;
187 packet_t *pkt;
188 ssize_t nr;
190 pkt = packet_alloc(chan);
191 if (pkt == NULL) {
192 chan->down(chan);
193 return;
196 nr = read(fd, pkt->buf, chan->mru);
197 if (nr == -1) {
198 log_err("channel read error: %m");
199 packet_free(pkt);
200 chan->down(chan);
201 return;
203 if (nr == 0) { /* EOF */
204 log_debug("(fd#%d) EOF", fd);
205 packet_free(pkt);
206 chan->down(chan);
207 return;
209 pkt->len = nr;
211 if (chan->recv(pkt) == true)
212 channel_dispatch(pkt);
214 packet_free(pkt);
217 static void
218 channel_dispatch(packet_t *pkt)
220 channel_t *chan;
223 * This is simple routing. I'm not sure if its allowed by
224 * the PAN or BNEP specifications, but it seems logical
225 * to send unicast packets to connected destinations where
226 * possible.
228 if (!ETHER_IS_MULTICAST(pkt->dst)) {
229 LIST_FOREACH(chan, &channel_list, next) {
230 if (chan == pkt->chan
231 || chan->state != CHANNEL_OPEN)
232 continue;
234 if (memcmp(pkt->dst, chan->raddr, ETHER_ADDR_LEN) == 0) {
235 if (chan->qlen > CHANNEL_MAXQLEN)
236 log_notice("Queue overflow");
237 else
238 channel_put(chan, pkt);
240 return;
245 LIST_FOREACH(chan, &channel_list, next) {
246 if (chan == pkt->chan
247 || chan->state != CHANNEL_OPEN)
248 continue;
250 if (chan->qlen > CHANNEL_MAXQLEN) {
251 log_notice("Queue overflow");
252 continue;
255 channel_put(chan, pkt);
259 void
260 channel_put(channel_t *chan, packet_t *pkt)
262 pkthdr_t *ph;
264 ph = pkthdr_alloc(pkt);
265 if (ph == NULL)
266 return;
268 chan->qlen++;
269 STAILQ_INSERT_TAIL(&chan->pktlist, ph, next);
271 if (!chan->oactive)
272 channel_start(chan->fd, EV_WRITE, chan);
276 * Simple watchdog timer, only ticks when it is required and
277 * closes the channel down if it times out.
279 void
280 channel_timeout(channel_t *chan, int to)
282 static struct event ev;
284 if (to == 0)
285 chan->tick = 0;
286 else
287 chan->tick = (channel_tick + to) % 60;
289 if (channel_tick == 0) {
290 evtimer_set(&ev, channel_watchdog, &ev);
291 channel_watchdog(0, 0, &ev);
295 static void
296 channel_watchdog(int fd, short ev, void *arg)
298 static struct timeval tv = { .tv_sec = 1 };
299 channel_t *chan, *next;
300 int tick;
302 tick = (channel_tick % 60) + 1;
303 channel_tick = 0;
305 next = LIST_FIRST(&channel_list);
306 while ((chan = next) != NULL) {
307 next = LIST_NEXT(chan, next);
309 if (chan->tick == tick)
310 chan->down(chan);
311 else if (chan->tick != 0)
312 channel_tick = tick;
315 if (channel_tick != 0 && evtimer_add(arg, &tv) < 0) {
316 log_err("Could not add watchdog event: %m");
317 exit(EXIT_FAILURE);