- reworked usage of IDs in various states
[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|LEVEL1, "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|LEVEL2, "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->belongs_to(my_id, config_my_id))
88 {
89 found = current->clone(current);
90 break;
91 }
92 }
93 }
94 iterator->destroy(iterator);
95
96 /* apply IDs as they are requsted, since they may be configured as %any or such */
97 if (found)
98 {
99 found->update_my_id(found, my_id->clone(my_id));
100 found->update_other_id(found, other_id->clone(other_id));
101 }
102 return found;
103 }
104
105 /**
106 * Implementation of policy_store_t.destroy.
107 */
108 static void destroy(private_local_policy_store_t *this)
109 {
110 policy_t *policy;
111
112 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
113 {
114 policy->destroy(policy);
115 }
116 this->policies->destroy(this->policies);
117 free(this);
118 }
119
120 /**
121 * Described in header.
122 */
123 local_policy_store_t *local_policy_store_create()
124 {
125 private_local_policy_store_t *this = malloc_thing(private_local_policy_store_t);
126
127 this->public.policy_store.add_policy = (void(*)(policy_store_t*,policy_t*))add_policy;
128 this->public.policy_store.get_policy = (policy_t*(*)(policy_store_t*,identification_t*,identification_t*))get_policy;
129 this->public.policy_store.destroy = (void(*)(policy_store_t*))destroy;
130
131 /* private variables */
132 this->policies = linked_list_create();
133 this->logger = logger_manager->get_logger(logger_manager, CONFIG);
134
135 return (&this->public);
136 }