aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/plugins/farp/farp_listener.c
blob: 87c84359cc4b454e71aecc3fe0dab9c013cd5145 (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
172
173
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 *
 * 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 "farp_listener.h"

#include <collections/linked_list.h>
#include <threading/rwlock.h>

typedef struct private_farp_listener_t private_farp_listener_t;

/**
 * Private data of an farp_listener_t object.
 */
struct private_farp_listener_t {

	/**
	 * Public farp_listener_t interface.
	 */
	farp_listener_t public;

	/**
	 * List with entry_t
	 */
	linked_list_t *entries;

	/**
	 * RWlock for IP list
	 */
	rwlock_t *lock;
};

/**
 * Traffic selector cache entry
 */
typedef struct {
	/** list of local selectors */
	linked_list_t *local;
	/** list of remote selectors */
	linked_list_t *remote;
	/** reqid of CHILD_SA */
	u_int32_t reqid;
} entry_t;

METHOD(listener_t, child_updown, bool,
	private_farp_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
	bool up)
{
	enumerator_t *enumerator;
	traffic_selector_t *ts;
	entry_t *entry;

	if (up)
	{
		INIT(entry,
			.local = linked_list_create(),
			.remote = linked_list_create(),
			.reqid = child_sa->get_reqid(child_sa),
		);

		enumerator = child_sa->create_ts_enumerator(child_sa, TRUE);
		while (enumerator->enumerate(enumerator, &ts))
		{
			entry->local->insert_last(entry->local, ts->clone(ts));
		}
		enumerator->destroy(enumerator);

		enumerator = child_sa->create_ts_enumerator(child_sa, FALSE);
		while (enumerator->enumerate(enumerator, &ts))
		{
			entry->remote->insert_last(entry->remote, ts->clone(ts));
		}
		enumerator->destroy(enumerator);

		this->lock->write_lock(this->lock);
		this->entries->insert_last(this->entries, entry);
		this->lock->unlock(this->lock);
	}
	else
	{
		this->lock->write_lock(this->lock);
		enumerator = this->entries->create_enumerator(this->entries);
		while (enumerator->enumerate(enumerator, &entry))
		{
			if (entry->reqid == child_sa->get_reqid(child_sa))
			{
				this->entries->remove_at(this->entries, enumerator);
				entry->local->destroy_offset(entry->local,
										offsetof(traffic_selector_t, destroy));
				entry->remote->destroy_offset(entry->remote,
										offsetof(traffic_selector_t, destroy));
				free(entry);
			}
		}
		enumerator->destroy(enumerator);
		this->lock->unlock(this->lock);
	}
	return TRUE;
}

METHOD(farp_listener_t, has_tunnel, bool,
	private_farp_listener_t *this, host_t *local, host_t *remote)
{
	enumerator_t *entries, *locals, *remotes;
	traffic_selector_t *ts;
	bool found = FALSE;
	entry_t *entry;

	this->lock->read_lock(this->lock);
	entries = this->entries->create_enumerator(this->entries);
	while (!found && entries->enumerate(entries, &entry))
	{
		remotes = entry->remote->create_enumerator(entry->remote);
		while (!found && remotes->enumerate(remotes, &ts))
		{
			if (ts->includes(ts, remote))
			{
				locals = entry->local->create_enumerator(entry->local);
				while (!found && locals->enumerate(locals, &ts))
				{
					found = ts->includes(ts, local);
				}
				locals->destroy(locals);
			}
		}
		remotes->destroy(remotes);
	}
	entries->destroy(entries);
	this->lock->unlock(this->lock);

	return found;
}

METHOD(farp_listener_t, destroy, void,
	private_farp_listener_t *this)
{
	this->entries->destroy(this->entries);
	this->lock->destroy(this->lock);
	free(this);
}

/**
 * See header
 */
farp_listener_t *farp_listener_create()
{
	private_farp_listener_t *this;

	INIT(this,
		.public = {
			.listener = {
				.child_updown = _child_updown,
			},
			.has_tunnel = _has_tunnel,
			.destroy = _destroy,
		},
		.entries = linked_list_create(),
		.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
	);

	return &this->public;
}