aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/encoding/payloads/endpoint_notify.c
blob: 98bfb2ea0b1d621eb1a43e58bf1b070c7be92cdb (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
 * @file endpoint_notify.c
 * 
 * @brief Implementation of endpoint_notify_t.
 * 
 */

/*
 * Copyright (C) 2007 Tobias Brunner
 * 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 "endpoint_notify.h"

#include <math.h>

#include <daemon.h>

typedef struct private_endpoint_notify_t private_endpoint_notify_t;

/**
 * Private data of an notify_payload_t object.
 * 
 */
struct private_endpoint_notify_t {
	/**
	 * Public endpoint_notify_t interface.
	 */
	endpoint_notify_t public;
	
	/**
	 * Priority
	 */
	u_int32_t priority;
	
	/**
	 * Family
	 */
	p2p_endpoint_family_t family;
		
	/**
	 * Endpoint type
	 */
	p2p_endpoint_type_t type;
	
	/**
	 * Endpoint
	 */
	host_t *endpoint;
	
	/**
	 * Base (used for server reflexive endpoints)
	 */
	host_t *base;
};

/*    Notification data:
                           1                   2                   3
       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
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      !                           Priority                            !
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      !     Family    !      Type     !              Port             !
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      !                       IP Address (variable)                   
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

ENUM(p2p_endpoint_type_names, HOST, RELAYED,
	"HOST",
	"SERVER_REFLEXIVE",
	"PEER_REFLEXIVE",
	"RELAYED"
);

/**
 * Helper functions to parse integer values
 */
static status_t parse_uint8(u_int8_t **cur, u_int8_t *top, u_int8_t *val)
{
	if (*cur + sizeof(u_int8_t) > top)
	{
		return FAILED;
	}
	*val =  *(u_int8_t*)*cur;
	*cur += sizeof(u_int8_t);
	return SUCCESS;
}

static status_t parse_uint16(u_int8_t **cur, u_int8_t *top, u_int16_t *val)
{
	if (*cur + sizeof(u_int16_t) > top)
	{
		return FAILED;
	}
	*val =  ntohs(*(u_int16_t*)*cur);
	*cur += sizeof(u_int16_t);
	return SUCCESS;
}

static status_t parse_uint32(u_int8_t **cur, u_int8_t *top, u_int32_t *val)
{
	if (*cur + sizeof(u_int32_t) > top)
	{
		return FAILED;
	}
	*val =  ntohl(*(u_int32_t*)*cur);
	*cur += sizeof(u_int32_t);
	return SUCCESS;
}

/**
 * Parses the notification data of a P2P_ENDPOINT notify
 */
static status_t parse_notification_data(private_endpoint_notify_t *this, chunk_t data)
{
	u_int8_t family, type, addr_family;
	u_int16_t port;
	chunk_t addr;
	u_int8_t *cur = data.ptr;
	u_int8_t *top = data.ptr + data.len;
	
	DBG3(DBG_IKE, "p2p_endpoint_data %B", &data);
	
	if (parse_uint32(&cur, top, &this->priority) != SUCCESS)
	{
		DBG1(DBG_IKE, "failed to parse P2P_ENDPOINT: invalid priority");
		return FAILED;
	}

	if (parse_uint8(&cur, top, &family) != SUCCESS || family >= MAX_FAMILY)
	{
		DBG1(DBG_IKE, "failed to parse P2P_ENDPOINT: invalid family");
		return FAILED;
	}
	
	this->family = (p2p_endpoint_family_t)family;
	
	if (parse_uint8(&cur, top, &type) != SUCCESS || type >= MAX_TYPE)
	{
		DBG1(DBG_IKE, "failed to parse P2P_ENDPOINT: invalid type");
		return FAILED;
	}
	
	this->type = (p2p_endpoint_type_t)type;
	
	addr_family = AF_INET;
	addr.len = 4;
	
	switch(this->family)
	{
		case IPv6:
			addr_family = AF_INET6;
			addr.len = 16;
			/* fall-through */
		case IPv4:
			if (parse_uint16(&cur, top, &port) != SUCCESS)
			{
				DBG1(DBG_IKE, "failed to parse P2P_ENDPOINT: invalid port");
				return FAILED;	
			}
			
			if (cur + addr.len > top)
			{
				DBG1(DBG_IKE, "failed to parse P2P_ENDPOINT: invalid IP address");
				return FAILED;
			}
			
			addr.ptr = cur;
			
			this->endpoint = host_create_from_chunk(addr_family, addr, port);
			break;
		case NO_FAMILY:
		default:
			this->endpoint = NULL;
			break;
	}	
	return SUCCESS;
}


/**
 * Generates the notification data of a P2P_ENDPOINT notify
 */
static chunk_t build_notification_data(private_endpoint_notify_t *this)
{
	chunk_t prio_chunk, family_chunk, type_chunk, port_chunk, addr_chunk;
	chunk_t data;
	u_int32_t prio;
	u_int16_t port;
	u_int8_t family, type;
	
	prio = htonl(this->priority);
	prio_chunk = chunk_from_thing(prio);
	family = this->family;
	family_chunk = chunk_from_thing(family);
	type = this->type;
	type_chunk = chunk_from_thing(type);
	
	if (this->endpoint)
	{
		port = htons(this->endpoint->get_port(this->endpoint));
		addr_chunk = this->endpoint->get_address(this->endpoint);
	}
	else
	{
		port = 0;
		addr_chunk = chunk_empty; 
	}
	port_chunk = chunk_from_thing(port);
	
	/* data = prio | family | type | port | addr */
	data = chunk_cat("ccccc", prio_chunk, family_chunk, type_chunk,
			port_chunk, addr_chunk);
	DBG3(DBG_IKE, "p2p_endpoint_data %B", &data);
	
	return data;
}

/**
 * Implementation of endpoint_notify_t.build_notify
 */
static notify_payload_t *build_notify(private_endpoint_notify_t *this)
{
	chunk_t data;
	notify_payload_t *notify;	
	
	notify = notify_payload_create();
	notify->set_notify_type(notify, P2P_ENDPOINT);
	data = build_notification_data(this);
	notify->set_notification_data(notify, data);
	chunk_free(&data);
	
	return notify;
}

/**
 * Implementation of endpoint_notify_t.get_priority.
 */
static u_int32_t get_priority(private_endpoint_notify_t *this)
{
	return this->priority;
}

/**
 * Implementation of endpoint_notify_t.set_priority.
 */
static void set_priority(private_endpoint_notify_t *this, u_int32_t priority)
{
	this->priority = priority;
}

/**
 * Implementation of endpoint_notify_t.get_type.
 */
static p2p_endpoint_type_t get_type(private_endpoint_notify_t *this)
{
	return this->type;
}

/**
 * Implementation of endpoint_notify_t.get_family.
 */
static p2p_endpoint_family_t get_family(private_endpoint_notify_t *this)
{
	return this->family;
}

/**
 * Implementation of endpoint_notify_t.get_host.
 */
static host_t *get_host(private_endpoint_notify_t *this)
{
	return this->endpoint;
}

/**
 * Implementation of endpoint_notify_t.get_base.
 */
static host_t *get_base(private_endpoint_notify_t *this)
{
	return (!this->base) ? this->endpoint : this->base;
}

/**
 * Implementation of endpoint_notify_t.clone.
 */
static endpoint_notify_t *_clone(private_endpoint_notify_t *this)
{
	private_endpoint_notify_t *clone = (private_endpoint_notify_t*)endpoint_notify_create();
	
	clone->priority = this->priority;
	clone->type = this->type;
	clone->family = this->family;
	if (this->endpoint)
	{
		clone->endpoint = this->endpoint->clone(this->endpoint);
	}
	
	if (this->base)
	{
		clone->base = this->base->clone(this->base);
	}
	
	return &clone->public;
}

/**
 * Implementation of endpoint_notify_t.destroy.
 */
static status_t destroy(private_endpoint_notify_t *this)
{
	DESTROY_IF(this->endpoint);
	free(this);
	return SUCCESS;
}

/*
 * Described in header
 */
endpoint_notify_t *endpoint_notify_create()
{
	private_endpoint_notify_t *this = malloc_thing(private_endpoint_notify_t);

	/* public functions */
	this->public.get_priority = (u_int32_t (*) (endpoint_notify_t *)) get_priority;
	this->public.set_priority = (void (*) (endpoint_notify_t *, u_int32_t)) set_priority;
	this->public.get_type = (p2p_endpoint_type_t (*) (endpoint_notify_t *)) get_type;
	this->public.get_family = (p2p_endpoint_family_t (*) (endpoint_notify_t *)) get_family;
	this->public.get_host = (host_t *(*) (endpoint_notify_t *)) get_host;
	this->public.get_base = (host_t *(*) (endpoint_notify_t *)) get_base;
	this->public.build_notify = (notify_payload_t *(*) (endpoint_notify_t *)) build_notify;
	this->public.clone = (endpoint_notify_t *(*) (endpoint_notify_t *)) _clone;
	this->public.destroy = (void (*) (endpoint_notify_t *)) destroy;
	
	/* set default values of the fields */
	this->priority = 0;
	this->family = NO_FAMILY;
	this->type = NO_TYPE;
	this->endpoint = NULL;
	this->base = NULL;
	
	return &this->public;
}

/**
 * Described in header
 */
endpoint_notify_t *endpoint_notify_create_from_host(p2p_endpoint_type_t type, host_t *host, host_t *base)
{
	private_endpoint_notify_t *this = (private_endpoint_notify_t*)endpoint_notify_create();
	
	this->type = type;
	
	switch(type)
	{
		case HOST:
			this->priority = pow(2, 16) * P2P_PRIO_HOST; 
			break;
		case SERVER_REFLEXIVE:
			this->priority = pow(2, 16) * P2P_PRIO_SERVER; 
			break;
		case PEER_REFLEXIVE:
			this->priority = pow(2, 16) * P2P_PRIO_PEER; 
			break;
		case RELAYED:
		default:
			this->priority = pow(2, 16) * P2P_PRIO_RELAY; 
			break;
	}
	
	this->priority += 65535;
	
	if (!host)
	{
		return &this->public;
	}
	
	switch(host->get_family(host))
	{
		case AF_INET:
			this->family = IPv4;
			break;
		case AF_INET6:
			this->family = IPv6;
			break;
		default:
			/* unsupported family type, we do not set the hsot
			 * (family is set to NO_FAMILY) */
			return &this->public;
	}
	
	this->endpoint = host->clone(host);
	
	if (base)
	{
		this->base = base->clone(base);
	}
	
	return &this->public;
}

/**
 * Described in header
 */
endpoint_notify_t *endpoint_notify_create_from_payload(notify_payload_t *notify)
{
	if (notify->get_notify_type(notify) != P2P_ENDPOINT)
	{
		return NULL;
	}
	
	private_endpoint_notify_t *this = (private_endpoint_notify_t*)endpoint_notify_create();
	chunk_t data = notify->get_notification_data(notify);
	if (parse_notification_data(this, data) != SUCCESS)
	{
		destroy(this);
		return NULL;
	}
	return &this->public;
}