Add g_inet_address_get_native_size (#583205)
[platform/upstream/glib.git] / gio / ginetaddress.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Christian Kellner <gicmo@gnome.org>
21  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27
28 #include <glib.h>
29
30 #include "ginetaddress.h"
31 #include "gioenums.h"
32 #include "gioenumtypes.h"
33 #include "glibintl.h"
34 #include "gnetworkingprivate.h"
35
36 #include "gioalias.h"
37
38 /**
39  * SECTION:ginetaddress
40  * @short_description: An IPv4/IPv6 address
41  *
42  * #GInetAddress represents an IPv4 or IPv6 internet address. Use
43  * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
44  * look up the #GInetAddress for a hostname. Use
45  * g_resolver_lookup_by_address() or
46  * g_resolver_lookup_by_address_async() to look up the hostname for a
47  * #GInetAddress.
48  *
49  * To actually connect to a remote host, you will need a
50  * #GInetSocketAddress (which includes a #GInetAddress as well as a
51  * port number).
52  **/
53
54 /**
55  * GInetAddress:
56  *
57  * An IPv4 or IPv6 internet address.
58  **/
59
60 /* Networking initialization function, called from inside the g_once of
61  * g_inet_address_get_type()
62  */
63 static void
64 _g_networking_init (void)
65 {
66 #ifdef G_OS_WIN32
67   WSADATA wsadata;
68   if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
69     g_error ("Windows Sockets could not be initialized");
70 #endif
71 }
72
73 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
74                          _g_networking_init ();)
75
76 struct _GInetAddressPrivate
77 {
78   GSocketFamily family;
79   union {
80     struct in_addr ipv4;
81     struct in6_addr ipv6;
82   } addr;
83 };
84
85 enum
86 {
87   PROP_0,
88   PROP_FAMILY,
89   PROP_BYTES,
90   PROP_IS_ANY,
91   PROP_IS_LOOPBACK,
92   PROP_IS_LINK_LOCAL,
93   PROP_IS_SITE_LOCAL,
94   PROP_IS_MULTICAST,
95   PROP_IS_MC_GLOBAL,
96   PROP_IS_MC_LINK_LOCAL,
97   PROP_IS_MC_NODE_LOCAL,
98   PROP_IS_MC_ORG_LOCAL,
99   PROP_IS_MC_SITE_LOCAL,
100 };
101
102 static void
103 g_inet_address_set_property (GObject      *object,
104                              guint         prop_id,
105                              const GValue *value,
106                              GParamSpec   *pspec)
107 {
108   GInetAddress *address = G_INET_ADDRESS (object);
109
110   switch (prop_id) 
111     {
112     case PROP_FAMILY:
113       address->priv->family = g_value_get_enum (value);
114       break;
115
116     case PROP_BYTES:
117       memcpy (&address->priv->addr, g_value_get_pointer (value),
118               address->priv->family == AF_INET ?
119               sizeof (address->priv->addr.ipv4) :
120               sizeof (address->priv->addr.ipv6));
121       break;
122
123     default:
124       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125       break;
126     }
127
128 }
129
130 static void
131 g_inet_address_get_property (GObject    *object,
132                              guint       prop_id,
133                              GValue     *value,
134                              GParamSpec *pspec)
135 {
136   GInetAddress *address = G_INET_ADDRESS (object);
137
138   switch (prop_id)
139     {
140     case PROP_FAMILY:
141       g_value_set_enum (value, address->priv->family);
142       break;
143
144     case PROP_BYTES:
145       g_value_set_pointer (value, &address->priv->addr);
146       break;
147
148     case PROP_IS_ANY:
149       g_value_set_boolean (value, g_inet_address_get_is_any (address));
150       break;
151
152     case PROP_IS_LOOPBACK:
153       g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
154       break;
155
156     case PROP_IS_LINK_LOCAL:
157       g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
158       break;
159
160     case PROP_IS_SITE_LOCAL:
161       g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
162       break;
163
164     case PROP_IS_MULTICAST:
165       g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
166       break;
167
168     case PROP_IS_MC_GLOBAL:
169       g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
170       break;
171
172     case PROP_IS_MC_LINK_LOCAL:
173       g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
174       break;
175
176     case PROP_IS_MC_NODE_LOCAL:
177       g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
178       break;
179
180     case PROP_IS_MC_ORG_LOCAL:
181       g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
182       break;
183
184     case PROP_IS_MC_SITE_LOCAL:
185       g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
186       break;
187
188     default:
189       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190     }
191 }
192
193 static void
194 g_inet_address_class_init (GInetAddressClass *klass)
195 {
196   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
197
198   g_type_class_add_private (klass, sizeof (GInetAddressPrivate));
199
200   gobject_class->set_property = g_inet_address_set_property;
201   gobject_class->get_property = g_inet_address_get_property;
202
203   g_object_class_install_property (gobject_class, PROP_FAMILY,
204                                    g_param_spec_enum ("family",
205                                                       P_("Address family"),
206                                                       P_("The address family (IPv4 or IPv6)"),
207                                                       G_TYPE_SOCKET_FAMILY,
208                                                       G_SOCKET_FAMILY_INVALID,
209                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
210
211   g_object_class_install_property (gobject_class, PROP_BYTES,
212                                    g_param_spec_pointer ("bytes",
213                                                          P_("Bytes"),
214                                                          P_("The raw address data"),
215                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
216
217   g_object_class_install_property (gobject_class, PROP_IS_ANY,
218                                    g_param_spec_boolean ("is-any",
219                                                          P_("Is any"),
220                                                          P_("See g_inet_address_get_is_any()"),
221                                                          FALSE,
222                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
223
224   g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
225                                    g_param_spec_boolean ("is-link-local",
226                                                          P_("Is link-local"),
227                                                          P_("See g_inet_address_get_is_link_local()"),
228                                                          FALSE,
229                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
230
231   g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
232                                    g_param_spec_boolean ("is-loopback",
233                                                          P_("Is loopback"),
234                                                          P_("See g_inet_address_get_is_loopback()"),
235                                                          FALSE,
236                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
237
238   g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
239                                    g_param_spec_boolean ("is-site-local",
240                                                          P_("Is site-local"),
241                                                          P_("See g_inet_address_get_is_site_local()"),
242                                                          FALSE,
243                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
244
245   g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
246                                    g_param_spec_boolean ("is-multicast",
247                                                          P_("Is multicast"),
248                                                          P_("See g_inet_address_get_is_multicast()"),
249                                                          FALSE,
250                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
251
252   g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
253                                    g_param_spec_boolean ("is-mc-global",
254                                                          P_("Is multicast global"),
255                                                          P_("See g_inet_address_get_is_mc_global()"),
256                                                          FALSE,
257                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
258
259   g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
260                                    g_param_spec_boolean ("is-mc-link-local",
261                                                          P_("Is multicast link-local"),
262                                                          P_("See g_inet_address_get_is_mc_link_local()"),
263                                                          FALSE,
264                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
265
266   g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
267                                    g_param_spec_boolean ("is-mc-node-local",
268                                                          P_("Is multicast node-local"),
269                                                          P_("See g_inet_address_get_is_mc_node_local()"),
270                                                          FALSE,
271                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
272
273   g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
274                                    g_param_spec_boolean ("is-mc-org-local",
275                                                          P_("Is multicast org-local"),
276                                                          P_("See g_inet_address_get_is_mc_org_local()"),
277                                                          FALSE,
278                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
279
280   g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
281                                    g_param_spec_boolean ("is-mc-site-local",
282                                                          P_("Is multicast site-local"),
283                                                          P_("See g_inet_address_get_is_mc_site_local()"),
284                                                          FALSE,
285                                                          G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
286 }
287
288 static void
289 g_inet_address_init (GInetAddress *address)
290 {
291   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
292                                                G_TYPE_INET_ADDRESS,
293                                                GInetAddressPrivate);
294 }
295
296 /**
297  * g_inet_address_new_from_string:
298  * @string: a string representation of an IP address
299  *
300  * Parses @string as an IP address and creates a new #GInetAddress.
301  *
302  * Returns: a new #GInetAddress corresponding to @string, or %NULL if
303  * @string could not be parsed.
304  *
305  * Since: 2.22
306  */
307 GInetAddress *
308 g_inet_address_new_from_string (const gchar *string)
309 {
310   volatile GType type;
311 #ifdef G_OS_WIN32
312   struct sockaddr_storage sa;
313   struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
314   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
315   gint len;
316 #else /* !G_OS_WIN32 */
317   struct in_addr in_addr;
318   struct in6_addr in6_addr;
319 #endif
320
321   /* Make sure _g_networking_init() has been called */
322   type = g_inet_address_get_type ();
323
324 #ifdef G_OS_WIN32
325   memset (&sa, 0, sizeof (sa));
326   len = sizeof (sa);
327   if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
328     return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
329   else if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
330     return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
331
332 #else /* !G_OS_WIN32 */
333
334   if (inet_pton (AF_INET, string, &in_addr) > 0)
335     return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
336   else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
337     return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
338 #endif
339
340   return NULL;
341 }
342
343 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
344
345 /**
346  * g_inet_address_new_from_bytes:
347  * @bytes: raw address data
348  * @family: the address family of @bytes
349  *
350  * Creates a new #GInetAddress from the given @family and @bytes.
351  * @bytes should be 4 bytes for %G_INET_ADDRESS_IPV4 and 16 bytes for
352  * %G_INET_ADDRESS_IPV6.
353  *
354  * Returns: a new #GInetAddress corresponding to @family and @bytes.
355  *
356  * Since: 2.22
357  */
358 GInetAddress *
359 g_inet_address_new_from_bytes (const guint8         *bytes,
360                                GSocketFamily  family)
361 {
362   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
363
364   return g_object_new (G_TYPE_INET_ADDRESS,
365                        "family", family,
366                        "bytes", bytes,
367                        NULL);
368 }
369
370 /**
371  * g_inet_address_new_loopback:
372  * @family: the address family
373  *
374  * Creates a #GInetAddress for the loopback address for @family.
375  *
376  * Returns: a new #GInetAddress corresponding to the loopback address
377  * for @family.
378  *
379  * Since: 2.22
380  */
381 GInetAddress *
382 g_inet_address_new_loopback (GSocketFamily family)
383 {
384   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
385
386   if (family == AF_INET)
387     {    
388       guint8 addr[4] = {127, 0, 0, 1};
389
390       return g_inet_address_new_from_bytes (addr, family);
391     }
392   else
393     return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
394 }
395
396 /**
397  * g_inet_address_new_any:
398  * @family: the address family
399  *
400  * Creates a #GInetAddress for the "any" address (unassigned/"don't
401  * care") for @family.
402  *
403  * Returns: a new #GInetAddress corresponding to the "any" address
404  * for @family.
405  *
406  * Since: 2.22
407  */
408 GInetAddress *
409 g_inet_address_new_any (GSocketFamily family)
410 {
411   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
412
413   if (family == AF_INET)
414     {    
415       guint8 addr[4] = {0, 0, 0, 0};
416
417       return g_inet_address_new_from_bytes (addr, family);
418     }
419   else
420     return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
421 }
422
423
424 /**
425  * g_inet_address_to_string:
426  * @address: a #GInetAddress
427  *
428  * Converts @address to string form.
429  *
430  * Returns: a representation of @address as a string, which should be
431  * freed after use.
432  *
433  * Since: 2.22
434  */
435 gchar *
436 g_inet_address_to_string (GInetAddress *address)
437 {
438   gchar buffer[INET6_ADDRSTRLEN];
439 #ifdef G_OS_WIN32
440   DWORD buflen = sizeof (buffer), addrlen;
441   struct sockaddr_storage sa;
442   struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
443   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
444 #endif
445
446   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
447
448 #ifdef G_OS_WIN32
449   sa.ss_family = address->priv->family;
450   if (address->priv->family == AF_INET)
451     {
452       addrlen = sizeof (*sin);
453       memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
454               sizeof (sin->sin_addr));
455       sin->sin_port = 0;
456     }
457   else
458     {
459       addrlen = sizeof (*sin6);
460       memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
461               sizeof (sin6->sin6_addr));
462       sin6->sin6_port = 0;
463     }
464   if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
465     return NULL;
466
467 #else /* !G_OS_WIN32 */
468
469   if (address->priv->family == AF_INET)
470     inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
471   else
472     inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
473 #endif
474
475   return g_strdup (buffer);
476 }
477
478 /**
479  * g_inet_address_to_bytes:
480  * @address: a #GInetAddress
481  *
482  * Gets the raw binary address data from @address.
483  *
484  * Returns: a pointer to an internal array of the bytes in @address,
485  * which should not be modified, stored, or freed. The size of this
486  * array can be gotten with g_inet_address_get_native_size().
487  *
488  * Since: 2.22
489  */
490 const guint8 *
491 g_inet_address_to_bytes (GInetAddress *address)
492 {
493   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
494
495   return (guint8 *)&address->priv->addr;
496 }
497
498 /**
499  * g_inet_address_get_native_size:
500  * @address: a #GInetAddress
501  *
502  * Gets the size of the native raw binary address for @address. This
503  * is the size of the data that you get from g_inet_address_to_bytes().
504  *
505  * Returns: the number of bytes used for the native version of @address.
506  *
507  * Since: 2.22
508  */
509 gsize
510 g_inet_address_get_native_size (GInetAddress *address)
511 {
512   if (address->priv->family == AF_INET)
513     return sizeof (address->priv->addr.ipv4);
514   return sizeof (address->priv->addr.ipv6);
515 }
516
517 /**
518  * g_inet_address_get_family:
519  * @address: a #GInetAddress
520  *
521  * Gets @address's family
522  *
523  * Returns: @address's family
524  *
525  * Since: 2.22
526  */
527 GSocketFamily
528 g_inet_address_get_family (GInetAddress *address)
529 {
530   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
531
532   return address->priv->family;
533 }
534
535 /**
536  * g_inet_address_get_is_any:
537  * @address: a #GInetAddress
538  *
539  * Tests whether @address is the "any" address for its family.
540  *
541  * Returns: %TRUE if @address is the "any" address for its family.
542  *
543  * Since: 2.22
544  */
545 gboolean
546 g_inet_address_get_is_any (GInetAddress *address)
547 {
548   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
549
550   if (address->priv->family == AF_INET)
551     {
552       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
553
554       return addr4 == INADDR_ANY;
555     }
556   else
557     return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
558 }
559
560 /**
561  * g_inet_address_get_is_loopback:
562  * @address: a #GInetAddress
563  *
564  * Tests whether @address is the loopback address for its family.
565  *
566  * Returns: %TRUE if @address is the loopback address for its family.
567  *
568  * Since: 2.22
569  */
570 gboolean
571 g_inet_address_get_is_loopback (GInetAddress *address)
572 {
573   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
574
575   if (address->priv->family == AF_INET)
576     {
577       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
578
579       /* 127.0.0.0/8 */
580       return ((addr4 & 0xff000000) == 0x7f000000);
581     }
582   else
583     return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
584 }
585
586 /**
587  * g_inet_address_get_is_link_local:
588  * @address: a #GInetAddress
589  *
590  * Tests whether @address is a link-local address (that is, if it
591  * identifies a host on a local network that is not connected to the
592  * Internet).
593  *
594  * Returns: %TRUE if @address is a link-local address.
595  *
596  * Since: 2.22
597  */
598 gboolean
599 g_inet_address_get_is_link_local (GInetAddress *address)
600 {
601   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
602
603   if (address->priv->family == AF_INET)
604     {
605       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
606
607       /* 169.254.0.0/16 */
608       return ((addr4 & 0xffff0000) == 0xa9fe0000);
609     }
610   else
611     return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
612 }
613
614 /**
615  * g_inet_address_get_is_site_local:
616  * @address: a #GInetAddress
617  *
618  * Tests whether @address is a site-local address such as 10.0.0.1
619  * (that is, the address identifies a host on a local network that can
620  * not be reached directly from the Internet, but which may have
621  * outgoing Internet connectivity via a NAT or firewall).
622  *
623  * Returns: %TRUE if @address is a site-local address.
624  *
625  * Since: 2.22
626  */
627 gboolean
628 g_inet_address_get_is_site_local (GInetAddress *address)
629 {
630   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
631
632   if (address->priv->family == AF_INET)
633     {
634       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
635
636       /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
637       return ((addr4 & 0xff000000) == 0x0a000000 ||
638               (addr4 & 0xfff00000) == 0xac100000 ||
639               (addr4 & 0xffff0000) == 0xc0a80000);
640     }
641   else
642     return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
643 }
644
645 /**
646  * g_inet_address_get_is_multicast:
647  * @address: a #GInetAddress
648  *
649  * Tests whether @address is a multicast address.
650  *
651  * Returns: %TRUE if @address is a multicast address.
652  *
653  * Since: 2.22
654  */
655 gboolean
656 g_inet_address_get_is_multicast (GInetAddress *address)
657 {
658   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
659
660   if (address->priv->family == AF_INET)
661     {
662       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
663
664       return IN_MULTICAST (addr4);
665     }
666   else
667     return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
668 }
669
670 /**
671  * g_inet_address_get_is_mc_global:
672  * @address: a #GInetAddress
673  *
674  * Tests whether @address is a global multicast address.
675  *
676  * Returns: %TRUE if @address is a global multicast address.
677  *
678  * Since: 2.22
679  */
680 gboolean
681 g_inet_address_get_is_mc_global (GInetAddress *address)
682 {
683   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
684
685   if (address->priv->family == AF_INET)
686     return FALSE;
687   else
688     return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
689 }
690
691 /**
692  * g_inet_address_get_is_mc_link_local:
693  * @address: a #GInetAddress
694  *
695  * Tests whether @address is a link-local multicast address.
696  *
697  * Returns: %TRUE if @address is a link-local multicast address.
698  *
699  * Since: 2.22
700  */
701 gboolean
702 g_inet_address_get_is_mc_link_local (GInetAddress *address)
703 {
704   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
705
706   if (address->priv->family == AF_INET)
707     return FALSE;
708   else
709     return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
710 }
711
712 /**
713  * g_inet_address_get_is_mc_node_local:
714  * @address: a #GInetAddress
715  *
716  * Tests whether @address is a node-local multicast address.
717  *
718  * Returns: %TRUE if @address is a node-local multicast address.
719  *
720  * Since: 2.22
721  */
722 gboolean
723 g_inet_address_get_is_mc_node_local (GInetAddress *address)
724 {
725   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
726
727   if (address->priv->family == AF_INET)
728     return FALSE;
729   else
730     return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
731 }
732
733 /**
734  * g_inet_address_get_is_mc_org_local:
735  * @address: a #GInetAddress
736  *
737  * Tests whether @address is an organization-local multicast address.
738  *
739  * Returns: %TRUE if @address is an organization-local multicast address.
740  *
741  * Since: 2.22
742  */
743 gboolean
744 g_inet_address_get_is_mc_org_local  (GInetAddress *address)
745 {
746   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
747
748   if (address->priv->family == AF_INET)
749     return FALSE;
750   else
751     return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
752 }
753
754 /**
755  * g_inet_address_get_is_mc_site_local:
756  * @address: a #GInetAddress
757  *
758  * Tests whether @address is a site-local multicast address.
759  *
760  * Returns: %TRUE if @address is a site-local multicast address.
761  *
762  * Since: 2.22
763  */
764 gboolean
765 g_inet_address_get_is_mc_site_local (GInetAddress *address)
766 {
767   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
768
769   if (address->priv->family == AF_INET)
770     return FALSE;
771   else
772     return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
773 }
774
775 #define __G_INET_ADDRESS_C__
776 #include "gioaliasdef.c"