X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gio%2Fginetsocketaddress.c;h=5cf26f7feda97175183928759f8a4afc9d177618;hb=e608ec7b2e47d29fa189fca6e97f484f41c115a4;hp=818f877e7f02626549f1ab3b51ce652f1ca1ad5b;hpb=63777d0c63d23c201affc8e2ea8a046a7d4390f5;p=platform%2Fupstream%2Fglib.git diff --git a/gio/ginetsocketaddress.c b/gio/ginetsocketaddress.c index 818f877..5cf26f7 100644 --- a/gio/ginetsocketaddress.c +++ b/gio/ginetsocketaddress.c @@ -329,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 * @@ -373,7 +449,7 @@ g_inet_socket_address_get_port (GInetSocketAddress *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 */ @@ -393,7 +469,7 @@ g_inet_socket_address_get_flowinfo (GInetSocketAddress *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 */