Added add_segment() method to IETF attributes
[strongswan.git] / src / libimcv / imv / imv_session_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 "imv_session_manager.h"
17
18 #include <threading/mutex.h>
19
20 typedef struct private_imv_session_manager_t private_imv_session_manager_t;
21
22 /**
23 * Private data of a imv_session_manager_t object.
24 */
25 struct private_imv_session_manager_t {
26
27 /**
28 * Public imv_session_manager_t interface.
29 */
30 imv_session_manager_t public;
31
32 /**
33 * Session list
34 */
35 linked_list_t *sessions;
36
37 /**
38 * mutex used to lock session list
39 */
40 mutex_t *mutex;
41
42 };
43
44 METHOD(imv_session_manager_t, add_session, imv_session_t*,
45 private_imv_session_manager_t *this, TNC_ConnectionID conn_id,
46 uint32_t ar_id_type, chunk_t ar_id_value)
47 {
48 enumerator_t *enumerator;
49 imv_session_t *current, *session = NULL;
50 time_t created;
51
52 this->mutex->lock(this->mutex);
53
54 /* check if a session has already been assigned */
55 enumerator = this->sessions->create_enumerator(this->sessions);
56 while (enumerator->enumerate(enumerator, &current))
57 {
58 if (conn_id == current->get_connection_id(current))
59 {
60 session = current;
61 break;
62 }
63 }
64 enumerator->destroy(enumerator);
65
66 /* session already exists */
67 if (session)
68 {
69 this->mutex->unlock(this->mutex);
70 return session->get_ref(session);
71 }
72
73 /* create a new session entry */
74 created = time(NULL);
75 session = imv_session_create(conn_id, created, ar_id_type, ar_id_value);
76 this->sessions->insert_last(this->sessions, session);
77
78 this->mutex->unlock(this->mutex);
79
80 return session;
81 }
82
83 METHOD(imv_session_manager_t, remove_session, void,
84 private_imv_session_manager_t *this, imv_session_t *session)
85 {
86 enumerator_t *enumerator;
87 imv_session_t *current;
88
89 this->mutex->lock(this->mutex);
90 enumerator = this->sessions->create_enumerator(this->sessions);
91 while (enumerator->enumerate(enumerator, &current))
92 {
93 if (current == session)
94 {
95 this->sessions->remove_at(this->sessions, enumerator);
96 break;
97 }
98 }
99 enumerator->destroy(enumerator);
100 this->mutex->unlock(this->mutex);
101 }
102
103 METHOD(imv_session_manager_t, destroy, void,
104 private_imv_session_manager_t *this)
105 {
106 this->sessions->destroy_offset(this->sessions,
107 offsetof(imv_session_t, destroy));
108 this->mutex->destroy(this->mutex);
109 free(this);
110 }
111
112 /**
113 * See header
114 */
115 imv_session_manager_t *imv_session_manager_create(void)
116 {
117 private_imv_session_manager_t *this;
118
119 INIT(this,
120 .public = {
121 .add_session = _add_session,
122 .remove_session = _remove_session,
123 .destroy = _destroy,
124 },
125 .sessions = linked_list_create(),
126 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
127 );
128
129 return &this->public;
130 }
131