Removed strayed code fragment
[strongswan.git] / src / charon / plugins / stroke / stroke_shared_key.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "stroke_shared_key.h"
17
18 #include <utils/linked_list.h>
19
20 typedef struct private_stroke_shared_key_t private_stroke_shared_key_t;
21
22 /**
23 * private data of shared_key
24 */
25 struct private_stroke_shared_key_t {
26
27 /**
28 * implements shared_key_t
29 */
30 stroke_shared_key_t public;
31
32 /**
33 * type of this key
34 */
35 shared_key_type_t type;
36
37 /**
38 * data of the key
39 */
40 chunk_t key;
41
42 /**
43 * list of key owners, as identification_t
44 */
45 linked_list_t *owners;
46
47 /**
48 * reference counter
49 */
50 refcount_t ref;
51 };
52
53 /**
54 * Implementation of shared_key_t.get_type.
55 */
56 static shared_key_type_t get_type(private_stroke_shared_key_t *this)
57 {
58 return this->type;
59 }
60
61 /**
62 * Implementation of shared_key_t.get_ref.
63 */
64 static private_stroke_shared_key_t* get_ref(private_stroke_shared_key_t *this)
65 {
66 ref_get(&this->ref);
67 return this;
68 }
69
70 /**
71 * Implementation of shared_key_t.get_key.
72 */
73 static chunk_t get_key(private_stroke_shared_key_t *this)
74 {
75 return this->key;
76 }
77
78 /**
79 * Implementation of stroke_shared_key_t.has_owner.
80 */
81 static id_match_t has_owner(private_stroke_shared_key_t *this, identification_t *owner)
82 {
83 enumerator_t *enumerator;
84 id_match_t match, best = ID_MATCH_NONE;
85 identification_t *current;
86
87 enumerator = this->owners->create_enumerator(this->owners);
88 while (enumerator->enumerate(enumerator, &current))
89 {
90 match = owner->matches(owner, current);
91 if (match > best)
92 {
93 best = match;
94 }
95 }
96 enumerator->destroy(enumerator);
97 return best;
98 }
99 /**
100 * Implementation of stroke_shared_key_t.add_owner.
101 */
102 static void add_owner(private_stroke_shared_key_t *this, identification_t *owner)
103 {
104 this->owners->insert_last(this->owners, owner);
105 }
106
107 /**
108 * Implementation of stroke_shared_key_t.destroy
109 */
110 static void destroy(private_stroke_shared_key_t *this)
111 {
112 if (ref_put(&this->ref))
113 {
114 this->owners->destroy_offset(this->owners, offsetof(identification_t, destroy));
115 chunk_free(&this->key);
116 free(this);
117 }
118 }
119
120 /**
121 * create a shared key
122 */
123 stroke_shared_key_t *stroke_shared_key_create(shared_key_type_t type, chunk_t key)
124 {
125 private_stroke_shared_key_t *this = malloc_thing(private_stroke_shared_key_t);
126
127 this->public.shared.get_type = (shared_key_type_t(*)(shared_key_t*))get_type;
128 this->public.shared.get_key = (chunk_t(*)(shared_key_t*))get_key;
129 this->public.shared.get_ref = (shared_key_t*(*)(shared_key_t*))get_ref;
130 this->public.shared.destroy = (void(*)(shared_key_t*))destroy;
131 this->public.add_owner = (void(*)(stroke_shared_key_t*, identification_t *owner))add_owner;
132 this->public.has_owner = (id_match_t(*)(stroke_shared_key_t*, identification_t *owner))has_owner;
133
134 this->owners = linked_list_create();
135 this->type = type;
136 this->key = key;
137 this->ref = 1;
138
139 return &this->public;
140 }