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"
39 * SECTION:ginetaddress
40 * @short_description: An IPv4/IPv6 address
42 * #GInetAddress represents an IPv4 or IPv6 internet address. Use
43 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
44 * look up the #GInetAddress for a hostname. Use
45 * g_resolver_lookup_by_address() or
46 * g_resolver_lookup_by_address_async() to look up the hostname for a
49 * To actually connect to a remote host, you will need a
50 * #GInetSocketAddress (which includes a #GInetAddress as well as a
57 * An IPv4 or IPv6 internet address.
60 /* Networking initialization function, called from inside the g_once of
61 * g_inet_address_get_type()
64 _g_networking_init (void)
68 if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
69 g_error ("Windows Sockets could not be initialized");
73 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
74 _g_networking_init ();)
76 struct _GInetAddressPrivate
96 PROP_IS_MC_LINK_LOCAL,
97 PROP_IS_MC_NODE_LOCAL,
99 PROP_IS_MC_SITE_LOCAL,
103 g_inet_address_set_property (GObject *object,
108 GInetAddress *address = G_INET_ADDRESS (object);
113 address->priv->family = g_value_get_enum (value);
117 memcpy (&address->priv->addr, g_value_get_pointer (value),
118 address->priv->family == AF_INET ?
119 sizeof (address->priv->addr.ipv4) :
120 sizeof (address->priv->addr.ipv6));
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 g_inet_address_get_property (GObject *object,
136 GInetAddress *address = G_INET_ADDRESS (object);
141 g_value_set_enum (value, address->priv->family);
145 g_value_set_pointer (value, &address->priv->addr);
149 g_value_set_boolean (value, g_inet_address_get_is_any (address));
152 case PROP_IS_LOOPBACK:
153 g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
156 case PROP_IS_LINK_LOCAL:
157 g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
160 case PROP_IS_SITE_LOCAL:
161 g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
164 case PROP_IS_MULTICAST:
165 g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
168 case PROP_IS_MC_GLOBAL:
169 g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
172 case PROP_IS_MC_LINK_LOCAL:
173 g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
176 case PROP_IS_MC_NODE_LOCAL:
177 g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
180 case PROP_IS_MC_ORG_LOCAL:
181 g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
184 case PROP_IS_MC_SITE_LOCAL:
185 g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 g_inet_address_class_init (GInetAddressClass *klass)
196 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
198 g_type_class_add_private (klass, sizeof (GInetAddressPrivate));
200 gobject_class->set_property = g_inet_address_set_property;
201 gobject_class->get_property = g_inet_address_get_property;
203 g_object_class_install_property (gobject_class, PROP_FAMILY,
204 g_param_spec_enum ("family",
205 P_("Address family"),
206 P_("The address family (IPv4 or IPv6)"),
207 G_TYPE_SOCKET_FAMILY,
208 G_SOCKET_FAMILY_INVALID,
209 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
211 g_object_class_install_property (gobject_class, PROP_BYTES,
212 g_param_spec_pointer ("bytes",
214 P_("The raw address data"),
215 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
217 g_object_class_install_property (gobject_class, PROP_IS_ANY,
218 g_param_spec_boolean ("is-any",
220 P_("See g_inet_address_get_is_any()"),
222 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
224 g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
225 g_param_spec_boolean ("is-link-local",
227 P_("See g_inet_address_get_is_link_local()"),
229 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
231 g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
232 g_param_spec_boolean ("is-loopback",
234 P_("See g_inet_address_get_is_loopback()"),
236 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
238 g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
239 g_param_spec_boolean ("is-site-local",
241 P_("See g_inet_address_get_is_site_local()"),
243 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
245 g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
246 g_param_spec_boolean ("is-multicast",
248 P_("See g_inet_address_get_is_multicast()"),
250 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
252 g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
253 g_param_spec_boolean ("is-mc-global",
254 P_("Is multicast global"),
255 P_("See g_inet_address_get_is_mc_global()"),
257 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
259 g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
260 g_param_spec_boolean ("is-mc-link-local",
261 P_("Is multicast link-local"),
262 P_("See g_inet_address_get_is_mc_link_local()"),
264 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
266 g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
267 g_param_spec_boolean ("is-mc-node-local",
268 P_("Is multicast node-local"),
269 P_("See g_inet_address_get_is_mc_node_local()"),
271 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
273 g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
274 g_param_spec_boolean ("is-mc-org-local",
275 P_("Is multicast org-local"),
276 P_("See g_inet_address_get_is_mc_org_local()"),
278 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
280 g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
281 g_param_spec_boolean ("is-mc-site-local",
282 P_("Is multicast site-local"),
283 P_("See g_inet_address_get_is_mc_site_local()"),
285 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
289 g_inet_address_init (GInetAddress *address)
291 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
293 GInetAddressPrivate);
297 * g_inet_address_new_from_string:
298 * @string: a string representation of an IP address
300 * Parses @string as an IP address and creates a new #GInetAddress.
302 * Returns: a new #GInetAddress corresponding to @string, or %NULL if
303 * @string could not be parsed.
308 g_inet_address_new_from_string (const gchar *string)
312 struct sockaddr_storage sa;
313 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
314 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
316 #else /* !G_OS_WIN32 */
317 struct in_addr in_addr;
318 struct in6_addr in6_addr;
321 /* Make sure _g_networking_init() has been called */
322 type = g_inet_address_get_type ();
325 memset (&sa, 0, sizeof (sa));
327 if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
328 return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
329 else if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
330 return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
332 #else /* !G_OS_WIN32 */
334 if (inet_pton (AF_INET, string, &in_addr) > 0)
335 return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
336 else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
337 return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
343 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
346 * g_inet_address_new_from_bytes:
347 * @bytes: raw address data
348 * @family: the address family of @bytes
350 * Creates a new #GInetAddress from the given @family and @bytes.
351 * @bytes should be 4 bytes for %G_INET_ADDRESS_IPV4 and 16 bytes for
352 * %G_INET_ADDRESS_IPV6.
354 * Returns: a new #GInetAddress corresponding to @family and @bytes.
359 g_inet_address_new_from_bytes (const guint8 *bytes,
360 GSocketFamily family)
362 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
364 return g_object_new (G_TYPE_INET_ADDRESS,
371 * g_inet_address_new_loopback:
372 * @family: the address family
374 * Creates a #GInetAddress for the loopback address for @family.
376 * Returns: a new #GInetAddress corresponding to the loopback address
382 g_inet_address_new_loopback (GSocketFamily family)
384 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
386 if (family == AF_INET)
388 guint8 addr[4] = {127, 0, 0, 1};
390 return g_inet_address_new_from_bytes (addr, family);
393 return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
397 * g_inet_address_new_any:
398 * @family: the address family
400 * Creates a #GInetAddress for the "any" address (unassigned/"don't
401 * care") for @family.
403 * Returns: a new #GInetAddress corresponding to the "any" address
409 g_inet_address_new_any (GSocketFamily family)
411 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
413 if (family == AF_INET)
415 guint8 addr[4] = {0, 0, 0, 0};
417 return g_inet_address_new_from_bytes (addr, family);
420 return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
425 * g_inet_address_to_string:
426 * @address: a #GInetAddress
428 * Converts @address to string form.
430 * Returns: a representation of @address as a string, which should be
436 g_inet_address_to_string (GInetAddress *address)
438 gchar buffer[INET6_ADDRSTRLEN];
440 DWORD buflen = sizeof (buffer), addrlen;
441 struct sockaddr_storage sa;
442 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
443 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
446 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
449 sa.ss_family = address->priv->family;
450 if (address->priv->family == AF_INET)
452 addrlen = sizeof (*sin);
453 memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
454 sizeof (sin->sin_addr));
459 addrlen = sizeof (*sin6);
460 memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
461 sizeof (sin6->sin6_addr));
464 if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
467 #else /* !G_OS_WIN32 */
469 if (address->priv->family == AF_INET)
470 inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
472 inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
475 return g_strdup (buffer);
479 * g_inet_address_to_bytes:
480 * @address: a #GInetAddress
482 * Gets the raw binary address data from @address.
484 * Returns: a pointer to an internal array of the bytes in @address,
485 * which should not be modified, stored, or freed. The size of this
486 * array can be gotten with g_inet_address_get_native_size().
491 g_inet_address_to_bytes (GInetAddress *address)
493 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
495 return (guint8 *)&address->priv->addr;
499 * g_inet_address_get_native_size:
500 * @address: a #GInetAddress
502 * Gets the size of the native raw binary address for @address. This
503 * is the size of the data that you get from g_inet_address_to_bytes().
505 * Returns: the number of bytes used for the native version of @address.
510 g_inet_address_get_native_size (GInetAddress *address)
512 if (address->priv->family == AF_INET)
513 return sizeof (address->priv->addr.ipv4);
514 return sizeof (address->priv->addr.ipv6);
518 * g_inet_address_get_family:
519 * @address: a #GInetAddress
521 * Gets @address's family
523 * Returns: @address's family
528 g_inet_address_get_family (GInetAddress *address)
530 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
532 return address->priv->family;
536 * g_inet_address_get_is_any:
537 * @address: a #GInetAddress
539 * Tests whether @address is the "any" address for its family.
541 * Returns: %TRUE if @address is the "any" address for its family.
546 g_inet_address_get_is_any (GInetAddress *address)
548 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
550 if (address->priv->family == AF_INET)
552 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
554 return addr4 == INADDR_ANY;
557 return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
561 * g_inet_address_get_is_loopback:
562 * @address: a #GInetAddress
564 * Tests whether @address is the loopback address for its family.
566 * Returns: %TRUE if @address is the loopback address for its family.
571 g_inet_address_get_is_loopback (GInetAddress *address)
573 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
575 if (address->priv->family == AF_INET)
577 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
580 return ((addr4 & 0xff000000) == 0x7f000000);
583 return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
587 * g_inet_address_get_is_link_local:
588 * @address: a #GInetAddress
590 * Tests whether @address is a link-local address (that is, if it
591 * identifies a host on a local network that is not connected to the
594 * Returns: %TRUE if @address is a link-local address.
599 g_inet_address_get_is_link_local (GInetAddress *address)
601 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
603 if (address->priv->family == AF_INET)
605 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
608 return ((addr4 & 0xffff0000) == 0xa9fe0000);
611 return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
615 * g_inet_address_get_is_site_local:
616 * @address: a #GInetAddress
618 * Tests whether @address is a site-local address such as 10.0.0.1
619 * (that is, the address identifies a host on a local network that can
620 * not be reached directly from the Internet, but which may have
621 * outgoing Internet connectivity via a NAT or firewall).
623 * Returns: %TRUE if @address is a site-local address.
628 g_inet_address_get_is_site_local (GInetAddress *address)
630 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
632 if (address->priv->family == AF_INET)
634 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
636 /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
637 return ((addr4 & 0xff000000) == 0x0a000000 ||
638 (addr4 & 0xfff00000) == 0xac100000 ||
639 (addr4 & 0xffff0000) == 0xc0a80000);
642 return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
646 * g_inet_address_get_is_multicast:
647 * @address: a #GInetAddress
649 * Tests whether @address is a multicast address.
651 * Returns: %TRUE if @address is a multicast address.
656 g_inet_address_get_is_multicast (GInetAddress *address)
658 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
660 if (address->priv->family == AF_INET)
662 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
664 return IN_MULTICAST (addr4);
667 return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
671 * g_inet_address_get_is_mc_global:
672 * @address: a #GInetAddress
674 * Tests whether @address is a global multicast address.
676 * Returns: %TRUE if @address is a global multicast address.
681 g_inet_address_get_is_mc_global (GInetAddress *address)
683 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
685 if (address->priv->family == AF_INET)
688 return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
692 * g_inet_address_get_is_mc_link_local:
693 * @address: a #GInetAddress
695 * Tests whether @address is a link-local multicast address.
697 * Returns: %TRUE if @address is a link-local multicast address.
702 g_inet_address_get_is_mc_link_local (GInetAddress *address)
704 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
706 if (address->priv->family == AF_INET)
709 return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
713 * g_inet_address_get_is_mc_node_local:
714 * @address: a #GInetAddress
716 * Tests whether @address is a node-local multicast address.
718 * Returns: %TRUE if @address is a node-local multicast address.
723 g_inet_address_get_is_mc_node_local (GInetAddress *address)
725 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
727 if (address->priv->family == AF_INET)
730 return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
734 * g_inet_address_get_is_mc_org_local:
735 * @address: a #GInetAddress
737 * Tests whether @address is an organization-local multicast address.
739 * Returns: %TRUE if @address is an organization-local multicast address.
744 g_inet_address_get_is_mc_org_local (GInetAddress *address)
746 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
748 if (address->priv->family == AF_INET)
751 return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
755 * g_inet_address_get_is_mc_site_local:
756 * @address: a #GInetAddress
758 * Tests whether @address is a site-local multicast address.
760 * Returns: %TRUE if @address is a site-local multicast address.
765 g_inet_address_get_is_mc_site_local (GInetAddress *address)
767 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
769 if (address->priv->family == AF_INET)
772 return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
775 #define __G_INET_ADDRESS_C__
776 #include "gioaliasdef.c"