Path: blob/main/usr.sbin/bluetooth/btpand/packet.c
105240 views
/* $NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */12/*-3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2008 Iain Hibbert6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.20* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,21* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT22* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF26* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/282930#include <sys/cdefs.h>31__RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");3233#define L2CAP_SOCKET_CHECKED34#include "btpand.h"3536packet_t *37packet_alloc(channel_t *chan)38{39packet_t *pkt;4041pkt = malloc(sizeof(packet_t) + chan->mru);42if (pkt == NULL) {43log_err("%s() failed: %m", __func__);44return NULL;45}4647memset(pkt, 0, sizeof(packet_t));48STAILQ_INIT(&pkt->extlist);49pkt->ptr = pkt->buf;5051pkt->chan = chan;52chan->refcnt++;5354return pkt;55}5657void58packet_free(packet_t *pkt)59{60exthdr_t *eh;6162if (pkt->refcnt-- > 0)63return;6465while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) {66STAILQ_REMOVE_HEAD(&pkt->extlist, next);67free(eh);68}6970pkt->chan->refcnt--;71if (pkt->chan->refcnt == 0)72channel_free(pkt->chan);7374free(pkt);75}7677void78packet_adj(packet_t *pkt, size_t size)79{8081assert(pkt->refcnt == 0);82assert(pkt->len >= size);8384pkt->ptr += size;85pkt->len -= size;86}8788pkthdr_t *89pkthdr_alloc(packet_t *pkt)90{91pkthdr_t *ph;9293ph = malloc(sizeof(pkthdr_t));94if (ph == NULL) {95log_err("%s() failed: %m", __func__);96return NULL;97}9899ph->data = pkt;100pkt->refcnt++;101102return ph;103}104105void106pkthdr_free(pkthdr_t *ph)107{108109packet_free(ph->data);110free(ph);111}112113114