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