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>
27 #include "ginetaddress.h"
29 #include "gioenumtypes.h"
31 #include "gnetworkingprivate.h"
36 * SECTION:ginetaddress
37 * @short_description: An IPv4/IPv6 address
39 * #GInetAddress represents an IPv4 or IPv6 internet address. Use
40 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
41 * look up the #GInetAddress for a hostname. Use
42 * g_resolver_lookup_by_address() or
43 * g_resolver_lookup_by_address_async() to look up the hostname for a
46 * To actually connect to a remote host, you will need a
47 * #GInetSocketAddress (which includes a #GInetAddress as well as a
54 * An IPv4 or IPv6 internet address.
57 /* Networking initialization function, called from inside the g_once of
58 * g_inet_address_get_type()
61 _g_networking_init (void)
65 if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
66 g_error ("Windows Sockets could not be initialized");
70 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
71 _g_networking_init ();)
73 struct _GInetAddressPrivate
93 PROP_IS_MC_LINK_LOCAL,
94 PROP_IS_MC_NODE_LOCAL,
96 PROP_IS_MC_SITE_LOCAL,
100 g_inet_address_set_property (GObject *object,
105 GInetAddress *address = G_INET_ADDRESS (object);
110 address->priv->family = g_value_get_enum (value);
114 memcpy (&address->priv->addr, g_value_get_pointer (value),
115 address->priv->family == AF_INET ?
116 sizeof (address->priv->addr.ipv4) :
117 sizeof (address->priv->addr.ipv6));
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 g_inet_address_get_property (GObject *object,
133 GInetAddress *address = G_INET_ADDRESS (object);
138 g_value_set_enum (value, address->priv->family);
142 g_value_set_pointer (value, &address->priv->addr);
146 g_value_set_boolean (value, g_inet_address_get_is_any (address));
149 case PROP_IS_LOOPBACK:
150 g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
153 case PROP_IS_LINK_LOCAL:
154 g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
157 case PROP_IS_SITE_LOCAL:
158 g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
161 case PROP_IS_MULTICAST:
162 g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
165 case PROP_IS_MC_GLOBAL:
166 g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
169 case PROP_IS_MC_LINK_LOCAL:
170 g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
173 case PROP_IS_MC_NODE_LOCAL:
174 g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
177 case PROP_IS_MC_ORG_LOCAL:
178 g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
181 case PROP_IS_MC_SITE_LOCAL:
182 g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 g_inet_address_class_init (GInetAddressClass *klass)
193 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
195 g_type_class_add_private (klass, sizeof (GInetAddressPrivate));
197 gobject_class->set_property = g_inet_address_set_property;
198 gobject_class->get_property = g_inet_address_get_property;
200 g_object_class_install_property (gobject_class, PROP_FAMILY,
201 g_param_spec_enum ("family",
203 _("The address family (IPv4 or IPv6)"),
204 G_TYPE_SOCKET_FAMILY,
205 G_SOCKET_FAMILY_INVALID,
206 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
208 g_object_class_install_property (gobject_class, PROP_BYTES,
209 g_param_spec_pointer ("bytes",
211 _("The raw address data"),
212 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
214 g_object_class_install_property (gobject_class, PROP_IS_ANY,
215 g_param_spec_boolean ("is-any",
217 _("See g_inet_address_get_is_any()"),
219 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
221 g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
222 g_param_spec_boolean ("is-link-local",
224 _("See g_inet_address_get_is_link_local()"),
226 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
228 g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
229 g_param_spec_boolean ("is-loopback",
231 _("See g_inet_address_get_is_loopback()"),
233 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
235 g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
236 g_param_spec_boolean ("is-site-local",
238 _("See g_inet_address_get_is_site_local()"),
240 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
242 g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
243 g_param_spec_boolean ("is-multicast",
245 _("See g_inet_address_get_is_multicast()"),
247 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
249 g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
250 g_param_spec_boolean ("is-mc-global",
251 _("Is multicast global"),
252 _("See g_inet_address_get_is_mc_global()"),
254 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
256 g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
257 g_param_spec_boolean ("is-mc-link-local",
258 _("Is multicast link-local"),
259 _("See g_inet_address_get_is_mc_link_local()"),
261 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
263 g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
264 g_param_spec_boolean ("is-mc-node-local",
265 _("Is multicast node-local"),
266 _("See g_inet_address_get_is_mc_node_local()"),
268 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
270 g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
271 g_param_spec_boolean ("is-mc-org-local",
272 _("Is multicast org-local"),
273 _("See g_inet_address_get_is_mc_org_local()"),
275 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
277 g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
278 g_param_spec_boolean ("is-mc-site-local",
279 _("Is multicast site-local"),
280 _("See g_inet_address_get_is_mc_site_local()"),
282 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
286 g_inet_address_init (GInetAddress *address)
288 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
290 GInetAddressPrivate);
294 * g_inet_address_new_from_string:
295 * @string: a string representation of an IP address
297 * Parses @string as an IP address and creates a new #GInetAddress.
299 * Returns: a new #GInetAddress corresponding to @string, or %NULL if
300 * @string could not be parsed.
305 g_inet_address_new_from_string (const gchar *string)
308 struct sockaddr_storage sa;
309 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
310 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
312 #else /* !G_OS_WIN32 */
313 struct in_addr in_addr;
314 struct in6_addr in6_addr;
317 /* Make sure _g_networking_init() has been called */
318 (void) g_inet_address_get_type ();
321 memset (&sa, 0, sizeof (sa));
323 if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
324 return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
325 else if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
326 return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
328 #else /* !G_OS_WIN32 */
330 if (inet_pton (AF_INET, string, &in_addr) > 0)
331 return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
332 else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
333 return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
339 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
342 * g_inet_address_new_from_bytes:
343 * @bytes: raw address data
344 * @family: the address family of @bytes
346 * Creates a new #GInetAddress from the given @family and @bytes.
347 * @bytes should be 4 bytes for %G_INET_ADDRESS_IPV4 and 16 bytes for
348 * %G_INET_ADDRESS_IPV6.
350 * Returns: a new #GInetAddress corresponding to @family and @bytes.
355 g_inet_address_new_from_bytes (const guint8 *bytes,
356 GSocketFamily family)
358 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
360 return g_object_new (G_TYPE_INET_ADDRESS,
367 * g_inet_address_new_loopback:
368 * @family: the address family
370 * Creates a #GInetAddress for the loopback address for @family.
372 * Returns: a new #GInetAddress corresponding to the loopback address
378 g_inet_address_new_loopback (GSocketFamily family)
380 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
382 if (family == AF_INET)
384 guint8 addr[4] = {127, 0, 0, 1};
386 return g_inet_address_new_from_bytes (addr, family);
389 return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
393 * g_inet_address_new_any:
394 * @family: the address family
396 * Creates a #GInetAddress for the "any" address (unassigned/"don't
397 * care") for @family.
399 * Returns: a new #GInetAddress corresponding to the "any" address
405 g_inet_address_new_any (GSocketFamily family)
407 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
409 if (family == AF_INET)
411 guint8 addr[4] = {0, 0, 0, 0};
413 return g_inet_address_new_from_bytes (addr, family);
416 return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
421 * g_inet_address_to_string:
422 * @address: a #GInetAddress
424 * Converts @address to string form.
426 * Returns: a representation of @address as a string, which should be
432 g_inet_address_to_string (GInetAddress *address)
434 gchar buffer[INET6_ADDRSTRLEN];
436 DWORD buflen = sizeof (buffer), addrlen;
437 struct sockaddr_storage sa;
438 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
439 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
442 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
445 sa.ss_family = address->priv->family;
446 if (address->priv->family == AF_INET)
448 addrlen = sizeof (*sin);
449 memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
450 sizeof (sin->sin_addr));
455 addrlen = sizeof (*sin6);
456 memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
457 sizeof (sin6->sin6_addr));
460 if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
463 #else /* !G_OS_WIN32 */
465 if (address->priv->family == AF_INET)
466 inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
468 inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
471 return g_strdup (buffer);
475 * g_inet_address_to_bytes:
476 * @address: a #GInetAddress
478 * Gets the raw binary address data from @address.
480 * Returns: a pointer to an internal array of the bytes in @address,
481 * which should not be modified, stored, or freed.
486 g_inet_address_to_bytes (GInetAddress *address)
488 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
490 return (guint8 *)&address->priv->addr;
494 * g_inet_address_get_family:
495 * @address: a #GInetAddress
497 * Gets @address's family
499 * Returns: @address's family
504 g_inet_address_get_family (GInetAddress *address)
506 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
508 return address->priv->family;
512 * g_inet_address_get_is_any:
513 * @address: a #GInetAddress
515 * Tests whether @address is the "any" address for its family.
517 * Returns: %TRUE if @address is the "any" address for its family.
522 g_inet_address_get_is_any (GInetAddress *address)
524 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
526 if (address->priv->family == AF_INET)
528 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
530 return addr4 == INADDR_ANY;
533 return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6.s6_addr);
537 * g_inet_address_get_is_loopback:
538 * @address: a #GInetAddress
540 * Tests whether @address is the loopback address for its family.
542 * Returns: %TRUE if @address is the loopback address for its family.
547 g_inet_address_get_is_loopback (GInetAddress *address)
549 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
551 if (address->priv->family == AF_INET)
553 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
556 return ((addr4 & 0xff000000) == 0x7f000000);
559 return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6.s6_addr);
563 * g_inet_address_get_is_link_local:
564 * @address: a #GInetAddress
566 * Tests whether @address is a link-local address (that is, if it
567 * identifies a host on a local network that is not connected to the
570 * Returns: %TRUE if @address is a link-local address.
575 g_inet_address_get_is_link_local (GInetAddress *address)
577 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
579 if (address->priv->family == AF_INET)
581 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
584 return ((addr4 & 0xffff0000) == 0xa9fe0000);
587 return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6.s6_addr);
591 * g_inet_address_get_is_site_local:
592 * @address: a #GInetAddress
594 * Tests whether @address is a site-local address such as 10.0.0.1
595 * (that is, the address identifies a host on a local network that can
596 * not be reached directly from the Internet, but which may have
597 * outgoing Internet connectivity via a NAT or firewall).
599 * Returns: %TRUE if @address is a site-local address.
604 g_inet_address_get_is_site_local (GInetAddress *address)
606 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
608 if (address->priv->family == AF_INET)
610 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
612 /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
613 return ((addr4 & 0xff000000) == 0x0a000000 ||
614 (addr4 & 0xfff00000) == 0xac100000 ||
615 (addr4 & 0xffff0000) == 0xc0a80000);
618 return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6.s6_addr);
622 * g_inet_address_get_is_multicast:
623 * @address: a #GInetAddress
625 * Tests whether @address is a multicast address.
627 * Returns: %TRUE if @address is a multicast address.
632 g_inet_address_get_is_multicast (GInetAddress *address)
634 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
636 if (address->priv->family == AF_INET)
638 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
640 return IN_MULTICAST (addr4);
643 return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6.s6_addr);
647 * g_inet_address_get_is_mc_global:
648 * @address: a #GInetAddress
650 * Tests whether @address is a global multicast address.
652 * Returns: %TRUE if @address is a global multicast address.
657 g_inet_address_get_is_mc_global (GInetAddress *address)
659 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
661 if (address->priv->family == AF_INET)
664 return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6.s6_addr);
668 * g_inet_address_get_is_mc_link_local:
669 * @address: a #GInetAddress
671 * Tests whether @address is a link-local multicast address.
673 * Returns: %TRUE if @address is a link-local multicast address.
678 g_inet_address_get_is_mc_link_local (GInetAddress *address)
680 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
682 if (address->priv->family == AF_INET)
685 return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6.s6_addr);
689 * g_inet_address_get_is_mc_node_local:
690 * @address: a #GInetAddress
692 * Tests whether @address is a node-local multicast address.
694 * Returns: %TRUE if @address is a node-local multicast address.
699 g_inet_address_get_is_mc_node_local (GInetAddress *address)
701 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
703 if (address->priv->family == AF_INET)
706 return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6.s6_addr);
710 * g_inet_address_get_is_mc_org_local:
711 * @address: a #GInetAddress
713 * Tests whether @address is an organization-local multicast address.
715 * Returns: %TRUE if @address is an organization-local multicast address.
720 g_inet_address_get_is_mc_org_local (GInetAddress *address)
722 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
724 if (address->priv->family == AF_INET)
727 return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6.s6_addr);
731 * g_inet_address_get_is_mc_site_local:
732 * @address: a #GInetAddress
734 * Tests whether @address is a site-local multicast address.
736 * Returns: %TRUE if @address is a site-local multicast address.
741 g_inet_address_get_is_mc_site_local (GInetAddress *address)
743 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
745 if (address->priv->family == AF_INET)
748 return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6.s6_addr);
751 #define __G_INET_ADDRESS_C__
752 #include "gioaliasdef.c"