~ubuntu-branches/ubuntu/precise/glib2.0/precise-updates

« back to all changes in this revision

Viewing changes to gio/gsocket.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2012-01-17 11:16:09 UTC
  • mfrom: (1.59.36)
  • Revision ID: package-import@ubuntu.com-20120117111609-l6hqg8snni7gh1a4
Tags: 2.31.10-0ubuntu1
* New upstream version:
  GResource:
  - A new facility to allow linking data files into binaries and make 
    them available as resources
  - Resources are compiled using glib-compile-resources
  - GIO supports resource:/// uris to access resources
* debian/libglib2.0-0.install.in: install glib-compile-resources there
* debian/libglib2.0-0.symbols: new version update

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#ifndef G_OS_WIN32
42
42
# include <fcntl.h>
43
43
# include <unistd.h>
 
44
# include <sys/ioctl.h>
44
45
#endif
45
46
 
46
47
#ifdef HAVE_SYS_UIO_H
137
138
  PROP_KEEPALIVE,
138
139
  PROP_LOCAL_ADDRESS,
139
140
  PROP_REMOTE_ADDRESS,
140
 
  PROP_TIMEOUT
 
141
  PROP_TIMEOUT,
 
142
  PROP_TTL,
 
143
  PROP_BROADCAST,
 
144
  PROP_MULTICAST_LOOPBACK,
 
145
  PROP_MULTICAST_TTL
141
146
};
142
147
 
143
148
struct _GSocketPrivate
613
618
        g_value_set_uint (value, socket->priv->timeout);
614
619
        break;
615
620
 
 
621
      case PROP_TTL:
 
622
        g_value_set_uint (value, g_socket_get_ttl (socket));
 
623
        break;
 
624
 
 
625
      case PROP_BROADCAST:
 
626
        g_value_set_boolean (value, g_socket_get_broadcast (socket));
 
627
        break;
 
628
 
 
629
      case PROP_MULTICAST_LOOPBACK:
 
630
        g_value_set_boolean (value, g_socket_get_multicast_loopback (socket));
 
631
        break;
 
632
 
 
633
      case PROP_MULTICAST_TTL:
 
634
        g_value_set_uint (value, g_socket_get_multicast_ttl (socket));
 
635
        break;
 
636
 
616
637
      default:
617
638
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
618
639
    }
660
681
        g_socket_set_timeout (socket, g_value_get_uint (value));
661
682
        break;
662
683
 
 
684
      case PROP_TTL:
 
685
        g_socket_set_ttl (socket, g_value_get_uint (value));
 
686
        break;
 
687
 
 
688
      case PROP_BROADCAST:
 
689
        g_socket_set_broadcast (socket, g_value_get_boolean (value));
 
690
        break;
 
691
 
 
692
      case PROP_MULTICAST_LOOPBACK:
 
693
        g_socket_set_multicast_loopback (socket, g_value_get_boolean (value));
 
694
        break;
 
695
 
 
696
      case PROP_MULTICAST_TTL:
 
697
        g_socket_set_multicast_ttl (socket, g_value_get_uint (value));
 
698
        break;
 
699
 
663
700
      default:
664
701
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
665
702
    }
817
854
                                                      0,
818
855
                                                      G_PARAM_READWRITE |
819
856
                                                      G_PARAM_STATIC_STRINGS));
 
857
 
 
858
  /**
 
859
   * GSocket:broadcast:
 
860
   *
 
861
   * Whether the socket should allow sending to and receiving from broadcast addresses.
 
862
   *
 
863
   * Since: 2.32
 
864
   */
 
865
  g_object_class_install_property (gobject_class, PROP_BROADCAST,
 
866
                                   g_param_spec_boolean ("broadcast",
 
867
                                                         P_("Broadcast"),
 
868
                                                         P_("Whether to allow sending to and receiving from broadcast addresses"),
 
869
                                                         FALSE,
 
870
                                                         G_PARAM_READWRITE |
 
871
                                                         G_PARAM_STATIC_STRINGS));
 
872
 
 
873
  /**
 
874
   * GSocket:ttl:
 
875
   *
 
876
   * Time-to-live for outgoing unicast packets
 
877
   *
 
878
   * Since: 2.32
 
879
   */
 
880
  g_object_class_install_property (gobject_class, PROP_TTL,
 
881
                                   g_param_spec_uint ("ttl",
 
882
                                                      P_("TTL"),
 
883
                                                      P_("Time-to-live of outgoing unicast packets"),
 
884
                                                      0, G_MAXUINT, 0,
 
885
                                                      G_PARAM_READWRITE |
 
886
                                                      G_PARAM_STATIC_STRINGS));
 
887
 
 
888
  /**
 
889
   * GSocket:multicast-loopback:
 
890
   *
 
891
   * Whether outgoing multicast packets loop back to the local host.
 
892
   *
 
893
   * Since: 2.32
 
894
   */
 
895
  g_object_class_install_property (gobject_class, PROP_MULTICAST_LOOPBACK,
 
896
                                   g_param_spec_boolean ("multicast-loopback",
 
897
                                                         P_("Multicast loopback"),
 
898
                                                         P_("Whether outgoing multicast packets loop back to the local host"),
 
899
                                                         TRUE,
 
900
                                                         G_PARAM_READWRITE |
 
901
                                                         G_PARAM_STATIC_STRINGS));
 
902
 
 
903
  /**
 
904
   * GSocket:multicast-ttl:
 
905
   *
 
906
   * Time-to-live out outgoing multicast packets
 
907
   *
 
908
   * Since: 2.32
 
909
   */
 
910
  g_object_class_install_property (gobject_class, PROP_MULTICAST_TTL,
 
911
                                   g_param_spec_uint ("multicast-ttl",
 
912
                                                      P_("Multicast TTL"),
 
913
                                                      P_("Time-to-live of outgoing multicast packets"),
 
914
                                                      0, G_MAXUINT, 1,
 
915
                                                      G_PARAM_READWRITE |
 
916
                                                      G_PARAM_STATIC_STRINGS));
820
917
}
821
918
 
822
919
static void
1164
1261
}
1165
1262
 
1166
1263
/**
 
1264
 * g_socket_get_ttl:
 
1265
 * @socket: a #GSocket.
 
1266
 *
 
1267
 * Gets the unicast time-to-live setting on @socket; see
 
1268
 * g_socket_set_ttl() for more details.
 
1269
 *
 
1270
 * Returns: the time-to-live setting on @socket
 
1271
 *
 
1272
 * Since: 2.32
 
1273
 */
 
1274
guint
 
1275
g_socket_get_ttl (GSocket *socket)
 
1276
{
 
1277
  int result;
 
1278
  guint value, optlen;
 
1279
 
 
1280
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
1281
 
 
1282
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1283
    {
 
1284
      guchar optval;
 
1285
 
 
1286
      optlen = sizeof (optval);
 
1287
      result = getsockopt (socket->priv->fd, IPPROTO_IP, IP_TTL,
 
1288
                           &optval, &optlen);
 
1289
      value = optval;
 
1290
    }
 
1291
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1292
    {
 
1293
      optlen = sizeof (value);
 
1294
      result = getsockopt (socket->priv->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
 
1295
                           &value, &optlen);
 
1296
    }
 
1297
  else
 
1298
    g_return_val_if_reached (FALSE);
 
1299
 
 
1300
  if (result < 0)
 
1301
    {
 
1302
      int errsv = get_socket_errno ();
 
1303
      g_warning ("error getting unicast ttl: %s", socket_strerror (errsv));
 
1304
      return FALSE;
 
1305
    }
 
1306
 
 
1307
  return value;
 
1308
}
 
1309
 
 
1310
/**
 
1311
 * g_socket_set_ttl:
 
1312
 * @socket: a #GSocket.
 
1313
 * @ttl: the time-to-live value for all unicast packets on @socket
 
1314
 *
 
1315
 * Sets the time-to-live for outgoing unicast packets on @socket.
 
1316
 * By default the platform-specific default value is used.
 
1317
 *
 
1318
 * Since: 2.32
 
1319
 */
 
1320
void
 
1321
g_socket_set_ttl (GSocket  *socket,
 
1322
                  guint     ttl)
 
1323
{
 
1324
  int result;
 
1325
 
 
1326
  g_return_if_fail (G_IS_SOCKET (socket));
 
1327
 
 
1328
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1329
    {
 
1330
      guchar optval = (guchar)ttl;
 
1331
 
 
1332
      result = setsockopt (socket->priv->fd, IPPROTO_IP, IP_TTL,
 
1333
                           &optval, sizeof (optval));
 
1334
    }
 
1335
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1336
    {
 
1337
      result = setsockopt (socket->priv->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
 
1338
                           &ttl, sizeof (ttl));
 
1339
    }
 
1340
  else
 
1341
    g_return_if_reached ();
 
1342
 
 
1343
  if (result < 0)
 
1344
    {
 
1345
      int errsv = get_socket_errno ();
 
1346
      g_warning ("error setting unicast ttl: %s", socket_strerror (errsv));
 
1347
      return;
 
1348
    }
 
1349
 
 
1350
  g_object_notify (G_OBJECT (socket), "ttl");
 
1351
}
 
1352
 
 
1353
/**
 
1354
 * g_socket_get_broadcast:
 
1355
 * @socket: a #GSocket.
 
1356
 *
 
1357
 * Gets the broadcast setting on @socket; if %TRUE,
 
1358
 * it is possible to send packets to broadcast
 
1359
 * addresses or receive from broadcast addresses.
 
1360
 *
 
1361
 * Returns: the broadcast setting on @socket
 
1362
 *
 
1363
 * Since: 2.32
 
1364
 */
 
1365
gboolean
 
1366
g_socket_get_broadcast (GSocket *socket)
 
1367
{
 
1368
  int result;
 
1369
  guint value = 0, optlen;
 
1370
 
 
1371
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
1372
 
 
1373
  optlen = sizeof (guchar);
 
1374
  result = getsockopt (socket->priv->fd, SOL_SOCKET, SO_BROADCAST,
 
1375
                       &value, &optlen);
 
1376
 
 
1377
  if (result < 0)
 
1378
    {
 
1379
      int errsv = get_socket_errno ();
 
1380
      g_warning ("error getting broadcast: %s", socket_strerror (errsv));
 
1381
      return FALSE;
 
1382
    }
 
1383
 
 
1384
  return !!value;
 
1385
}
 
1386
 
 
1387
/**
 
1388
 * g_socket_set_broadcast:
 
1389
 * @socket: a #GSocket.
 
1390
 * @loopback: whether @socket should allow sending to and receiving
 
1391
 *     from broadcast addresses
 
1392
 *
 
1393
 * Sets whether @socket should allow sending to and receiving from
 
1394
 * broadcast addresses. This is %FALSE by default.
 
1395
 *
 
1396
 * Since: 2.32
 
1397
 */
 
1398
void
 
1399
g_socket_set_broadcast (GSocket    *socket,
 
1400
                        gboolean    broadcast)
 
1401
{
 
1402
  int result;
 
1403
  gint value;
 
1404
 
 
1405
  g_return_if_fail (G_IS_SOCKET (socket));
 
1406
 
 
1407
  broadcast = !!broadcast;
 
1408
  value = (guchar)broadcast;
 
1409
 
 
1410
  result = setsockopt (socket->priv->fd, SOL_SOCKET, SO_BROADCAST,
 
1411
                       &value, sizeof (value));
 
1412
 
 
1413
  if (result < 0)
 
1414
    {
 
1415
      int errsv = get_socket_errno ();
 
1416
      g_warning ("error setting broadcast: %s", socket_strerror (errsv));
 
1417
      return;
 
1418
    }
 
1419
 
 
1420
  g_object_notify (G_OBJECT (socket), "broadcast");
 
1421
}
 
1422
 
 
1423
/**
 
1424
 * g_socket_get_multicast_loopback:
 
1425
 * @socket: a #GSocket.
 
1426
 *
 
1427
 * Gets the multicast loopback setting on @socket; if %TRUE (the
 
1428
 * default), outgoing multicast packets will be looped back to
 
1429
 * multicast listeners on the same host.
 
1430
 *
 
1431
 * Returns: the multicast loopback setting on @socket
 
1432
 *
 
1433
 * Since: 2.32
 
1434
 */
 
1435
gboolean
 
1436
g_socket_get_multicast_loopback (GSocket *socket)
 
1437
{
 
1438
  int result;
 
1439
  guint value = 0, optlen;
 
1440
 
 
1441
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
1442
 
 
1443
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1444
    {
 
1445
      optlen = sizeof (guchar);
 
1446
      result = getsockopt (socket->priv->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
 
1447
                           &value, &optlen);
 
1448
    }
 
1449
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1450
    {
 
1451
      optlen = sizeof (guint);
 
1452
      result = getsockopt (socket->priv->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
 
1453
                           &value, &optlen);
 
1454
    }
 
1455
  else
 
1456
    g_return_val_if_reached (FALSE);
 
1457
 
 
1458
  if (result < 0)
 
1459
    {
 
1460
      int errsv = get_socket_errno ();
 
1461
      g_warning ("error getting multicast loopback: %s", socket_strerror (errsv));
 
1462
      return FALSE;
 
1463
    }
 
1464
 
 
1465
  return !!value;
 
1466
}
 
1467
 
 
1468
/**
 
1469
 * g_socket_set_multicast_loopback:
 
1470
 * @socket: a #GSocket.
 
1471
 * @loopback: whether @socket should receive messages sent to its
 
1472
 *   multicast groups from the local host
 
1473
 *
 
1474
 * Sets whether outgoing multicast packets will be received by sockets
 
1475
 * listening on that multicast address on the same host. This is %TRUE
 
1476
 * by default.
 
1477
 *
 
1478
 * Since: 2.32
 
1479
 */
 
1480
void
 
1481
g_socket_set_multicast_loopback (GSocket    *socket,
 
1482
                                 gboolean    loopback)
 
1483
{
 
1484
  int result;
 
1485
 
 
1486
  g_return_if_fail (G_IS_SOCKET (socket));
 
1487
 
 
1488
  loopback = !!loopback;
 
1489
 
 
1490
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1491
    {
 
1492
      guchar value = (guchar)loopback;
 
1493
 
 
1494
      result = setsockopt (socket->priv->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
 
1495
                           &value, sizeof (value));
 
1496
    }
 
1497
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1498
    {
 
1499
      guint value = (guint)loopback;
 
1500
 
 
1501
      result = setsockopt (socket->priv->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
 
1502
                           &value, sizeof (value));
 
1503
    }
 
1504
  else
 
1505
    g_return_if_reached ();
 
1506
 
 
1507
  if (result < 0)
 
1508
    {
 
1509
      int errsv = get_socket_errno ();
 
1510
      g_warning ("error setting multicast loopback: %s", socket_strerror (errsv));
 
1511
      return;
 
1512
    }
 
1513
 
 
1514
  g_object_notify (G_OBJECT (socket), "multicast-loopback");
 
1515
}
 
1516
 
 
1517
/**
 
1518
 * g_socket_get_multicast_ttl:
 
1519
 * @socket: a #GSocket.
 
1520
 *
 
1521
 * Gets the multicast time-to-live setting on @socket; see
 
1522
 * g_socket_set_multicast_ttl() for more details.
 
1523
 *
 
1524
 * Returns: the multicast time-to-live setting on @socket
 
1525
 *
 
1526
 * Since: 2.32
 
1527
 */
 
1528
guint
 
1529
g_socket_get_multicast_ttl (GSocket *socket)
 
1530
{
 
1531
  int result;
 
1532
  guint value, optlen;
 
1533
 
 
1534
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
1535
 
 
1536
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1537
    {
 
1538
      guchar optval;
 
1539
 
 
1540
      optlen = sizeof (optval);
 
1541
      result = getsockopt (socket->priv->fd, IPPROTO_IP, IP_MULTICAST_TTL,
 
1542
                           &optval, &optlen);
 
1543
      value = optval;
 
1544
    }
 
1545
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1546
    {
 
1547
      optlen = sizeof (value);
 
1548
      result = getsockopt (socket->priv->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
 
1549
                           &value, &optlen);
 
1550
    }
 
1551
  else
 
1552
    g_return_val_if_reached (FALSE);
 
1553
 
 
1554
  if (result < 0)
 
1555
    {
 
1556
      int errsv = get_socket_errno ();
 
1557
      g_warning ("error getting multicast ttl: %s", socket_strerror (errsv));
 
1558
      return FALSE;
 
1559
    }
 
1560
 
 
1561
  return value;
 
1562
}
 
1563
 
 
1564
/**
 
1565
 * g_socket_set_multicast_ttl:
 
1566
 * @socket: a #GSocket.
 
1567
 * @ttl: the time-to-live value for all multicast datagrams on @socket
 
1568
 *
 
1569
 * Sets the time-to-live for outgoing multicast datagrams on @socket.
 
1570
 * By default, this is 1, meaning that multicast packets will not leave
 
1571
 * the local network.
 
1572
 *
 
1573
 * Since: 2.32
 
1574
 */
 
1575
void
 
1576
g_socket_set_multicast_ttl (GSocket  *socket,
 
1577
                            guint     ttl)
 
1578
{
 
1579
  int result;
 
1580
 
 
1581
  g_return_if_fail (G_IS_SOCKET (socket));
 
1582
 
 
1583
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1584
    {
 
1585
      guchar optval = (guchar)ttl;
 
1586
 
 
1587
      result = setsockopt (socket->priv->fd, IPPROTO_IP, IP_MULTICAST_TTL,
 
1588
                           &optval, sizeof (optval));
 
1589
    }
 
1590
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1591
    {
 
1592
      result = setsockopt (socket->priv->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
 
1593
                           &ttl, sizeof (ttl));
 
1594
    }
 
1595
  else
 
1596
    g_return_if_reached ();
 
1597
 
 
1598
  if (result < 0)
 
1599
    {
 
1600
      int errsv = get_socket_errno ();
 
1601
      g_warning ("error setting multicast ttl: %s", socket_strerror (errsv));
 
1602
      return;
 
1603
    }
 
1604
 
 
1605
  g_object_notify (G_OBJECT (socket), "multicast-ttl");
 
1606
}
 
1607
 
 
1608
/**
1167
1609
 * g_socket_get_family:
1168
1610
 * @socket: a #GSocket.
1169
1611
 *
1452
1894
  return TRUE;
1453
1895
}
1454
1896
 
 
1897
static gboolean
 
1898
g_socket_multicast_group_operation (GSocket       *socket,
 
1899
                                    GInetAddress  *group,
 
1900
                                    gboolean       source_specific,
 
1901
                                    const gchar   *interface,
 
1902
                                    gboolean       join_group,
 
1903
                                    GError       **error)
 
1904
{
 
1905
  const guint8 *native_addr;
 
1906
  gint optname, result;
 
1907
 
 
1908
  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
 
1909
  g_return_val_if_fail (socket->priv->type == G_SOCKET_TYPE_DATAGRAM, FALSE);
 
1910
  g_return_val_if_fail (G_IS_INET_ADDRESS (group), FALSE);
 
1911
  g_return_val_if_fail (g_inet_address_get_family (group) == socket->priv->family, FALSE);
 
1912
 
 
1913
  if (!check_socket (socket, error))
 
1914
    return FALSE;
 
1915
 
 
1916
  native_addr = g_inet_address_to_bytes (group);
 
1917
  if (socket->priv->family == G_SOCKET_FAMILY_IPV4)
 
1918
    {
 
1919
#ifdef HAVE_IP_MREQN
 
1920
      struct ip_mreqn mc_req;
 
1921
#else
 
1922
      struct ip_mreq mc_req;
 
1923
#endif
 
1924
 
 
1925
      memcpy (&mc_req.imr_multiaddr, native_addr, sizeof (struct in_addr));
 
1926
 
 
1927
#ifdef HAVE_IP_MREQN
 
1928
      if (interface)
 
1929
        mc_req.imr_ifindex = if_nametoindex (interface);
 
1930
      else
 
1931
        mc_req.imr_ifindex = 0;  /* Pick any.  */
 
1932
#else
 
1933
      mc_req.imr_interface.s_addr = g_htonl (INADDR_ANY);
 
1934
#endif
 
1935
 
 
1936
      if (source_specific)
 
1937
        optname = join_group ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
 
1938
      else
 
1939
        optname = join_group ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
 
1940
      result = setsockopt (socket->priv->fd, IPPROTO_IP, optname,
 
1941
                           &mc_req, sizeof (mc_req));
 
1942
    }
 
1943
  else if (socket->priv->family == G_SOCKET_FAMILY_IPV6)
 
1944
    {
 
1945
      struct ipv6_mreq mc_req_ipv6;
 
1946
 
 
1947
      memcpy (&mc_req_ipv6.ipv6mr_multiaddr, native_addr, sizeof (struct in6_addr));
 
1948
      if (interface)
 
1949
        mc_req_ipv6.ipv6mr_interface = if_nametoindex (interface);
 
1950
      else
 
1951
        mc_req_ipv6.ipv6mr_interface = 0;
 
1952
 
 
1953
      optname = join_group ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
 
1954
      result = setsockopt (socket->priv->fd, IPPROTO_IPV6, optname,
 
1955
                           &mc_req_ipv6, sizeof (mc_req_ipv6));
 
1956
    }
 
1957
  else
 
1958
    g_return_val_if_reached (FALSE);
 
1959
 
 
1960
  if (result < 0)
 
1961
    {
 
1962
      int errsv = get_socket_errno ();
 
1963
 
 
1964
      g_set_error (error, G_IO_ERROR, socket_io_error_from_errno (errsv),
 
1965
                   join_group ?
 
1966
                   _("Error joining multicast group: %s") :
 
1967
                   _("Error leaving multicast group: %s"),
 
1968
                   socket_strerror (errsv));
 
1969
      return FALSE;
 
1970
    }
 
1971
 
 
1972
  return TRUE;
 
1973
}
 
1974
 
 
1975
/**
 
1976
 * g_socket_join_multicast_group:
 
1977
 * @socket: a #GSocket.
 
1978
 * @group: a #GInetAddress specifying the group address to join.
 
1979
 * @interface: Interface to use
 
1980
 * @source_specific: %TRUE if source-specific multicast should be used
 
1981
 * @error: #GError for error reporting, or %NULL to ignore.
 
1982
 *
 
1983
 * Registers @socket to receive multicast messages sent to @group.
 
1984
 * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have
 
1985
 * been bound to an appropriate interface and port with
 
1986
 * g_socket_bind().
 
1987
 *
 
1988
 * If @source_specific is %TRUE, source-specific multicast as defined
 
1989
 * in RFC 4604 is used.
 
1990
 *
 
1991
 * Returns: %TRUE on success, %FALSE on error.
 
1992
 *
 
1993
 * Since: 2.32
 
1994
 */
 
1995
gboolean
 
1996
g_socket_join_multicast_group (GSocket       *socket,
 
1997
                               GInetAddress  *group,
 
1998
                               gboolean       source_specific,
 
1999
                               const gchar   *interface,
 
2000
                               GError       **error)
 
2001
{
 
2002
  return g_socket_multicast_group_operation (socket, group, source_specific, interface, TRUE, error);
 
2003
}
 
2004
 
 
2005
/**
 
2006
 * g_socket_leave_multicast_group:
 
2007
 * @socket: a #GSocket.
 
2008
 * @group: a #GInetAddress specifying the group address to leave.
 
2009
 * @interface: Interface to use
 
2010
 * @source_specific: %TRUE if source-specific multicast should be used
 
2011
 * @error: #GError for error reporting, or %NULL to ignore.
 
2012
 *
 
2013
 * Removes @socket from the multicast group @group (while still
 
2014
 * allowing it to receive unicast messages).
 
2015
 *
 
2016
 * If @source_specific is %TRUE, source-specific multicast as defined
 
2017
 * in RFC 4604 is used.
 
2018
 *
 
2019
 * Returns: %TRUE on success, %FALSE on error.
 
2020
 *
 
2021
 * Since: 2.32
 
2022
 */
 
2023
gboolean
 
2024
g_socket_leave_multicast_group (GSocket       *socket,
 
2025
                                GInetAddress  *group,
 
2026
                                gboolean       source_specific,
 
2027
                                const gchar   *interface,
 
2028
                                GError       **error)
 
2029
{
 
2030
  return g_socket_multicast_group_operation (socket, group, source_specific, interface, FALSE, error);
 
2031
}
 
2032
 
1455
2033
/**
1456
2034
 * g_socket_speaks_ipv4:
1457
2035
 * @socket: a #GSocket
1759
2337
}
1760
2338
 
1761
2339
/**
 
2340
 * g_socket_get_available_bytes:
 
2341
 * @socket: a #GSocket
 
2342
 *
 
2343
 * Get the amount of data pending in the OS input buffer.
 
2344
 *
 
2345
 * Returns: the number of bytes that can be read from the socket
 
2346
 * without blocking or -1 on error.
 
2347
 *
 
2348
 * Since: 2.32
 
2349
 */
 
2350
gssize
 
2351
g_socket_get_available_bytes (GSocket *socket)
 
2352
{
 
2353
#ifndef G_OS_WIN32
 
2354
  gulong avail = 0;
 
2355
#else
 
2356
  gint avail = 0;
 
2357
#endif
 
2358
 
 
2359
  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
 
2360
 
 
2361
#ifndef G_OS_WIN32
 
2362
  if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
 
2363
    return -1;
 
2364
#else
 
2365
  if (ioctlsocket (socket->priv->fd, FIONREAD, &avail) == SOCKET_ERROR)
 
2366
    return -1;
 
2367
#endif
 
2368
 
 
2369
  return avail;
 
2370
}
 
2371
 
 
2372
/**
1762
2373
 * g_socket_receive:
1763
2374
 * @socket: a #GSocket
1764
2375
 * @buffer: a buffer to read data into (which should be at least @size
1890
2501
/**
1891
2502
 * g_socket_receive_from:
1892
2503
 * @socket: a #GSocket
1893
 
 * @address: a pointer to a #GSocketAddress pointer, or %NULL
1894
 
 * @buffer: a buffer to read data into (which should be at least @size
1895
 
 *     bytes long).
 
2504
 * @address: (out) (allow-none): a pointer to a #GSocketAddress
 
2505
 *     pointer, or %NULL
 
2506
 * @buffer: (array length=size) (element-type guint8): a buffer to
 
2507
 *     read data into (which should be at least @size bytes long).
1896
2508
 * @size: the number of bytes you want to read from the socket
1897
2509
 * @cancellable: (allow-none): a %GCancellable or %NULL
1898
2510
 * @error: #GError for error reporting, or %NULL to ignore.
1944
2556
/**
1945
2557
 * g_socket_send:
1946
2558
 * @socket: a #GSocket
1947
 
 * @buffer: (array length=size): the buffer containing the data to send.
 
2559
 * @buffer: (array length=size) (element-type guint8): the buffer
 
2560
 *     containing the data to send.
1948
2561
 * @size: the number of bytes to send
1949
2562
 * @cancellable: (allow-none): a %GCancellable or %NULL
1950
2563
 * @error: #GError for error reporting, or %NULL to ignore.
1984
2597
/**
1985
2598
 * g_socket_send_with_blocking:
1986
2599
 * @socket: a #GSocket
1987
 
 * @buffer: (array length=size): the buffer containing the data to send.
 
2600
 * @buffer: (array length=size) (element-type guint8): the buffer
 
2601
 *     containing the data to send.
1988
2602
 * @size: the number of bytes to send
1989
2603
 * @blocking: whether to do blocking or non-blocking I/O
1990
2604
 * @cancellable: (allow-none): a %GCancellable or %NULL
2063
2677
 * g_socket_send_to:
2064
2678
 * @socket: a #GSocket
2065
2679
 * @address: a #GSocketAddress, or %NULL
2066
 
 * @buffer: (array length=size): the buffer containing the data to send.
 
2680
 * @buffer: (array length=size) (element-type guint8): the buffer
 
2681
 *     containing the data to send.
2067
2682
 * @size: the number of bytes to send
2068
2683
 * @cancellable: (allow-none): a %GCancellable or %NULL
2069
2684
 * @error: #GError for error reporting, or %NULL to ignore.
2497
3112
{
2498
3113
  GSocketSourceFunc func = (GSocketSourceFunc)callback;
2499
3114
  GSocketSource *socket_source = (GSocketSource *)source;
 
3115
  GSocket *socket = socket_source->socket;
 
3116
  gboolean ret;
2500
3117
 
2501
3118
#ifdef G_OS_WIN32
2502
3119
  socket_source->pollfd.revents = update_condition (socket_source->socket);
2504
3121
  if (socket_source->socket->priv->timed_out)
2505
3122
    socket_source->pollfd.revents |= socket_source->condition & (G_IO_IN | G_IO_OUT);
2506
3123
 
2507
 
  return (*func) (socket_source->socket,
2508
 
                  socket_source->pollfd.revents & socket_source->condition,
2509
 
                  user_data);
 
3124
  ret = (*func) (socket,
 
3125
                 socket_source->pollfd.revents & socket_source->condition,
 
3126
                 user_data);
 
3127
 
 
3128
  if (socket->priv->timeout)
 
3129
    socket_source->timeout_time = g_get_monotonic_time () +
 
3130
                                  socket->priv->timeout * 1000000;
 
3131
 
 
3132
  else
 
3133
    socket_source->timeout_time = 0;
 
3134
 
 
3135
  return ret;
2510
3136
}
2511
3137
 
2512
3138
static void
3158
3784
/**
3159
3785
 * g_socket_receive_message:
3160
3786
 * @socket: a #GSocket
3161
 
 * @address: a pointer to a #GSocketAddress pointer, or %NULL
 
3787
 * @address: (out) (allow-none): a pointer to a #GSocketAddress
 
3788
 *     pointer, or %NULL
3162
3789
 * @vectors: (array length=num_vectors): an array of #GInputVector structs
3163
3790
 * @num_vectors: the number of elements in @vectors, or -1
3164
3791
 * @messages: (array length=num_messages) (allow-none): a pointer which