/** * @file packet.c * * @brief Implementation of packet_t. * */ /* * Copyright (C) 2005 Jan Hutter, Martin Willi * Hochschule fuer Technik Rapperswil * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. See . * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */ #include "packet.h" #include typedef struct private_packet_t private_packet_t; /** * Private data of an packet_t object. */ struct private_packet_t { /** * Public part of a packet_t object. */ packet_t public; }; /** * Implements packet_t.destroy. */ static void destroy(private_packet_t *this) { if (this->public.source != NULL) { this->public.source->destroy(this->public.source); } if (this->public.destination != NULL) { this->public.destination->destroy(this->public.destination); } allocator_free(this->public.data.ptr); allocator_free(this); } /** * Implements packet_t.clone. */ static packet_t *clone (private_packet_t *this) { packet_t *other; other = packet_create(); if (this->public.destination != NULL) { other->destination = this->public.destination->clone(this->public.destination); } else { other->destination = NULL; } if (this->public.source != NULL) { other->source = this->public.source->clone(this->public.source); } else { other->source = NULL; } /* only clone existing chunks :-) */ if (this->public.data.ptr != NULL) { other->data.ptr = allocator_clone_bytes(this->public.data.ptr,this->public.data.len); other->data.len = this->public.data.len; } else { other->data = CHUNK_INITIALIZER; } return other; } /* * Documented in header */ packet_t *packet_create() { private_packet_t *this = allocator_alloc_thing(private_packet_t); this->public.destroy = (void(*) (packet_t *)) destroy; this->public.clone = (packet_t*(*) (packet_t *))clone; this->public.destination = NULL; this->public.source = NULL; this->public.data = CHUNK_INITIALIZER; return &(this->public); }