aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/packet.c
blob: f67d7a6e4e8f743d27abaf7e692431e1ba4a64fb (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
/**
 * @file packet.h
 * 
 * @brief UDP-Packet, contains data, sender and receiver
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, 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 "packet.h"


static status_t destroy(packet_t *this)
{
	pfree(this->data.ptr);
	pfree(this);
	return SUCCESS;
}

/**
 * @brief helper function to set address used by set_dest & set_source
 */
static status_t set_addr(int family, struct sockaddr *saddr, char *address, u_int16_t port)
{
	switch (family)
	{
		/* IPv4 */
		case AF_INET:
			{
				struct sockaddr_in *sin = (struct sockaddr_in*)saddr;
				sin->sin_family = AF_INET;
				sin->sin_addr.s_addr = inet_addr("127.0.0.1");
				sin->sin_port = htons(port);
				return SUCCESS;;
			}
	}
	return NOT_SUPPORTED;
}

status_t set_destination(packet_t *this, char *address, u_int16_t port)
{
	struct sockaddr *saddr = &(this->destination);
	return set_addr(this->family, saddr, address, port);
}

status_t set_source(packet_t *this, char *address, u_int16_t port)
{
	struct sockaddr *saddr = &(this->source);
	return set_addr(this->family, saddr, address, port);
}


packet_t *packet_create(int family)
{
	packet_t *this = alloc_thing(packet_t, "packet_t");
	
	this->destroy = destroy;
	this->set_destination = set_destination;
	this->set_source = set_source;
		
	this->family = family;
	switch (family)
	{
		case AF_INET:
			this->sockaddr_len = sizeof(struct sockaddr_in);
			break;
		default: /* not supported */
			pfree(this);
			return NULL;	
	}

	this->data.len = 0;
	this->data.ptr = NULL;	
	return this;
}