d099436fc2afdd7b91c06601e3181f6ff08070af
[strongswan.git] / src / libimcv / seg / seg_contract_manager.c
1 /*
2 * Copyright (C) 2014 Andreas Steffen
3 * HSR 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 "seg_contract_manager.h"
17
18 typedef struct private_seg_contract_manager_t private_seg_contract_manager_t;
19
20 /**
21 * Private data of a seg_contract_manager_t object.
22 *
23 */
24 struct private_seg_contract_manager_t {
25
26 /**
27 * Public seg_contract_manager_t interface.
28 */
29 seg_contract_manager_t public;
30
31 /**
32 * List of PA-TNC segmentation contracts
33 */
34 linked_list_t *contracts;
35
36 };
37
38 METHOD(seg_contract_manager_t, add_contract, void,
39 private_seg_contract_manager_t *this, seg_contract_t *contract)
40 {
41 this->contracts->insert_last(this->contracts, contract);
42 }
43
44 METHOD(seg_contract_manager_t, get_contract, seg_contract_t*,
45 private_seg_contract_manager_t *this, pen_type_t msg_type, bool is_issuer)
46 {
47 enumerator_t *enumerator;
48 seg_contract_t *contract, *found = NULL;
49
50 enumerator = this->contracts->create_enumerator(this->contracts);
51 while (enumerator->enumerate(enumerator, &contract))
52 {
53 if (contract->is_issuer(contract) == is_issuer &&
54 pen_type_equals(contract->get_msg_type(contract), msg_type))
55 {
56 found = contract;
57 break;
58 }
59 }
60 enumerator->destroy(enumerator);
61
62 return found;
63 }
64
65 METHOD(seg_contract_manager_t, destroy, void,
66 private_seg_contract_manager_t *this)
67 {
68 this->contracts->destroy_offset(this->contracts,
69 offsetof(seg_contract_t, destroy));
70 free(this);
71 }
72
73 /**
74 * See header
75 */
76 seg_contract_manager_t *seg_contract_manager_create(void)
77 {
78 private_seg_contract_manager_t *this;
79
80 INIT(this,
81 .public = {
82 .add_contract = _add_contract,
83 .get_contract = _get_contract,
84 .destroy = _destroy,
85 },
86 .contracts = linked_list_create(),
87 );
88
89 return &this->public;
90 }
91