- fixed compliation error using --enable-smartcard
[strongswan.git] / src / charon / network / packet.c
1 /**
2 * @file packet.c
3 *
4 * @brief Implementation of packet_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23
24 #include "packet.h"
25
26
27 typedef struct private_packet_t private_packet_t;
28
29 /**
30 * Private data of an packet_t object.
31 */
32 struct private_packet_t {
33
34 /**
35 * Public part of a packet_t object.
36 */
37 packet_t public;
38
39 /**
40 * source address
41 */
42 host_t *source;
43
44 /**
45 * destination address
46 */
47 host_t *destination;
48
49 /**
50 * message data
51 */
52 chunk_t data;
53 };
54
55 /**
56 * Implements packet_t.get_source
57 */
58 static void set_source(private_packet_t *this, host_t *source)
59 {
60 if (this->source)
61 {
62 this->source->destroy(this->source);
63 }
64 this->source = source;
65 }
66
67 /**
68 * Implements packet_t.set_destination
69 */
70 static void set_destination(private_packet_t *this, host_t *destination)
71 {
72 if (this->destination)
73 {
74 this->destination->destroy(this->destination);
75 }
76 this->destination = destination;
77 }
78
79 /**
80 * Implements packet_t.get_source
81 */
82 static host_t *get_source(private_packet_t *this)
83 {
84 return this->source;
85 }
86
87 /**
88 * Implements packet_t.get_destination
89 */
90 static host_t *get_destination(private_packet_t *this)
91 {
92 return this->destination;
93 }
94
95 /**
96 * Implements packet_t.get_data
97 */
98 static chunk_t get_data(private_packet_t *this)
99 {
100 return this->data;
101 }
102
103 /**
104 * Implements packet_t.set_data
105 */
106 static void set_data(private_packet_t *this, chunk_t data)
107 {
108 free(this->data.ptr);
109 this->data = data;
110 }
111
112 /**
113 * Implements packet_t.destroy.
114 */
115 static void destroy(private_packet_t *this)
116 {
117 if (this->source != NULL)
118 {
119 this->source->destroy(this->source);
120 }
121 if (this->destination != NULL)
122 {
123 this->destination->destroy(this->destination);
124 }
125 free(this->data.ptr);
126 free(this);
127 }
128
129 /**
130 * Implements packet_t.clone.
131 */
132 static packet_t *clone(private_packet_t *this)
133 {
134 private_packet_t *other = (private_packet_t*)packet_create();
135
136 if (this->destination != NULL)
137 {
138 other->destination = this->destination->clone(this->destination);
139 }
140 else
141 {
142 other->destination = NULL;
143 }
144
145 if (this->source != NULL)
146 {
147 other->source = this->source->clone(this->source);
148 }
149 else
150 {
151 other->source = NULL;
152 }
153
154 /* only clone existing chunks :-) */
155 if (this->data.ptr != NULL)
156 {
157 other->data.ptr = clalloc(this->data.ptr,this->data.len);
158 other->data.len = this->data.len;
159 }
160 else
161 {
162 other->data = CHUNK_INITIALIZER;
163 }
164 return &(other->public);
165 }
166
167
168 /*
169 * Documented in header
170 */
171 packet_t *packet_create(void)
172 {
173 private_packet_t *this = malloc_thing(private_packet_t);
174
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;
183
184 this->destination = NULL;
185 this->source = NULL;
186 this->data = CHUNK_INITIALIZER;
187
188 return &(this->public);
189 }