better split up of library files "types.h" & "definitions.h"
[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-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24
25 #include "packet.h"
26
27
28 typedef struct private_packet_t private_packet_t;
29
30 /**
31 * Private data of an packet_t object.
32 */
33 struct private_packet_t {
34
35 /**
36 * Public part of a packet_t object.
37 */
38 packet_t public;
39
40 /**
41 * source address
42 */
43 host_t *source;
44
45 /**
46 * destination address
47 */
48 host_t *destination;
49
50 /**
51 * message data
52 */
53 chunk_t data;
54 };
55
56 /**
57 * Implements packet_t.get_source
58 */
59 static void set_source(private_packet_t *this, host_t *source)
60 {
61 if (this->source)
62 {
63 this->source->destroy(this->source);
64 }
65 this->source = source;
66 }
67
68 /**
69 * Implements packet_t.set_destination
70 */
71 static void set_destination(private_packet_t *this, host_t *destination)
72 {
73 if (this->destination)
74 {
75 this->destination->destroy(this->destination);
76 }
77 this->destination = destination;
78 }
79
80 /**
81 * Implements packet_t.get_source
82 */
83 static host_t *get_source(private_packet_t *this)
84 {
85 return this->source;
86 }
87
88 /**
89 * Implements packet_t.get_destination
90 */
91 static host_t *get_destination(private_packet_t *this)
92 {
93 return this->destination;
94 }
95
96 /**
97 * Implements packet_t.get_data
98 */
99 static chunk_t get_data(private_packet_t *this)
100 {
101 return this->data;
102 }
103
104 /**
105 * Implements packet_t.set_data
106 */
107 static void set_data(private_packet_t *this, chunk_t data)
108 {
109 free(this->data.ptr);
110 this->data = data;
111 }
112
113 /**
114 * Implements packet_t.destroy.
115 */
116 static void destroy(private_packet_t *this)
117 {
118 if (this->source != NULL)
119 {
120 this->source->destroy(this->source);
121 }
122 if (this->destination != NULL)
123 {
124 this->destination->destroy(this->destination);
125 }
126 free(this->data.ptr);
127 free(this);
128 }
129
130 /**
131 * Implements packet_t.clone.
132 */
133 static packet_t *clone(private_packet_t *this)
134 {
135 private_packet_t *other = (private_packet_t*)packet_create();
136
137 if (this->destination != NULL)
138 {
139 other->destination = this->destination->clone(this->destination);
140 }
141 if (this->source != NULL)
142 {
143 other->source = this->source->clone(this->source);
144 }
145 if (this->data.ptr != NULL)
146 {
147 other->data.ptr = clalloc(this->data.ptr,this->data.len);
148 other->data.len = this->data.len;
149 }
150 return &(other->public);
151 }
152
153 /*
154 * Documented in header
155 */
156 packet_t *packet_create(void)
157 {
158 private_packet_t *this = malloc_thing(private_packet_t);
159
160 this->public.set_data = (void(*) (packet_t *,chunk_t)) set_data;
161 this->public.get_data = (chunk_t(*) (packet_t *)) get_data;
162 this->public.set_source = (void(*) (packet_t *,host_t*)) set_source;
163 this->public.get_source = (host_t*(*) (packet_t *)) get_source;
164 this->public.set_destination = (void(*) (packet_t *,host_t*)) set_destination;
165 this->public.get_destination = (host_t*(*) (packet_t *)) get_destination;
166 this->public.clone = (packet_t*(*) (packet_t *))clone;
167 this->public.destroy = (void(*) (packet_t *)) destroy;
168
169 this->destination = NULL;
170 this->source = NULL;
171 this->data = chunk_empty;
172
173 return &(this->public);
174 }