4 * @brief Implementation of packet_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include <utils/allocator.h>
29 typedef struct private_packet_t private_packet_t
;
32 * Private data of an packet_t object.
34 struct private_packet_t
{
37 * Public part of a packet_t object.
43 * Implements packet_t.destroy.
45 static void destroy(private_packet_t
*this)
47 if (this->public.source
!= NULL
)
49 this->public.source
->destroy(this->public.source
);
51 if (this->public.destination
!= NULL
)
53 this->public.destination
->destroy(this->public.destination
);
55 allocator_free(this->public.data
.ptr
);
60 * Implements packet_t.clone.
62 static packet_t
*clone (private_packet_t
*this)
65 other
= packet_create();
67 if (this->public.destination
!= NULL
)
69 other
->destination
= this->public.destination
->clone(this->public.destination
);
73 other
->destination
= NULL
;
76 if (this->public.source
!= NULL
)
78 other
->source
= this->public.source
->clone(this->public.source
);
85 /* only clone existing chunks :-) */
86 if (this->public.data
.ptr
!= NULL
)
88 other
->data
.ptr
= allocator_clone_bytes(this->public.data
.ptr
,this->public.data
.len
);
89 other
->data
.len
= this->public.data
.len
;
93 other
->data
= CHUNK_INITIALIZER
;
100 * Documented in header
102 packet_t
*packet_create()
104 private_packet_t
*this = allocator_alloc_thing(private_packet_t
);
106 this->public.destroy
= (void(*) (packet_t
*)) destroy
;
107 this->public.clone
= (packet_t
*(*) (packet_t
*))clone
;
109 this->public.destination
= NULL
;
110 this->public.source
= NULL
;
111 this->public.data
= CHUNK_INITIALIZER
;
113 return &(this->public);