Add GError to g_socket_address_to_native

This is nice for some callers so they can report an error.
It is also required to support opional address types like
abstract paths for unix domain sockets.
This commit is contained in:
Alexander Larsson 2009-05-18 14:10:07 +02:00
parent eefd7cd8db
commit d8bdc3e567
6 changed files with 47 additions and 17 deletions

View File

@ -28,6 +28,8 @@
#include "ginetsocketaddress.h"
#include "ginetaddress.h"
#include "gnetworkingprivate.h"
#include "gioerror.h"
#include "glibintl.h"
#include "gioalias.h"
@ -159,7 +161,8 @@ g_inet_socket_address_get_native_size (GSocketAddress *address)
static gboolean
g_inet_socket_address_to_native (GSocketAddress *address,
gpointer dest,
gsize destlen)
gsize destlen,
GError **error)
{
GInetSocketAddress *addr;
GSocketFamily family;
@ -174,7 +177,11 @@ g_inet_socket_address_to_native (GSocketAddress *address,
struct sockaddr_in *sock = (struct sockaddr_in *) dest;
if (destlen < sizeof (*sock))
return FALSE;
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
_("Not enough space for socket address"));
return FALSE;
}
sock->sin_family = AF_INET;
sock->sin_port = g_htons (addr->priv->port);
@ -187,7 +194,11 @@ g_inet_socket_address_to_native (GSocketAddress *address,
struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
if (destlen < sizeof (*sock))
return FALSE;
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
_("Not enough space for socket address"));
return FALSE;
}
memset (sock, 0, sizeof (sock));
sock->sin6_family = AF_INET6;
@ -196,7 +207,11 @@ g_inet_socket_address_to_native (GSocketAddress *address,
return TRUE;
}
else
return FALSE;
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Unsupported socket address"));
return FALSE;
}
}
static void

View File

@ -697,7 +697,7 @@ _g_resolver_address_to_sockaddr (GInetAddress *address,
GSocketAddress *sockaddr;
sockaddr = g_inet_socket_address_new (address, 0);
g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa));
g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa), NULL);
*sa_len = g_socket_address_get_native_size (sockaddr);
g_object_unref (sockaddr);
}

View File

@ -1353,7 +1353,7 @@ g_socket_bind (GSocket *socket,
}
#endif
if (!g_socket_address_to_native (address, addr, sizeof addr))
if (!g_socket_address_to_native (address, addr, sizeof addr, error))
return FALSE;
if (bind (socket->priv->fd, (struct sockaddr *) addr,
@ -1518,7 +1518,8 @@ g_socket_connect (GSocket *socket,
if (!check_socket (socket, error))
return FALSE;
g_socket_address_to_native (address, buffer, sizeof buffer);
if (!g_socket_address_to_native (address, buffer, sizeof buffer, error))
return FALSE;
while (1)
{
@ -2546,7 +2547,8 @@ g_socket_send_message (GSocket *socket,
{
msg.msg_namelen = g_socket_address_get_native_size (address);
msg.msg_name = g_alloca (msg.msg_namelen);
g_socket_address_to_native (address, msg.msg_name, msg.msg_namelen);
if (!g_socket_address_to_native (address, msg.msg_name, msg.msg_namelen, error))
return -1;
}
/* iov */
@ -2666,7 +2668,8 @@ g_socket_send_message (GSocket *socket,
if (address)
{
addrlen = g_socket_address_get_native_size (address);
g_socket_address_to_native (address, &addr, sizeof addr);
if (!g_socket_address_to_native (address, &addr, sizeof addr, error))
return -1;
}
while (1)

View File

@ -161,24 +161,29 @@ g_socket_address_get_native_size (GSocketAddress *address)
* <type>struct sockaddr</type>.
* @destlen: the size of @dest. Must be at least as large as
* g_socket_address_get_native_size().
* @error: #GError for error reporting, or %NULL to ignore.
*
* Converts a #GSocketAddress to a native <type>struct
* sockaddr</type>, which can be passed to low-level functions like
* connect() or bind().
*
* Returns: %TRUE if @dest was filled in, %FALSE if @address is invalid
* or @destlen is too small.
* If not enough space is availible, a %G_IO_ERROR_NO_SPACE error is
* returned. If the address type is not known on the system
* then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
*
* Returns: %TRUE if @dest was filled in, %FALSE on error
*
* Since: 2.22
*/
gboolean
g_socket_address_to_native (GSocketAddress *address,
gpointer dest,
gsize destlen)
gsize destlen,
GError **error)
{
g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen);
return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
}
/**

View File

@ -56,7 +56,8 @@ struct _GSocketAddressClass
gboolean (*to_native) (GSocketAddress *address,
gpointer dest,
gsize destlen);
gsize destlen,
GError **error);
};
GType g_socket_address_get_type (void) G_GNUC_CONST;
@ -68,7 +69,8 @@ GSocketAddress * g_socket_address_new_from_native (gpointer native,
gboolean g_socket_address_to_native (GSocketAddress *address,
gpointer dest,
gsize destlen);
gsize destlen,
GError **error);
gssize g_socket_address_get_native_size (GSocketAddress *address);

View File

@ -133,13 +133,18 @@ g_unix_socket_address_get_native_size (GSocketAddress *address)
static gboolean
g_unix_socket_address_to_native (GSocketAddress *address,
gpointer dest,
gsize destlen)
gsize destlen,
GError **error)
{
GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
struct sockaddr_un *sock;
if (destlen < sizeof (*sock))
return FALSE;
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
_("Not enough space for socket address"));
return FALSE;
}
sock = (struct sockaddr_un *) dest;
sock->sun_family = AF_UNIX;