xref: /freebsd/usr.sbin/bluetooth/btpand/packet.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*	$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Iain Hibbert
5  * All rights reserved.
6  *
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.
15  *
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.
26  */
27 
28 /* $FreeBSD$ */
29 
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
32 
33 #define L2CAP_SOCKET_CHECKED
34 #include "btpand.h"
35 
36 packet_t *
37 packet_alloc(channel_t *chan)
38 {
39 	packet_t *pkt;
40 
41 	pkt = malloc(sizeof(packet_t) + chan->mru);
42 	if (pkt == NULL) {
43 		log_err("%s() failed: %m", __func__);
44 		return NULL;
45 	}
46 
47 	memset(pkt, 0, sizeof(packet_t));
48 	STAILQ_INIT(&pkt->extlist);
49 	pkt->ptr = pkt->buf;
50 
51 	pkt->chan = chan;
52 	chan->refcnt++;
53 
54 	return pkt;
55 }
56 
57 void
58 packet_free(packet_t *pkt)
59 {
60 	exthdr_t *eh;
61 
62 	if (pkt->refcnt-- > 0)
63 		return;
64 
65 	while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) {
66 		STAILQ_REMOVE_HEAD(&pkt->extlist, next);
67 		free(eh);
68 	}
69 
70 	pkt->chan->refcnt--;
71 	if (pkt->chan->refcnt == 0)
72 		channel_free(pkt->chan);
73 
74 	free(pkt);
75 }
76 
77 void
78 packet_adj(packet_t *pkt, size_t size)
79 {
80 
81 	assert(pkt->refcnt == 0);
82 	assert(pkt->len >= size);
83 
84 	pkt->ptr += size;
85 	pkt->len -= size;
86 }
87 
88 pkthdr_t *
89 pkthdr_alloc(packet_t *pkt)
90 {
91 	pkthdr_t *ph;
92 
93 	ph = malloc(sizeof(pkthdr_t));
94 	if (ph == NULL) {
95 		log_err("%s() failed: %m", __func__);
96 		return NULL;
97 	}
98 
99 	ph->data = pkt;
100 	pkt->refcnt++;
101 
102 	return ph;
103 }
104 
105 void
106 pkthdr_free(pkthdr_t *ph)
107 {
108 
109 	packet_free(ph->data);
110 	free(ph);
111 }
112