Moved packet_t to libstrongswan
[strongswan.git] / src / libstrongswan / utils / packet.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * 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 * message data
44 */
45 chunk_t data;
46
47 /**
48 * actual chunk returned from get_data, adjusted when skip_bytes is called
49 */
50 chunk_t adjusted_data;
51 };
52
53 METHOD(packet_t, set_source, void,
54 private_packet_t *this, host_t *source)
55 {
56 DESTROY_IF(this->source);
57 this->source = source;
58 }
59
60 METHOD(packet_t, set_destination, void,
61 private_packet_t *this, host_t *destination)
62 {
63 DESTROY_IF(this->destination);
64 this->destination = destination;
65 }
66
67 METHOD(packet_t, get_source, host_t*,
68 private_packet_t *this)
69 {
70 return this->source;
71 }
72
73 METHOD(packet_t, get_destination, host_t*,
74 private_packet_t *this)
75 {
76 return this->destination;
77 }
78
79 METHOD(packet_t, get_data, chunk_t,
80 private_packet_t *this)
81 {
82 return this->adjusted_data;
83 }
84
85 METHOD(packet_t, set_data, void,
86 private_packet_t *this, chunk_t data)
87 {
88 free(this->data.ptr);
89 this->adjusted_data = this->data = data;
90 }
91
92 METHOD(packet_t, skip_bytes, void,
93 private_packet_t *this, size_t bytes)
94 {
95 this->adjusted_data = chunk_skip(this->adjusted_data, bytes);
96 }
97
98 METHOD(packet_t, destroy, void,
99 private_packet_t *this)
100 {
101 DESTROY_IF(this->source);
102 DESTROY_IF(this->destination);
103 free(this->data.ptr);
104 free(this);
105 }
106
107 METHOD(packet_t, clone_, packet_t*,
108 private_packet_t *this)
109 {
110 packet_t *other;
111
112 other = packet_create();
113 if (this->destination)
114 {
115 other->set_destination(other,
116 this->destination->clone(this->destination));
117 }
118 if (this->source)
119 {
120 other->set_source(other, this->source->clone(this->source));
121 }
122 if (this->data.ptr)
123 {
124 other->set_data(other, chunk_clone(this->adjusted_data));
125 }
126 return other;
127 }
128
129 /*
130 * Documented in header
131 */
132 packet_t *packet_create()
133 {
134 private_packet_t *this;
135
136 INIT(this,
137 .public = {
138 .set_data = _set_data,
139 .get_data = _get_data,
140 .set_source = _set_source,
141 .get_source = _get_source,
142 .set_destination = _set_destination,
143 .get_destination = _get_destination,
144 .skip_bytes = _skip_bytes,
145 .clone = _clone_,
146 .destroy = _destroy,
147 },
148 );
149
150 return &this->public;
151 }
152