17718ced0SMaksim Yevmenkin /* $NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */ 27718ced0SMaksim Yevmenkin 37718ced0SMaksim Yevmenkin /*- 47718ced0SMaksim Yevmenkin * Copyright (c) 2008 Iain Hibbert 57718ced0SMaksim Yevmenkin * All rights reserved. 67718ced0SMaksim Yevmenkin * 77718ced0SMaksim Yevmenkin * Redistribution and use in source and binary forms, with or without 87718ced0SMaksim Yevmenkin * modification, are permitted provided that the following conditions 97718ced0SMaksim Yevmenkin * are met: 107718ced0SMaksim Yevmenkin * 1. Redistributions of source code must retain the above copyright 117718ced0SMaksim Yevmenkin * notice, this list of conditions and the following disclaimer. 127718ced0SMaksim Yevmenkin * 2. Redistributions in binary form must reproduce the above copyright 137718ced0SMaksim Yevmenkin * notice, this list of conditions and the following disclaimer in the 147718ced0SMaksim Yevmenkin * documentation and/or other materials provided with the distribution. 157718ced0SMaksim Yevmenkin * 167718ced0SMaksim Yevmenkin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 177718ced0SMaksim Yevmenkin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 187718ced0SMaksim Yevmenkin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 197718ced0SMaksim Yevmenkin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 207718ced0SMaksim Yevmenkin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 217718ced0SMaksim Yevmenkin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 227718ced0SMaksim Yevmenkin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 237718ced0SMaksim Yevmenkin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 247718ced0SMaksim Yevmenkin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 257718ced0SMaksim Yevmenkin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 267718ced0SMaksim Yevmenkin */ 277718ced0SMaksim Yevmenkin 287718ced0SMaksim Yevmenkin /* $FreeBSD$ */ 297718ced0SMaksim Yevmenkin 307718ced0SMaksim Yevmenkin #include <sys/cdefs.h> 317718ced0SMaksim Yevmenkin __RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $"); 327718ced0SMaksim Yevmenkin 337718ced0SMaksim Yevmenkin #include "btpand.h" 347718ced0SMaksim Yevmenkin 357718ced0SMaksim Yevmenkin packet_t * 367718ced0SMaksim Yevmenkin packet_alloc(channel_t *chan) 377718ced0SMaksim Yevmenkin { 387718ced0SMaksim Yevmenkin packet_t *pkt; 397718ced0SMaksim Yevmenkin 407718ced0SMaksim Yevmenkin pkt = malloc(sizeof(packet_t) + chan->mru); 417718ced0SMaksim Yevmenkin if (pkt == NULL) { 427718ced0SMaksim Yevmenkin log_err("%s() failed: %m", __func__); 437718ced0SMaksim Yevmenkin return NULL; 447718ced0SMaksim Yevmenkin } 457718ced0SMaksim Yevmenkin 467718ced0SMaksim Yevmenkin memset(pkt, 0, sizeof(packet_t)); 477718ced0SMaksim Yevmenkin STAILQ_INIT(&pkt->extlist); 487718ced0SMaksim Yevmenkin pkt->ptr = pkt->buf; 497718ced0SMaksim Yevmenkin 507718ced0SMaksim Yevmenkin pkt->chan = chan; 517718ced0SMaksim Yevmenkin chan->refcnt++; 527718ced0SMaksim Yevmenkin 537718ced0SMaksim Yevmenkin return pkt; 547718ced0SMaksim Yevmenkin } 557718ced0SMaksim Yevmenkin 567718ced0SMaksim Yevmenkin void 577718ced0SMaksim Yevmenkin packet_free(packet_t *pkt) 587718ced0SMaksim Yevmenkin { 597718ced0SMaksim Yevmenkin exthdr_t *eh; 607718ced0SMaksim Yevmenkin 617718ced0SMaksim Yevmenkin if (pkt->refcnt-- > 0) 627718ced0SMaksim Yevmenkin return; 637718ced0SMaksim Yevmenkin 647718ced0SMaksim Yevmenkin while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) { 657718ced0SMaksim Yevmenkin STAILQ_REMOVE_HEAD(&pkt->extlist, next); 667718ced0SMaksim Yevmenkin free(eh); 677718ced0SMaksim Yevmenkin } 687718ced0SMaksim Yevmenkin 697718ced0SMaksim Yevmenkin pkt->chan->refcnt--; 707718ced0SMaksim Yevmenkin if (pkt->chan->refcnt == 0) 717718ced0SMaksim Yevmenkin channel_free(pkt->chan); 727718ced0SMaksim Yevmenkin 737718ced0SMaksim Yevmenkin free(pkt); 747718ced0SMaksim Yevmenkin } 757718ced0SMaksim Yevmenkin 767718ced0SMaksim Yevmenkin void 777718ced0SMaksim Yevmenkin packet_adj(packet_t *pkt, size_t size) 787718ced0SMaksim Yevmenkin { 797718ced0SMaksim Yevmenkin 807718ced0SMaksim Yevmenkin assert(pkt->refcnt == 0); 817718ced0SMaksim Yevmenkin assert(pkt->len >= size); 827718ced0SMaksim Yevmenkin 837718ced0SMaksim Yevmenkin pkt->ptr += size; 847718ced0SMaksim Yevmenkin pkt->len -= size; 857718ced0SMaksim Yevmenkin } 867718ced0SMaksim Yevmenkin 877718ced0SMaksim Yevmenkin pkthdr_t * 887718ced0SMaksim Yevmenkin pkthdr_alloc(packet_t *pkt) 897718ced0SMaksim Yevmenkin { 907718ced0SMaksim Yevmenkin pkthdr_t *ph; 917718ced0SMaksim Yevmenkin 927718ced0SMaksim Yevmenkin ph = malloc(sizeof(pkthdr_t)); 937718ced0SMaksim Yevmenkin if (ph == NULL) { 947718ced0SMaksim Yevmenkin log_err("%s() failed: %m", __func__); 957718ced0SMaksim Yevmenkin return NULL; 967718ced0SMaksim Yevmenkin } 977718ced0SMaksim Yevmenkin 987718ced0SMaksim Yevmenkin ph->data = pkt; 997718ced0SMaksim Yevmenkin pkt->refcnt++; 1007718ced0SMaksim Yevmenkin 1017718ced0SMaksim Yevmenkin return ph; 1027718ced0SMaksim Yevmenkin } 1037718ced0SMaksim Yevmenkin 1047718ced0SMaksim Yevmenkin void 1057718ced0SMaksim Yevmenkin pkthdr_free(pkthdr_t *ph) 1067718ced0SMaksim Yevmenkin { 1077718ced0SMaksim Yevmenkin 1087718ced0SMaksim Yevmenkin packet_free(ph->data); 1097718ced0SMaksim Yevmenkin free(ph); 1107718ced0SMaksim Yevmenkin } 111