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