- jobs moved into queues
[strongswan.git] / Source / charon / packet.c
1 /**
2 * @file packet.c
3 *
4 * @brief UDP-Packet, contains data, sender and receiver.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23
24 #include "packet.h"
25
26 #include <utils/allocator.h>
27
28
29 /**
30 * Private data of an packet_t object
31 */
32 typedef struct private_packet_s private_packet_t;
33
34 struct private_packet_s {
35
36 /**
37 * Public part of a packet_t object
38 */
39 packet_t public;
40 };
41
42 /**
43 * Implements packet_t's destroy function.
44 * See #packet_s.destroy for description.
45 */
46 static status_t destroy(private_packet_t *this)
47 {
48 if (this->public.source != NULL)
49 {
50 this->public.source->destroy(this->public.source);
51 }
52 if (this->public.destination != NULL)
53 {
54 this->public.destination->destroy(this->public.destination);
55 }
56 if (this->public.data.ptr != NULL)
57 {
58 allocator_free(this->public.data.ptr);
59 }
60 allocator_free(this);
61 return SUCCESS;
62 }
63
64 /**
65 * Implements packet_t's clone function.
66 * See #packet_s.clone for description.
67 */
68 static status_t clone (private_packet_t *this, packet_t **clone)
69 {
70 packet_t *other;
71 other = packet_create();
72 if (other == NULL)
73 {
74 return OUT_OF_RES;
75 }
76
77 if (this->public.destination != NULL)
78 {
79 this->public.destination->clone(this->public.destination, &(other->destination));
80 }
81 else {
82 other->destination = NULL;
83 }
84
85 if (this->public.source != NULL)
86 {
87 this->public.source->clone(this->public.source, &(other->source));
88 }
89 else {
90 other->source = NULL;
91 }
92
93 /* only clone existing chunks :-) */
94 if (this->public.data.ptr != NULL)
95 {
96 other->data.ptr = allocator_clone_bytes(this->public.data.ptr,this->public.data.len);
97 if (other->data.ptr == NULL)
98 {
99 other->destroy(other);
100 return OUT_OF_RES;
101 }
102 other->data.len = this->public.data.len;
103 }
104 else
105 {
106 other->data.ptr = NULL;
107 other->data.len = 0;
108 }
109 *clone = other;
110 return SUCCESS;
111 }
112
113
114 /*
115 * Documented in header
116 */
117 packet_t *packet_create()
118 {
119 private_packet_t *this = allocator_alloc_thing(private_packet_t);
120
121 this->public.destroy = (status_t(*) (packet_t *)) destroy;
122 this->public.clone = (status_t(*) (packet_t *,packet_t**))clone;
123
124 this->public.destination = NULL;
125 this->public.source = NULL;
126
127 this->public.data.len = 0;
128 this->public.data.ptr = NULL;
129 return &(this->public);
130 }