X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gio%2Fginetaddress.c;h=9ff0c441761592a543808ca466dd0f605831dd00;hb=853692bdfd9f8a87aed70d21f643dc13b57c92d1;hp=72c08b8045c7e5f13bdc62201d4e1dbf7a28864c;hpb=c94d3f92885456e1dc9e2fb27b709017f29d04ce;p=platform%2Fupstream%2Fglib.git diff --git a/gio/ginetaddress.c b/gio/ginetaddress.c index 72c08b8..9ff0c44 100644 --- a/gio/ginetaddress.c +++ b/gio/ginetaddress.c @@ -13,15 +13,16 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General - * Public License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307, USA. + * Public License along with this library; if not, see . * * Authors: Christian Kellner * Samuel Cormier-Iijima */ #include + +#include + #include #include "ginetaddress.h" @@ -30,11 +31,19 @@ #include "glibintl.h" #include "gnetworkingprivate.h" -#include "gioalias.h" +struct _GInetAddressPrivate +{ + GSocketFamily family; + union { + struct in_addr ipv4; + struct in6_addr ipv6; + } addr; +}; /** * SECTION:ginetaddress * @short_description: An IPv4/IPv6 address + * @include: gio/gio.h * * #GInetAddress represents an IPv4 or IPv6 internet address. Use * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to @@ -46,38 +55,17 @@ * To actually connect to a remote host, you will need a * #GInetSocketAddress (which includes a #GInetAddress as well as a * port number). - **/ + */ /** * GInetAddress: * * An IPv4 or IPv6 internet address. - **/ - -/* Networking initialization function, called from inside the g_once of - * g_inet_address_get_type() */ -static void -_g_networking_init (void) -{ -#ifdef G_OS_WIN32 - WSADATA wsadata; - if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0) - g_error ("Windows Sockets could not be initialized"); -#endif -} G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT, - _g_networking_init ();) - -struct _GInetAddressPrivate -{ - GSocketFamily family; - union { - struct in_addr ipv4; - struct in6_addr ipv6; - } addr; -}; + G_ADD_PRIVATE (GInetAddress) + g_networking_init ();) enum { @@ -104,7 +92,7 @@ g_inet_address_set_property (GObject *object, { GInetAddress *address = G_INET_ADDRESS (object); - switch (prop_id) + switch (prop_id) { case PROP_FAMILY: address->priv->family = g_value_get_enum (value); @@ -192,102 +180,193 @@ g_inet_address_class_init (GInetAddressClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - g_type_class_add_private (klass, sizeof (GInetAddressPrivate)); - gobject_class->set_property = g_inet_address_set_property; gobject_class->get_property = g_inet_address_get_property; g_object_class_install_property (gobject_class, PROP_FAMILY, g_param_spec_enum ("family", - _("Address family"), - _("The address family (IPv4 or IPv6)"), + P_("Address family"), + P_("The address family (IPv4 or IPv6)"), G_TYPE_SOCKET_FAMILY, G_SOCKET_FAMILY_INVALID, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); g_object_class_install_property (gobject_class, PROP_BYTES, g_param_spec_pointer ("bytes", - _("Bytes"), - _("The raw address data"), - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + P_("Bytes"), + P_("The raw address data"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-any: + * + * Whether this is the "any" address for its family. + * See g_inet_address_get_is_any(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_ANY, g_param_spec_boolean ("is-any", - _("Is any"), - _("See g_inet_address_get_is_any()"), + P_("Is any"), + P_("Whether this is the \"any\" address for its family"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-link-local: + * + * Whether this is a link-local address. + * See g_inet_address_get_is_link_local(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL, g_param_spec_boolean ("is-link-local", - _("Is link-local"), - _("See g_inet_address_get_is_link_local()"), + P_("Is link-local"), + P_("Whether this is a link-local address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-loopback: + * + * Whether this is the loopback address for its family. + * See g_inet_address_get_is_loopback(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK, g_param_spec_boolean ("is-loopback", - _("Is loopback"), - _("See g_inet_address_get_is_loopback()"), + P_("Is loopback"), + P_("Whether this is the loopback address for its family"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-site-local: + * + * Whether this is a site-local address. + * See g_inet_address_get_is_loopback(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL, g_param_spec_boolean ("is-site-local", - _("Is site-local"), - _("See g_inet_address_get_is_site_local()"), + P_("Is site-local"), + P_("Whether this is a site-local address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-multicast: + * + * Whether this is a multicast address. + * See g_inet_address_get_is_multicast(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_MULTICAST, g_param_spec_boolean ("is-multicast", - _("Is multicast"), - _("See g_inet_address_get_is_multicast()"), + P_("Is multicast"), + P_("Whether this is a multicast address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-mc-global: + * + * Whether this is a global multicast address. + * See g_inet_address_get_is_mc_global(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL, g_param_spec_boolean ("is-mc-global", - _("Is multicast global"), - _("See g_inet_address_get_is_mc_global()"), + P_("Is multicast global"), + P_("Whether this is a global multicast address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + + /** + * GInetAddress:is-mc-link-local: + * + * Whether this is a link-local multicast address. + * See g_inet_address_get_is_mc_link_local(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL, g_param_spec_boolean ("is-mc-link-local", - _("Is multicast link-local"), - _("See g_inet_address_get_is_mc_link_local()"), + P_("Is multicast link-local"), + P_("Whether this is a link-local multicast address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-mc-node-local: + * + * Whether this is a node-local multicast address. + * See g_inet_address_get_is_mc_node_local(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL, g_param_spec_boolean ("is-mc-node-local", - _("Is multicast node-local"), - _("See g_inet_address_get_is_mc_node_local()"), + P_("Is multicast node-local"), + P_("Whether this is a node-local multicast address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-mc-org-local: + * + * Whether this is an organization-local multicast address. + * See g_inet_address_get_is_mc_org_local(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL, g_param_spec_boolean ("is-mc-org-local", - _("Is multicast org-local"), - _("See g_inet_address_get_is_mc_org_local()"), + P_("Is multicast org-local"), + P_("Whether this is an organization-local multicast address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); - + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * GInetAddress:is-mc-site-local: + * + * Whether this is a site-local multicast address. + * See g_inet_address_get_is_mc_site_local(). + * + * Since: 2.22 + */ g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL, g_param_spec_boolean ("is-mc-site-local", - _("Is multicast site-local"), - _("See g_inet_address_get_is_mc_site_local()"), + P_("Is multicast site-local"), + P_("Whether this is a site-local multicast address"), FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME)); + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); } static void g_inet_address_init (GInetAddress *address) { - address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address, - G_TYPE_INET_ADDRESS, - GInetAddressPrivate); + address->priv = g_inet_address_get_instance_private (address); } /** @@ -314,16 +393,35 @@ g_inet_address_new_from_string (const gchar *string) struct in6_addr in6_addr; #endif - /* Make sure _g_networking_init() has been called */ - (void) g_inet_address_get_type (); + g_return_val_if_fail (string != NULL, NULL); + + /* If this GInetAddress is the first networking-related object to be + * created, then we won't have called g_networking_init() yet at + * this point. + */ + g_networking_init (); #ifdef G_OS_WIN32 - memset (&sa, 0, sizeof (sa)); - len = sizeof (sa); - if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0) - return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET); - else if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0) - return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6); + /* We need to make sure to not pass a string of the form + * "IPv4addr:port" or "[IPv6addr]:port" to WSAStringToAddress(), + * since it would accept them (returning both the address and the + * port), but we only want to accept standalone IP addresses. (In + * the IPv6 case, WINE actually only checks for the ']', not the + * '[', which is why we do the same here.) + */ + if (!strchr (string, ':')) + { + len = sizeof (sa); + if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0) + return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET); + } + + if (!strchr (string, ']')) + { + len = sizeof (sa); + if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0) + return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6); + } #else /* !G_OS_WIN32 */ @@ -340,12 +438,12 @@ g_inet_address_new_from_string (const gchar *string) /** * g_inet_address_new_from_bytes: - * @bytes: raw address data + * @bytes: (array) (element-type guint8): raw address data * @family: the address family of @bytes * * Creates a new #GInetAddress from the given @family and @bytes. - * @bytes should be 4 bytes for %G_INET_ADDRESS_IPV4 and 16 bytes for - * %G_INET_ADDRESS_IPV6. + * @bytes should be 4 bytes for %G_SOCKET_FAMILY_IPV4 and 16 bytes for + * %G_SOCKET_FAMILY_IPV6. * * Returns: a new #GInetAddress corresponding to @family and @bytes. * @@ -442,20 +540,19 @@ g_inet_address_to_string (GInetAddress *address) g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL); #ifdef G_OS_WIN32 + memset (&sa, 0, sizeof (sa)); sa.ss_family = address->priv->family; if (address->priv->family == AF_INET) { addrlen = sizeof (*sin); memcpy (&sin->sin_addr, &address->priv->addr.ipv4, sizeof (sin->sin_addr)); - sin->sin_port = 0; } else { addrlen = sizeof (*sin6); memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6, sizeof (sin6->sin6_addr)); - sin6->sin6_port = 0; } if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0) return NULL; @@ -472,13 +569,14 @@ g_inet_address_to_string (GInetAddress *address) } /** - * g_inet_address_to_bytes: + * g_inet_address_to_bytes: (skip) * @address: a #GInetAddress * * Gets the raw binary address data from @address. * * Returns: a pointer to an internal array of the bytes in @address, - * which should not be modified, stored, or freed. + * which should not be modified, stored, or freed. The size of this + * array can be gotten with g_inet_address_get_native_size(). * * Since: 2.22 */ @@ -491,6 +589,25 @@ g_inet_address_to_bytes (GInetAddress *address) } /** + * g_inet_address_get_native_size: + * @address: a #GInetAddress + * + * Gets the size of the native raw binary address for @address. This + * is the size of the data that you get from g_inet_address_to_bytes(). + * + * Returns: the number of bytes used for the native version of @address. + * + * Since: 2.22 + */ +gsize +g_inet_address_get_native_size (GInetAddress *address) +{ + if (address->priv->family == AF_INET) + return sizeof (address->priv->addr.ipv4); + return sizeof (address->priv->addr.ipv6); +} + +/** * g_inet_address_get_family: * @address: a #GInetAddress * @@ -530,7 +647,7 @@ g_inet_address_get_is_any (GInetAddress *address) return addr4 == INADDR_ANY; } else - return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6); } /** @@ -556,7 +673,7 @@ g_inet_address_get_is_loopback (GInetAddress *address) return ((addr4 & 0xff000000) == 0x7f000000); } else - return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6); } /** @@ -584,7 +701,7 @@ g_inet_address_get_is_link_local (GInetAddress *address) return ((addr4 & 0xffff0000) == 0xa9fe0000); } else - return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6); } /** @@ -615,7 +732,7 @@ g_inet_address_get_is_site_local (GInetAddress *address) (addr4 & 0xffff0000) == 0xc0a80000); } else - return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6); } /** @@ -640,7 +757,7 @@ g_inet_address_get_is_multicast (GInetAddress *address) return IN_MULTICAST (addr4); } else - return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6); } /** @@ -661,7 +778,7 @@ g_inet_address_get_is_mc_global (GInetAddress *address) if (address->priv->family == AF_INET) return FALSE; else - return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6); } /** @@ -682,7 +799,7 @@ g_inet_address_get_is_mc_link_local (GInetAddress *address) if (address->priv->family == AF_INET) return FALSE; else - return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6); } /** @@ -703,7 +820,7 @@ g_inet_address_get_is_mc_node_local (GInetAddress *address) if (address->priv->family == AF_INET) return FALSE; else - return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6); } /** @@ -724,7 +841,7 @@ g_inet_address_get_is_mc_org_local (GInetAddress *address) if (address->priv->family == AF_INET) return FALSE; else - return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6); } /** @@ -745,8 +862,34 @@ g_inet_address_get_is_mc_site_local (GInetAddress *address) if (address->priv->family == AF_INET) return FALSE; else - return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6.s6_addr); + return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6); } -#define __G_INET_ADDRESS_C__ -#include "gioaliasdef.c" +/** + * g_inet_address_equal: + * @address: A #GInetAddress. + * @other_address: Another #GInetAddress. + * + * Checks if two #GInetAddress instances are equal, e.g. the same address. + * + * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise. + * + * Since: 2.30 + */ +gboolean +g_inet_address_equal (GInetAddress *address, + GInetAddress *other_address) +{ + g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE); + g_return_val_if_fail (G_IS_INET_ADDRESS (other_address), FALSE); + + if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address)) + return FALSE; + + if (memcmp (g_inet_address_to_bytes (address), + g_inet_address_to_bytes (other_address), + g_inet_address_get_native_size (address)) != 0) + return FALSE; + + return TRUE; +}