packet.c revision 281210
1187938Semax/*	$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
2187938Semax
3187938Semax/*-
4187938Semax * Copyright (c) 2008 Iain Hibbert
5187938Semax * All rights reserved.
6187938Semax *
7187938Semax * Redistribution and use in source and binary forms, with or without
8187938Semax * modification, are permitted provided that the following conditions
9187938Semax * are met:
10187938Semax * 1. Redistributions of source code must retain the above copyright
11187938Semax *    notice, this list of conditions and the following disclaimer.
12187938Semax * 2. Redistributions in binary form must reproduce the above copyright
13187938Semax *    notice, this list of conditions and the following disclaimer in the
14187938Semax *    documentation and/or other materials provided with the distribution.
15187938Semax *
16187938Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17187938Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18187938Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19187938Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20187938Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21187938Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22187938Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23187938Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24187938Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25187938Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26187938Semax */
27187938Semax
28187938Semax/* $FreeBSD: head/usr.sbin/bluetooth/btpand/packet.c 281210 2015-04-07 16:48:23Z takawata $ */
29187938Semax
30187938Semax#include <sys/cdefs.h>
31187938Semax__RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
32187938Semax
33281210Stakawata#define L2CAP_SOCKET_CHECKED
34187938Semax#include "btpand.h"
35187938Semax
36187938Semaxpacket_t *
37187938Semaxpacket_alloc(channel_t *chan)
38187938Semax{
39187938Semax	packet_t *pkt;
40187938Semax
41187938Semax	pkt = malloc(sizeof(packet_t) + chan->mru);
42187938Semax	if (pkt == NULL) {
43187938Semax		log_err("%s() failed: %m", __func__);
44187938Semax		return NULL;
45187938Semax	}
46187938Semax
47187938Semax	memset(pkt, 0, sizeof(packet_t));
48187938Semax	STAILQ_INIT(&pkt->extlist);
49187938Semax	pkt->ptr = pkt->buf;
50187938Semax
51187938Semax	pkt->chan = chan;
52187938Semax	chan->refcnt++;
53187938Semax
54187938Semax	return pkt;
55187938Semax}
56187938Semax
57187938Semaxvoid
58187938Semaxpacket_free(packet_t *pkt)
59187938Semax{
60187938Semax	exthdr_t *eh;
61187938Semax
62187938Semax	if (pkt->refcnt-- > 0)
63187938Semax		return;
64187938Semax
65187938Semax	while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) {
66187938Semax		STAILQ_REMOVE_HEAD(&pkt->extlist, next);
67187938Semax		free(eh);
68187938Semax	}
69187938Semax
70187938Semax	pkt->chan->refcnt--;
71187938Semax	if (pkt->chan->refcnt == 0)
72187938Semax		channel_free(pkt->chan);
73187938Semax
74187938Semax	free(pkt);
75187938Semax}
76187938Semax
77187938Semaxvoid
78187938Semaxpacket_adj(packet_t *pkt, size_t size)
79187938Semax{
80187938Semax
81187938Semax	assert(pkt->refcnt == 0);
82187938Semax	assert(pkt->len >= size);
83187938Semax
84187938Semax	pkt->ptr += size;
85187938Semax	pkt->len -= size;
86187938Semax}
87187938Semax
88187938Semaxpkthdr_t *
89187938Semaxpkthdr_alloc(packet_t *pkt)
90187938Semax{
91187938Semax	pkthdr_t *ph;
92187938Semax
93187938Semax	ph = malloc(sizeof(pkthdr_t));
94187938Semax	if (ph == NULL) {
95187938Semax		log_err("%s() failed: %m", __func__);
96187938Semax		return NULL;
97187938Semax	}
98187938Semax
99187938Semax	ph->data = pkt;
100187938Semax	pkt->refcnt++;
101187938Semax
102187938Semax	return ph;
103187938Semax}
104187938Semax
105187938Semaxvoid
106187938Semaxpkthdr_free(pkthdr_t *ph)
107187938Semax{
108187938Semax
109187938Semax	packet_free(ph->data);
110187938Semax	free(ph);
111187938Semax}
112