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