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
27 typedef struct private_packet_t private_packet_t
;
30 * Private data of an packet_t object.
32 struct private_packet_t
{
35 * Public part of a packet_t object.
56 * Implements packet_t.get_source
58 static void set_source(private_packet_t
*this, host_t
*source
)
62 this->source
->destroy(this->source
);
64 this->source
= source
;
68 * Implements packet_t.set_destination
70 static void set_destination(private_packet_t
*this, host_t
*destination
)
72 if (this->destination
)
74 this->destination
->destroy(this->destination
);
76 this->destination
= destination
;
80 * Implements packet_t.get_source
82 static host_t
*get_source(private_packet_t
*this)
88 * Implements packet_t.get_destination
90 static host_t
*get_destination(private_packet_t
*this)
92 return this->destination
;
96 * Implements packet_t.get_data
98 static chunk_t
get_data(private_packet_t
*this)
104 * Implements packet_t.set_data
106 static void set_data(private_packet_t
*this, chunk_t data
)
108 free(this->data
.ptr
);
113 * Implements packet_t.destroy.
115 static void destroy(private_packet_t
*this)
117 if (this->source
!= NULL
)
119 this->source
->destroy(this->source
);
121 if (this->destination
!= NULL
)
123 this->destination
->destroy(this->destination
);
125 free(this->data
.ptr
);
130 * Implements packet_t.clone.
132 static packet_t
*clone(private_packet_t
*this)
134 private_packet_t
*other
= (private_packet_t
*)packet_create();
136 if (this->destination
!= NULL
)
138 other
->destination
= this->destination
->clone(this->destination
);
142 other
->destination
= NULL
;
145 if (this->source
!= NULL
)
147 other
->source
= this->source
->clone(this->source
);
151 other
->source
= NULL
;
154 /* only clone existing chunks :-) */
155 if (this->data
.ptr
!= NULL
)
157 other
->data
.ptr
= clalloc(this->data
.ptr
,this->data
.len
);
158 other
->data
.len
= this->data
.len
;
162 other
->data
= CHUNK_INITIALIZER
;
164 return &(other
->public);
169 * Documented in header
171 packet_t
*packet_create(void)
173 private_packet_t
*this = malloc_thing(private_packet_t
);
175 this->public.set_data
= (void(*) (packet_t
*,chunk_t
)) set_data
;
176 this->public.get_data
= (chunk_t(*) (packet_t
*)) get_data
;
177 this->public.set_source
= (void(*) (packet_t
*,host_t
*)) set_source
;
178 this->public.get_source
= (host_t
*(*) (packet_t
*)) get_source
;
179 this->public.set_destination
= (void(*) (packet_t
*,host_t
*)) set_destination
;
180 this->public.get_destination
= (host_t
*(*) (packet_t
*)) get_destination
;
181 this->public.clone
= (packet_t
*(*) (packet_t
*))clone
;
182 this->public.destroy
= (void(*) (packet_t
*)) destroy
;
184 this->destination
= NULL
;
186 this->data
= CHUNK_INITIALIZER
;
188 return &(this->public);