4 * @brief UDP-Packet, contains data, sender and receiver.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "allocator.h"
28 * Private data of an packet_t object
30 typedef struct private_packet_s private_packet_t
;
32 struct private_packet_s
{
35 * Public part of a packet_t object
39 /* private functions */
42 * @brief helper function to set address used by set_dest & set_source.
44 * @param this calling object_t
45 * @param family address family
46 * @param saddr source address
47 * @param address address as string
49 * - SUCCESS if successfuly
50 * - NOT_SUPPORTED if family is not supported
52 status_t (*set_addr
) (private_packet_t
*this, int family
, struct sockaddr
*saddr
, char *address
, u_int16_t port
);
56 * Implements packet_t's destroy function.
57 * See #packet_s.destroy for description.
59 static status_t
destroy(private_packet_t
*this)
61 if (this->public.data
.ptr
!= NULL
)
63 allocator_free(this->public.data
.ptr
);
70 * Implements packet_t's clone function.
71 * See #packet_s.clone for description.
73 static status_t
clone (private_packet_t
*packet
, packet_t
**clone
)
75 *clone
= packet_create(packet
->public.family
);
83 (*clone
)->sockaddr_len
= packet
->public.sockaddr_len
;
84 (*clone
)->source
= packet
->public.source
;
85 (*clone
)->destination
= packet
->public.destination
;
86 /* only clone existing chunks :-) */
87 if (packet
->public.data
.ptr
!= NULL
)
89 (*clone
)->data
.ptr
= allocator_clone_bytes(packet
->public.data
.ptr
,packet
->public.data
.len
);
90 if ((*clone
)->data
.ptr
== NULL
)
92 (*clone
)->destroy((*clone
));
95 (*clone
)->data
.len
= packet
->public.data
.len
;
101 * Implements private_packet_t's set_addr function.
102 * See #private_packet_t.set_addr for description.
104 static status_t
set_addr(int family
, struct sockaddr
*saddr
, char *address
, u_int16_t port
)
111 struct sockaddr_in
*sin
= (struct sockaddr_in
*)saddr
;
112 sin
->sin_family
= AF_INET
;
113 sin
->sin_addr
.s_addr
= inet_addr("127.0.0.1");
114 sin
->sin_port
= htons(port
);
118 return NOT_SUPPORTED
;
122 * Implements packet_t's set_destination function.
123 * See #packet_t.set_destination for description.
125 static status_t
set_destination(packet_t
*this, char *address
, u_int16_t port
)
127 struct sockaddr
*saddr
= &(this->destination
);
128 return set_addr(this->family
, saddr
, address
, port
);
132 * Implements packet_t's set_source function.
133 * See #packet_t.set_source for description.
135 static status_t
set_source(packet_t
*this, char *address
, u_int16_t port
)
137 struct sockaddr
*saddr
= &(this->source
);
138 return set_addr(this->family
, saddr
, address
, port
);
142 * Documented in header
144 packet_t
*packet_create(int family
)
146 private_packet_t
*this = allocator_alloc_thing(private_packet_t
);
148 this->public.destroy
= (status_t(*) (packet_t
*)) destroy
;
149 this->public.set_destination
= set_destination
;
150 this->public.set_source
= set_source
;
151 this->public.clone
= (status_t(*) (packet_t
*,packet_t
**))clone
;
153 this->public.family
= family
;
157 this->public.sockaddr_len
= sizeof(struct sockaddr_in
);
159 default: /* not supported */
160 allocator_free(this);
164 this->public.data
.len
= 0;
165 this->public.data
.ptr
= NULL
;
166 return &(this->public);