added a dispatcher class to receive HA sync messages
[strongswan.git] / src / charon / plugins / ha_sync / ha_sync_dispatcher.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * 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 * $Id$
16 */
17
18 #include "ha_sync_dispatcher.h"
19
20 #include <daemon.h>
21 #include <processing/jobs/callback_job.h>
22
23 typedef struct private_ha_sync_dispatcher_t private_ha_sync_dispatcher_t;
24
25 /**
26 * Private data of an ha_sync_dispatcher_t object.
27 */
28 struct private_ha_sync_dispatcher_t {
29
30 /**
31 * Public ha_sync_dispatcher_t interface.
32 */
33 ha_sync_dispatcher_t public;
34
35 /**
36 * socket to pull messages from
37 */
38 ha_sync_socket_t *socket;
39
40 /**
41 * Dispatcher job
42 */
43 callback_job_t *job;
44 };
45
46 /**
47 * Process messages of type IKE_ADD
48 */
49 static void process_ike_add(private_ha_sync_dispatcher_t *this,
50 ha_sync_message_t *message)
51 {
52 ha_sync_message_attribute_t attribute;
53 ha_sync_message_value_t value;
54 enumerator_t *enumerator;
55
56 enumerator = message->create_attribute_enumerator(message);
57 while (enumerator->enumerate(enumerator, &attribute, &value))
58 {
59 switch (attribute)
60 {
61 /* ike_sa_id_t* */
62 case HA_SYNC_IKE_ID:
63 case HA_SYNC_IKE_REKEY_ID:
64 DBG1(DBG_IKE, " %d -> %llu:%llu %s", attribute,
65 value.ike_sa_id->get_initiator_spi(value.ike_sa_id),
66 value.ike_sa_id->get_responder_spi(value.ike_sa_id),
67 value.ike_sa_id->is_initiator(value.ike_sa_id) ?
68 "initiator" : "responder");
69 break;
70 /* identification_t* */
71 case HA_SYNC_LOCAL_ID:
72 case HA_SYNC_REMOTE_ID:
73 case HA_SYNC_EAP_ID:
74 DBG1(DBG_IKE, " %d -> %D", attribute, value.id);
75 break;
76 /* host_t* */
77 case HA_SYNC_LOCAL_ADDR:
78 case HA_SYNC_REMOTE_ADDR:
79 case HA_SYNC_LOCAL_VIP:
80 case HA_SYNC_REMOTE_VIP:
81 case HA_SYNC_ADDITIONAL_ADDR:
82 DBG1(DBG_IKE, " %d -> %H", attribute, value.host);
83 break;
84 /* char* */
85 case HA_SYNC_CONFIG_NAME:
86 DBG1(DBG_IKE, " %d -> %s", attribute, value.str);
87 break;
88 /** u_int32_t */
89 case HA_SYNC_CONDITIONS:
90 case HA_SYNC_EXTENSIONS:
91 DBG1(DBG_IKE, " %d -> %lu", attribute, value.u32);
92 break;
93 /** chunk_t */
94 case HA_SYNC_NONCE_I:
95 case HA_SYNC_NONCE_R:
96 case HA_SYNC_SECRET:
97 DBG1(DBG_IKE, " %d -> %B", attribute, &value.chunk);
98 break;
99 }
100 }
101 enumerator->destroy(enumerator);
102 }
103
104 /**
105 * Dispatcher job function
106 */
107 static job_requeue_t dispatch(private_ha_sync_dispatcher_t *this)
108 {
109 ha_sync_message_t *message;
110
111 message = this->socket->pull(this->socket);
112 switch (message->get_type(message))
113 {
114 case HA_SYNC_IKE_ADD:
115 process_ike_add(this, message);
116 break;
117 case HA_SYNC_IKE_UPDATE:
118 break;
119 case HA_SYNC_IKE_DELETE:
120 break;
121 case HA_SYNC_IKE_REKEY:
122 break;
123 case HA_SYNC_CHILD_ADD:
124 break;
125 case HA_SYNC_CHILD_DELETE:
126 break;
127 default:
128 DBG1(DBG_CFG, "received unknown HA sync message type %d",
129 message->get_type(message));
130 break;
131 }
132 message->destroy(message);
133
134 return JOB_REQUEUE_DIRECT;
135 }
136
137 /**
138 * Implementation of ha_sync_dispatcher_t.destroy.
139 */
140 static void destroy(private_ha_sync_dispatcher_t *this)
141 {
142 this->job->cancel(this->job);
143 free(this);
144 }
145
146 /**
147 * See header
148 */
149 ha_sync_dispatcher_t *ha_sync_dispatcher_create(ha_sync_socket_t *socket)
150 {
151 private_ha_sync_dispatcher_t *this = malloc_thing(private_ha_sync_dispatcher_t);
152
153 this->public.destroy = (void(*)(ha_sync_dispatcher_t*))destroy;
154
155 this->socket = socket;
156 this->job = callback_job_create((callback_job_cb_t)dispatch,
157 this, NULL, NULL);
158 charon->processor->queue_job(charon->processor, (job_t*)this->job);
159
160 return &this->public;
161 }