- revised packet and socket interface
[strongswan.git] / Source / charon / packet.c
1 /**
2 * @file packet.h
3 *
4 * @brief UDP-Packet, contains data, sender and receiver
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "packet.h"
24
25
26 static status_t destroy(packet_t *this)
27 {
28 pfree(this->data.ptr);
29 pfree(this);
30 return SUCCESS;
31 }
32
33 /**
34 * @brief helper function to set address used by set_dest & set_source
35 */
36 static status_t set_addr(int family, struct sockaddr *saddr, char *address, u_int16_t port)
37 {
38 switch (family)
39 {
40 /* IPv4 */
41 case AF_INET:
42 {
43 struct sockaddr_in *sin = (struct sockaddr_in*)saddr;
44 sin->sin_family = AF_INET;
45 sin->sin_addr.s_addr = inet_addr("127.0.0.1");
46 sin->sin_port = htons(port);
47 return SUCCESS;;
48 }
49 }
50 return NOT_SUPPORTED;
51 }
52
53 status_t set_destination(packet_t *this, char *address, u_int16_t port)
54 {
55 struct sockaddr *saddr = &(this->destination);
56 return set_addr(this->family, saddr, address, port);
57 }
58
59 status_t set_source(packet_t *this, char *address, u_int16_t port)
60 {
61 struct sockaddr *saddr = &(this->source);
62 return set_addr(this->family, saddr, address, port);
63 }
64
65
66 packet_t *packet_create(int family)
67 {
68 packet_t *this = alloc_thing(packet_t, "packet_t");
69
70 this->destroy = destroy;
71 this->set_destination = set_destination;
72 this->set_source = set_source;
73
74 this->family = family;
75 switch (family)
76 {
77 case AF_INET:
78 this->sockaddr_len = sizeof(struct sockaddr_in);
79 break;
80 default: /* not supported */
81 pfree(this);
82 return NULL;
83 }
84
85 this->data.len = 0;
86 this->data.ptr = NULL;
87 return this;
88 }