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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
30 #include "ginetaddress.h"
32 #include "gioenumtypes.h"
34 #include "gnetworkingprivate.h"
36 struct _GInetAddressPrivate
46 * SECTION:ginetaddress
47 * @short_description: An IPv4/IPv6 address
49 * #GInetAddress represents an IPv4 or IPv6 internet address. Use
50 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
51 * look up the #GInetAddress for a hostname. Use
52 * g_resolver_lookup_by_address() or
53 * g_resolver_lookup_by_address_async() to look up the hostname for a
56 * To actually connect to a remote host, you will need a
57 * #GInetSocketAddress (which includes a #GInetAddress as well as a
64 * An IPv4 or IPv6 internet address.
67 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
68 G_ADD_PRIVATE (GInetAddress)
69 g_networking_init ();)
82 PROP_IS_MC_LINK_LOCAL,
83 PROP_IS_MC_NODE_LOCAL,
85 PROP_IS_MC_SITE_LOCAL,
89 g_inet_address_set_property (GObject *object,
94 GInetAddress *address = G_INET_ADDRESS (object);
99 address->priv->family = g_value_get_enum (value);
103 memcpy (&address->priv->addr, g_value_get_pointer (value),
104 address->priv->family == AF_INET ?
105 sizeof (address->priv->addr.ipv4) :
106 sizeof (address->priv->addr.ipv6));
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 g_inet_address_get_property (GObject *object,
122 GInetAddress *address = G_INET_ADDRESS (object);
127 g_value_set_enum (value, address->priv->family);
131 g_value_set_pointer (value, &address->priv->addr);
135 g_value_set_boolean (value, g_inet_address_get_is_any (address));
138 case PROP_IS_LOOPBACK:
139 g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
142 case PROP_IS_LINK_LOCAL:
143 g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
146 case PROP_IS_SITE_LOCAL:
147 g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
150 case PROP_IS_MULTICAST:
151 g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
154 case PROP_IS_MC_GLOBAL:
155 g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
158 case PROP_IS_MC_LINK_LOCAL:
159 g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
162 case PROP_IS_MC_NODE_LOCAL:
163 g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
166 case PROP_IS_MC_ORG_LOCAL:
167 g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
170 case PROP_IS_MC_SITE_LOCAL:
171 g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 g_inet_address_class_init (GInetAddressClass *klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->set_property = g_inet_address_set_property;
185 gobject_class->get_property = g_inet_address_get_property;
187 g_object_class_install_property (gobject_class, PROP_FAMILY,
188 g_param_spec_enum ("family",
189 P_("Address family"),
190 P_("The address family (IPv4 or IPv6)"),
191 G_TYPE_SOCKET_FAMILY,
192 G_SOCKET_FAMILY_INVALID,
194 G_PARAM_CONSTRUCT_ONLY |
195 G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_BYTES,
198 g_param_spec_pointer ("bytes",
200 P_("The raw address data"),
202 G_PARAM_CONSTRUCT_ONLY |
203 G_PARAM_STATIC_STRINGS));
206 * GInetAddress:is-any:
208 * Whether this is the "any" address for its family.
209 * See g_inet_address_get_is_any().
213 g_object_class_install_property (gobject_class, PROP_IS_ANY,
214 g_param_spec_boolean ("is-any",
216 P_("Whether this is the \"any\" address for its family"),
219 G_PARAM_STATIC_STRINGS));
222 * GInetAddress:is-link-local:
224 * Whether this is a link-local address.
225 * See g_inet_address_get_is_link_local().
229 g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
230 g_param_spec_boolean ("is-link-local",
232 P_("Whether this is a link-local address"),
235 G_PARAM_STATIC_STRINGS));
238 * GInetAddress:is-loopback:
240 * Whether this is the loopback address for its family.
241 * See g_inet_address_get_is_loopback().
245 g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
246 g_param_spec_boolean ("is-loopback",
248 P_("Whether this is the loopback address for its family"),
251 G_PARAM_STATIC_STRINGS));
254 * GInetAddress:is-site-local:
256 * Whether this is a site-local address.
257 * See g_inet_address_get_is_loopback().
261 g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
262 g_param_spec_boolean ("is-site-local",
264 P_("Whether this is a site-local address"),
267 G_PARAM_STATIC_STRINGS));
270 * GInetAddress:is-multicast:
272 * Whether this is a multicast address.
273 * See g_inet_address_get_is_multicast().
277 g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
278 g_param_spec_boolean ("is-multicast",
280 P_("Whether this is a multicast address"),
283 G_PARAM_STATIC_STRINGS));
286 * GInetAddress:is-mc-global:
288 * Whether this is a global multicast address.
289 * See g_inet_address_get_is_mc_global().
293 g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
294 g_param_spec_boolean ("is-mc-global",
295 P_("Is multicast global"),
296 P_("Whether this is a global multicast address"),
299 G_PARAM_STATIC_STRINGS));
303 * GInetAddress:is-mc-link-local:
305 * Whether this is a link-local multicast address.
306 * See g_inet_address_get_is_mc_link_local().
310 g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
311 g_param_spec_boolean ("is-mc-link-local",
312 P_("Is multicast link-local"),
313 P_("Whether this is a link-local multicast address"),
316 G_PARAM_STATIC_STRINGS));
319 * GInetAddress:is-mc-node-local:
321 * Whether this is a node-local multicast address.
322 * See g_inet_address_get_is_mc_node_local().
326 g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
327 g_param_spec_boolean ("is-mc-node-local",
328 P_("Is multicast node-local"),
329 P_("Whether this is a node-local multicast address"),
332 G_PARAM_STATIC_STRINGS));
335 * GInetAddress:is-mc-org-local:
337 * Whether this is an organization-local multicast address.
338 * See g_inet_address_get_is_mc_org_local().
342 g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
343 g_param_spec_boolean ("is-mc-org-local",
344 P_("Is multicast org-local"),
345 P_("Whether this is an organization-local multicast address"),
348 G_PARAM_STATIC_STRINGS));
351 * GInetAddress:is-mc-site-local:
353 * Whether this is a site-local multicast address.
354 * See g_inet_address_get_is_mc_site_local().
358 g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
359 g_param_spec_boolean ("is-mc-site-local",
360 P_("Is multicast site-local"),
361 P_("Whether this is a site-local multicast address"),
364 G_PARAM_STATIC_STRINGS));
368 g_inet_address_init (GInetAddress *address)
370 address->priv = g_inet_address_get_instance_private (address);
374 * g_inet_address_new_from_string:
375 * @string: a string representation of an IP address
377 * Parses @string as an IP address and creates a new #GInetAddress.
379 * Returns: a new #GInetAddress corresponding to @string, or %NULL if
380 * @string could not be parsed.
385 g_inet_address_new_from_string (const gchar *string)
388 struct sockaddr_storage sa;
389 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
390 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
392 #else /* !G_OS_WIN32 */
393 struct in_addr in_addr;
394 struct in6_addr in6_addr;
397 /* If this GInetAddress is the first networking-related object to be
398 * created, then we won't have called g_networking_init() yet at
401 g_networking_init ();
404 /* We need to make sure to not pass a string of the form
405 * "IPv4addr:port" or "[IPv6addr]:port" to WSAStringToAddress(),
406 * since it would accept them (returning both the address and the
407 * port), but we only want to accept standalone IP addresses. (In
408 * the IPv6 case, WINE actually only checks for the ']', not the
409 * '[', which is why we do the same here.)
411 if (!strchr (string, ':'))
414 if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
415 return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
418 if (!strchr (string, ']'))
421 if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
422 return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
425 #else /* !G_OS_WIN32 */
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);
436 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
439 * g_inet_address_new_from_bytes:
440 * @bytes: (array) (element-type guint8): raw address data
441 * @family: the address family of @bytes
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.
447 * Returns: a new #GInetAddress corresponding to @family and @bytes.
452 g_inet_address_new_from_bytes (const guint8 *bytes,
453 GSocketFamily family)
455 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
457 return g_object_new (G_TYPE_INET_ADDRESS,
464 * g_inet_address_new_loopback:
465 * @family: the address family
467 * Creates a #GInetAddress for the loopback address for @family.
469 * Returns: a new #GInetAddress corresponding to the loopback address
475 g_inet_address_new_loopback (GSocketFamily family)
477 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
479 if (family == AF_INET)
481 guint8 addr[4] = {127, 0, 0, 1};
483 return g_inet_address_new_from_bytes (addr, family);
486 return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
490 * g_inet_address_new_any:
491 * @family: the address family
493 * Creates a #GInetAddress for the "any" address (unassigned/"don't
494 * care") for @family.
496 * Returns: a new #GInetAddress corresponding to the "any" address
502 g_inet_address_new_any (GSocketFamily family)
504 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
506 if (family == AF_INET)
508 guint8 addr[4] = {0, 0, 0, 0};
510 return g_inet_address_new_from_bytes (addr, family);
513 return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
518 * g_inet_address_to_string:
519 * @address: a #GInetAddress
521 * Converts @address to string form.
523 * Returns: a representation of @address as a string, which should be
529 g_inet_address_to_string (GInetAddress *address)
531 gchar buffer[INET6_ADDRSTRLEN];
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;
539 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
542 memset (&sa, 0, sizeof (sa));
543 sa.ss_family = address->priv->family;
544 if (address->priv->family == AF_INET)
546 addrlen = sizeof (*sin);
547 memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
548 sizeof (sin->sin_addr));
552 addrlen = sizeof (*sin6);
553 memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
554 sizeof (sin6->sin6_addr));
556 if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
559 #else /* !G_OS_WIN32 */
561 if (address->priv->family == AF_INET)
562 inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
564 inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
567 return g_strdup (buffer);
571 * g_inet_address_to_bytes: (skip)
572 * @address: a #GInetAddress
574 * Gets the raw binary address data from @address.
576 * Returns: a pointer to an internal array of the bytes in @address,
577 * which should not be modified, stored, or freed. The size of this
578 * array can be gotten with g_inet_address_get_native_size().
583 g_inet_address_to_bytes (GInetAddress *address)
585 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
587 return (guint8 *)&address->priv->addr;
591 * g_inet_address_get_native_size:
592 * @address: a #GInetAddress
594 * Gets the size of the native raw binary address for @address. This
595 * is the size of the data that you get from g_inet_address_to_bytes().
597 * Returns: the number of bytes used for the native version of @address.
602 g_inet_address_get_native_size (GInetAddress *address)
604 if (address->priv->family == AF_INET)
605 return sizeof (address->priv->addr.ipv4);
606 return sizeof (address->priv->addr.ipv6);
610 * g_inet_address_get_family:
611 * @address: a #GInetAddress
613 * Gets @address's family
615 * Returns: @address's family
620 g_inet_address_get_family (GInetAddress *address)
622 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
624 return address->priv->family;
628 * g_inet_address_get_is_any:
629 * @address: a #GInetAddress
631 * Tests whether @address is the "any" address for its family.
633 * Returns: %TRUE if @address is the "any" address for its family.
638 g_inet_address_get_is_any (GInetAddress *address)
640 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
642 if (address->priv->family == AF_INET)
644 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
646 return addr4 == INADDR_ANY;
649 return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
653 * g_inet_address_get_is_loopback:
654 * @address: a #GInetAddress
656 * Tests whether @address is the loopback address for its family.
658 * Returns: %TRUE if @address is the loopback address for its family.
663 g_inet_address_get_is_loopback (GInetAddress *address)
665 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
667 if (address->priv->family == AF_INET)
669 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
672 return ((addr4 & 0xff000000) == 0x7f000000);
675 return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
679 * g_inet_address_get_is_link_local:
680 * @address: a #GInetAddress
682 * Tests whether @address is a link-local address (that is, if it
683 * identifies a host on a local network that is not connected to the
686 * Returns: %TRUE if @address is a link-local address.
691 g_inet_address_get_is_link_local (GInetAddress *address)
693 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
695 if (address->priv->family == AF_INET)
697 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
700 return ((addr4 & 0xffff0000) == 0xa9fe0000);
703 return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
707 * g_inet_address_get_is_site_local:
708 * @address: a #GInetAddress
710 * Tests whether @address is a site-local address such as 10.0.0.1
711 * (that is, the address identifies a host on a local network that can
712 * not be reached directly from the Internet, but which may have
713 * outgoing Internet connectivity via a NAT or firewall).
715 * Returns: %TRUE if @address is a site-local address.
720 g_inet_address_get_is_site_local (GInetAddress *address)
722 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
724 if (address->priv->family == AF_INET)
726 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
728 /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
729 return ((addr4 & 0xff000000) == 0x0a000000 ||
730 (addr4 & 0xfff00000) == 0xac100000 ||
731 (addr4 & 0xffff0000) == 0xc0a80000);
734 return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
738 * g_inet_address_get_is_multicast:
739 * @address: a #GInetAddress
741 * Tests whether @address is a multicast address.
743 * Returns: %TRUE if @address is a multicast address.
748 g_inet_address_get_is_multicast (GInetAddress *address)
750 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
752 if (address->priv->family == AF_INET)
754 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
756 return IN_MULTICAST (addr4);
759 return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
763 * g_inet_address_get_is_mc_global:
764 * @address: a #GInetAddress
766 * Tests whether @address is a global multicast address.
768 * Returns: %TRUE if @address is a global multicast address.
773 g_inet_address_get_is_mc_global (GInetAddress *address)
775 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
777 if (address->priv->family == AF_INET)
780 return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
784 * g_inet_address_get_is_mc_link_local:
785 * @address: a #GInetAddress
787 * Tests whether @address is a link-local multicast address.
789 * Returns: %TRUE if @address is a link-local multicast address.
794 g_inet_address_get_is_mc_link_local (GInetAddress *address)
796 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
798 if (address->priv->family == AF_INET)
801 return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
805 * g_inet_address_get_is_mc_node_local:
806 * @address: a #GInetAddress
808 * Tests whether @address is a node-local multicast address.
810 * Returns: %TRUE if @address is a node-local multicast address.
815 g_inet_address_get_is_mc_node_local (GInetAddress *address)
817 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
819 if (address->priv->family == AF_INET)
822 return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
826 * g_inet_address_get_is_mc_org_local:
827 * @address: a #GInetAddress
829 * Tests whether @address is an organization-local multicast address.
831 * Returns: %TRUE if @address is an organization-local multicast address.
836 g_inet_address_get_is_mc_org_local (GInetAddress *address)
838 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
840 if (address->priv->family == AF_INET)
843 return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
847 * g_inet_address_get_is_mc_site_local:
848 * @address: a #GInetAddress
850 * Tests whether @address is a site-local multicast address.
852 * Returns: %TRUE if @address is a site-local multicast address.
857 g_inet_address_get_is_mc_site_local (GInetAddress *address)
859 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
861 if (address->priv->family == AF_INET)
864 return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
868 * g_inet_address_equal:
869 * @address: A #GInetAddress.
870 * @other_address: Another #GInetAddress.
872 * Checks if two #GInetAddress instances are equal, e.g. the same address.
874 * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise.
879 g_inet_address_equal (GInetAddress *address,
880 GInetAddress *other_address)
882 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
883 g_return_val_if_fail (G_IS_INET_ADDRESS (other_address), FALSE);
885 if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address))
888 if (memcmp (g_inet_address_to_bytes (address),
889 g_inet_address_to_bytes (other_address),
890 g_inet_address_get_native_size (address)) != 0)