- started implementation of netlink kernel interface
[strongswan.git] / Source / charon / network / packet.c
1 /**
2 * @file packet.c
3 *
4 * @brief Implementation of packet_t.
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 typedef struct private_packet_t private_packet_t;
30
31 /**
32 * Private data of an packet_t object.
33 */
34 struct private_packet_t {
35
36 /**
37 * Public part of a packet_t object.
38 */
39 packet_t public;
40 };
41
42 /**
43 * Implements packet_t.destroy.
44 */
45 static void destroy(private_packet_t *this)
46 {
47 if (this->public.source != NULL)
48 {
49 this->public.source->destroy(this->public.source);
50 }
51 if (this->public.destination != NULL)
52 {
53 this->public.destination->destroy(this->public.destination);
54 }
55 allocator_free(this->public.data.ptr);
56 allocator_free(this);
57 }
58
59 /**
60 * Implements packet_t.clone.
61 */
62 static packet_t *clone (private_packet_t *this)
63 {
64 packet_t *other;
65 other = packet_create();
66
67 if (this->public.destination != NULL)
68 {
69 other->destination = this->public.destination->clone(this->public.destination);
70 }
71 else
72 {
73 other->destination = NULL;
74 }
75
76 if (this->public.source != NULL)
77 {
78 other->source = this->public.source->clone(this->public.source);
79 }
80 else
81 {
82 other->source = NULL;
83 }
84
85 /* only clone existing chunks :-) */
86 if (this->public.data.ptr != NULL)
87 {
88 other->data.ptr = allocator_clone_bytes(this->public.data.ptr,this->public.data.len);
89 other->data.len = this->public.data.len;
90 }
91 else
92 {
93 other->data = CHUNK_INITIALIZER;
94 }
95 return other;
96 }
97
98
99 /*
100 * Documented in header
101 */
102 packet_t *packet_create()
103 {
104 private_packet_t *this = allocator_alloc_thing(private_packet_t);
105
106 this->public.destroy = (void(*) (packet_t *)) destroy;
107 this->public.clone = (packet_t*(*) (packet_t *))clone;
108
109 this->public.destination = NULL;
110 this->public.source = NULL;
111 this->public.data = CHUNK_INITIALIZER;
112
113 return &(this->public);
114 }