splitted stroke plugin to several files:
[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 * $Id$
16 */
17
18 #include "stroke_shared_key.h"
19
20 #include <utils/linked_list.h>
21
22 typedef struct private_stroke_shared_key_t private_stroke_shared_key_t;
23
24 /**
25 * private data of shared_key
26 */
27 struct private_stroke_shared_key_t {
28
29 /**
30 * implements shared_key_t
31 */
32 stroke_shared_key_t public;
33
34 /**
35 * type of this key
36 */
37 shared_key_type_t type;
38
39 /**
40 * data of the key
41 */
42 chunk_t key;
43
44 /**
45 * list of key owners, as identification_t
46 */
47 linked_list_t *owners;
48
49 /**
50 * reference counter
51 */
52 refcount_t ref;
53 };
54
55 /**
56 * Implementation of shared_key_t.get_type.
57 */
58 static shared_key_type_t get_type(private_stroke_shared_key_t *this)
59 {
60 return this->type;
61 }
62
63 /**
64 * Implementation of shared_key_t.get_ref.
65 */
66 static private_stroke_shared_key_t* get_ref(private_stroke_shared_key_t *this)
67 {
68 ref_get(&this->ref);
69 return this;
70 }
71
72 /**
73 * Implementation of shared_key_t.get_key.
74 */
75 static chunk_t get_key(private_stroke_shared_key_t *this)
76 {
77 return this->key;
78 }
79
80 /**
81 * Implementation of stroke_shared_key_t.has_owner.
82 */
83 static id_match_t has_owner(private_stroke_shared_key_t *this, identification_t *owner)
84 {
85 enumerator_t *enumerator;
86 id_match_t match, best = ID_MATCH_NONE;
87 identification_t *current;
88
89 enumerator = this->owners->create_enumerator(this->owners);
90 while (enumerator->enumerate(enumerator, &current))
91 {
92 match = owner->matches(owner, current);
93 if (match > best)
94 {
95 best = match;
96 }
97 }
98 enumerator->destroy(enumerator);
99 return best;
100 }
101 /**
102 * Implementation of stroke_shared_key_t.add_owner.
103 */
104 static void add_owner(private_stroke_shared_key_t *this, identification_t *owner)
105 {
106 this->owners->insert_last(this->owners, owner);
107 }
108
109 /**
110 * Implementation of stroke_shared_key_t.destroy
111 */
112 static void destroy(private_stroke_shared_key_t *this)
113 {
114 if (ref_put(&this->ref))
115 {
116 this->owners->destroy_offset(this->owners, offsetof(identification_t, destroy));
117 chunk_free(&this->key);
118 free(this);
119 }
120 }
121
122 /**
123 * create a shared key
124 */
125 stroke_shared_key_t *stroke_shared_key_create(shared_key_type_t type, chunk_t key)
126 {
127 private_stroke_shared_key_t *this = malloc_thing(private_stroke_shared_key_t);
128
129 this->public.shared.get_type = (shared_key_type_t(*)(shared_key_t*))get_type;
130 this->public.shared.get_key = (chunk_t(*)(shared_key_t*))get_key;
131 this->public.shared.get_ref = (shared_key_t*(*)(shared_key_t*))get_ref;
132 this->public.shared.destroy = (void(*)(shared_key_t*))destroy;
133 this->public.add_owner = (void(*)(stroke_shared_key_t*, identification_t *owner))add_owner;
134 this->public.has_owner = (id_match_t(*)(stroke_shared_key_t*, identification_t *owner))has_owner;
135
136 this->owners = linked_list_create();
137 this->type = type;
138 this->key = key;
139 this->ref = 1;
140
141 return &this->public;
142 }