TCP/IP Programming Manual

a multicast datagram has a TTL value greater than 1 and a multicast router is attached to the
sending host's network, multicast datagrams can be forwarded beyond the local subnet. Multicast
routers forward the datagram to known networks that have hosts belonging to the specified multicast
group. The TTL value is decremented by each multicast router in the path. When the TTL value is
decremented to 0, the datagram is not forwarded further.
The following example shows how to use the IP_MULTICAST_TTL option to the setsockopt
library call:
u_char ttl;
ttl=2;
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof(ttl)) == -1)
perror("setsockopt");
A datagram addressed to an IP multicast destination is transmitted from the default network interface
unless the application specifies that an alternate network interface is associated with the socket.
The default interface is determined by the interface associated with the default route in the kernel
routing table or by the interface associated with an explicit route, if one exists. Using the
IP_MULTICAST_IF option to the setsockopt library call, an application can specify a network
interface other than that specified by the route in the kernel routing table.
The following example shows how to use the IP_MULTICAST_IF option to the setsockopt
library call to specify an interface other than the default:
int sock;
struct in_addr ifaddress;
char *if_to_use = "16.141.64.251";
.
.
.
ifaddress.s_addr = inet_addr(if_to_use);
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
ifaddress,
sizeof(ifaddress)) == -1)
perror ("error from setsockopt IP_MULTICAST_IF");
else
printf ("new interface set for sending multicast
datagrams\n");
If a multicast datagram is sent to a group of which the sending host is a member, a copy of the
datagram is, by default, looped back by the IP layer for local delivery. The IP_MULTICAST_LOOP
option to the setsockopt library call allows an application to disable this loopback delivery.
The following example shows how to use the IP_MULTICAST_LOOP option to the setsockopt
library call:
u_char loop=0;
if (setsockopt( sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop
sizeof(loop
)) == -1)
perror("setsockopt");
When the value of loop is 0, loopback is disabled. When the value of loop is 1, loopback is
enabled. For performance reasons, you should disable the default, unless applications on the same
host must receive copies of the datagrams.
Receiving IPv4 Multicast Datagrams
This subsection describe IPv4 only. For information about multicast for IPv6, see Multicast Changes
for IPv6 (page 59).
Multicasting Operations 45