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_finalize (GstNetBuffer * nbuf);
38 static GstNetBuffer *gst_netbuffer_copy (GstNetBuffer * nbuf);
40 static GstBufferClass *parent_class;
42 G_DEFINE_TYPE (GstNetBuffer, gst_netbuffer, GST_TYPE_BUFFER);
45 gst_netbuffer_class_init (GstNetBufferClass * netbuffer_class)
47 GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (netbuffer_class);
49 parent_class = g_type_class_peek_parent (netbuffer_class);
51 mo_class->copy = (GstMiniObjectCopyFunction) gst_netbuffer_copy;
52 mo_class->finalize = (GstMiniObjectFinalizeFunction) gst_netbuffer_finalize;
56 gst_netbuffer_init (GstNetBuffer * instance)
61 gst_netbuffer_finalize (GstNetBuffer * nbuf)
63 GST_MINI_OBJECT_CLASS (parent_class)->finalize (GST_MINI_OBJECT (nbuf));
67 gst_netbuffer_copy (GstNetBuffer * nbuf)
71 copy = gst_netbuffer_new ();
73 /* we simply copy everything from our parent */
74 GST_BUFFER_DATA (copy) =
75 g_memdup (GST_BUFFER_DATA (nbuf), GST_BUFFER_SIZE (nbuf));
76 /* make sure it gets freed (even if the parent is subclassed, we return a
78 GST_BUFFER_MALLOCDATA (copy) = GST_BUFFER_DATA (copy);
79 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (nbuf);
81 memcpy (©->to, &nbuf->to, sizeof (nbuf->to));
82 memcpy (©->from, &nbuf->from, sizeof (nbuf->from));
85 gst_buffer_copy_metadata (GST_BUFFER_CAST (copy),
86 GST_BUFFER_CAST (nbuf), GST_BUFFER_COPY_ALL);
94 * Create a new network buffer.
96 * Returns: a new #GstNetBuffer.
99 gst_netbuffer_new (void)
103 buf = (GstNetBuffer *) gst_mini_object_new (GST_TYPE_NETBUFFER);
109 * gst_netaddress_set_ip4_address:
110 * @naddr: a network address
111 * @address: an IPv4 network address.
112 * @port: a port number to set.
114 * Set @naddr with the IPv4 @address and @port pair.
116 * Note that @port and @address must be expressed in network byte order,
117 * use g_htons() and g_htonl() to convert them to network byte order.
120 gst_netaddress_set_ip4_address (GstNetAddress * naddr, guint32 address,
123 g_return_if_fail (naddr != NULL);
125 naddr->type = GST_NET_TYPE_IP4;
126 naddr->address.ip4 = address;
131 * gst_netaddress_set_ip6_address:
132 * @naddr: a network address
133 * @address: an IPv6 network address.
134 * @port: a port number to set.
136 * Set @naddr with the IPv6 @address and @port pair.
138 * Note that @port must be expressed in network byte order, use g_htons() to convert
139 * it to network byte order.
142 gst_netaddress_set_ip6_address (GstNetAddress * naddr, guint8 address[16],
145 g_return_if_fail (naddr != NULL);
147 naddr->type = GST_NET_TYPE_IP6;
148 memcpy (&naddr->address.ip6, address, 16);
153 * gst_netaddress_get_net_type:
154 * @naddr: a network address
156 * Get the type of address stored in @naddr.
158 * Returns: the network type stored in @naddr.
161 gst_netaddress_get_net_type (const GstNetAddress * naddr)
163 g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
169 * gst_netaddress_get_ip4_address:
170 * @naddr: a network address
171 * @address: a location to store the address.
172 * @port: a location to store the port.
174 * Get the IPv4 address stored in @naddr into @address. This function requires
175 * that the address type of @naddr is of type #GST_NET_TYPE_IP4.
177 * Note that @port and @address are expressed in network byte order, use
178 * g_ntohs() and g_ntohl() to convert them to host order.
180 * Returns: TRUE if the address could be retrieved.
183 gst_netaddress_get_ip4_address (const GstNetAddress * naddr, guint32 * address,
186 g_return_val_if_fail (naddr != NULL, FALSE);
188 if (naddr->type == GST_NET_TYPE_UNKNOWN || naddr->type == GST_NET_TYPE_IP6)
192 *address = naddr->address.ip4;
200 * gst_netaddress_get_ip6_address:
201 * @naddr: a network address
202 * @address: a location to store the result.
203 * @port: a location to store the port.
205 * Get the IPv6 address stored in @naddr into @address.
207 * If @naddr is of type GST_NET_TYPE_IP4, the transitional IP6 address is
210 * Note that @port is expressed in network byte order, use g_ntohs() to convert
213 * Returns: TRUE if the address could be retrieved.
216 gst_netaddress_get_ip6_address (const GstNetAddress * naddr, guint8 address[16],
219 static guint8 ip4_transition[16] =
220 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
221 g_return_val_if_fail (naddr != NULL, FALSE);
223 if (naddr->type == GST_NET_TYPE_UNKNOWN)
227 if (naddr->type == GST_NET_TYPE_IP6) {
228 memcpy (address, naddr->address.ip6, 16);
229 } else { /* naddr->type == GST_NET_TYPE_IP4 */
230 memcpy (address, ip4_transition, 12);
231 memcpy (address + 12, (guint8 *) & (naddr->address.ip4), 4);
241 * gst_netaddress_get_address_bytes:
242 * @naddr: a network address
243 * @address: a location to store the result.
244 * @port: a location to store the port.
246 * Get just the address bytes stored in @naddr into @address.
248 * Note that @port is expressed in network byte order, use g_ntohs() to convert
249 * it to host order. IP4 addresses are also stored in network byte order.
251 * Returns: number of bytes actually copied
256 gst_netaddress_get_address_bytes (const GstNetAddress * naddr,
257 guint8 address[16], guint16 * port)
261 g_return_val_if_fail (naddr != NULL, FALSE);
263 if (naddr->type == GST_NET_TYPE_UNKNOWN)
267 if (naddr->type == GST_NET_TYPE_IP6) {
268 memcpy (address, naddr->address.ip6, 16);
270 } else { /* naddr->type == GST_NET_TYPE_IP4 */
271 memcpy (address, (guint8 *) & (naddr->address.ip4), 4);
282 * gst_netaddress_set_address_bytes:
283 * @naddr: a network address
284 * @type: the address type (IPv4 or IPV6)
285 * @address: a location to store the result.
286 * @port: a location to store the port.
288 * Set just the address bytes stored in @naddr into @address.
290 * Note that @port must be expressed in network byte order, use g_htons() to
291 * convert it to network byte order order. IP4 address bytes must also be
292 * stored in network byte order.
294 * Returns: number of bytes actually copied
299 gst_netaddress_set_address_bytes (GstNetAddress * naddr, GstNetType type,
300 guint8 address[16], guint16 port)
304 g_return_val_if_fail (naddr != NULL, 0);
307 switch (naddr->type) {
308 case GST_NET_TYPE_UNKNOWN:
309 case GST_NET_TYPE_IP6:
311 memcpy (naddr->address.ip6, address, 16);
313 case GST_NET_TYPE_IP4:
315 memcpy ((guint8 *) & (naddr->address.ip4), address, 4);
326 * gst_netaddress_equal:
327 * @naddr1: The first #GstNetAddress
328 * @naddr2: The second #GstNetAddress
330 * Compare two #GstNetAddress structures
332 * Returns: TRUE if they are identical, FALSE otherwise
337 gst_netaddress_equal (const GstNetAddress * naddr1,
338 const GstNetAddress * naddr2)
340 g_return_val_if_fail (naddr1 != NULL, FALSE);
341 g_return_val_if_fail (naddr2 != NULL, FALSE);
343 if (naddr1->type != naddr2->type)
346 if (naddr1->port != naddr2->port)
349 switch (naddr1->type) {
350 case GST_NET_TYPE_IP4:
351 if (naddr1->address.ip4 != naddr2->address.ip4)
354 case GST_NET_TYPE_IP6:
355 if (memcmp (naddr1->address.ip6, naddr2->address.ip6,
356 sizeof (naddr1->address.ip6)))
366 * gst_netaddress_to_string:
367 * @naddr: a #GstNetAddress
371 * Copies a string representation of @naddr into @dest. Up to @len bytes are
374 * Returns: the number of bytes which would be produced if the buffer was large
380 gst_netaddress_to_string (const GstNetAddress * naddr, gchar * dest, gulong len)
384 g_return_val_if_fail (naddr != NULL, FALSE);
385 g_return_val_if_fail (dest != NULL, FALSE);
387 switch (naddr->type) {
388 case GST_NET_TYPE_IP4:
393 gst_netaddress_get_ip4_address (naddr, &address, &port);
394 address = g_ntohl (address);
396 result = g_snprintf (dest, len, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
397 (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
401 case GST_NET_TYPE_IP6:
406 gst_netaddress_get_ip6_address (naddr, address, &port);
409 g_snprintf (dest, len, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%d",
410 (address[0] << 8) | address[1], (address[2] << 8) | address[3],
411 (address[4] << 8) | address[5], (address[6] << 8) | address[7],
412 (address[8] << 8) | address[9], (address[10] << 8) | address[11],
413 (address[12] << 8) | address[13], (address[14] << 8) | address[15],