X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gio%2Fginetsocketaddress.c;h=5cf26f7feda97175183928759f8a4afc9d177618;hb=f2786908a8858ec9d063e8fae7e4b2d8d612b682;hp=8f42849987b0e2c6f2f20520c064449332819f32;hpb=49eeaa9bbdec4b7f57d28697971a63be6854b63e;p=platform%2Fupstream%2Fglib.git
diff --git a/gio/ginetsocketaddress.c b/gio/ginetsocketaddress.c
index 8f42849..5cf26f7 100644
--- a/gio/ginetsocketaddress.c
+++ b/gio/ginetsocketaddress.c
@@ -13,9 +13,7 @@
* 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
@@ -35,6 +33,7 @@
/**
* SECTION:ginetsocketaddress
* @short_description: Internet GSocketAddress
+ * @include: gio/gio.h
*
* An IPv4 or IPv6 socket address; that is, the combination of a
* #GInetAddress and a port number.
@@ -43,18 +42,9 @@
/**
* GInetSocketAddress:
*
- * An IPv4 or IPv6 socket address, corresponding to a struct
- * sockaddr_in or struct sockaddr_in6.
+ * An IPv4 or IPv6 socket address, corresponding to a struct
+ * sockaddr_in or struct sockaddr_in6.
*/
-G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
-
-enum {
- PROP_0,
- PROP_ADDRESS,
- PROP_PORT,
- PROP_FLOWINFO,
- PROP_SCOPE_ID
-};
struct _GInetSocketAddressPrivate
{
@@ -64,24 +54,24 @@ struct _GInetSocketAddressPrivate
guint32 scope_id;
};
-static void
-g_inet_socket_address_finalize (GObject *object)
-{
- GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
+G_DEFINE_TYPE_WITH_PRIVATE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS)
- if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
- (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
-}
+enum {
+ PROP_0,
+ PROP_ADDRESS,
+ PROP_PORT,
+ PROP_FLOWINFO,
+ PROP_SCOPE_ID
+};
static void
g_inet_socket_address_dispose (GObject *object)
{
- GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
+ GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
- g_object_unref (address->priv->address);
+ g_clear_object (&(address->priv->address));
- if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
- (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
+ G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
}
static void
@@ -227,8 +217,8 @@ g_inet_socket_address_to_native (GSocketAddress *address,
memset (sock, 0, sizeof (*sock));
sock->sin6_family = AF_INET6;
sock->sin6_port = g_htons (addr->priv->port);
- sock->sin6_flowinfo = g_htonl (addr->priv->flowinfo);
- sock->sin6_scope_id = g_htonl (addr->priv->scope_id);
+ sock->sin6_flowinfo = addr->priv->flowinfo;
+ sock->sin6_scope_id = addr->priv->scope_id;
memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
return TRUE;
}
@@ -246,9 +236,6 @@ g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
- g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
-
- gobject_class->finalize = g_inet_socket_address_finalize;
gobject_class->dispose = g_inet_socket_address_dispose;
gobject_class->set_property = g_inet_socket_address_set_property;
gobject_class->get_property = g_inet_socket_address_get_property;
@@ -280,7 +267,7 @@ g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
/**
* GInetSocketAddress:flowinfo:
*
- * The sin6_flowinfo field, for IPv6 addresses.
+ * The `sin6_flowinfo` field, for IPv6 addresses.
*
* Since: 2.32
*/
@@ -298,7 +285,7 @@ g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
/**
* GInetSocketAddress:scope_id:
*
- * The sin6_scope_id field, for IPv6 addresses.
+ * The `sin6_scope_id` field, for IPv6 addresses.
*
* Since: 2.32
*/
@@ -317,9 +304,7 @@ g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
static void
g_inet_socket_address_init (GInetSocketAddress *address)
{
- address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
- G_TYPE_INET_SOCKET_ADDRESS,
- GInetSocketAddressPrivate);
+ address->priv = g_inet_socket_address_get_instance_private (address);
}
/**
@@ -344,6 +329,82 @@ g_inet_socket_address_new (GInetAddress *address,
}
/**
+ * g_inet_socket_address_new_from_string:
+ * @address: the string form of an IP address
+ * @port: a port number
+ *
+ * Creates a new #GInetSocketAddress for @address and @port.
+ *
+ * If @address is an IPv6 address, it can also contain a scope ID
+ * (separated from the address by a "%").
+ *
+ * Returns: a new #GInetSocketAddress, or %NULL if @address cannot be
+ * parsed.
+ *
+ * Since: 2.40
+ */
+GSocketAddress *
+g_inet_socket_address_new_from_string (const char *address,
+ guint port)
+{
+ static struct addrinfo *hints, hints_struct;
+ GSocketAddress *saddr;
+ GInetAddress *iaddr;
+ struct addrinfo *res;
+ gint status;
+
+ if (strchr (address, ':'))
+ {
+ /* IPv6 address (or it's invalid). We use getaddrinfo() because
+ * it will handle parsing a scope_id as well.
+ */
+
+ if (G_UNLIKELY (g_once_init_enter (&hints)))
+ {
+ hints_struct.ai_family = AF_UNSPEC;
+ hints_struct.ai_socktype = SOCK_STREAM;
+ hints_struct.ai_protocol = 0;
+ hints_struct.ai_flags = AI_NUMERICHOST;
+ g_once_init_leave (&hints, &hints_struct);
+ }
+
+ status = getaddrinfo (address, NULL, hints, &res);
+ if (status != 0)
+ return NULL;
+
+ if (res->ai_family == AF_INET6 &&
+ res->ai_addrlen == sizeof (struct sockaddr_in6))
+ {
+ ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = g_htons (port);
+ saddr = g_socket_address_new_from_native (res->ai_addr, res->ai_addrlen);
+ }
+ else
+ saddr = NULL;
+
+ freeaddrinfo (res);
+ }
+ else
+ {
+ /* IPv4 (or invalid). We don't want to use getaddrinfo() here,
+ * because it accepts the stupid "IPv4 numbers-and-dots
+ * notation" addresses that are never used for anything except
+ * phishing. Since we don't have to worry about scope IDs for
+ * IPv4, we can just use g_inet_address_new_from_string().
+ */
+ iaddr = g_inet_address_new_from_string (address);
+ if (!iaddr)
+ return NULL;
+
+ g_warn_if_fail (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4);
+
+ saddr = g_inet_socket_address_new (iaddr, port);
+ g_object_unref (iaddr);
+ }
+
+ return saddr;
+}
+
+/**
* g_inet_socket_address_get_address:
* @address: a #GInetSocketAddress
*
@@ -385,10 +446,10 @@ g_inet_socket_address_get_port (GInetSocketAddress *address)
* g_inet_socket_address_get_flowinfo:
* @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
*
- * Gets the sin6_flowinfo field from @address,
+ * Gets the `sin6_flowinfo` field from @address,
* which must be an IPv6 address.
*
- * Return value: the flowinfo field
+ * Returns: the flowinfo field
*
* Since: 2.32
*/
@@ -405,10 +466,10 @@ g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
* g_inet_socket_address_get_scope_id:
* @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
*
- * Gets the sin6_scope_id field from @address,
+ * Gets the `sin6_scope_id` field from @address,
* which must be an IPv6 address.
*
- * Return value: the scope id field
+ * Returns: the scope id field
*
* Since: 2.32
*/