updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / endpoint_notify.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
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 "endpoint_notify.h"
19
20 #include <math.h>
21
22 #include <daemon.h>
23
24 typedef struct private_endpoint_notify_t private_endpoint_notify_t;
25
26 /**
27 * Private data of an notify_payload_t object.
28 *
29 */
30 struct private_endpoint_notify_t {
31 /**
32 * Public endpoint_notify_t interface.
33 */
34 endpoint_notify_t public;
35
36 /**
37 * Priority
38 */
39 u_int32_t priority;
40
41 /**
42 * Family
43 */
44 me_endpoint_family_t family;
45
46 /**
47 * Endpoint type
48 */
49 me_endpoint_type_t type;
50
51 /**
52 * Endpoint
53 */
54 host_t *endpoint;
55
56 /**
57 * Base (used for server reflexive endpoints)
58 */
59 host_t *base;
60 };
61
62 /* Notification data:
63 1 2 3
64 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
65 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 ! Priority !
67 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 ! Family ! Type ! Port !
69 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 ! IP Address (variable)
71 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 */
73
74 ENUM(me_endpoint_type_names, HOST, RELAYED,
75 "HOST",
76 "PEER_REFLEXIVE",
77 "SERVER_REFLEXIVE",
78 "RELAYED"
79 );
80
81 /**
82 * Helper functions to parse integer values
83 */
84 static status_t parse_uint8(u_int8_t **cur, u_int8_t *top, u_int8_t *val)
85 {
86 if (*cur + sizeof(u_int8_t) > top)
87 {
88 return FAILED;
89 }
90 *val = *(u_int8_t*)*cur;
91 *cur += sizeof(u_int8_t);
92 return SUCCESS;
93 }
94
95 static status_t parse_uint16(u_int8_t **cur, u_int8_t *top, u_int16_t *val)
96 {
97 if (*cur + sizeof(u_int16_t) > top)
98 {
99 return FAILED;
100 }
101 *val = ntohs(*(u_int16_t*)*cur);
102 *cur += sizeof(u_int16_t);
103 return SUCCESS;
104 }
105
106 static status_t parse_uint32(u_int8_t **cur, u_int8_t *top, u_int32_t *val)
107 {
108 if (*cur + sizeof(u_int32_t) > top)
109 {
110 return FAILED;
111 }
112 *val = ntohl(*(u_int32_t*)*cur);
113 *cur += sizeof(u_int32_t);
114 return SUCCESS;
115 }
116
117 /**
118 * Parses the notification data of a ME_ENDPOINT notify
119 */
120 static status_t parse_notification_data(private_endpoint_notify_t *this, chunk_t data)
121 {
122 u_int8_t family, type, addr_family;
123 u_int16_t port;
124 chunk_t addr;
125 u_int8_t *cur = data.ptr;
126 u_int8_t *top = data.ptr + data.len;
127
128 DBG3(DBG_IKE, "me_endpoint_data %B", &data);
129
130 if (parse_uint32(&cur, top, &this->priority) != SUCCESS)
131 {
132 DBG1(DBG_IKE, "failed to parse ME_ENDPOINT: invalid priority");
133 return FAILED;
134 }
135
136 if (parse_uint8(&cur, top, &family) != SUCCESS || family >= MAX_FAMILY)
137 {
138 DBG1(DBG_IKE, "failed to parse ME_ENDPOINT: invalid family");
139 return FAILED;
140 }
141
142 this->family = (me_endpoint_family_t)family;
143
144 if (parse_uint8(&cur, top, &type) != SUCCESS || type >= MAX_TYPE)
145 {
146 DBG1(DBG_IKE, "failed to parse ME_ENDPOINT: invalid type");
147 return FAILED;
148 }
149
150 this->type = (me_endpoint_type_t)type;
151
152 addr_family = AF_INET;
153 addr.len = 4;
154
155 switch(this->family)
156 {
157 case IPv6:
158 addr_family = AF_INET6;
159 addr.len = 16;
160 /* fall-through */
161 case IPv4:
162 if (parse_uint16(&cur, top, &port) != SUCCESS)
163 {
164 DBG1(DBG_IKE, "failed to parse ME_ENDPOINT: invalid port");
165 return FAILED;
166 }
167
168 if (cur + addr.len > top)
169 {
170 DBG1(DBG_IKE, "failed to parse ME_ENDPOINT: invalid IP address");
171 return FAILED;
172 }
173
174 addr.ptr = cur;
175
176 this->endpoint = host_create_from_chunk(addr_family, addr, port);
177 break;
178 case NO_FAMILY:
179 default:
180 this->endpoint = NULL;
181 break;
182 }
183 return SUCCESS;
184 }
185
186
187 /**
188 * Generates the notification data of a ME_ENDPOINT notify
189 */
190 static chunk_t build_notification_data(private_endpoint_notify_t *this)
191 {
192 chunk_t prio_chunk, family_chunk, type_chunk, port_chunk, addr_chunk;
193 chunk_t data;
194 u_int32_t prio;
195 u_int16_t port;
196 u_int8_t family, type;
197
198 prio = htonl(this->priority);
199 prio_chunk = chunk_from_thing(prio);
200 family = this->family;
201 family_chunk = chunk_from_thing(family);
202 type = this->type;
203 type_chunk = chunk_from_thing(type);
204
205 if (this->endpoint)
206 {
207 port = htons(this->endpoint->get_port(this->endpoint));
208 addr_chunk = this->endpoint->get_address(this->endpoint);
209 }
210 else
211 {
212 port = 0;
213 addr_chunk = chunk_empty;
214 }
215 port_chunk = chunk_from_thing(port);
216
217 /* data = prio | family | type | port | addr */
218 data = chunk_cat("ccccc", prio_chunk, family_chunk, type_chunk,
219 port_chunk, addr_chunk);
220 DBG3(DBG_IKE, "me_endpoint_data %B", &data);
221
222 return data;
223 }
224
225 /**
226 * Implementation of endpoint_notify_t.build_notify
227 */
228 static notify_payload_t *build_notify(private_endpoint_notify_t *this)
229 {
230 chunk_t data;
231 notify_payload_t *notify;
232
233 notify = notify_payload_create();
234 notify->set_notify_type(notify, ME_ENDPOINT);
235 data = build_notification_data(this);
236 notify->set_notification_data(notify, data);
237 chunk_free(&data);
238
239 return notify;
240 }
241
242 /**
243 * Implementation of endpoint_notify_t.get_priority.
244 */
245 static u_int32_t get_priority(private_endpoint_notify_t *this)
246 {
247 return this->priority;
248 }
249
250 /**
251 * Implementation of endpoint_notify_t.set_priority.
252 */
253 static void set_priority(private_endpoint_notify_t *this, u_int32_t priority)
254 {
255 this->priority = priority;
256 }
257
258 /**
259 * Implementation of endpoint_notify_t.get_type.
260 */
261 static me_endpoint_type_t get_type(private_endpoint_notify_t *this)
262 {
263 return this->type;
264 }
265
266 /**
267 * Implementation of endpoint_notify_t.get_family.
268 */
269 static me_endpoint_family_t get_family(private_endpoint_notify_t *this)
270 {
271 return this->family;
272 }
273
274 /**
275 * Implementation of endpoint_notify_t.get_host.
276 */
277 static host_t *get_host(private_endpoint_notify_t *this)
278 {
279 return this->endpoint;
280 }
281
282 /**
283 * Implementation of endpoint_notify_t.get_base.
284 */
285 static host_t *get_base(private_endpoint_notify_t *this)
286 {
287 return (!this->base) ? this->endpoint : this->base;
288 }
289
290 /**
291 * Implementation of endpoint_notify_t.clone.
292 */
293 static endpoint_notify_t *_clone(private_endpoint_notify_t *this)
294 {
295 private_endpoint_notify_t *clone = (private_endpoint_notify_t*)endpoint_notify_create();
296
297 clone->priority = this->priority;
298 clone->type = this->type;
299 clone->family = this->family;
300 if (this->endpoint)
301 {
302 clone->endpoint = this->endpoint->clone(this->endpoint);
303 }
304
305 if (this->base)
306 {
307 clone->base = this->base->clone(this->base);
308 }
309
310 return &clone->public;
311 }
312
313 /**
314 * Implementation of endpoint_notify_t.destroy.
315 */
316 static status_t destroy(private_endpoint_notify_t *this)
317 {
318 DESTROY_IF(this->endpoint);
319 DESTROY_IF(this->base);
320 free(this);
321 return SUCCESS;
322 }
323
324 /*
325 * Described in header
326 */
327 endpoint_notify_t *endpoint_notify_create()
328 {
329 private_endpoint_notify_t *this = malloc_thing(private_endpoint_notify_t);
330
331 /* public functions */
332 this->public.get_priority = (u_int32_t (*) (endpoint_notify_t *)) get_priority;
333 this->public.set_priority = (void (*) (endpoint_notify_t *, u_int32_t)) set_priority;
334 this->public.get_type = (me_endpoint_type_t (*) (endpoint_notify_t *)) get_type;
335 this->public.get_family = (me_endpoint_family_t (*) (endpoint_notify_t *)) get_family;
336 this->public.get_host = (host_t *(*) (endpoint_notify_t *)) get_host;
337 this->public.get_base = (host_t *(*) (endpoint_notify_t *)) get_base;
338 this->public.build_notify = (notify_payload_t *(*) (endpoint_notify_t *)) build_notify;
339 this->public.clone = (endpoint_notify_t *(*) (endpoint_notify_t *)) _clone;
340 this->public.destroy = (void (*) (endpoint_notify_t *)) destroy;
341
342 /* set default values of the fields */
343 this->priority = 0;
344 this->family = NO_FAMILY;
345 this->type = NO_TYPE;
346 this->endpoint = NULL;
347 this->base = NULL;
348
349 return &this->public;
350 }
351
352 /**
353 * Described in header
354 */
355 endpoint_notify_t *endpoint_notify_create_from_host(me_endpoint_type_t type, host_t *host, host_t *base)
356 {
357 private_endpoint_notify_t *this = (private_endpoint_notify_t*)endpoint_notify_create();
358
359 this->type = type;
360
361 switch(type)
362 {
363 case HOST:
364 this->priority = pow(2, 16) * ME_PRIO_HOST;
365 break;
366 case PEER_REFLEXIVE:
367 this->priority = pow(2, 16) * ME_PRIO_PEER;
368 break;
369 case SERVER_REFLEXIVE:
370 this->priority = pow(2, 16) * ME_PRIO_SERVER;
371 break;
372 case RELAYED:
373 default:
374 this->priority = pow(2, 16) * ME_PRIO_RELAY;
375 break;
376 }
377
378 /* FIXME: if there is more than one ip address we should vary this priority */
379 this->priority += 65535;
380
381 if (!host)
382 {
383 return &this->public;
384 }
385
386 switch(host->get_family(host))
387 {
388 case AF_INET:
389 this->family = IPv4;
390 break;
391 case AF_INET6:
392 this->family = IPv6;
393 break;
394 default:
395 /* unsupported family type, we do not set the host
396 * (family is set to NO_FAMILY) */
397 return &this->public;
398 }
399
400 this->endpoint = host->clone(host);
401
402 if (base)
403 {
404 this->base = base->clone(base);
405 }
406
407 return &this->public;
408 }
409
410 /**
411 * Described in header
412 */
413 endpoint_notify_t *endpoint_notify_create_from_payload(notify_payload_t *notify)
414 {
415 if (notify->get_notify_type(notify) != ME_ENDPOINT)
416 {
417 return NULL;
418 }
419
420 private_endpoint_notify_t *this = (private_endpoint_notify_t*)endpoint_notify_create();
421 chunk_t data = notify->get_notification_data(notify);
422 if (parse_notification_data(this, data) != SUCCESS)
423 {
424 destroy(this);
425 return NULL;
426 }
427 return &this->public;
428 }