packet.c revision 1.2 1 1.2 plunky /* $NetBSD: packet.c,v 1.2 2009/05/02 20:07:51 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.1 plunky * Copyright (c) 2008 Iain Hibbert
5 1.1 plunky * All rights reserved.
6 1.1 plunky *
7 1.1 plunky * Redistribution and use in source and binary forms, with or without
8 1.1 plunky * modification, are permitted provided that the following conditions
9 1.1 plunky * are met:
10 1.1 plunky * 1. Redistributions of source code must retain the above copyright
11 1.1 plunky * notice, this list of conditions and the following disclaimer.
12 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 plunky * notice, this list of conditions and the following disclaimer in the
14 1.1 plunky * documentation and/or other materials provided with the distribution.
15 1.1 plunky *
16 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 plunky */
27 1.1 plunky
28 1.1 plunky #include <sys/cdefs.h>
29 1.2 plunky __RCSID("$NetBSD: packet.c,v 1.2 2009/05/02 20:07:51 plunky Exp $");
30 1.1 plunky
31 1.1 plunky #include "btpand.h"
32 1.1 plunky
33 1.1 plunky packet_t *
34 1.1 plunky packet_alloc(channel_t *chan)
35 1.1 plunky {
36 1.1 plunky packet_t *pkt;
37 1.1 plunky
38 1.1 plunky pkt = malloc(sizeof(packet_t) + chan->mru);
39 1.1 plunky if (pkt == NULL) {
40 1.1 plunky log_err("%s() failed: %m", __func__);
41 1.1 plunky return NULL;
42 1.1 plunky }
43 1.1 plunky
44 1.1 plunky memset(pkt, 0, sizeof(packet_t));
45 1.1 plunky STAILQ_INIT(&pkt->extlist);
46 1.1 plunky pkt->ptr = pkt->buf;
47 1.1 plunky
48 1.1 plunky pkt->chan = chan;
49 1.1 plunky chan->refcnt++;
50 1.1 plunky
51 1.1 plunky return pkt;
52 1.1 plunky }
53 1.1 plunky
54 1.1 plunky void
55 1.1 plunky packet_free(packet_t *pkt)
56 1.1 plunky {
57 1.1 plunky exthdr_t *eh;
58 1.1 plunky
59 1.1 plunky if (pkt->refcnt-- > 0)
60 1.1 plunky return;
61 1.1 plunky
62 1.1 plunky while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) {
63 1.1 plunky STAILQ_REMOVE_HEAD(&pkt->extlist, next);
64 1.1 plunky free(eh);
65 1.1 plunky }
66 1.1 plunky
67 1.1 plunky pkt->chan->refcnt--;
68 1.1 plunky if (pkt->chan->refcnt == 0)
69 1.1 plunky channel_free(pkt->chan);
70 1.1 plunky
71 1.1 plunky free(pkt);
72 1.1 plunky }
73 1.1 plunky
74 1.1 plunky void
75 1.1 plunky packet_adj(packet_t *pkt, size_t size)
76 1.1 plunky {
77 1.1 plunky
78 1.2 plunky assert(pkt->refcnt == 0);
79 1.2 plunky assert(pkt->len >= size);
80 1.1 plunky
81 1.1 plunky pkt->ptr += size;
82 1.1 plunky pkt->len -= size;
83 1.1 plunky }
84 1.1 plunky
85 1.1 plunky pkthdr_t *
86 1.1 plunky pkthdr_alloc(packet_t *pkt)
87 1.1 plunky {
88 1.1 plunky pkthdr_t *ph;
89 1.1 plunky
90 1.1 plunky ph = malloc(sizeof(pkthdr_t));
91 1.1 plunky if (ph == NULL) {
92 1.1 plunky log_err("%s() failed: %m", __func__);
93 1.1 plunky return NULL;
94 1.1 plunky }
95 1.1 plunky
96 1.1 plunky ph->data = pkt;
97 1.1 plunky pkt->refcnt++;
98 1.1 plunky
99 1.1 plunky return ph;
100 1.1 plunky }
101 1.1 plunky
102 1.1 plunky void
103 1.1 plunky pkthdr_free(pkthdr_t *ph)
104 1.1 plunky {
105 1.1 plunky
106 1.1 plunky packet_free(ph->data);
107 1.1 plunky free(ph);
108 1.1 plunky }
109