4 * @brief UDP-Packet, contains data, sender and receiver.
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>
30 * Private data of an packet_t object
32 typedef struct private_packet_s private_packet_t
;
34 struct private_packet_s
{
37 * Public part of a packet_t object
43 * Implements packet_t's destroy function.
44 * See #packet_s.destroy for description.
46 static status_t
destroy(private_packet_t
*this)
48 if (this->public.source
!= NULL
)
50 this->public.source
->destroy(this->public.source
);
52 if (this->public.destination
!= NULL
)
54 this->public.destination
->destroy(this->public.destination
);
56 if (this->public.data
.ptr
!= NULL
)
58 allocator_free(this->public.data
.ptr
);
65 * Implements packet_t's clone function.
66 * See #packet_s.clone for description.
68 static status_t
clone (private_packet_t
*this, packet_t
**clone
)
71 other
= packet_create();
77 if (this->public.destination
!= NULL
)
79 this->public.destination
->clone(this->public.destination
, &(other
->destination
));
82 other
->destination
= NULL
;
85 if (this->public.source
!= NULL
)
87 this->public.source
->clone(this->public.source
, &(other
->source
));
93 /* only clone existing chunks :-) */
94 if (this->public.data
.ptr
!= NULL
)
96 other
->data
.ptr
= allocator_clone_bytes(this->public.data
.ptr
,this->public.data
.len
);
97 if (other
->data
.ptr
== NULL
)
99 other
->destroy(other
);
102 other
->data
.len
= this->public.data
.len
;
106 other
->data
.ptr
= NULL
;
115 * Documented in header
117 packet_t
*packet_create()
119 private_packet_t
*this = allocator_alloc_thing(private_packet_t
);
121 this->public.destroy
= (status_t(*) (packet_t
*)) destroy
;
122 this->public.clone
= (status_t(*) (packet_t
*,packet_t
**))clone
;
124 this->public.destination
= NULL
;
125 this->public.source
= NULL
;
127 this->public.data
.len
= 0;
128 this->public.data
.ptr
= NULL
;
129 return &(this->public);