~ubuntu-branches/ubuntu/saucy/glib2.0/saucy-proposed

« back to all changes in this revision

Viewing changes to gio/gsocket.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-08-21 10:08:49 UTC
  • mfrom: (1.63.24)
  • Revision ID: package-import@ubuntu.com-20130821100849-enhti9o2tk0iug5r
Tags: 2.37.6-1ubuntu1
* Resynchronise with Debian, remaining change:
  - Build-Depend on python:any for cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
895
895
  /**
896
896
   * GSocket:broadcast:
897
897
   *
898
 
   * Whether the socket should allow sending to and receiving from broadcast addresses.
 
898
   * Whether the socket should allow sending to broadcast addresses.
899
899
   *
900
900
   * Since: 2.32
901
901
   */
902
902
  g_object_class_install_property (gobject_class, PROP_BROADCAST,
903
903
                                   g_param_spec_boolean ("broadcast",
904
904
                                                         P_("Broadcast"),
905
 
                                                         P_("Whether to allow sending to and receiving from broadcast addresses"),
 
905
                                                         P_("Whether to allow sending to broadcast addresses"),
906
906
                                                         FALSE,
907
907
                                                         G_PARAM_READWRITE |
908
908
                                                         G_PARAM_STATIC_STRINGS));
1387
1387
 *
1388
1388
 * Gets the broadcast setting on @socket; if %TRUE,
1389
1389
 * it is possible to send packets to broadcast
1390
 
 * addresses or receive from broadcast addresses.
 
1390
 * addresses.
1391
1391
 *
1392
1392
 * Returns: the broadcast setting on @socket
1393
1393
 *
1415
1415
/**
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
 
1419
 *     addresses
1420
1420
 *
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.
1423
1423
 *
1424
1424
 * Since: 2.32
1425
1425
 */
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.
1855
1855
 *
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.)
 
1863
 *
 
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.)
1864
1870
 *
1865
1871
 * Returns: %TRUE on success, %FALSE on error.
1866
1872
 *
1873
1879
               GError         **error)
1874
1880
{
1875
1881
  struct sockaddr_storage addr;
 
1882
  gboolean so_reuseaddr;
 
1883
#ifdef SO_REUSEPORT
 
1884
  gboolean so_reuseport;
 
1885
#endif
1876
1886
 
1877
1887
  g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
1878
1888
 
1879
1889
  if (!check_socket (socket, error))
1880
1890
    return FALSE;
1881
1891
 
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 */
1884
 
#ifndef G_OS_WIN32
1885
 
  {
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);
1891
 
  }
1892
 
#endif
1893
 
 
1894
1892
  if (!g_socket_address_to_native (address, &addr, sizeof addr, error))
1895
1893
    return FALSE;
1896
1894
 
 
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
 
1897
   * sockets.
 
1898
   *
 
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).
 
1904
   */
 
1905
 
 
1906
#ifdef G_OS_WIN32
 
1907
  so_reuseaddr = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
 
1908
#else
 
1909
  so_reuseaddr = !!reuse_address;
 
1910
#endif
 
1911
 
 
1912
#ifdef SO_REUSEPORT
 
1913
  so_reuseport = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
 
1914
#endif
 
1915
 
 
1916
  /* Ignore errors here, the only likely error is "not supported", and
 
1917
   * this is a "best effort" thing mainly.
 
1918
   */
 
1919
  g_socket_set_option (socket, SOL_SOCKET, SO_REUSEADDR, so_reuseaddr, NULL);
 
1920
#ifdef SO_REUSEPORT
 
1921
  g_socket_set_option (socket, SOL_SOCKET, SO_REUSEPORT, so_reuseport, NULL);
 
1922
#endif
 
1923
 
1897
1924
  if (bind (socket->priv->fd, (struct sockaddr *) &addr,
1898
1925
            g_socket_address_get_native_size (address)) < 0)
1899
1926
    {
1907
1934
  return TRUE;
1908
1935
}
1909
1936
 
 
1937
#if !defined(HAVE_IF_NAMETOINDEX) && defined(G_OS_WIN32)
 
1938
static guint
 
1939
if_nametoindex (const gchar *iface)
 
1940
{
 
1941
  PIP_ADAPTER_ADDRESSES addresses = NULL, p;
 
1942
  gulong addresses_len = 0;
 
1943
  guint idx = 0;
 
1944
  DWORD res;
 
1945
 
 
1946
  res = GetAdaptersAddresses (AF_UNSPEC, 0, NULL, NULL, &addresses_len);
 
1947
  if (res != NO_ERROR && res != ERROR_BUFFER_OVERFLOW)
 
1948
    {
 
1949
      if (res == ERROR_NO_DATA)
 
1950
        errno = ENXIO;
 
1951
      else
 
1952
        errno = EINVAL;
 
1953
      return 0;
 
1954
    }
 
1955
 
 
1956
  addresses = g_malloc (addresses_len);
 
1957
  res = GetAdaptersAddresses (AF_UNSPEC, 0, NULL, addresses, &addresses_len);
 
1958
 
 
1959
  if (res != NO_ERROR)
 
1960
    {
 
1961
      g_free (addresses);
 
1962
      if (res == ERROR_NO_DATA)
 
1963
        errno = ENXIO;
 
1964
      else
 
1965
        errno = EINVAL;
 
1966
      return 0;
 
1967
    }
 
1968
 
 
1969
  p = addresses;
 
1970
  while (p)
 
1971
    {
 
1972
      if (strcmp (p->AdapterName, iface) == 0)
 
1973
        {
 
1974
          idx = p->IfIndex;
 
1975
          break;
 
1976
        }
 
1977
      p = p->Next;
 
1978
    }
 
1979
 
 
1980
  if (p == NULL)
 
1981
    errno = ENXIO;
 
1982
 
 
1983
  g_free (addresses);
 
1984
 
 
1985
  return idx;
 
1986
}
 
1987
 
 
1988
#define HAVE_IF_NAMETOINDEX 1
 
1989
#endif
 
1990
 
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);
1943
2024
      else
1944
2025
        mc_req.imr_ifindex = 0;  /* Pick any.  */
 
2026
#elif defined(G_OS_WIN32)
 
2027
      if (iface)
 
2028
        mc_req.imr_interface.s_addr = g_htonl (if_nametoindex (iface));
 
2029
      else
 
2030
        mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
1945
2031
#else
1946
2032
      mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
1947
2033
#endif
4354
4440
        g_set_error (error,
4355
4441
                     G_IO_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));
4359
4445
      }
4360
4446
    else