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