aboutsummaryrefslogtreecommitdiffstats
path: root/src/manager/controller/gateway_controller.c
blob: bdc7792567e64ad299fb4150238f42d5197b5912 (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
/**
 * @file gateway_controller.c
 *
 * @brief Implementation of gateway_controller_t.
 *
 */

/*
 * Copyright (C) 2007 Martin Willi
 * 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 "gateway_controller.h"
#include "../manager.h"
#include "../gateway.h"

#include <library.h>


typedef struct private_gateway_controller_t private_gateway_controller_t;

/**
 * private data of the gateway_controller
 */
struct private_gateway_controller_t {

	/**
	 * public functions
	 */
	gateway_controller_t public;
	
	/**
	 * manager instance
	 */
	manager_t *manager;
	
};

static void list(private_gateway_controller_t *this, request_t *request)
{
	enumerator_t *enumerator;
	char *name, *address;
	int id, port;
	
	enumerator = this->manager->create_gateway_enumerator(this->manager);
	while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
	{
		request->setf(request, "gateways.%d.name=%s", id, name);
		if (port)
		{
			request->setf(request, "gateways.%d.address=tcp://%s:%d",
						  id, address, port);
		}
		else
		{
			request->setf(request, "gateways.%d.address=unix://%s",
						  id, IPSEC_PIDDIR"/charon.xml");
		}
	}
	enumerator->destroy(enumerator);
	request->set(request, "action", "select");
	request->set(request, "title", "Choose gateway");
	request->render(request, "templates/gateway/list.cs");
}

static void _select(private_gateway_controller_t *this, request_t *request)
{
	char *id;
	
	id = request->get_query_data(request, "gateway");
	if (id)
	{
		if (this->manager->select_gateway(this->manager, atoi(id)))
		{
			request->redirect(request, "status/ikesalist");
			return;
		}
	}
	request->redirect(request, "gateway/list");
}

/**
 * Implementation of controller_t.get_name
 */
static char* get_name(private_gateway_controller_t *this)
{
	return "gateway";
}

/**
 * Implementation of controller_t.handle
 */
static void handle(private_gateway_controller_t *this,
				   request_t *request, char *action)
{
	if (!this->manager->logged_in(this->manager))
	{
		return request->redirect(request, "auth/login");
	}
	if (action)
	{
		if (streq(action, "list"))
		{
			return list(this, request);
		}
		else if (streq(action, "select")) 
		{
			return _select(this, request);
		}
	}
	request->redirect(request, "gateway/list");
}


/**
 * Implementation of controller_t.destroy
 */
static void destroy(private_gateway_controller_t *this)
{
	free(this);
}

/*
 * see header file
 */
controller_t *gateway_controller_create(context_t *context, void *param)
{
	private_gateway_controller_t *this = malloc_thing(private_gateway_controller_t);

	this->public.controller.get_name = (char*(*)(controller_t*))get_name;
	this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
	this->public.controller.destroy = (void(*)(controller_t*))destroy;
	
	this->manager = (manager_t*)context;
	
	return &this->public.controller;
}