xdgmime: plug a small leak
[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 #ifdef G_OS_WIN32
405   struct sockaddr_storage sa;
406   struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
407   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
408   gint len;
409 #else /* !G_OS_WIN32 */
410   struct in_addr in_addr;
411   struct in6_addr in6_addr;
412 #endif
413
414   /* Make sure _g_networking_init() has been called */
415   g_type_ensure (G_TYPE_INET_ADDRESS);
416
417 #ifdef G_OS_WIN32
418   memset (&sa, 0, sizeof (sa));
419   len = sizeof (sa);
420   if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
421     return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
422   else if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
423     return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
424
425 #else /* !G_OS_WIN32 */
426
427   if (inet_pton (AF_INET, string, &in_addr) > 0)
428     return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
429   else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
430     return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
431 #endif
432
433   return NULL;
434 }
435
436 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
437
438 /**
439  * g_inet_address_new_from_bytes:
440  * @bytes: (array) (element-type guint8): raw address data
441  * @family: the address family of @bytes
442  *
443  * Creates a new #GInetAddress from the given @family and @bytes.
444  * @bytes should be 4 bytes for %G_SOCKET_FAMILY_IPV4 and 16 bytes for
445  * %G_SOCKET_FAMILY_IPV6.
446  *
447  * Returns: a new #GInetAddress corresponding to @family and @bytes.
448  *
449  * Since: 2.22
450  */
451 GInetAddress *
452 g_inet_address_new_from_bytes (const guint8         *bytes,
453                                GSocketFamily  family)
454 {
455   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
456
457   return g_object_new (G_TYPE_INET_ADDRESS,
458                        "family", family,
459                        "bytes", bytes,
460                        NULL);
461 }
462
463 /**
464  * g_inet_address_new_loopback:
465  * @family: the address family
466  *
467  * Creates a #GInetAddress for the loopback address for @family.
468  *
469  * Returns: a new #GInetAddress corresponding to the loopback address
470  * for @family.
471  *
472  * Since: 2.22
473  */
474 GInetAddress *
475 g_inet_address_new_loopback (GSocketFamily family)
476 {
477   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
478
479   if (family == AF_INET)
480     {    
481       guint8 addr[4] = {127, 0, 0, 1};
482
483       return g_inet_address_new_from_bytes (addr, family);
484     }
485   else
486     return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
487 }
488
489 /**
490  * g_inet_address_new_any:
491  * @family: the address family
492  *
493  * Creates a #GInetAddress for the "any" address (unassigned/"don't
494  * care") for @family.
495  *
496  * Returns: a new #GInetAddress corresponding to the "any" address
497  * for @family.
498  *
499  * Since: 2.22
500  */
501 GInetAddress *
502 g_inet_address_new_any (GSocketFamily family)
503 {
504   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
505
506   if (family == AF_INET)
507     {    
508       guint8 addr[4] = {0, 0, 0, 0};
509
510       return g_inet_address_new_from_bytes (addr, family);
511     }
512   else
513     return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
514 }
515
516
517 /**
518  * g_inet_address_to_string:
519  * @address: a #GInetAddress
520  *
521  * Converts @address to string form.
522  *
523  * Returns: a representation of @address as a string, which should be
524  * freed after use.
525  *
526  * Since: 2.22
527  */
528 gchar *
529 g_inet_address_to_string (GInetAddress *address)
530 {
531   gchar buffer[INET6_ADDRSTRLEN];
532 #ifdef G_OS_WIN32
533   DWORD buflen = sizeof (buffer), addrlen;
534   struct sockaddr_storage sa;
535   struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
536   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
537 #endif
538
539   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
540
541 #ifdef G_OS_WIN32
542   sa.ss_family = address->priv->family;
543   if (address->priv->family == AF_INET)
544     {
545       addrlen = sizeof (*sin);
546       memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
547               sizeof (sin->sin_addr));
548       sin->sin_port = 0;
549     }
550   else
551     {
552       addrlen = sizeof (*sin6);
553       memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
554               sizeof (sin6->sin6_addr));
555       sin6->sin6_port = 0;
556     }
557   if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
558     return NULL;
559
560 #else /* !G_OS_WIN32 */
561
562   if (address->priv->family == AF_INET)
563     inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
564   else
565     inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
566 #endif
567
568   return g_strdup (buffer);
569 }
570
571 /**
572  * g_inet_address_to_bytes: (skip)
573  * @address: a #GInetAddress
574  *
575  * Gets the raw binary address data from @address.
576  *
577  * Returns: a pointer to an internal array of the bytes in @address,
578  * which should not be modified, stored, or freed. The size of this
579  * array can be gotten with g_inet_address_get_native_size().
580  *
581  * Since: 2.22
582  */
583 const guint8 *
584 g_inet_address_to_bytes (GInetAddress *address)
585 {
586   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
587
588   return (guint8 *)&address->priv->addr;
589 }
590
591 /**
592  * g_inet_address_get_native_size:
593  * @address: a #GInetAddress
594  *
595  * Gets the size of the native raw binary address for @address. This
596  * is the size of the data that you get from g_inet_address_to_bytes().
597  *
598  * Returns: the number of bytes used for the native version of @address.
599  *
600  * Since: 2.22
601  */
602 gsize
603 g_inet_address_get_native_size (GInetAddress *address)
604 {
605   if (address->priv->family == AF_INET)
606     return sizeof (address->priv->addr.ipv4);
607   return sizeof (address->priv->addr.ipv6);
608 }
609
610 /**
611  * g_inet_address_get_family:
612  * @address: a #GInetAddress
613  *
614  * Gets @address's family
615  *
616  * Returns: @address's family
617  *
618  * Since: 2.22
619  */
620 GSocketFamily
621 g_inet_address_get_family (GInetAddress *address)
622 {
623   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
624
625   return address->priv->family;
626 }
627
628 /**
629  * g_inet_address_get_is_any:
630  * @address: a #GInetAddress
631  *
632  * Tests whether @address is the "any" address for its family.
633  *
634  * Returns: %TRUE if @address is the "any" address for its family.
635  *
636  * Since: 2.22
637  */
638 gboolean
639 g_inet_address_get_is_any (GInetAddress *address)
640 {
641   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
642
643   if (address->priv->family == AF_INET)
644     {
645       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
646
647       return addr4 == INADDR_ANY;
648     }
649   else
650     return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
651 }
652
653 /**
654  * g_inet_address_get_is_loopback:
655  * @address: a #GInetAddress
656  *
657  * Tests whether @address is the loopback address for its family.
658  *
659  * Returns: %TRUE if @address is the loopback address for its family.
660  *
661  * Since: 2.22
662  */
663 gboolean
664 g_inet_address_get_is_loopback (GInetAddress *address)
665 {
666   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
667
668   if (address->priv->family == AF_INET)
669     {
670       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
671
672       /* 127.0.0.0/8 */
673       return ((addr4 & 0xff000000) == 0x7f000000);
674     }
675   else
676     return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
677 }
678
679 /**
680  * g_inet_address_get_is_link_local:
681  * @address: a #GInetAddress
682  *
683  * Tests whether @address is a link-local address (that is, if it
684  * identifies a host on a local network that is not connected to the
685  * Internet).
686  *
687  * Returns: %TRUE if @address is a link-local address.
688  *
689  * Since: 2.22
690  */
691 gboolean
692 g_inet_address_get_is_link_local (GInetAddress *address)
693 {
694   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
695
696   if (address->priv->family == AF_INET)
697     {
698       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
699
700       /* 169.254.0.0/16 */
701       return ((addr4 & 0xffff0000) == 0xa9fe0000);
702     }
703   else
704     return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
705 }
706
707 /**
708  * g_inet_address_get_is_site_local:
709  * @address: a #GInetAddress
710  *
711  * Tests whether @address is a site-local address such as 10.0.0.1
712  * (that is, the address identifies a host on a local network that can
713  * not be reached directly from the Internet, but which may have
714  * outgoing Internet connectivity via a NAT or firewall).
715  *
716  * Returns: %TRUE if @address is a site-local address.
717  *
718  * Since: 2.22
719  */
720 gboolean
721 g_inet_address_get_is_site_local (GInetAddress *address)
722 {
723   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
724
725   if (address->priv->family == AF_INET)
726     {
727       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
728
729       /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
730       return ((addr4 & 0xff000000) == 0x0a000000 ||
731               (addr4 & 0xfff00000) == 0xac100000 ||
732               (addr4 & 0xffff0000) == 0xc0a80000);
733     }
734   else
735     return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
736 }
737
738 /**
739  * g_inet_address_get_is_multicast:
740  * @address: a #GInetAddress
741  *
742  * Tests whether @address is a multicast address.
743  *
744  * Returns: %TRUE if @address is a multicast address.
745  *
746  * Since: 2.22
747  */
748 gboolean
749 g_inet_address_get_is_multicast (GInetAddress *address)
750 {
751   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
752
753   if (address->priv->family == AF_INET)
754     {
755       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
756
757       return IN_MULTICAST (addr4);
758     }
759   else
760     return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
761 }
762
763 /**
764  * g_inet_address_get_is_mc_global:
765  * @address: a #GInetAddress
766  *
767  * Tests whether @address is a global multicast address.
768  *
769  * Returns: %TRUE if @address is a global multicast address.
770  *
771  * Since: 2.22
772  */
773 gboolean
774 g_inet_address_get_is_mc_global (GInetAddress *address)
775 {
776   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
777
778   if (address->priv->family == AF_INET)
779     return FALSE;
780   else
781     return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
782 }
783
784 /**
785  * g_inet_address_get_is_mc_link_local:
786  * @address: a #GInetAddress
787  *
788  * Tests whether @address is a link-local multicast address.
789  *
790  * Returns: %TRUE if @address is a link-local multicast address.
791  *
792  * Since: 2.22
793  */
794 gboolean
795 g_inet_address_get_is_mc_link_local (GInetAddress *address)
796 {
797   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
798
799   if (address->priv->family == AF_INET)
800     return FALSE;
801   else
802     return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
803 }
804
805 /**
806  * g_inet_address_get_is_mc_node_local:
807  * @address: a #GInetAddress
808  *
809  * Tests whether @address is a node-local multicast address.
810  *
811  * Returns: %TRUE if @address is a node-local multicast address.
812  *
813  * Since: 2.22
814  */
815 gboolean
816 g_inet_address_get_is_mc_node_local (GInetAddress *address)
817 {
818   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
819
820   if (address->priv->family == AF_INET)
821     return FALSE;
822   else
823     return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
824 }
825
826 /**
827  * g_inet_address_get_is_mc_org_local:
828  * @address: a #GInetAddress
829  *
830  * Tests whether @address is an organization-local multicast address.
831  *
832  * Returns: %TRUE if @address is an organization-local multicast address.
833  *
834  * Since: 2.22
835  */
836 gboolean
837 g_inet_address_get_is_mc_org_local  (GInetAddress *address)
838 {
839   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
840
841   if (address->priv->family == AF_INET)
842     return FALSE;
843   else
844     return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
845 }
846
847 /**
848  * g_inet_address_get_is_mc_site_local:
849  * @address: a #GInetAddress
850  *
851  * Tests whether @address is a site-local multicast address.
852  *
853  * Returns: %TRUE if @address is a site-local multicast address.
854  *
855  * Since: 2.22
856  */
857 gboolean
858 g_inet_address_get_is_mc_site_local (GInetAddress *address)
859 {
860   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
861
862   if (address->priv->family == AF_INET)
863     return FALSE;
864   else
865     return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
866 }
867
868 /**
869  * g_inet_address_equal:
870  * @address: A #GInetAddress.
871  * @other_address: Another #GInetAddress.
872  *
873  * Checks if two #GInetAddress instances are equal, e.g. the same address.
874  *
875  * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise.
876  *
877  * Since: 2.30
878  */
879 gboolean
880 g_inet_address_equal (GInetAddress *address,
881                       GInetAddress *other_address)
882 {
883   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
884   g_return_val_if_fail (G_IS_INET_ADDRESS (other_address), FALSE);
885
886   if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address))
887     return FALSE;
888
889   if (memcmp (g_inet_address_to_bytes (address),
890               g_inet_address_to_bytes (other_address),
891               g_inet_address_get_native_size (address)) != 0)
892     return FALSE;
893
894   return TRUE;
895 }