Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / libs / gst / net / gstnetaddressmeta.c
1 /* GStreamer
2  * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:gstnetaddressmeta
22  * @short_description: Network address metadata
23  *
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.
27  *
28  * Last reviewed on 2011-11-03 (0.11.2)
29  */
30
31 #include <string.h>
32
33 #include "gstnetaddressmeta.h"
34
35 static void
36 net_address_meta_copy (GstBuffer * copybuf, GstNetAddressMeta * meta,
37     GstBuffer * buffer, gsize offset, gsize size)
38 {
39   GstNetAddressMeta *naddr;
40
41   naddr = gst_buffer_add_net_address_meta (copybuf);
42   memcpy (&naddr->naddr, &meta->naddr, sizeof (meta->naddr));
43 }
44
45 const GstMetaInfo *
46 gst_net_address_meta_get_info (void)
47 {
48   static const GstMetaInfo *meta_info = NULL;
49
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);
57   }
58   return meta_info;
59 }
60
61 /**
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.
66  *
67  * Set @naddr with the IPv4 @address and @port pair.
68  *
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.
71  */
72 void
73 gst_net_address_set_ip4_address (GstNetAddress * naddr, guint32 address,
74     guint16 port)
75 {
76   g_return_if_fail (naddr != NULL);
77
78   naddr->type = GST_NET_TYPE_IP4;
79   naddr->address.ip4 = address;
80   naddr->port = port;
81 }
82
83 /**
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.
88  *
89  * Set @naddr with the IPv6 @address and @port pair.
90  *
91  * Note that @port must be expressed in network byte order, use g_htons() to convert
92  * it to network byte order.
93  */
94 void
95 gst_net_address_set_ip6_address (GstNetAddress * naddr, guint8 address[16],
96     guint16 port)
97 {
98   g_return_if_fail (naddr != NULL);
99
100   naddr->type = GST_NET_TYPE_IP6;
101   memcpy (&naddr->address.ip6, address, 16);
102   naddr->port = port;
103 }
104
105 /**
106  * gst_net_address_get_net_type:
107  * @naddr: a network address
108  *
109  * Get the type of address stored in @naddr.
110  *
111  * Returns: the network type stored in @naddr.
112  */
113 GstNetType
114 gst_net_address_get_net_type (const GstNetAddress * naddr)
115 {
116   g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
117
118   return naddr->type;
119 }
120
121 /**
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.
126  *
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.
129  *
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.
132  *
133  * Returns: TRUE if the address could be retrieved.
134  */
135 gboolean
136 gst_net_address_get_ip4_address (const GstNetAddress * naddr, guint32 * address,
137     guint16 * port)
138 {
139   g_return_val_if_fail (naddr != NULL, FALSE);
140
141   if (naddr->type == GST_NET_TYPE_UNKNOWN || naddr->type == GST_NET_TYPE_IP6)
142     return FALSE;
143
144   if (address)
145     *address = naddr->address.ip4;
146   if (port)
147     *port = naddr->port;
148
149   return TRUE;
150 }
151
152 /**
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.
157  *
158  * Get the IPv6 address stored in @naddr into @address.
159  *
160  * If @naddr is of type GST_NET_TYPE_IP4, the transitional IP6 address is
161  * returned.
162  *
163  * Note that @port is expressed in network byte order, use g_ntohs() to convert
164  * it to host order.
165  *
166  * Returns: TRUE if the address could be retrieved.
167  */
168 gboolean
169 gst_net_address_get_ip6_address (const GstNetAddress * naddr,
170     guint8 address[16], guint16 * port)
171 {
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);
175
176   if (naddr->type == GST_NET_TYPE_UNKNOWN)
177     return FALSE;
178
179   if (address) {
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);
185     }
186   }
187   if (port)
188     *port = naddr->port;
189
190   return TRUE;
191 }
192
193 /**
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.
198  *
199  * Get just the address bytes stored in @naddr into @address.
200  *
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.
203  *
204  * Returns: number of bytes actually copied
205  *
206  * Since: 0.10.22
207  */
208 gint
209 gst_net_address_get_address_bytes (const GstNetAddress * naddr,
210     guint8 address[16], guint16 * port)
211 {
212   gint ret = 0;
213
214   g_return_val_if_fail (naddr != NULL, FALSE);
215
216   if (naddr->type == GST_NET_TYPE_UNKNOWN)
217     return 0;
218
219   if (address) {
220     if (naddr->type == GST_NET_TYPE_IP6) {
221       memcpy (address, naddr->address.ip6, 16);
222       ret = 16;
223     } else {                    /* naddr->type == GST_NET_TYPE_IP4 */
224       memcpy (address, (guint8 *) & (naddr->address.ip4), 4);
225       ret = 4;
226     }
227   }
228   if (port)
229     *port = naddr->port;
230
231   return ret;
232 }
233
234 /**
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.
240  *
241  * Set just the address bytes stored in @naddr into @address.
242  *
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.
246  *
247  * Returns: number of bytes actually copied
248  *
249  * Since: 0.10.22
250  */
251 gint
252 gst_net_address_set_address_bytes (GstNetAddress * naddr, GstNetType type,
253     guint8 address[16], guint16 port)
254 {
255   gint len = 0;
256
257   g_return_val_if_fail (naddr != NULL, 0);
258
259   naddr->type = type;
260   switch (naddr->type) {
261     case GST_NET_TYPE_UNKNOWN:
262     case GST_NET_TYPE_IP6:
263       len = 16;
264       memcpy (naddr->address.ip6, address, 16);
265       break;
266     case GST_NET_TYPE_IP4:
267       len = 4;
268       memcpy ((guint8 *) & (naddr->address.ip4), address, 4);
269       break;
270   }
271
272   if (port)
273     naddr->port = port;
274
275   return len;
276 }
277
278 /**
279  * gst_net_address_equal:
280  * @naddr1: The first #GstNetAddress
281  * @naddr2: The second #GstNetAddress
282  *
283  * Compare two #GstNetAddress structures
284  *
285  * Returns: TRUE if they are identical, FALSE otherwise
286  *
287  * Since: 0.10.18
288  */
289 gboolean
290 gst_net_address_equal (const GstNetAddress * naddr1,
291     const GstNetAddress * naddr2)
292 {
293   g_return_val_if_fail (naddr1 != NULL, FALSE);
294   g_return_val_if_fail (naddr2 != NULL, FALSE);
295
296   if (naddr1->type != naddr2->type)
297     return FALSE;
298
299   if (naddr1->port != naddr2->port)
300     return FALSE;
301
302   switch (naddr1->type) {
303     case GST_NET_TYPE_IP4:
304       if (naddr1->address.ip4 != naddr2->address.ip4)
305         return FALSE;
306       break;
307     case GST_NET_TYPE_IP6:
308       if (memcmp (naddr1->address.ip6, naddr2->address.ip6,
309               sizeof (naddr1->address.ip6)))
310         return FALSE;
311       break;
312     default:
313       break;
314   }
315   return TRUE;
316 }
317
318 /**
319  * gst_net_address_to_string:
320  * @naddr: a #GstNetAddress
321  * @dest: destination
322  * @len: len of @dest
323  *
324  * Copies a string representation of @naddr into @dest. Up to @len bytes are
325  * copied.
326  *
327  * Returns: the number of bytes which would be produced if the buffer was large
328  * enough
329  *
330  * Since: 0.10.24
331  */
332 gint
333 gst_net_address_to_string (const GstNetAddress * naddr, gchar * dest, gsize len)
334 {
335   gint result;
336
337   g_return_val_if_fail (naddr != NULL, FALSE);
338   g_return_val_if_fail (dest != NULL, FALSE);
339
340   switch (naddr->type) {
341     case GST_NET_TYPE_IP4:
342     {
343       guint32 address;
344       guint16 port;
345
346       gst_net_address_get_ip4_address (naddr, &address, &port);
347       address = g_ntohl (address);
348
349       result = g_snprintf (dest, len, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
350           (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
351           g_ntohs (port));
352       break;
353     }
354     case GST_NET_TYPE_IP6:
355     {
356       guint8 address[16];
357       guint16 port;
358
359       gst_net_address_get_ip6_address (naddr, address, &port);
360
361       result =
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],
367           g_ntohs (port));
368       break;
369     }
370     default:
371       dest[0] = 0;
372       result = 0;
373       break;
374   }
375   return result;
376 }