* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*
* Parses @string as an IP address and creates a new #GInetAddress.
*
- * Returns: a new #GInetAddress corresponding to @string, or %NULL if
- * @string could not be parsed.
+ * Returns: (nullable) (transfer full): a new #GInetAddress corresponding
+ * to @string, or %NULL if @string could not be parsed.
+ * Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GInetAddress *
g_inet_address_new_from_string (const gchar *string)
{
-#ifdef G_OS_WIN32
- struct sockaddr_storage sa;
- struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
- gint len;
-#else /* !G_OS_WIN32 */
struct in_addr in_addr;
struct in6_addr in6_addr;
-#endif
+
+ 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
*/
g_networking_init ();
-#ifdef G_OS_WIN32
- /* 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 */
-
if (inet_pton (AF_INET, string, &in_addr) > 0)
return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
-#endif
return NULL;
}
* %G_SOCKET_FAMILY_IPV6.
*
* Returns: a new #GInetAddress corresponding to @family and @bytes.
+ * Free the returned object with g_object_unref().
*
* Since: 2.22
*/
*
* Returns: a new #GInetAddress corresponding to the loopback address
* for @family.
+ * Free the returned object with g_object_unref().
*
* Since: 2.22
*/
*
* Returns: a new #GInetAddress corresponding to the "any" address
* for @family.
+ * Free the returned object with g_object_unref().
*
* Since: 2.22
*/
g_inet_address_to_string (GInetAddress *address)
{
gchar buffer[INET6_ADDRSTRLEN];
-#ifdef G_OS_WIN32
- DWORD buflen = sizeof (buffer), addrlen;
- struct sockaddr_storage sa;
- struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
-#endif
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));
- }
- else
- {
- addrlen = sizeof (*sin6);
- memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
- sizeof (sin6->sin6_addr));
- }
- if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
- return NULL;
-
-#else /* !G_OS_WIN32 */
-
if (address->priv->family == AF_INET)
inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
else
inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
-#endif
return g_strdup (buffer);
}