7dcdf1728bcf147b140ee3df7aa573a5eedb5c35
[strongswan.git] / Source / charon / config / policies / local_policy_store.c
1 /**
2 * @file local_policy_store.c
3 *
4 * @brief Implementation of local_policy_store_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 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 #include "local_policy_store.h"
24
25 #include <utils/linked_list.h>
26 #include <utils/logger_manager.h>
27
28
29 typedef struct private_local_policy_store_t private_local_policy_store_t;
30
31 /**
32 * Private data of an local_policy_store_t object
33 */
34 struct private_local_policy_store_t {
35
36 /**
37 * Public part
38 */
39 local_policy_store_t public;
40
41 /**
42 * list of policy_t's
43 */
44 linked_list_t *policies;
45
46 /**
47 * Assigned logger
48 */
49 logger_t *logger;
50 };
51
52 /**
53 * Implementation of policy_store_t.add_policy.
54 */
55 static void add_policy(private_local_policy_store_t *this, policy_t *policy)
56 {
57 this->policies->insert_last(this->policies, (void*)policy);
58 }
59
60
61 /**
62 * Implementation of policy_store_t.get_policy.
63 */
64 static policy_t *get_policy(private_local_policy_store_t *this, identification_t *my_id, identification_t *other_id)
65 {
66 iterator_t *iterator;
67 policy_t *current, *found = NULL;
68
69 this->logger->log(this->logger, CONTROL|LEVEL0, "Looking for policy for IDs %s - %s",
70 my_id ? my_id->get_string(my_id) : "%any",
71 other_id->get_string(other_id));
72 iterator = this->policies->create_iterator(this->policies, TRUE);
73 while (iterator->has_next(iterator))
74 {
75 iterator->current(iterator, (void **)&current);
76 identification_t *config_my_id = current->get_my_id(current);
77 identification_t *config_other_id = current->get_other_id(current);
78
79 this->logger->log(this->logger, CONTROL|LEVEL0, "Found one for %s - %s",
80 config_my_id->get_string(config_my_id),
81 config_other_id->get_string(config_other_id));
82
83 /* check other host first */
84 if (other_id->belongs_to(other_id, config_other_id))
85 {
86 /* get it if my_id not specified */
87 if (my_id == NULL)
88 {
89 found = current->clone(current);
90 break;
91 }
92 if (my_id->belongs_to(my_id, config_my_id))
93 {
94 found = current->clone(current);
95 break;
96 }
97 }
98 }
99 iterator->destroy(iterator);
100
101 /* apply IDs as they are requsted, since they may be configured as %any or such */
102 if (found)
103 {
104 if (my_id)
105 {
106 found->update_my_id(found, my_id->clone(my_id));
107 }
108 found->update_other_id(found, other_id->clone(other_id));
109 }
110 return found;
111 }
112
113 /**
114 * Implementation of policy_store_t.destroy.
115 */
116 static void destroy(private_local_policy_store_t *this)
117 {
118 policy_t *policy;
119
120 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
121 {
122 policy->destroy(policy);
123 }
124 this->policies->destroy(this->policies);
125 free(this);
126 }
127
128 /**
129 * Described in header.
130 */
131 local_policy_store_t *local_policy_store_create()
132 {
133 private_local_policy_store_t *this = malloc_thing(private_local_policy_store_t);
134
135 this->public.policy_store.add_policy = (void(*)(policy_store_t*,policy_t*))add_policy;
136 this->public.policy_store.get_policy = (policy_t*(*)(policy_store_t*,identification_t*,identification_t*))get_policy;
137 this->public.policy_store.destroy = (void(*)(policy_store_t*))destroy;
138
139 /* private variables */
140 this->policies = linked_list_create();
141 this->logger = logger_manager->get_logger(logger_manager, CONFIG);
142
143 return (&this->public);
144 }