summaryrefslogtreecommitdiff
path: root/src/libimcv/imv/imv_session.c
blob: 754f1f74c607bacc1a1999ae3db326923f9d4646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2013 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "imv_session.h"

#include <utils/debug.h>

typedef struct private_imv_session_t private_imv_session_t;

/**
 * Private data of a imv_session_t object.
 */
struct private_imv_session_t {

	/**
	 * Public imv_session_t interface.
	 */
	imv_session_t public;

	/**
	 * Unique Session ID
	 */
	int session_id;

	/**
	 * TNCCS connection ID
	 */
	TNC_ConnectionID conn_id;

	/**
	 * Have the workitems been generated?
	 */
	bool policy_started;

	/**
	 * List of worklist items
	 */
	linked_list_t *workitems;

	/**
	 * Reference count
	 */
	refcount_t ref;

};

METHOD(imv_session_t, get_session_id, int,
	private_imv_session_t *this)
{
	return this->session_id;
}

METHOD(imv_session_t, get_connection_id, TNC_ConnectionID,
	private_imv_session_t *this)
{
	return this->conn_id;
}

METHOD(imv_session_t, set_policy_started, void,
	private_imv_session_t *this, bool start)
{
	this->policy_started = start;
}

METHOD(imv_session_t, get_policy_started, bool,
	private_imv_session_t *this)
{
	return this->policy_started;
}

METHOD(imv_session_t, insert_workitem, void,
	private_imv_session_t *this, imv_workitem_t *workitem)
{
	this->workitems->insert_last(this->workitems, workitem);
}

METHOD(imv_session_t, remove_workitem, void,
	private_imv_session_t *this, enumerator_t *enumerator)
{
	this->workitems->remove_at(this->workitems, enumerator);
}

METHOD(imv_session_t, create_workitem_enumerator, enumerator_t*,
	private_imv_session_t *this)
{
	if (!this->policy_started)
	{
		return NULL;
	}
	return this->workitems->create_enumerator(this->workitems);
}

METHOD(imv_session_t, get_workitem_count, int,
	private_imv_session_t *this, TNC_IMVID imv_id)
{
	enumerator_t *enumerator;
	imv_workitem_t *workitem;
	int count = 0;

	enumerator = this->workitems->create_enumerator(this->workitems);
	while (enumerator->enumerate(enumerator, &workitem))
	{
		if (workitem->get_imv_id(workitem) == imv_id)
		{
			count++;
		}
	}
	enumerator->destroy(enumerator);

	return count;
}

METHOD(imv_session_t, get_ref, imv_session_t*,
	private_imv_session_t *this)
{
	ref_get(&this->ref);

	return &this->public;
}

METHOD(imv_session_t, destroy, void,
	private_imv_session_t *this)
{
	if (ref_put(&this->ref))
	{
		this->workitems->destroy_offset(this->workitems,
								 offsetof(imv_workitem_t, destroy));
		free(this);
	}
}

/**
 * See header
 */
imv_session_t *imv_session_create(int session_id, TNC_ConnectionID conn_id)
{
	private_imv_session_t *this;

	INIT(this,
		.public = {
			.get_session_id = _get_session_id,
			.get_connection_id = _get_connection_id,
			.set_policy_started = _set_policy_started,
			.get_policy_started = _get_policy_started,
			.insert_workitem = _insert_workitem,
			.remove_workitem = _remove_workitem,
			.create_workitem_enumerator = _create_workitem_enumerator,
			.get_workitem_count = _get_workitem_count,
			.get_ref = _get_ref,
			.destroy = _destroy,
		},
		.session_id = session_id,
		.conn_id = conn_id,
		.workitems = linked_list_create(),
		.ref = 1,
	);

	return &this->public;
}