2 * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.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:gstnetaddressmeta
22 * @short_description: Network address metadata
24 * #GstNetAddress can be used to store a network address. #GstNetAddressMeta can
25 * be used to store a network address in a #GstBuffer so that it network
26 * elements can track the to and from address of the buffer.
28 * Last reviewed on 2011-11-03 (0.11.2)
33 #include "gstnetaddressmeta.h"
36 net_address_meta_copy (GstBuffer * copybuf, GstNetAddressMeta * meta,
37 GstBuffer * buffer, gsize offset, gsize size)
39 GstNetAddressMeta *naddr;
41 naddr = gst_buffer_add_net_address_meta (copybuf);
42 memcpy (&naddr->naddr, &meta->naddr, sizeof (meta->naddr));
46 gst_net_address_meta_get_info (void)
48 static const GstMetaInfo *meta_info = NULL;
50 if (meta_info == NULL) {
51 meta_info = gst_meta_register ("GstNetAddressMeta", "GstNetAddressMeta",
52 sizeof (GstNetAddressMeta),
53 (GstMetaInitFunction) NULL,
54 (GstMetaFreeFunction) NULL,
55 (GstMetaCopyFunction) net_address_meta_copy,
56 (GstMetaTransformFunction) NULL);
62 * gst_net_address_set_ip4_address:
63 * @naddr: a network address
64 * @address: an IPv4 network address.
65 * @port: a port number to set.
67 * Set @naddr with the IPv4 @address and @port pair.
69 * Note that @port and @address must be expressed in network byte order,
70 * use g_htons() and g_htonl() to convert them to network byte order.
73 gst_net_address_set_ip4_address (GstNetAddress * naddr, guint32 address,
76 g_return_if_fail (naddr != NULL);
78 naddr->type = GST_NET_TYPE_IP4;
79 naddr->address.ip4 = address;
84 * gst_net_address_set_ip6_address:
85 * @naddr: a network address
86 * @address: an IPv6 network address.
87 * @port: a port number to set.
89 * Set @naddr with the IPv6 @address and @port pair.
91 * Note that @port must be expressed in network byte order, use g_htons() to convert
92 * it to network byte order.
95 gst_net_address_set_ip6_address (GstNetAddress * naddr, guint8 address[16],
98 g_return_if_fail (naddr != NULL);
100 naddr->type = GST_NET_TYPE_IP6;
101 memcpy (&naddr->address.ip6, address, 16);
106 * gst_net_address_get_net_type:
107 * @naddr: a network address
109 * Get the type of address stored in @naddr.
111 * Returns: the network type stored in @naddr.
114 gst_net_address_get_net_type (const GstNetAddress * naddr)
116 g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
122 * gst_net_address_get_ip4_address:
123 * @naddr: a network address
124 * @address: a location to store the address.
125 * @port: a location to store the port.
127 * Get the IPv4 address stored in @naddr into @address. This function requires
128 * that the address type of @naddr is of type #GST_NET_TYPE_IP4.
130 * Note that @port and @address are expressed in network byte order, use
131 * g_ntohs() and g_ntohl() to convert them to host order.
133 * Returns: TRUE if the address could be retrieved.
136 gst_net_address_get_ip4_address (const GstNetAddress * naddr, guint32 * address,
139 g_return_val_if_fail (naddr != NULL, FALSE);
141 if (naddr->type == GST_NET_TYPE_UNKNOWN || naddr->type == GST_NET_TYPE_IP6)
145 *address = naddr->address.ip4;
153 * gst_net_address_get_ip6_address:
154 * @naddr: a network address
155 * @address: a location to store the result.
156 * @port: a location to store the port.
158 * Get the IPv6 address stored in @naddr into @address.
160 * If @naddr is of type GST_NET_TYPE_IP4, the transitional IP6 address is
163 * Note that @port is expressed in network byte order, use g_ntohs() to convert
166 * Returns: TRUE if the address could be retrieved.
169 gst_net_address_get_ip6_address (const GstNetAddress * naddr,
170 guint8 address[16], guint16 * port)
172 static guint8 ip4_transition[16] =
173 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
174 g_return_val_if_fail (naddr != NULL, FALSE);
176 if (naddr->type == GST_NET_TYPE_UNKNOWN)
180 if (naddr->type == GST_NET_TYPE_IP6) {
181 memcpy (address, naddr->address.ip6, 16);
182 } else { /* naddr->type == GST_NET_TYPE_IP4 */
183 memcpy (address, ip4_transition, 12);
184 memcpy (address + 12, (guint8 *) & (naddr->address.ip4), 4);
194 * gst_net_address_get_address_bytes:
195 * @naddr: a network address
196 * @address: a location to store the result.
197 * @port: a location to store the port.
199 * Get just the address bytes stored in @naddr into @address.
201 * Note that @port is expressed in network byte order, use g_ntohs() to convert
202 * it to host order. IP4 addresses are also stored in network byte order.
204 * Returns: number of bytes actually copied
209 gst_net_address_get_address_bytes (const GstNetAddress * naddr,
210 guint8 address[16], guint16 * port)
214 g_return_val_if_fail (naddr != NULL, FALSE);
216 if (naddr->type == GST_NET_TYPE_UNKNOWN)
220 if (naddr->type == GST_NET_TYPE_IP6) {
221 memcpy (address, naddr->address.ip6, 16);
223 } else { /* naddr->type == GST_NET_TYPE_IP4 */
224 memcpy (address, (guint8 *) & (naddr->address.ip4), 4);
235 * gst_net_address_set_address_bytes:
236 * @naddr: a network address
237 * @type: the address type (IPv4 or IPV6)
238 * @address: a location to store the result.
239 * @port: a location to store the port.
241 * Set just the address bytes stored in @naddr into @address.
243 * Note that @port must be expressed in network byte order, use g_htons() to
244 * convert it to network byte order order. IP4 address bytes must also be
245 * stored in network byte order.
247 * Returns: number of bytes actually copied
252 gst_net_address_set_address_bytes (GstNetAddress * naddr, GstNetType type,
253 guint8 address[16], guint16 port)
257 g_return_val_if_fail (naddr != NULL, 0);
260 switch (naddr->type) {
261 case GST_NET_TYPE_UNKNOWN:
262 case GST_NET_TYPE_IP6:
264 memcpy (naddr->address.ip6, address, 16);
266 case GST_NET_TYPE_IP4:
268 memcpy ((guint8 *) & (naddr->address.ip4), address, 4);
279 * gst_net_address_equal:
280 * @naddr1: The first #GstNetAddress
281 * @naddr2: The second #GstNetAddress
283 * Compare two #GstNetAddress structures
285 * Returns: TRUE if they are identical, FALSE otherwise
290 gst_net_address_equal (const GstNetAddress * naddr1,
291 const GstNetAddress * naddr2)
293 g_return_val_if_fail (naddr1 != NULL, FALSE);
294 g_return_val_if_fail (naddr2 != NULL, FALSE);
296 if (naddr1->type != naddr2->type)
299 if (naddr1->port != naddr2->port)
302 switch (naddr1->type) {
303 case GST_NET_TYPE_IP4:
304 if (naddr1->address.ip4 != naddr2->address.ip4)
307 case GST_NET_TYPE_IP6:
308 if (memcmp (naddr1->address.ip6, naddr2->address.ip6,
309 sizeof (naddr1->address.ip6)))
319 * gst_net_address_to_string:
320 * @naddr: a #GstNetAddress
324 * Copies a string representation of @naddr into @dest. Up to @len bytes are
327 * Returns: the number of bytes which would be produced if the buffer was large
333 gst_net_address_to_string (const GstNetAddress * naddr, gchar * dest, gsize len)
337 g_return_val_if_fail (naddr != NULL, FALSE);
338 g_return_val_if_fail (dest != NULL, FALSE);
340 switch (naddr->type) {
341 case GST_NET_TYPE_IP4:
346 gst_net_address_get_ip4_address (naddr, &address, &port);
347 address = g_ntohl (address);
349 result = g_snprintf (dest, len, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
350 (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
354 case GST_NET_TYPE_IP6:
359 gst_net_address_get_ip6_address (naddr, address, &port);
362 g_snprintf (dest, len, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%d",
363 (address[0] << 8) | address[1], (address[2] << 8) | address[3],
364 (address[4] << 8) | address[5], (address[6] << 8) | address[7],
365 (address[8] << 8) | address[9], (address[10] << 8) | address[11],
366 (address[12] << 8) | address[13], (address[14] << 8) | address[15],