896
896
* GSocket:broadcast:
898
* Whether the socket should allow sending to and receiving from broadcast addresses.
898
* Whether the socket should allow sending to broadcast addresses.
902
902
g_object_class_install_property (gobject_class, PROP_BROADCAST,
903
903
g_param_spec_boolean ("broadcast",
905
P_("Whether to allow sending to and receiving from broadcast addresses"),
905
P_("Whether to allow sending to broadcast addresses"),
907
907
G_PARAM_READWRITE |
908
908
G_PARAM_STATIC_STRINGS));
1416
1416
* g_socket_set_broadcast:
1417
1417
* @socket: a #GSocket.
1418
* @broadcast: whether @socket should allow sending to and receiving
1419
* from broadcast addresses
1418
* @broadcast: whether @socket should allow sending to broadcast
1421
* Sets whether @socket should allow sending to and receiving from
1422
* broadcast addresses. This is %FALSE by default.
1421
* Sets whether @socket should allow sending to broadcast addresses.
1422
* This is %FALSE by default.
1853
1853
* In certain situations, you may also want to bind a socket that will be
1854
1854
* used to initiate connections, though this is not normally required.
1856
* @allow_reuse should be %TRUE for server sockets (sockets that you will
1857
* eventually call g_socket_accept() on), and %FALSE for client sockets.
1858
* (Specifically, if it is %TRUE, then g_socket_bind() will set the
1859
* %SO_REUSEADDR flag on the socket, allowing it to bind @address even if
1860
* that address was previously used by another socket that has not yet been
1861
* fully cleaned-up by the kernel. Failing to set this flag on a server
1862
* socket may cause the bind call to return %G_IO_ERROR_ADDRESS_IN_USE if
1863
* the server program is stopped and then immediately restarted.)
1856
* If @socket is a TCP socket, then @allow_reuse controls the setting
1857
* of the <literal>SO_REUSEADDR</literal> socket option; normally it
1858
* should be %TRUE for server sockets (sockets that you will
1859
* eventually call g_socket_accept() on), and %FALSE for client
1860
* sockets. (Failing to set this flag on a server socket may cause
1861
* g_socket_bind() to return %G_IO_ERROR_ADDRESS_IN_USE if the server
1862
* program is stopped and then immediately restarted.)
1864
* If @socket is a UDP socket, then @allow_reuse determines whether or
1865
* not other UDP sockets can be bound to the same address at the same
1866
* time. In particular, you can have several UDP sockets bound to the
1867
* same address, and they will all receive all of the multicast and
1868
* broadcast packets sent to that address. (The behavior of unicast
1869
* UDP packets to an address with multiple listeners is not defined.)
1865
1871
* Returns: %TRUE on success, %FALSE on error.
1873
1879
GError **error)
1875
1881
struct sockaddr_storage addr;
1882
gboolean so_reuseaddr;
1884
gboolean so_reuseport;
1877
1887
g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
1879
1889
if (!check_socket (socket, error))
1882
/* SO_REUSEADDR on Windows means something else and is not what we want.
1883
It always allows the unix variant of SO_REUSEADDR anyway */
1886
reuse_address = !!reuse_address;
1887
/* Ignore errors here, the only likely error is "not supported", and
1888
this is a "best effort" thing mainly */
1889
g_socket_set_option (socket, SOL_SOCKET, SO_REUSEADDR,
1890
reuse_address, NULL);
1894
1892
if (!g_socket_address_to_native (address, &addr, sizeof addr, error))
1895
/* On Windows, SO_REUSEADDR has the semantics we want for UDP
1896
* sockets, but has nasty side effects we don't want for TCP
1899
* On other platforms, we set SO_REUSEPORT, if it exists, for
1900
* UDP sockets, and SO_REUSEADDR for all sockets, hoping that
1901
* if SO_REUSEPORT doesn't exist, then SO_REUSEADDR will have
1902
* the desired semantics on UDP (as it does on Linux, although
1903
* Linux has SO_REUSEPORT too as of 3.9).
1907
so_reuseaddr = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
1909
so_reuseaddr = !!reuse_address;
1913
so_reuseport = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
1916
/* Ignore errors here, the only likely error is "not supported", and
1917
* this is a "best effort" thing mainly.
1919
g_socket_set_option (socket, SOL_SOCKET, SO_REUSEADDR, so_reuseaddr, NULL);
1921
g_socket_set_option (socket, SOL_SOCKET, SO_REUSEPORT, so_reuseport, NULL);
1897
1924
if (bind (socket->priv->fd, (struct sockaddr *) &addr,
1898
1925
g_socket_address_get_native_size (address)) < 0)
1937
#if !defined(HAVE_IF_NAMETOINDEX) && defined(G_OS_WIN32)
1939
if_nametoindex (const gchar *iface)
1941
PIP_ADAPTER_ADDRESSES addresses = NULL, p;
1942
gulong addresses_len = 0;
1946
res = GetAdaptersAddresses (AF_UNSPEC, 0, NULL, NULL, &addresses_len);
1947
if (res != NO_ERROR && res != ERROR_BUFFER_OVERFLOW)
1949
if (res == ERROR_NO_DATA)
1956
addresses = g_malloc (addresses_len);
1957
res = GetAdaptersAddresses (AF_UNSPEC, 0, NULL, addresses, &addresses_len);
1959
if (res != NO_ERROR)
1962
if (res == ERROR_NO_DATA)
1972
if (strcmp (p->AdapterName, iface) == 0)
1988
#define HAVE_IF_NAMETOINDEX 1
1910
1991
static gboolean
1911
1992
g_socket_multicast_group_operation (GSocket *socket,
1912
1993
GInetAddress *group,
1942
2023
mc_req.imr_ifindex = if_nametoindex (iface);
1944
2025
mc_req.imr_ifindex = 0; /* Pick any. */
2026
#elif defined(G_OS_WIN32)
2028
mc_req.imr_interface.s_addr = g_htonl (if_nametoindex (iface));
2030
mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
1946
2032
mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
4354
4440
g_set_error (error,
4356
4442
socket_io_error_from_errno (errsv),
4357
_("Unable to get pending error: %s"),
4443
_("Unable to read socket credentials: %s"),
4358
4444
socket_strerror (errsv));