Title: Chaptere19 multicasting
1Chaptere19multicasting
2contents
- multicast address
- multicasting versus broadcasting on a LAN
- multicasting on a WAN
- multicast socket option
- mcast_join and related function
- dg_cli function using multicasting
- receiving MBone session announcements
- sending and receiving
- SNTP
3- Unicast
- a single interface
- broadcast
- multiple interfaces
- LAN
- multicast
- a set of interfaces
- LAN or WAN
- Mbone
- five socket opetions
4multicast address
- IPv4 class D address
- 224.0.0.0 239.255.255.255
- (224.0.0.1 all hosts group), (224.0.0.2
all-routers group)
5multicast address(2)
- IPv6 multicast address
- special IPv6 multicast address
- ff021 gt all-nodes group(all multicast-capable
hosts on subnet must join this group on all
multicast-capable interfaces) - ff022 gt all-routers group(all
multicast-capable routers on subnet must join
this group on all multicast-capable interfaces)
6Scope of multicast addresses
- IPv6 multicast address have an explicit 4-bit
scope field that specifies how far the multicast
packet will travel. - Node-local (1)
- link-local (2)
- site-local (5)
- organization-local (8)
- global (14)
- remaining values are unassigned or reserved
7(No Transcript)
8multicasting versus broadcasting on a LAN
- One host sends a multicast packet and any
interested host receives the packet. - Benefit
- reducing the load on all the hosts not interested
in the multicast packets
9(No Transcript)
10multicasting on a WAN
11A host sends the audio packets and the multicast
receivers are waiting to receive
MRP Multicast Routing Protocol
12(No Transcript)
13multicast socket option
- The API support for multicasting requires only
five new socket options. - Figure 19.7
14multicast socket option(2)
- IP_ADD_MEMBERSHIP, IPV6_ADD_MEMBERSHIP
- join a multicast group on s specified local
interface.
Struct ip_mreq struct in_addr imr_multiaddr stru
ct in_addr imr_interface
Struct ipv6_mreq struct in6_addr
ipv6mr_multiaddr struct int ipv6mr_interface
15multicast socket option(3)
- IP_DROP_MEMBERSHIP, IPV6_DROP_MEMBERSHIP
- leave a multicast group on a specified local
interface. - If the local interface is not specified, the
first matching multicasting group membership is
dropped.
16multicast socket option(4)
- IP_MULTICAST_IF, IPV6_MULTICAST_IF
- specify the interface for outgoing multicast
datagrams sent on this socket. - This interface is specified as either an in_addr
structure for IPv4 or an interface index for IPv6.
17multicast socket option(5)
- IP_MULTICAST_TTL, IPV6_MULTICAST_TTL
- set the IPv4 TTL or the IPv6 hop limit for
outgoing multicast datagrams. - If this is not specified , both default to 1,
which restricts the datagram to the local subnet.
18multicast socket option(6)
- IP_MULTICAST_LOOP, IPV6_MULTICAST_LOOP
- enable or disable local loopback of multicast
datagrams. - Default loopback is enabled.
- This is similar to broadcasting.
19mcast_join and related function
- include unp.h
- int mcast_join(int sockfd, const struct sockaddr
addr, socklen_t salen, const char ifname, u_int
ifindex) - int mcast_leave(int sockfd, const struct sockaddr
addr, socklen_t salen) - int mcast_set_if(int sockfd, const char ifname,
u_int ifindex) - int mcast_set_loop(int sockfd, int flag)
- int mcast_set_ttl(int sockfd, int ttl)
- All above return 0 if ok, -1 on
error - int mcast_get_if(int sockfd)
- return nonnegative interface index if OK,
-1 error - int mcast_get_loop(int sockfd)
- return current loopback flag if OK, -1
error - int mcast_get_ttl(int sockfd)
- return current TTL or hop limit if OK, -1
error
20- include "unp.h"
- include ltnet/if.hgt
- int mcast_join(int sockfd, const SA sa,
socklen_t salen, const char ifname, u_int
ifindex) -
- switch (sa-gtsa_family)
- case AF_INET
- struct ip_mreq mreq
- struct ifreq ifreq
- memcpy(mreq.imr_multiaddr, ((struct
sockaddr_in ) sa)-gtsin_addr, - sizeof(struct in_addr))
- if (ifindex gt 0)
- if (if_indextoname(ifindex, ifreq.ifr_name)
NULL) - errno ENXIO / i/f index not found /
- return(-1)
-
- goto doioctl
- else if (ifname ! NULL)
21- ifdef IPV6
- case AF_INET6
- struct ipv6_mreq mreq6
- memcpy(mreq6.ipv6mr_multiaddr, ((struct
sockaddr_in6 ) sa)-gtsin6_addr, - sizeof(struct in6_addr))
- if (ifindex gt 0)
- mreq6.ipv6mr_interface ifindex
- else if (ifname ! NULL)
- if ( (mreq6.ipv6mr_interface
if_nametoindex(ifname)) 0) - errno ENXIO / i/f name not found /
- return(-1)
-
- else
- mreq6.ipv6mr_interface 0
- return(setsockopt(sockfd, IPPROTO_IPV6,
IPV6_ADD_MEMBERSHIP, - mreq6, sizeof(mreq6)))
-
22- include "unp.h"
- int mcast_set_loop(int sockfd, int onoff)
-
- switch (sockfd_to_family(sockfd))
- case AF_INET
- u_char flag
- flag onoff
- return(setsockopt(sockfd, IPPROTO_IP,
IP_MULTICAST_LOOP, - flag, sizeof(flag)))
-
- ifdef IPV6
- case AF_INET6
- u_int flag
- flag onoff
- return(setsockopt(sockfd, IPPROTO_IPV6,
IPV6_MULTICAST_LOOP, flag, sizeof(flag))) -
- endif
- default
23dg_cli function using multicasting
24Receiving MBone session announcements
- To receive a multimedia conference on the MBone
a site needs to know only the multicast address
of the conference and the UDP ports for the
conferences data streams.(audio, video) - SAP(Session Announce Protocol)
- describe the way
- SDP(Session Description Protocol)
- the contents of these announcements
25- A site wishing to announce a session on the Mbone
- periodically sends a multicast packet containing
a description of the session to a well-known
multicast group and UDP port - Sites on Mbone
- run sdr program
- receives these announcements
- provides an interactive user interface that
displays the information - lets user send announcements
- A sample program
- only receives these session announcements to show
an example of a simple multicast receiving
program
26The program receiving the periodic SAP/SDP
announcements
- include "unp.h"
- define SAP_NAME "sap.mcast.net" / default group
name and port / - define SAP_PORT "9875"
- void loop(int, socklen_t)
- int main(int argc, char argv)
-
- int sockfd
- const int on 1
- socklen_t salen
- struct sockaddr sa
- if (argc 1)
- sockfd Udp_client(SAP_NAME, SAP_PORT, (void
) sa, salen) - else if (argc 4)
- sockfd Udp_client(argv1, argv2, (void )
sa, salen) - else err_quit("usage mysdr ltmcast-addrgt ltportgt
ltinterface-namegt") - Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
on, sizeof(on)) - Bind(sockfd, sa, salen)
27- include "unp.h"
- void loop(int sockfd, socklen_t salen)
- char bufMAXLINE1
- socklen_t len
- ssize_t n
- struct sockaddr sa
- struct sap_packet
- uint32_t sap_header
- uint32_t sap_src
- char sap_data1
- sapptr
- sa Malloc(salen)
- for ( )
- len salen
- n Recvfrom(sockfd, buf, MAXLINE, 0, sa,
len) - bufn 0 / null terminate /
- sapptr (struct sap_packet ) buf
- if ( (n - 2 sizeof(uint32_t)) lt 0)
28sending and receiving
- Sends and receives multicast datagrams
- First part
- sends a multicast datagram to a specific group
every 5 seconds and the datagram contains the
senders hostname and process ID - Second part
- an infinite loop that joins the multicast group
to which the first part is sending and prints
every received datagrams
29- include "unp.h"
- void recv_all(int, socklen_t)
- void send_all(int, SA , socklen_t)
- int main(int argc, char argv)
-
- int sendfd, recvfd
- const int on 1
- socklen_t salen
- struct sockaddr sasend, sarecv
- if (argc ! 3) err_quit("usage sendrecv
ltIP-multicast-addressgt ltportgt") - sendfd Udp_client(argv1, argv2, (void )
sasend, salen) - recvfd Socket(sasend-gtsa_family, SOCK_DGRAM,
0) - Setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR,
on, sizeof(on)) - sarecv Malloc(salen)
- memcpy(sarecv, sasend, salen)
- Bind(recvfd, sarecv, salen)
30- include "unp.h"
- include ltsys/utsname.hgt
- define SENDRATE 5 / send one datagram every 5
seconds / - void
- send_all(int sendfd, SA sadest, socklen_t salen)
-
- static char lineMAXLINE / hostname and
process ID / - struct utsname myname
- if (uname(myname) lt 0)
- err_sys("uname error")
- snprintf(line, sizeof(line), "s, d\n",
myname.nodename, getpid()) - for ( )
- Sendto(sendfd, line, strlen(line), 0, sadest,
salen) - sleep(SENDRATE)
31- include "unp.h"
- void
- recv_all(int recvfd, socklen_t salen)
-
- int n
- char lineMAXLINE1
- socklen_t len
- struct sockaddr safrom
- safrom Malloc(salen)
- for ( )
- len salen
- n Recvfrom(recvfd, line, MAXLINE, 0, safrom,
len) - linen 0 / null terminate /
- printf("from s s", Sock_ntop(safrom, len),
line) -
32SNTP
- NTP gt so sophisticated protocol
- SNTP gt simplified version of NTP
- hosts do not need the complexity of a complete
NTP implementation. - A client listening for NTP broadcast or
multicasts on all attached networks and then
prints the time difference between the NTP packet
and the hosts current time-of-day
33- define JAN_1970 2208988800UL / 1970 - 1900 in
seconds / - struct l_fixedpt / 64-bit fixed-point /
- uint32_t int_part
- uint32_t fraction
- struct s_fixedpt / 32-bit fixed-point /
- u_short int_part
- u_short fraction
- struct ntpdata / NTP header /
- u_char status
- u_char stratum
- u_char ppoll
- int precision8
- struct s_fixedpt distance
- struct s_fixedpt dispersion
- uint32_t refid
- struct l_fixedpt reftime
- struct l_fixedpt org
- struct l_fixedpt rec
- struct l_fixedpt xmt
34- include "sntp.h"
- int main(int argc, char argv)
-
- int sockfd
- char bufMAXLINE
- ssize_t n
- socklen_t salen, len
- struct ifi_info ifi
- struct sockaddr mcastsa, wild, from
- struct timeval now
- if (argc ! 2) err_quit("usage ssntp
ltIPaddressgt") - sockfd Udp_client(argv1, "ntp", (void )
mcastsa, salen) - wild Malloc(salen)
- memcpy(wild, mcastsa, salen) / copy family and
port / - sock_set_wild(wild, salen)
35Figure 19.18 ( 2/2 )
- ifdef MCAST
- / 4obtain interface list and process each one
/ - for (ifi Get_ifi_info(mcastsa-gtsa_family, 1)
ifi ! NULL - ifi ifi-gtifi_next)
- if (ifi-gtifi_flags IFF_MULTICAST)
- Mcast_join(sockfd, mcastsa, salen,
ifi-gtifi_name, 0) - printf("joined s on s\n",
- Sock_ntop(mcastsa, salen), ifi-gtifi_name)
-
- endif
- from Malloc(salen)
- for ( )
- len salen
- n Recvfrom(sockfd, buf, sizeof(buf), 0, from,
len) - Gettimeofday(now, NULL)
- sntp_proc(buf, n, now)
-
36- include "sntp.h
- void sntp_proc(char buf, ssize_t n, struct
timeval nowptr) -
- int version, mode
- uint32_t nsec, useci
- double usecf
- struct timeval curr, diff
- struct ntpdata ntp
- if (n lt sizeof(struct ntpdata))
- printf("\npacket too small d bytes\n", n)
- return
-
- ntp (struct ntpdata ) buf
- version (ntp-gtstatus VERSION_MASK) gtgt 3
- mode ntp-gtstatus MODE_MASK
- printf("\nvd, mode d, strat d, ", version,
mode, ntp-gtstratum) - if (mode MODE_CLIENT)
37- nsec ntohl(ntp-gtxmt.int_part) - JAN_1970
- useci htonl(ntp-gtxmt.fraction) / 32-bit
integer fraction / - usecf useci / integer fraction -gt double
/ - usecf / 4294967296.0 / divide by 232 -gt
0, 1.0) / - useci usecf 1000000.0 / fraction -gt parts
per million / - curr nowptr / make a copy as we might
modify it below / - if ( (diff.tv_usec curr.tv_usec - useci) lt 0)
- diff.tv_usec 1000000
- curr.tv_sec--
-
- diff.tv_sec curr.tv_sec - nsec
- useci (diff.tv_sec 1000000)
diff.tv_usec / diff in microsec / - printf("clock difference d usec\n", useci)
38SNTP(2)
One socket per unicast address One socket per
broadcast address One socket per interface on
which the multicast group is joined
39(No Transcript)