GSocket: Add function to set/get the broadcast setting on a socket

https://bugzilla.gnome.org/show_bug.cgi?id=623187
This commit is contained in:
Sebastian Dröge 2012-01-13 13:01:35 +01:00
parent 5560d9b880
commit ffb5f8b101
4 changed files with 102 additions and 0 deletions

View File

@ -1816,6 +1816,8 @@ g_socket_get_timeout
g_socket_set_timeout g_socket_set_timeout
g_socket_set_ttl g_socket_set_ttl
g_socket_get_ttl g_socket_get_ttl
g_socket_get_broadcast
g_socket_set_broadcast
g_socket_get_family g_socket_get_family
g_socket_get_fd g_socket_get_fd
g_socket_get_local_address g_socket_get_local_address

View File

@ -950,6 +950,7 @@ g_socket_condition_wait
g_socket_connect g_socket_connect
g_socket_create_source g_socket_create_source
g_socket_get_blocking g_socket_get_blocking
g_socket_get_broadcast
g_socket_get_family g_socket_get_family
g_socket_get_fd g_socket_get_fd
g_socket_get_timeout g_socket_get_timeout
@ -978,6 +979,7 @@ g_socket_send_message
g_socket_send_to g_socket_send_to
g_socket_send_with_blocking g_socket_send_with_blocking
g_socket_set_blocking g_socket_set_blocking
g_socket_set_broadcast
g_socket_set_timeout g_socket_set_timeout
g_socket_set_ttl g_socket_set_ttl
g_socket_set_keepalive g_socket_set_keepalive

View File

@ -139,6 +139,7 @@ enum
PROP_REMOTE_ADDRESS, PROP_REMOTE_ADDRESS,
PROP_TIMEOUT, PROP_TIMEOUT,
PROP_TTL, PROP_TTL,
PROP_BROADCAST,
PROP_MULTICAST_LOOPBACK, PROP_MULTICAST_LOOPBACK,
PROP_MULTICAST_TTL PROP_MULTICAST_TTL
}; };
@ -620,6 +621,10 @@ g_socket_get_property (GObject *object,
g_value_set_uint (value, g_socket_get_ttl (socket)); g_value_set_uint (value, g_socket_get_ttl (socket));
break; break;
case PROP_BROADCAST:
g_value_set_boolean (value, g_socket_get_broadcast (socket));
break;
case PROP_MULTICAST_LOOPBACK: case PROP_MULTICAST_LOOPBACK:
g_value_set_boolean (value, g_socket_get_multicast_loopback (socket)); g_value_set_boolean (value, g_socket_get_multicast_loopback (socket));
break; break;
@ -679,6 +684,10 @@ g_socket_set_property (GObject *object,
g_socket_set_ttl (socket, g_value_get_uint (value)); g_socket_set_ttl (socket, g_value_get_uint (value));
break; break;
case PROP_BROADCAST:
g_socket_set_broadcast (socket, g_value_get_boolean (value));
break;
case PROP_MULTICAST_LOOPBACK: case PROP_MULTICAST_LOOPBACK:
g_socket_set_multicast_loopback (socket, g_value_get_boolean (value)); g_socket_set_multicast_loopback (socket, g_value_get_boolean (value));
break; break;
@ -845,6 +854,21 @@ g_socket_class_init (GSocketClass *klass)
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS)); G_PARAM_STATIC_STRINGS));
/**
* GSocket:broadcast:
*
* Whether the socket should allow sending to and receiving from broadcast addresses.
*
* Since: 2.32
*/
g_object_class_install_property (gobject_class, PROP_BROADCAST,
g_param_spec_boolean ("broadcast",
P_("Broadcast"),
P_("Whether to allow sending to and receiving from broadcast addresses"),
FALSE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/** /**
* GSocket:ttl: * GSocket:ttl:
* *
@ -1325,6 +1349,76 @@ g_socket_set_ttl (GSocket *socket,
g_object_notify (G_OBJECT (socket), "ttl"); g_object_notify (G_OBJECT (socket), "ttl");
} }
/**
* g_socket_get_broadcast:
* @socket: a #GSocket.
*
* Gets the broadcast setting on @socket; if %TRUE,
* it is possible to send packets to broadcast
* addresses or receive from broadcast addresses.
*
* Returns: the broadcast setting on @socket
*
* Since: 2.32
*/
gboolean
g_socket_get_broadcast (GSocket *socket)
{
int result;
guint value = 0, optlen;
g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
optlen = sizeof (guchar);
result = getsockopt (socket->priv->fd, SOL_SOCKET, SO_BROADCAST,
&value, &optlen);
if (result < 0)
{
int errsv = get_socket_errno ();
g_warning ("error getting broadcast: %s", socket_strerror (errsv));
return FALSE;
}
return !!value;
}
/**
* g_socket_set_broadcast:
* @socket: a #GSocket.
* @loopback: whether @socket should allow sending to and receiving
* from broadcast addresses
*
* Sets whether @socket should allow sending to and receiving from
* broadcast addresses. This is %FALSE by default.
*
* Since: 2.32
*/
void
g_socket_set_broadcast (GSocket *socket,
gboolean broadcast)
{
int result;
guchar value;
g_return_if_fail (G_IS_SOCKET (socket));
broadcast = !!broadcast;
value = (guchar)broadcast;
result = setsockopt (socket->priv->fd, SOL_SOCKET, SO_BROADCAST,
&value, sizeof (value));
if (result < 0)
{
int errsv = get_socket_errno ();
g_warning ("error setting broadcast: %s", socket_strerror (errsv));
return;
}
g_object_notify (G_OBJECT (socket), "broadcast");
}
/** /**
* g_socket_get_multicast_loopback: * g_socket_get_multicast_loopback:
* @socket: a #GSocket. * @socket: a #GSocket.

View File

@ -105,6 +105,10 @@ guint g_socket_get_ttl (GSocket
void g_socket_set_ttl (GSocket *socket, void g_socket_set_ttl (GSocket *socket,
guint ttl); guint ttl);
gboolean g_socket_get_broadcast (GSocket *socket);
void g_socket_set_broadcast (GSocket *socket,
gboolean broadcast);
gboolean g_socket_get_multicast_loopback (GSocket *socket); gboolean g_socket_get_multicast_loopback (GSocket *socket);
void g_socket_set_multicast_loopback (GSocket *socket, void g_socket_set_multicast_loopback (GSocket *socket,
gboolean loopback); gboolean loopback);