Version bump to 5.8.2dr1
[strongswan.git] / src / libstrongswan / networking / packet.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * HSR Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "packet.h"
19
20 typedef struct private_packet_t private_packet_t;
21
22 /**
23 * Private data of an packet_t object.
24 */
25 struct private_packet_t {
26
27 /**
28 * Public part of a packet_t object.
29 */
30 packet_t public;
31
32 /**
33 * source address
34 */
35 host_t *source;
36
37 /**
38 * destination address
39 */
40 host_t *destination;
41
42 /**
43 * DSCP value on packet
44 */
45 uint8_t dscp;
46
47 /**
48 * message data
49 */
50 chunk_t data;
51
52 /**
53 * actual chunk returned from get_data, adjusted when skip_bytes is called
54 */
55 chunk_t adjusted_data;
56 };
57
58 METHOD(packet_t, set_source, void,
59 private_packet_t *this, host_t *source)
60 {
61 DESTROY_IF(this->source);
62 this->source = source;
63 }
64
65 METHOD(packet_t, set_destination, void,
66 private_packet_t *this, host_t *destination)
67 {
68 DESTROY_IF(this->destination);
69 this->destination = destination;
70 }
71
72 METHOD(packet_t, get_source, host_t*,
73 private_packet_t *this)
74 {
75 return this->source;
76 }
77
78 METHOD(packet_t, get_destination, host_t*,
79 private_packet_t *this)
80 {
81 return this->destination;
82 }
83
84 METHOD(packet_t, get_data, chunk_t,
85 private_packet_t *this)
86 {
87 return this->adjusted_data;
88 }
89
90 METHOD(packet_t, set_data, void,
91 private_packet_t *this, chunk_t data)
92 {
93 free(this->data.ptr);
94 this->adjusted_data = this->data = data;
95 }
96
97 METHOD(packet_t, get_dscp, uint8_t,
98 private_packet_t *this)
99 {
100 return this->dscp;
101 }
102 METHOD(packet_t, set_dscp, void,
103 private_packet_t *this, uint8_t value)
104 {
105 this->dscp = value;
106 }
107
108 METHOD(packet_t, skip_bytes, void,
109 private_packet_t *this, size_t bytes)
110 {
111 this->adjusted_data = chunk_skip(this->adjusted_data, bytes);
112 }
113
114 METHOD(packet_t, destroy, void,
115 private_packet_t *this)
116 {
117 DESTROY_IF(this->source);
118 DESTROY_IF(this->destination);
119 free(this->data.ptr);
120 free(this);
121 }
122
123 METHOD(packet_t, clone_, packet_t*,
124 private_packet_t *this)
125 {
126 packet_t *other;
127
128 other = packet_create();
129 if (this->destination)
130 {
131 other->set_destination(other,
132 this->destination->clone(this->destination));
133 }
134 if (this->source)
135 {
136 other->set_source(other, this->source->clone(this->source));
137 }
138 if (this->data.ptr)
139 {
140 other->set_data(other, chunk_clone(this->adjusted_data));
141 }
142 other->set_dscp(other, this->dscp);
143 return other;
144 }
145
146 /**
147 * Described in header.
148 */
149 packet_t *packet_create_from_data(host_t *src, host_t *dst, chunk_t data)
150 {
151 private_packet_t *this;
152
153 INIT(this,
154 .public = {
155 .set_data = _set_data,
156 .get_data = _get_data,
157 .set_source = _set_source,
158 .get_source = _get_source,
159 .set_destination = _set_destination,
160 .get_destination = _get_destination,
161 .get_dscp = _get_dscp,
162 .set_dscp = _set_dscp,
163 .skip_bytes = _skip_bytes,
164 .clone = _clone_,
165 .destroy = _destroy,
166 },
167 .source = src,
168 .destination = dst,
169 .adjusted_data = data,
170 .data = data,
171 );
172
173 return &this->public;
174 }
175
176 /*
177 * Described in header.
178 */
179 packet_t *packet_create()
180 {
181 return packet_create_from_data(NULL, NULL, chunk_empty);
182 }