show kind of notify contained in messages in log
[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 DESTROY_IF(this->source);
62 this->source = source;
63 }
64
65 /**
66 * Implements packet_t.set_destination
67 */
68 static void set_destination(private_packet_t *this, host_t *destination)
69 {
70 DESTROY_IF(this->destination);
71 this->destination = destination;
72 }
73
74 /**
75 * Implements packet_t.get_source
76 */
77 static host_t *get_source(private_packet_t *this)
78 {
79 return this->source;
80 }
81
82 /**
83 * Implements packet_t.get_destination
84 */
85 static host_t *get_destination(private_packet_t *this)
86 {
87 return this->destination;
88 }
89
90 /**
91 * Implements packet_t.get_data
92 */
93 static chunk_t get_data(private_packet_t *this)
94 {
95 return this->data;
96 }
97
98 /**
99 * Implements packet_t.set_data
100 */
101 static void set_data(private_packet_t *this, chunk_t data)
102 {
103 free(this->data.ptr);
104 this->data = data;
105 }
106
107 /**
108 * Implements packet_t.destroy.
109 */
110 static void destroy(private_packet_t *this)
111 {
112 if (this->source != NULL)
113 {
114 this->source->destroy(this->source);
115 }
116 if (this->destination != NULL)
117 {
118 this->destination->destroy(this->destination);
119 }
120 free(this->data.ptr);
121 free(this);
122 }
123
124 /**
125 * Implements packet_t.clone.
126 */
127 static packet_t *clone_(private_packet_t *this)
128 {
129 private_packet_t *other = (private_packet_t*)packet_create();
130
131 if (this->destination != NULL)
132 {
133 other->destination = this->destination->clone(this->destination);
134 }
135 if (this->source != NULL)
136 {
137 other->source = this->source->clone(this->source);
138 }
139 if (this->data.ptr != NULL)
140 {
141 other->data.ptr = clalloc(this->data.ptr,this->data.len);
142 other->data.len = this->data.len;
143 }
144 return &(other->public);
145 }
146
147 /*
148 * Documented in header
149 */
150 packet_t *packet_create(void)
151 {
152 private_packet_t *this = malloc_thing(private_packet_t);
153
154 this->public.set_data = (void(*) (packet_t *,chunk_t)) set_data;
155 this->public.get_data = (chunk_t(*) (packet_t *)) get_data;
156 this->public.set_source = (void(*) (packet_t *,host_t*)) set_source;
157 this->public.get_source = (host_t*(*) (packet_t *)) get_source;
158 this->public.set_destination = (void(*) (packet_t *,host_t*)) set_destination;
159 this->public.get_destination = (host_t*(*) (packet_t *)) get_destination;
160 this->public.clone = (packet_t*(*) (packet_t *))clone_;
161 this->public.destroy = (void(*) (packet_t *)) destroy;
162
163 this->destination = NULL;
164 this->source = NULL;
165 this->data = chunk_empty;
166
167 return &(this->public);
168 }