2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:gstnetbuffer
22 * @short_description: Buffer for use in network sources and sinks
24 * #GstNetBuffer is a subclass of a normal #GstBuffer that contains two
25 * additional metadata fields of type #GstNetAddress named 'to' and 'from'. The
26 * buffer can be used to store additional information about the origin of the
27 * buffer data and is used in various network elements to track the to and from
30 * Last reviewed on 2006-08-21 (0.10.10)
35 #include "gstnetbuffer.h"
37 static void gst_netbuffer_init (GTypeInstance * instance, gpointer g_class);
38 static void gst_netbuffer_class_init (gpointer g_class, gpointer class_data);
39 static void gst_netbuffer_finalize (GstNetBuffer * nbuf);
40 static GstNetBuffer *gst_netbuffer_copy (GstNetBuffer * nbuf);
42 static GstBufferClass *parent_class;
45 gst_netbuffer_get_type (void)
47 static GType _gst_netbuffer_type = 0;
49 if (G_UNLIKELY (_gst_netbuffer_type == 0)) {
50 static const GTypeInfo netbuffer_info = {
51 sizeof (GstNetBufferClass),
54 gst_netbuffer_class_init,
57 sizeof (GstNetBuffer),
63 _gst_netbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
64 "GstNetBuffer", &netbuffer_info, 0);
66 return _gst_netbuffer_type;
70 gst_netbuffer_class_init (gpointer g_class, gpointer class_data)
72 GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (g_class);
74 parent_class = g_type_class_peek_parent (g_class);
76 mo_class->copy = (GstMiniObjectCopyFunction) gst_netbuffer_copy;
77 mo_class->finalize = (GstMiniObjectFinalizeFunction) gst_netbuffer_finalize;
81 gst_netbuffer_init (GTypeInstance * instance, gpointer g_class)
86 gst_netbuffer_finalize (GstNetBuffer * nbuf)
88 GST_MINI_OBJECT_CLASS (parent_class)->finalize (GST_MINI_OBJECT (nbuf));
92 gst_netbuffer_copy (GstNetBuffer * nbuf)
96 copy = gst_netbuffer_new ();
98 /* we simply copy everything from our parent */
99 GST_BUFFER_DATA (copy) =
100 g_memdup (GST_BUFFER_DATA (nbuf), GST_BUFFER_SIZE (nbuf));
101 /* make sure it gets freed (even if the parent is subclassed, we return a
103 GST_BUFFER_MALLOCDATA (copy) = GST_BUFFER_DATA (copy);
104 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (nbuf);
106 memcpy (©->to, &nbuf->to, sizeof (nbuf->to));
107 memcpy (©->from, &nbuf->from, sizeof (nbuf->from));
110 gst_buffer_copy_metadata (GST_BUFFER_CAST (copy),
111 GST_BUFFER_CAST (nbuf), GST_BUFFER_COPY_ALL);
119 * Create a new network buffer.
121 * Returns: a new #GstNetBuffer.
124 gst_netbuffer_new (void)
128 buf = (GstNetBuffer *) gst_mini_object_new (GST_TYPE_NETBUFFER);
134 * gst_netaddress_set_ip4_address:
135 * @naddr: a network address
136 * @address: an IPv4 network address.
137 * @port: a port number to set.
139 * Set @naddr with the IPv4 @address and @port pair.
141 * Note that @port and @address must be expressed in network byte order,
142 * use g_htons() and g_htonl() to convert them to network byte order.
145 gst_netaddress_set_ip4_address (GstNetAddress * naddr, guint32 address,
148 g_return_if_fail (naddr != NULL);
150 naddr->type = GST_NET_TYPE_IP4;
151 naddr->address.ip4 = address;
156 * gst_netaddress_set_ip6_address:
157 * @naddr: a network address
158 * @address: an IPv6 network address.
159 * @port: a port number to set.
161 * Set @naddr with the IPv6 @address and @port pair.
163 * Note that @port must be expressed in network byte order, use g_htons() to convert
164 * it to network byte order.
167 gst_netaddress_set_ip6_address (GstNetAddress * naddr, guint8 address[16],
170 g_return_if_fail (naddr != NULL);
172 naddr->type = GST_NET_TYPE_IP6;
173 memcpy (&naddr->address.ip6, address, 16);
178 * gst_netaddress_get_net_type:
179 * @naddr: a network address
181 * Get the type of address stored in @naddr.
183 * Returns: the network type stored in @naddr.
186 gst_netaddress_get_net_type (GstNetAddress * naddr)
188 g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
194 * gst_netaddress_get_ip4_address:
195 * @naddr: a network address
196 * @address: a location to store the address.
197 * @port: a location to store the port.
199 * Get the IPv4 address stored in @naddr into @address. This function requires
200 * that the address type of @naddr is of type #GST_NET_TYPE_IP4.
202 * Note that @port and @address are expressed in network byte order, use
203 * g_ntohs() and g_ntohl() to convert them to host order.
205 * Returns: TRUE if the address could be retrieved.
208 gst_netaddress_get_ip4_address (GstNetAddress * naddr, guint32 * address,
211 g_return_val_if_fail (naddr != NULL, FALSE);
213 if (naddr->type == GST_NET_TYPE_UNKNOWN || naddr->type == GST_NET_TYPE_IP6)
217 *address = naddr->address.ip4;
225 * gst_netaddress_get_ip6_address:
226 * @naddr: a network address
227 * @address: a location to store the result.
228 * @port: a location to store the port.
230 * Get the IPv6 address stored in @naddr into @address.
232 * If @naddr is of type GST_NET_TYPE_IP4, the transitional IP6 address is
235 * Note that @port is expressed in network byte order, use g_ntohs() to convert
238 * Returns: TRUE if the address could be retrieved.
241 gst_netaddress_get_ip6_address (GstNetAddress * naddr, guint8 address[16],
244 static guint8 ip4_transition[16] =
245 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
246 g_return_val_if_fail (naddr != NULL, FALSE);
248 if (naddr->type == GST_NET_TYPE_UNKNOWN)
252 if (naddr->type == GST_NET_TYPE_IP6) {
253 memcpy (address, naddr->address.ip6, 16);
254 } else { /* naddr->type == GST_NET_TYPE_IP4 */
255 memcpy (address, ip4_transition, 12);
256 memcpy (address + 12, (guint8 *) & (naddr->address.ip4), 4);
266 * gst_netaddress_get_address_bytes:
267 * @naddr: a network address
268 * @address: a location to store the result.
269 * @port: a location to store the port.
271 * Get just the address bytes stored in @naddr into @address.
273 * Note that @port is expressed in network byte order, use g_ntohs() to convert
274 * it to host order. IP4 addresses are also stored in network byte order.
276 * Returns: number of bytes actually copied
281 gst_netaddress_get_address_bytes (GstNetAddress * naddr, guint8 address[16],
286 g_return_val_if_fail (naddr != NULL, FALSE);
288 if (naddr->type == GST_NET_TYPE_UNKNOWN)
292 if (naddr->type == GST_NET_TYPE_IP6) {
293 memcpy (address, naddr->address.ip6, 16);
295 } else { /* naddr->type == GST_NET_TYPE_IP4 */
296 memcpy (address, (guint8 *) & (naddr->address.ip4), 4);
307 * gst_netaddress_set_address_bytes:
308 * @naddr: a network address
309 * @address: a location to store the result.
310 * @port: a location to store the port.
312 * Set just the address bytes stored in @naddr into @address.
314 * Note that @port must be expressed in network byte order, use g_htons() to convert
315 * it to network byte order order. IP4 address bytes must also be stored in
316 * network byte order.
318 * Returns: number of bytes actually copied
323 gst_netaddress_set_address_bytes (GstNetAddress * naddr, GstNetType type,
324 guint8 address[16], guint16 port)
328 g_return_val_if_fail (naddr != NULL, 0);
331 switch (naddr->type) {
332 case GST_NET_TYPE_UNKNOWN:
333 case GST_NET_TYPE_IP6:
335 memcpy (naddr->address.ip6, address, 16);
337 case GST_NET_TYPE_IP4:
339 memcpy ((guint8 *) & (naddr->address.ip4), address, 4);
350 * gst_netaddress_equal:
351 * @naddr1: The first #GstNetAddress
352 * @naddr2: The second #GstNetAddress
354 * Compare two #GstNetAddress structures
356 * Returns: TRUE if they are identical, FALSE otherwise
361 gst_netaddress_equal (const GstNetAddress * naddr1,
362 const GstNetAddress * naddr2)
364 g_return_val_if_fail (naddr1 != NULL, FALSE);
365 g_return_val_if_fail (naddr2 != NULL, FALSE);
367 if (naddr1->type != naddr2->type)
370 if (naddr1->port != naddr2->port)
373 switch (naddr1->type) {
374 case GST_NET_TYPE_IP4:
375 if (naddr1->address.ip4 != naddr2->address.ip4)
378 case GST_NET_TYPE_IP6:
379 if (memcmp (naddr1->address.ip6, naddr2->address.ip6,
380 sizeof (naddr1->address.ip6)))