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