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"
38 * SECTION:ginetaddress
39 * @short_description: An IPv4/IPv6 address
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
48 * To actually connect to a remote host, you will need a
49 * #GInetSocketAddress (which includes a #GInetAddress as well as a
56 * An IPv4 or IPv6 internet address.
59 /* Networking initialization function, called from inside the g_once of
60 * g_inet_address_get_type()
63 _g_networking_init (void)
67 if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
68 g_error ("Windows Sockets could not be initialized");
72 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
73 _g_networking_init ();)
75 struct _GInetAddressPrivate
95 PROP_IS_MC_LINK_LOCAL,
96 PROP_IS_MC_NODE_LOCAL,
98 PROP_IS_MC_SITE_LOCAL,
102 g_inet_address_set_property (GObject *object,
107 GInetAddress *address = G_INET_ADDRESS (object);
112 address->priv->family = g_value_get_enum (value);
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));
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 g_inet_address_get_property (GObject *object,
135 GInetAddress *address = G_INET_ADDRESS (object);
140 g_value_set_enum (value, address->priv->family);
144 g_value_set_pointer (value, &address->priv->addr);
148 g_value_set_boolean (value, g_inet_address_get_is_any (address));
151 case PROP_IS_LOOPBACK:
152 g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
155 case PROP_IS_LINK_LOCAL:
156 g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
159 case PROP_IS_SITE_LOCAL:
160 g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
163 case PROP_IS_MULTICAST:
164 g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
167 case PROP_IS_MC_GLOBAL:
168 g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
171 case PROP_IS_MC_LINK_LOCAL:
172 g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
175 case PROP_IS_MC_NODE_LOCAL:
176 g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
179 case PROP_IS_MC_ORG_LOCAL:
180 g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
183 case PROP_IS_MC_SITE_LOCAL:
184 g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 g_inet_address_class_init (GInetAddressClass *klass)
195 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
197 g_type_class_add_private (klass, sizeof (GInetAddressPrivate));
199 gobject_class->set_property = g_inet_address_set_property;
200 gobject_class->get_property = g_inet_address_get_property;
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,
209 G_PARAM_CONSTRUCT_ONLY |
210 G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_BYTES,
213 g_param_spec_pointer ("bytes",
215 P_("The raw address data"),
217 G_PARAM_CONSTRUCT_ONLY |
218 G_PARAM_STATIC_STRINGS));
221 * GInetAddress:is-any:
223 * Whether this is the "any" address for its family.
224 * See g_inet_address_get_is_any().
228 g_object_class_install_property (gobject_class, PROP_IS_ANY,
229 g_param_spec_boolean ("is-any",
231 P_("Whether this is the \"any\" address for its family"),
234 G_PARAM_STATIC_STRINGS));
237 * GInetAddress:is-link-local:
239 * Whether this is a link-local address.
240 * See g_inet_address_get_is_link_local().
244 g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
245 g_param_spec_boolean ("is-link-local",
247 P_("Whether this is a link-local address"),
250 G_PARAM_STATIC_STRINGS));
253 * GInetAddress:is-loopback:
255 * Whether this is the loopback address for its family.
256 * See g_inet_address_get_is_loopback().
260 g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
261 g_param_spec_boolean ("is-loopback",
263 P_("Whether this is the loopback address for its family"),
266 G_PARAM_STATIC_STRINGS));
269 * GInetAddress:is-site-local:
271 * Whether this is a site-local address.
272 * See g_inet_address_get_is_loopback().
276 g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
277 g_param_spec_boolean ("is-site-local",
279 P_("Whether this is a site-local address"),
282 G_PARAM_STATIC_STRINGS));
285 * GInetAddress:is-multicast:
287 * Whether this is a multicast address.
288 * See g_inet_address_get_is_multicast().
292 g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
293 g_param_spec_boolean ("is-multicast",
295 P_("Whether this is a multicast address"),
298 G_PARAM_STATIC_STRINGS));
301 * GInetAddress:is-mc-global:
303 * Whether this is a global multicast address.
304 * See g_inet_address_get_is_mc_global().
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"),
314 G_PARAM_STATIC_STRINGS));
318 * GInetAddress:is-mc-link-local:
320 * Whether this is a link-local multicast address.
321 * See g_inet_address_get_is_mc_link_local().
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"),
331 G_PARAM_STATIC_STRINGS));
334 * GInetAddress:is-mc-node-local:
336 * Whether this is a node-local multicast address.
337 * See g_inet_address_get_is_mc_node_local().
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"),
347 G_PARAM_STATIC_STRINGS));
350 * GInetAddress:is-mc-org-local:
352 * Whether this is an organization-local multicast address.
353 * See g_inet_address_get_is_mc_org_local().
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"),
363 G_PARAM_STATIC_STRINGS));
366 * GInetAddress:is-mc-site-local:
368 * Whether this is a site-local multicast address.
369 * See g_inet_address_get_is_mc_site_local().
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"),
379 G_PARAM_STATIC_STRINGS));
383 g_inet_address_init (GInetAddress *address)
385 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
387 GInetAddressPrivate);
391 * g_inet_address_new_from_string:
392 * @string: a string representation of an IP address
394 * Parses @string as an IP address and creates a new #GInetAddress.
396 * Returns: a new #GInetAddress corresponding to @string, or %NULL if
397 * @string could not be parsed.
402 g_inet_address_new_from_string (const gchar *string)
405 struct sockaddr_storage sa;
406 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
407 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
409 #else /* !G_OS_WIN32 */
410 struct in_addr in_addr;
411 struct in6_addr in6_addr;
414 /* Make sure _g_networking_init() has been called */
415 g_type_ensure (G_TYPE_INET_ADDRESS);
418 memset (&sa, 0, 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);
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 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));
552 addrlen = sizeof (*sin6);
553 memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
554 sizeof (sin6->sin6_addr));
557 if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
560 #else /* !G_OS_WIN32 */
562 if (address->priv->family == AF_INET)
563 inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
565 inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
568 return g_strdup (buffer);
572 * g_inet_address_to_bytes: (skip)
573 * @address: a #GInetAddress
575 * Gets the raw binary address data from @address.
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().
584 g_inet_address_to_bytes (GInetAddress *address)
586 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
588 return (guint8 *)&address->priv->addr;
592 * g_inet_address_get_native_size:
593 * @address: a #GInetAddress
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().
598 * Returns: the number of bytes used for the native version of @address.
603 g_inet_address_get_native_size (GInetAddress *address)
605 if (address->priv->family == AF_INET)
606 return sizeof (address->priv->addr.ipv4);
607 return sizeof (address->priv->addr.ipv6);
611 * g_inet_address_get_family:
612 * @address: a #GInetAddress
614 * Gets @address's family
616 * Returns: @address's family
621 g_inet_address_get_family (GInetAddress *address)
623 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
625 return address->priv->family;
629 * g_inet_address_get_is_any:
630 * @address: a #GInetAddress
632 * Tests whether @address is the "any" address for its family.
634 * Returns: %TRUE if @address is the "any" address for its family.
639 g_inet_address_get_is_any (GInetAddress *address)
641 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
643 if (address->priv->family == AF_INET)
645 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
647 return addr4 == INADDR_ANY;
650 return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
654 * g_inet_address_get_is_loopback:
655 * @address: a #GInetAddress
657 * Tests whether @address is the loopback address for its family.
659 * Returns: %TRUE if @address is the loopback address for its family.
664 g_inet_address_get_is_loopback (GInetAddress *address)
666 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
668 if (address->priv->family == AF_INET)
670 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
673 return ((addr4 & 0xff000000) == 0x7f000000);
676 return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
680 * g_inet_address_get_is_link_local:
681 * @address: a #GInetAddress
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
687 * Returns: %TRUE if @address is a link-local address.
692 g_inet_address_get_is_link_local (GInetAddress *address)
694 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
696 if (address->priv->family == AF_INET)
698 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
701 return ((addr4 & 0xffff0000) == 0xa9fe0000);
704 return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
708 * g_inet_address_get_is_site_local:
709 * @address: a #GInetAddress
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).
716 * Returns: %TRUE if @address is a site-local address.
721 g_inet_address_get_is_site_local (GInetAddress *address)
723 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
725 if (address->priv->family == AF_INET)
727 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
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);
735 return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
739 * g_inet_address_get_is_multicast:
740 * @address: a #GInetAddress
742 * Tests whether @address is a multicast address.
744 * Returns: %TRUE if @address is a multicast address.
749 g_inet_address_get_is_multicast (GInetAddress *address)
751 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
753 if (address->priv->family == AF_INET)
755 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
757 return IN_MULTICAST (addr4);
760 return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
764 * g_inet_address_get_is_mc_global:
765 * @address: a #GInetAddress
767 * Tests whether @address is a global multicast address.
769 * Returns: %TRUE if @address is a global multicast address.
774 g_inet_address_get_is_mc_global (GInetAddress *address)
776 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
778 if (address->priv->family == AF_INET)
781 return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
785 * g_inet_address_get_is_mc_link_local:
786 * @address: a #GInetAddress
788 * Tests whether @address is a link-local multicast address.
790 * Returns: %TRUE if @address is a link-local multicast address.
795 g_inet_address_get_is_mc_link_local (GInetAddress *address)
797 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
799 if (address->priv->family == AF_INET)
802 return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
806 * g_inet_address_get_is_mc_node_local:
807 * @address: a #GInetAddress
809 * Tests whether @address is a node-local multicast address.
811 * Returns: %TRUE if @address is a node-local multicast address.
816 g_inet_address_get_is_mc_node_local (GInetAddress *address)
818 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
820 if (address->priv->family == AF_INET)
823 return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
827 * g_inet_address_get_is_mc_org_local:
828 * @address: a #GInetAddress
830 * Tests whether @address is an organization-local multicast address.
832 * Returns: %TRUE if @address is an organization-local multicast address.
837 g_inet_address_get_is_mc_org_local (GInetAddress *address)
839 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
841 if (address->priv->family == AF_INET)
844 return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
848 * g_inet_address_get_is_mc_site_local:
849 * @address: a #GInetAddress
851 * Tests whether @address is a site-local multicast address.
853 * Returns: %TRUE if @address is a site-local multicast address.
858 g_inet_address_get_is_mc_site_local (GInetAddress *address)
860 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
862 if (address->priv->family == AF_INET)
865 return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
869 * g_inet_address_equal:
870 * @address: A #GInetAddress.
871 * @other_address: Another #GInetAddress.
873 * Checks if two #GInetAddress instances are equal, e.g. the same address.
875 * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise.
880 g_inet_address_equal (GInetAddress *address,
881 GInetAddress *other_address)
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);
886 if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address))
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)