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 #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 |
210                                                       G_PARAM_CONSTRUCT_ONLY |
211                                                       G_PARAM_STATIC_STRINGS));
212
213   g_object_class_install_property (gobject_class, PROP_BYTES,
214                                    g_param_spec_pointer ("bytes",
215                                                          P_("Bytes"),
216                                                          P_("The raw address data"),
217                                                          G_PARAM_READWRITE |
218                                                          G_PARAM_CONSTRUCT_ONLY |
219                                                          G_PARAM_STATIC_STRINGS));
220
221   /**
222    * GInetAddress:is-any:
223    *
224    * Whether this is the "any" address for its family.
225    * See g_inet_address_get_is_any().
226    *
227    * Since: 2.22
228    */
229   g_object_class_install_property (gobject_class, PROP_IS_ANY,
230                                    g_param_spec_boolean ("is-any",
231                                                          P_("Is any"),
232                                                          P_("Whether this is the \"any\" address for its family"),
233                                                          FALSE,
234                                                          G_PARAM_READABLE |
235                                                          G_PARAM_STATIC_STRINGS));
236
237   /**
238    * GInetAddress:is-link-local:
239    *
240    * Whether this is a link-local address.
241    * See g_inet_address_get_is_link_local().
242    *
243    * Since: 2.22
244    */
245   g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
246                                    g_param_spec_boolean ("is-link-local",
247                                                          P_("Is link-local"),
248                                                          P_("Whether this is a link-local address"),
249                                                          FALSE,
250                                                          G_PARAM_READABLE |
251                                                          G_PARAM_STATIC_STRINGS));
252
253   /**
254    * GInetAddress:is-loopback:
255    *
256    * Whether this is the loopback address for its family.
257    * See g_inet_address_get_is_loopback().
258    *
259    * Since: 2.22
260    */
261   g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
262                                    g_param_spec_boolean ("is-loopback",
263                                                          P_("Is loopback"),
264                                                          P_("Whether this is the loopback address for its family"),
265                                                          FALSE,
266                                                          G_PARAM_READABLE |
267                                                          G_PARAM_STATIC_STRINGS));
268
269   /**
270    * GInetAddress:is-site-local:
271    *
272    * Whether this is a site-local address.
273    * See g_inet_address_get_is_loopback().
274    *
275    * Since: 2.22
276    */
277   g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
278                                    g_param_spec_boolean ("is-site-local",
279                                                          P_("Is site-local"),
280                                                          P_("Whether this is a site-local address"),
281                                                          FALSE,
282                                                          G_PARAM_READABLE |
283                                                          G_PARAM_STATIC_STRINGS));
284
285   /**
286    * GInetAddress:is-multicast:
287    *
288    * Whether this is a multicast address.
289    * See g_inet_address_get_is_multicast().
290    *
291    * Since: 2.22
292    */
293   g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
294                                    g_param_spec_boolean ("is-multicast",
295                                                          P_("Is multicast"),
296                                                          P_("Whether this is a multicast address"),
297                                                          FALSE,
298                                                          G_PARAM_READABLE |
299                                                          G_PARAM_STATIC_STRINGS));
300
301   /**
302    * GInetAddress:is-mc-global:
303    *
304    * Whether this is a global multicast address.
305    * See g_inet_address_get_is_mc_global().
306    *
307    * Since: 2.22
308    */
309   g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
310                                    g_param_spec_boolean ("is-mc-global",
311                                                          P_("Is multicast global"),
312                                                          P_("Whether this is a global multicast address"),
313                                                          FALSE,
314                                                          G_PARAM_READABLE |
315                                                          G_PARAM_STATIC_STRINGS));
316
317
318   /**
319    * GInetAddress:is-mc-link-local:
320    *
321    * Whether this is a link-local multicast address.
322    * See g_inet_address_get_is_mc_link_local().
323    *
324    * Since: 2.22
325    */
326   g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
327                                    g_param_spec_boolean ("is-mc-link-local",
328                                                          P_("Is multicast link-local"),
329                                                          P_("Whether this is a link-local multicast address"),
330                                                          FALSE,
331                                                          G_PARAM_READABLE |
332                                                          G_PARAM_STATIC_STRINGS));
333
334   /**
335    * GInetAddress:is-mc-node-local:
336    *
337    * Whether this is a node-local multicast address.
338    * See g_inet_address_get_is_mc_node_local().
339    *
340    * Since: 2.22
341    */
342   g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
343                                    g_param_spec_boolean ("is-mc-node-local",
344                                                          P_("Is multicast node-local"),
345                                                          P_("Whether this is a node-local multicast address"),
346                                                          FALSE,
347                                                          G_PARAM_READABLE |
348                                                          G_PARAM_STATIC_STRINGS));
349
350   /**
351    * GInetAddress:is-mc-org-local:
352    *
353    * Whether this is an organization-local multicast address.
354    * See g_inet_address_get_is_mc_org_local().
355    *
356    * Since: 2.22
357    */
358   g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
359                                    g_param_spec_boolean ("is-mc-org-local",
360                                                          P_("Is multicast org-local"),
361                                                          P_("Whether this is an organization-local multicast address"),
362                                                          FALSE,
363                                                          G_PARAM_READABLE |
364                                                          G_PARAM_STATIC_STRINGS));
365
366   /**
367    * GInetAddress:is-mc-site-local:
368    *
369    * Whether this is a site-local multicast address.
370    * See g_inet_address_get_is_mc_site_local().
371    *
372    * Since: 2.22
373    */
374   g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
375                                    g_param_spec_boolean ("is-mc-site-local",
376                                                          P_("Is multicast site-local"),
377                                                          P_("Whether this is a site-local multicast address"),
378                                                          FALSE,
379                                                          G_PARAM_READABLE |
380                                                          G_PARAM_STATIC_STRINGS));
381 }
382
383 static void
384 g_inet_address_init (GInetAddress *address)
385 {
386   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
387                                                G_TYPE_INET_ADDRESS,
388                                                GInetAddressPrivate);
389 }
390
391 /**
392  * g_inet_address_new_from_string:
393  * @string: a string representation of an IP address
394  *
395  * Parses @string as an IP address and creates a new #GInetAddress.
396  *
397  * Returns: a new #GInetAddress corresponding to @string, or %NULL if
398  * @string could not be parsed.
399  *
400  * Since: 2.22
401  */
402 GInetAddress *
403 g_inet_address_new_from_string (const gchar *string)
404 {
405   volatile GType type;
406 #ifdef G_OS_WIN32
407   struct sockaddr_storage sa;
408   struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
409   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
410   gint len;
411 #else /* !G_OS_WIN32 */
412   struct in_addr in_addr;
413   struct in6_addr in6_addr;
414 #endif
415
416   /* Make sure _g_networking_init() has been called */
417   type = g_inet_address_get_type ();
418
419 #ifdef G_OS_WIN32
420   memset (&sa, 0, sizeof (sa));
421   len = sizeof (sa);
422   if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
423     return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
424   else if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
425     return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
426
427 #else /* !G_OS_WIN32 */
428
429   if (inet_pton (AF_INET, string, &in_addr) > 0)
430     return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
431   else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
432     return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
433 #endif
434
435   return NULL;
436 }
437
438 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
439
440 /**
441  * g_inet_address_new_from_bytes:
442  * @bytes: raw address data
443  * @family: the address family of @bytes
444  *
445  * Creates a new #GInetAddress from the given @family and @bytes.
446  * @bytes should be 4 bytes for %G_INET_ADDRESS_IPV4 and 16 bytes for
447  * %G_INET_ADDRESS_IPV6.
448  *
449  * Returns: a new #GInetAddress corresponding to @family and @bytes.
450  *
451  * Since: 2.22
452  */
453 GInetAddress *
454 g_inet_address_new_from_bytes (const guint8         *bytes,
455                                GSocketFamily  family)
456 {
457   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
458
459   return g_object_new (G_TYPE_INET_ADDRESS,
460                        "family", family,
461                        "bytes", bytes,
462                        NULL);
463 }
464
465 /**
466  * g_inet_address_new_loopback:
467  * @family: the address family
468  *
469  * Creates a #GInetAddress for the loopback address for @family.
470  *
471  * Returns: a new #GInetAddress corresponding to the loopback address
472  * for @family.
473  *
474  * Since: 2.22
475  */
476 GInetAddress *
477 g_inet_address_new_loopback (GSocketFamily family)
478 {
479   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
480
481   if (family == AF_INET)
482     {    
483       guint8 addr[4] = {127, 0, 0, 1};
484
485       return g_inet_address_new_from_bytes (addr, family);
486     }
487   else
488     return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
489 }
490
491 /**
492  * g_inet_address_new_any:
493  * @family: the address family
494  *
495  * Creates a #GInetAddress for the "any" address (unassigned/"don't
496  * care") for @family.
497  *
498  * Returns: a new #GInetAddress corresponding to the "any" address
499  * for @family.
500  *
501  * Since: 2.22
502  */
503 GInetAddress *
504 g_inet_address_new_any (GSocketFamily family)
505 {
506   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
507
508   if (family == AF_INET)
509     {    
510       guint8 addr[4] = {0, 0, 0, 0};
511
512       return g_inet_address_new_from_bytes (addr, family);
513     }
514   else
515     return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
516 }
517
518
519 /**
520  * g_inet_address_to_string:
521  * @address: a #GInetAddress
522  *
523  * Converts @address to string form.
524  *
525  * Returns: a representation of @address as a string, which should be
526  * freed after use.
527  *
528  * Since: 2.22
529  */
530 gchar *
531 g_inet_address_to_string (GInetAddress *address)
532 {
533   gchar buffer[INET6_ADDRSTRLEN];
534 #ifdef G_OS_WIN32
535   DWORD buflen = sizeof (buffer), addrlen;
536   struct sockaddr_storage sa;
537   struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
538   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
539 #endif
540
541   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
542
543 #ifdef G_OS_WIN32
544   sa.ss_family = address->priv->family;
545   if (address->priv->family == AF_INET)
546     {
547       addrlen = sizeof (*sin);
548       memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
549               sizeof (sin->sin_addr));
550       sin->sin_port = 0;
551     }
552   else
553     {
554       addrlen = sizeof (*sin6);
555       memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
556               sizeof (sin6->sin6_addr));
557       sin6->sin6_port = 0;
558     }
559   if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
560     return NULL;
561
562 #else /* !G_OS_WIN32 */
563
564   if (address->priv->family == AF_INET)
565     inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
566   else
567     inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
568 #endif
569
570   return g_strdup (buffer);
571 }
572
573 /**
574  * g_inet_address_to_bytes:
575  * @address: a #GInetAddress
576  *
577  * Gets the raw binary address data from @address.
578  *
579  * Returns: a pointer to an internal array of the bytes in @address,
580  * which should not be modified, stored, or freed. The size of this
581  * array can be gotten with g_inet_address_get_native_size().
582  *
583  * Since: 2.22
584  */
585 const guint8 *
586 g_inet_address_to_bytes (GInetAddress *address)
587 {
588   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
589
590   return (guint8 *)&address->priv->addr;
591 }
592
593 /**
594  * g_inet_address_get_native_size:
595  * @address: a #GInetAddress
596  *
597  * Gets the size of the native raw binary address for @address. This
598  * is the size of the data that you get from g_inet_address_to_bytes().
599  *
600  * Returns: the number of bytes used for the native version of @address.
601  *
602  * Since: 2.22
603  */
604 gsize
605 g_inet_address_get_native_size (GInetAddress *address)
606 {
607   if (address->priv->family == AF_INET)
608     return sizeof (address->priv->addr.ipv4);
609   return sizeof (address->priv->addr.ipv6);
610 }
611
612 /**
613  * g_inet_address_get_family:
614  * @address: a #GInetAddress
615  *
616  * Gets @address's family
617  *
618  * Returns: @address's family
619  *
620  * Since: 2.22
621  */
622 GSocketFamily
623 g_inet_address_get_family (GInetAddress *address)
624 {
625   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
626
627   return address->priv->family;
628 }
629
630 /**
631  * g_inet_address_get_is_any:
632  * @address: a #GInetAddress
633  *
634  * Tests whether @address is the "any" address for its family.
635  *
636  * Returns: %TRUE if @address is the "any" address for its family.
637  *
638  * Since: 2.22
639  */
640 gboolean
641 g_inet_address_get_is_any (GInetAddress *address)
642 {
643   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
644
645   if (address->priv->family == AF_INET)
646     {
647       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
648
649       return addr4 == INADDR_ANY;
650     }
651   else
652     return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
653 }
654
655 /**
656  * g_inet_address_get_is_loopback:
657  * @address: a #GInetAddress
658  *
659  * Tests whether @address is the loopback address for its family.
660  *
661  * Returns: %TRUE if @address is the loopback address for its family.
662  *
663  * Since: 2.22
664  */
665 gboolean
666 g_inet_address_get_is_loopback (GInetAddress *address)
667 {
668   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
669
670   if (address->priv->family == AF_INET)
671     {
672       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
673
674       /* 127.0.0.0/8 */
675       return ((addr4 & 0xff000000) == 0x7f000000);
676     }
677   else
678     return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
679 }
680
681 /**
682  * g_inet_address_get_is_link_local:
683  * @address: a #GInetAddress
684  *
685  * Tests whether @address is a link-local address (that is, if it
686  * identifies a host on a local network that is not connected to the
687  * Internet).
688  *
689  * Returns: %TRUE if @address is a link-local address.
690  *
691  * Since: 2.22
692  */
693 gboolean
694 g_inet_address_get_is_link_local (GInetAddress *address)
695 {
696   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
697
698   if (address->priv->family == AF_INET)
699     {
700       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
701
702       /* 169.254.0.0/16 */
703       return ((addr4 & 0xffff0000) == 0xa9fe0000);
704     }
705   else
706     return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
707 }
708
709 /**
710  * g_inet_address_get_is_site_local:
711  * @address: a #GInetAddress
712  *
713  * Tests whether @address is a site-local address such as 10.0.0.1
714  * (that is, the address identifies a host on a local network that can
715  * not be reached directly from the Internet, but which may have
716  * outgoing Internet connectivity via a NAT or firewall).
717  *
718  * Returns: %TRUE if @address is a site-local address.
719  *
720  * Since: 2.22
721  */
722 gboolean
723 g_inet_address_get_is_site_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     {
729       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
730
731       /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
732       return ((addr4 & 0xff000000) == 0x0a000000 ||
733               (addr4 & 0xfff00000) == 0xac100000 ||
734               (addr4 & 0xffff0000) == 0xc0a80000);
735     }
736   else
737     return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
738 }
739
740 /**
741  * g_inet_address_get_is_multicast:
742  * @address: a #GInetAddress
743  *
744  * Tests whether @address is a multicast address.
745  *
746  * Returns: %TRUE if @address is a multicast address.
747  *
748  * Since: 2.22
749  */
750 gboolean
751 g_inet_address_get_is_multicast (GInetAddress *address)
752 {
753   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
754
755   if (address->priv->family == AF_INET)
756     {
757       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
758
759       return IN_MULTICAST (addr4);
760     }
761   else
762     return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
763 }
764
765 /**
766  * g_inet_address_get_is_mc_global:
767  * @address: a #GInetAddress
768  *
769  * Tests whether @address is a global multicast address.
770  *
771  * Returns: %TRUE if @address is a global multicast address.
772  *
773  * Since: 2.22
774  */
775 gboolean
776 g_inet_address_get_is_mc_global (GInetAddress *address)
777 {
778   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
779
780   if (address->priv->family == AF_INET)
781     return FALSE;
782   else
783     return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
784 }
785
786 /**
787  * g_inet_address_get_is_mc_link_local:
788  * @address: a #GInetAddress
789  *
790  * Tests whether @address is a link-local multicast address.
791  *
792  * Returns: %TRUE if @address is a link-local multicast address.
793  *
794  * Since: 2.22
795  */
796 gboolean
797 g_inet_address_get_is_mc_link_local (GInetAddress *address)
798 {
799   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
800
801   if (address->priv->family == AF_INET)
802     return FALSE;
803   else
804     return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
805 }
806
807 /**
808  * g_inet_address_get_is_mc_node_local:
809  * @address: a #GInetAddress
810  *
811  * Tests whether @address is a node-local multicast address.
812  *
813  * Returns: %TRUE if @address is a node-local multicast address.
814  *
815  * Since: 2.22
816  */
817 gboolean
818 g_inet_address_get_is_mc_node_local (GInetAddress *address)
819 {
820   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
821
822   if (address->priv->family == AF_INET)
823     return FALSE;
824   else
825     return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
826 }
827
828 /**
829  * g_inet_address_get_is_mc_org_local:
830  * @address: a #GInetAddress
831  *
832  * Tests whether @address is an organization-local multicast address.
833  *
834  * Returns: %TRUE if @address is an organization-local multicast address.
835  *
836  * Since: 2.22
837  */
838 gboolean
839 g_inet_address_get_is_mc_org_local  (GInetAddress *address)
840 {
841   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
842
843   if (address->priv->family == AF_INET)
844     return FALSE;
845   else
846     return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
847 }
848
849 /**
850  * g_inet_address_get_is_mc_site_local:
851  * @address: a #GInetAddress
852  *
853  * Tests whether @address is a site-local multicast address.
854  *
855  * Returns: %TRUE if @address is a site-local multicast address.
856  *
857  * Since: 2.22
858  */
859 gboolean
860 g_inet_address_get_is_mc_site_local (GInetAddress *address)
861 {
862   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
863
864   if (address->priv->family == AF_INET)
865     return FALSE;
866   else
867     return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
868 }
869
870 #define __G_INET_ADDRESS_C__
871 #include "gioaliasdef.c"