1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Christian Kellner <gicmo@gnome.org>
19 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
28 #include "ginetaddress.h"
30 #include "gioenumtypes.h"
32 #include "gnetworkingprivate.h"
34 struct _GInetAddressPrivate
44 * SECTION:ginetaddress
45 * @short_description: An IPv4/IPv6 address
48 * #GInetAddress represents an IPv4 or IPv6 internet address. Use
49 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
50 * look up the #GInetAddress for a hostname. Use
51 * g_resolver_lookup_by_address() or
52 * g_resolver_lookup_by_address_async() to look up the hostname for a
55 * To actually connect to a remote host, you will need a
56 * #GInetSocketAddress (which includes a #GInetAddress as well as a
63 * An IPv4 or IPv6 internet address.
66 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
67 G_ADD_PRIVATE (GInetAddress)
68 g_networking_init ();)
81 PROP_IS_MC_LINK_LOCAL,
82 PROP_IS_MC_NODE_LOCAL,
84 PROP_IS_MC_SITE_LOCAL,
88 g_inet_address_set_property (GObject *object,
93 GInetAddress *address = G_INET_ADDRESS (object);
98 address->priv->family = g_value_get_enum (value);
102 memcpy (&address->priv->addr, g_value_get_pointer (value),
103 address->priv->family == AF_INET ?
104 sizeof (address->priv->addr.ipv4) :
105 sizeof (address->priv->addr.ipv6));
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 g_inet_address_get_property (GObject *object,
121 GInetAddress *address = G_INET_ADDRESS (object);
126 g_value_set_enum (value, address->priv->family);
130 g_value_set_pointer (value, &address->priv->addr);
134 g_value_set_boolean (value, g_inet_address_get_is_any (address));
137 case PROP_IS_LOOPBACK:
138 g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
141 case PROP_IS_LINK_LOCAL:
142 g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
145 case PROP_IS_SITE_LOCAL:
146 g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
149 case PROP_IS_MULTICAST:
150 g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
153 case PROP_IS_MC_GLOBAL:
154 g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
157 case PROP_IS_MC_LINK_LOCAL:
158 g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
161 case PROP_IS_MC_NODE_LOCAL:
162 g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
165 case PROP_IS_MC_ORG_LOCAL:
166 g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
169 case PROP_IS_MC_SITE_LOCAL:
170 g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 g_inet_address_class_init (GInetAddressClass *klass)
181 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183 gobject_class->set_property = g_inet_address_set_property;
184 gobject_class->get_property = g_inet_address_get_property;
186 g_object_class_install_property (gobject_class, PROP_FAMILY,
187 g_param_spec_enum ("family",
188 P_("Address family"),
189 P_("The address family (IPv4 or IPv6)"),
190 G_TYPE_SOCKET_FAMILY,
191 G_SOCKET_FAMILY_INVALID,
193 G_PARAM_CONSTRUCT_ONLY |
194 G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_class, PROP_BYTES,
197 g_param_spec_pointer ("bytes",
199 P_("The raw address data"),
201 G_PARAM_CONSTRUCT_ONLY |
202 G_PARAM_STATIC_STRINGS));
205 * GInetAddress:is-any:
207 * Whether this is the "any" address for its family.
208 * See g_inet_address_get_is_any().
212 g_object_class_install_property (gobject_class, PROP_IS_ANY,
213 g_param_spec_boolean ("is-any",
215 P_("Whether this is the \"any\" address for its family"),
218 G_PARAM_STATIC_STRINGS));
221 * GInetAddress:is-link-local:
223 * Whether this is a link-local address.
224 * See g_inet_address_get_is_link_local().
228 g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
229 g_param_spec_boolean ("is-link-local",
231 P_("Whether this is a link-local address"),
234 G_PARAM_STATIC_STRINGS));
237 * GInetAddress:is-loopback:
239 * Whether this is the loopback address for its family.
240 * See g_inet_address_get_is_loopback().
244 g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
245 g_param_spec_boolean ("is-loopback",
247 P_("Whether this is the loopback address for its family"),
250 G_PARAM_STATIC_STRINGS));
253 * GInetAddress:is-site-local:
255 * Whether this is a site-local address.
256 * See g_inet_address_get_is_loopback().
260 g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
261 g_param_spec_boolean ("is-site-local",
263 P_("Whether this is a site-local address"),
266 G_PARAM_STATIC_STRINGS));
269 * GInetAddress:is-multicast:
271 * Whether this is a multicast address.
272 * See g_inet_address_get_is_multicast().
276 g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
277 g_param_spec_boolean ("is-multicast",
279 P_("Whether this is a multicast address"),
282 G_PARAM_STATIC_STRINGS));
285 * GInetAddress:is-mc-global:
287 * Whether this is a global multicast address.
288 * See g_inet_address_get_is_mc_global().
292 g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
293 g_param_spec_boolean ("is-mc-global",
294 P_("Is multicast global"),
295 P_("Whether this is a global multicast address"),
298 G_PARAM_STATIC_STRINGS));
302 * GInetAddress:is-mc-link-local:
304 * Whether this is a link-local multicast address.
305 * See g_inet_address_get_is_mc_link_local().
309 g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
310 g_param_spec_boolean ("is-mc-link-local",
311 P_("Is multicast link-local"),
312 P_("Whether this is a link-local multicast address"),
315 G_PARAM_STATIC_STRINGS));
318 * GInetAddress:is-mc-node-local:
320 * Whether this is a node-local multicast address.
321 * See g_inet_address_get_is_mc_node_local().
325 g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
326 g_param_spec_boolean ("is-mc-node-local",
327 P_("Is multicast node-local"),
328 P_("Whether this is a node-local multicast address"),
331 G_PARAM_STATIC_STRINGS));
334 * GInetAddress:is-mc-org-local:
336 * Whether this is an organization-local multicast address.
337 * See g_inet_address_get_is_mc_org_local().
341 g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
342 g_param_spec_boolean ("is-mc-org-local",
343 P_("Is multicast org-local"),
344 P_("Whether this is an organization-local multicast address"),
347 G_PARAM_STATIC_STRINGS));
350 * GInetAddress:is-mc-site-local:
352 * Whether this is a site-local multicast address.
353 * See g_inet_address_get_is_mc_site_local().
357 g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
358 g_param_spec_boolean ("is-mc-site-local",
359 P_("Is multicast site-local"),
360 P_("Whether this is a site-local multicast address"),
363 G_PARAM_STATIC_STRINGS));
367 g_inet_address_init (GInetAddress *address)
369 address->priv = g_inet_address_get_instance_private (address);
373 * g_inet_address_new_from_string:
374 * @string: a string representation of an IP address
376 * Parses @string as an IP address and creates a new #GInetAddress.
378 * Returns: a new #GInetAddress corresponding to @string, or %NULL if
379 * @string could not be parsed.
384 g_inet_address_new_from_string (const gchar *string)
387 struct sockaddr_storage sa;
388 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
389 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
391 #else /* !G_OS_WIN32 */
392 struct in_addr in_addr;
393 struct in6_addr in6_addr;
396 /* If this GInetAddress is the first networking-related object to be
397 * created, then we won't have called g_networking_init() yet at
400 g_networking_init ();
403 /* We need to make sure to not pass a string of the form
404 * "IPv4addr:port" or "[IPv6addr]:port" to WSAStringToAddress(),
405 * since it would accept them (returning both the address and the
406 * port), but we only want to accept standalone IP addresses. (In
407 * the IPv6 case, WINE actually only checks for the ']', not the
408 * '[', which is why we do the same here.)
410 if (!strchr (string, ':'))
413 if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
414 return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
417 if (!strchr (string, ']'))
420 if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
421 return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
424 #else /* !G_OS_WIN32 */
426 if (inet_pton (AF_INET, string, &in_addr) > 0)
427 return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
428 else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
429 return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
435 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
438 * g_inet_address_new_from_bytes:
439 * @bytes: (array) (element-type guint8): raw address data
440 * @family: the address family of @bytes
442 * Creates a new #GInetAddress from the given @family and @bytes.
443 * @bytes should be 4 bytes for %G_SOCKET_FAMILY_IPV4 and 16 bytes for
444 * %G_SOCKET_FAMILY_IPV6.
446 * Returns: a new #GInetAddress corresponding to @family and @bytes.
451 g_inet_address_new_from_bytes (const guint8 *bytes,
452 GSocketFamily family)
454 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
456 return g_object_new (G_TYPE_INET_ADDRESS,
463 * g_inet_address_new_loopback:
464 * @family: the address family
466 * Creates a #GInetAddress for the loopback address for @family.
468 * Returns: a new #GInetAddress corresponding to the loopback address
474 g_inet_address_new_loopback (GSocketFamily family)
476 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
478 if (family == AF_INET)
480 guint8 addr[4] = {127, 0, 0, 1};
482 return g_inet_address_new_from_bytes (addr, family);
485 return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
489 * g_inet_address_new_any:
490 * @family: the address family
492 * Creates a #GInetAddress for the "any" address (unassigned/"don't
493 * care") for @family.
495 * Returns: a new #GInetAddress corresponding to the "any" address
501 g_inet_address_new_any (GSocketFamily family)
503 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
505 if (family == AF_INET)
507 guint8 addr[4] = {0, 0, 0, 0};
509 return g_inet_address_new_from_bytes (addr, family);
512 return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
517 * g_inet_address_to_string:
518 * @address: a #GInetAddress
520 * Converts @address to string form.
522 * Returns: a representation of @address as a string, which should be
528 g_inet_address_to_string (GInetAddress *address)
530 gchar buffer[INET6_ADDRSTRLEN];
532 DWORD buflen = sizeof (buffer), addrlen;
533 struct sockaddr_storage sa;
534 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
535 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
538 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
541 memset (&sa, 0, sizeof (sa));
542 sa.ss_family = address->priv->family;
543 if (address->priv->family == AF_INET)
545 addrlen = sizeof (*sin);
546 memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
547 sizeof (sin->sin_addr));
551 addrlen = sizeof (*sin6);
552 memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
553 sizeof (sin6->sin6_addr));
555 if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
558 #else /* !G_OS_WIN32 */
560 if (address->priv->family == AF_INET)
561 inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
563 inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
566 return g_strdup (buffer);
570 * g_inet_address_to_bytes: (skip)
571 * @address: a #GInetAddress
573 * Gets the raw binary address data from @address.
575 * Returns: a pointer to an internal array of the bytes in @address,
576 * which should not be modified, stored, or freed. The size of this
577 * array can be gotten with g_inet_address_get_native_size().
582 g_inet_address_to_bytes (GInetAddress *address)
584 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
586 return (guint8 *)&address->priv->addr;
590 * g_inet_address_get_native_size:
591 * @address: a #GInetAddress
593 * Gets the size of the native raw binary address for @address. This
594 * is the size of the data that you get from g_inet_address_to_bytes().
596 * Returns: the number of bytes used for the native version of @address.
601 g_inet_address_get_native_size (GInetAddress *address)
603 if (address->priv->family == AF_INET)
604 return sizeof (address->priv->addr.ipv4);
605 return sizeof (address->priv->addr.ipv6);
609 * g_inet_address_get_family:
610 * @address: a #GInetAddress
612 * Gets @address's family
614 * Returns: @address's family
619 g_inet_address_get_family (GInetAddress *address)
621 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
623 return address->priv->family;
627 * g_inet_address_get_is_any:
628 * @address: a #GInetAddress
630 * Tests whether @address is the "any" address for its family.
632 * Returns: %TRUE if @address is the "any" address for its family.
637 g_inet_address_get_is_any (GInetAddress *address)
639 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
641 if (address->priv->family == AF_INET)
643 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
645 return addr4 == INADDR_ANY;
648 return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
652 * g_inet_address_get_is_loopback:
653 * @address: a #GInetAddress
655 * Tests whether @address is the loopback address for its family.
657 * Returns: %TRUE if @address is the loopback address for its family.
662 g_inet_address_get_is_loopback (GInetAddress *address)
664 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
666 if (address->priv->family == AF_INET)
668 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
671 return ((addr4 & 0xff000000) == 0x7f000000);
674 return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
678 * g_inet_address_get_is_link_local:
679 * @address: a #GInetAddress
681 * Tests whether @address is a link-local address (that is, if it
682 * identifies a host on a local network that is not connected to the
685 * Returns: %TRUE if @address is a link-local address.
690 g_inet_address_get_is_link_local (GInetAddress *address)
692 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
694 if (address->priv->family == AF_INET)
696 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
699 return ((addr4 & 0xffff0000) == 0xa9fe0000);
702 return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
706 * g_inet_address_get_is_site_local:
707 * @address: a #GInetAddress
709 * Tests whether @address is a site-local address such as 10.0.0.1
710 * (that is, the address identifies a host on a local network that can
711 * not be reached directly from the Internet, but which may have
712 * outgoing Internet connectivity via a NAT or firewall).
714 * Returns: %TRUE if @address is a site-local address.
719 g_inet_address_get_is_site_local (GInetAddress *address)
721 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
723 if (address->priv->family == AF_INET)
725 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
727 /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
728 return ((addr4 & 0xff000000) == 0x0a000000 ||
729 (addr4 & 0xfff00000) == 0xac100000 ||
730 (addr4 & 0xffff0000) == 0xc0a80000);
733 return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
737 * g_inet_address_get_is_multicast:
738 * @address: a #GInetAddress
740 * Tests whether @address is a multicast address.
742 * Returns: %TRUE if @address is a multicast address.
747 g_inet_address_get_is_multicast (GInetAddress *address)
749 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
751 if (address->priv->family == AF_INET)
753 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
755 return IN_MULTICAST (addr4);
758 return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
762 * g_inet_address_get_is_mc_global:
763 * @address: a #GInetAddress
765 * Tests whether @address is a global multicast address.
767 * Returns: %TRUE if @address is a global multicast address.
772 g_inet_address_get_is_mc_global (GInetAddress *address)
774 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
776 if (address->priv->family == AF_INET)
779 return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
783 * g_inet_address_get_is_mc_link_local:
784 * @address: a #GInetAddress
786 * Tests whether @address is a link-local multicast address.
788 * Returns: %TRUE if @address is a link-local multicast address.
793 g_inet_address_get_is_mc_link_local (GInetAddress *address)
795 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
797 if (address->priv->family == AF_INET)
800 return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
804 * g_inet_address_get_is_mc_node_local:
805 * @address: a #GInetAddress
807 * Tests whether @address is a node-local multicast address.
809 * Returns: %TRUE if @address is a node-local multicast address.
814 g_inet_address_get_is_mc_node_local (GInetAddress *address)
816 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
818 if (address->priv->family == AF_INET)
821 return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
825 * g_inet_address_get_is_mc_org_local:
826 * @address: a #GInetAddress
828 * Tests whether @address is an organization-local multicast address.
830 * Returns: %TRUE if @address is an organization-local multicast address.
835 g_inet_address_get_is_mc_org_local (GInetAddress *address)
837 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
839 if (address->priv->family == AF_INET)
842 return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
846 * g_inet_address_get_is_mc_site_local:
847 * @address: a #GInetAddress
849 * Tests whether @address is a site-local multicast address.
851 * Returns: %TRUE if @address is a site-local multicast address.
856 g_inet_address_get_is_mc_site_local (GInetAddress *address)
858 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
860 if (address->priv->family == AF_INET)
863 return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
867 * g_inet_address_equal:
868 * @address: A #GInetAddress.
869 * @other_address: Another #GInetAddress.
871 * Checks if two #GInetAddress instances are equal, e.g. the same address.
873 * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise.
878 g_inet_address_equal (GInetAddress *address,
879 GInetAddress *other_address)
881 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
882 g_return_val_if_fail (G_IS_INET_ADDRESS (other_address), FALSE);
884 if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address))
887 if (memcmp (g_inet_address_to_bytes (address),
888 g_inet_address_to_bytes (other_address),
889 g_inet_address_get_native_size (address)) != 0)