Network Programming. Multicast on a LAN 1/4. Sending & Receiving Multicast Messages. Multicasting II. Dr. Thaier Hayajneh

Similar documents
Multicast on a LAN 3/4. Multicast on a LAN 2/4. Multicast on a WAN. Multicast on a LAN 4/4

MCAST_BLOCK_SOURCE (struct group_source_req) Adds a source address to the list of blocked sources for a multicast group in "exclude" mode.

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Advanced WinSock Multiplayer Game Programming: Multicasting by Denis Lukianov

Network programming(ii) Lenuta Alboaie

Lecture 6 Overview. Last Lecture. This Lecture. Next Lecture. Name and address conversions

UNIT IV- SOCKETS Part A

Introduction to Network Programming using C/C++

UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

CSE 124 Discussion Section Sockets Programming 10/10/17

Multicast. Note. Sending Multicast Messages

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Introduction to Network Programming in C/C++

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

EEC-484/584 Computer Networks

Elementary TCP Sockets

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

PoE VoIP Paging Server Operations Guide

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

IPv4 and ipv6 INTEROPERABILITY

AdaSockets reference manual

Group-A Assignment No. 6

A. Basic Function Calls for Network Communications

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

AdaSockets reference manual

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Chapter 5. TCP Client-Server

Εργαστήριο 9 I/O Multiplexing

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

The Modification and Implementation of Campus Network Client. Lingfang Huanga

Oral. Total. Dated Sign (2) (5) (3) (2)

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

UDP CONNECT TO A SERVER

Introduction! Overview! Signal-driven I/O for Sockets! Two different UDP servers!

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

use Socket qw(pf_inet SOCK_STREAM pack_sockaddr_in inet_aton);

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Socket Programming for TCP and UDP

Redesde Computadores(RCOMP)

The User Datagram Protocol

Vision Components. The Smart Camera People. VCRT 5.0 TCP/ IP Functions. TCP/ IP Operation System Functions

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

UNIT IV - SOCKET OPTIONS, ELEMENTARY UDP SOCKETS

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

NetX, NetX Duo BSD Support Module Guide

Connection Oriented Networking MPLS and ATM

Introduction to Client-Server Model

Any of the descriptors in the set {1, 4} have an exception condition pending

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Network Communication

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Overview. Daemon processes and advanced I/O. Source: Chapters 13&14 of Stevens book

Sockets 15H2. Inshik Song

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

CS 3516: Computer Networks

Socket Programming(2/2)

µtasker Document µtasker Multicasting and Internet Group Management Protocol (IGMP)

Department of Computer Science

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

IP - The Internet Protocol. Based on the slides of Dr. Jorg Liebeherr, University of Virginia

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

A Client-Server Exchange

Semester 2, Computer Communication 352 Module 4

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

CSE 124: OPTIONS, SIGNALS, TIMEOUTS, AND CONCURRENCY. George Porter Oct 13, 2017

CSE 333 Section 8 - Client-Side Networking

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Sockets Sockets Communication domains

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Internet Group Communication: IP Multicasting

SOCKET. Valerio Di Valerio

A set of processes distributed over a network that communicate via messages. Processes communicate via services offered by the operating system

Internetworking II: Network programming. April 20, 2000

Unix Network Programming

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

Network Layer II. Getting IP addresses. DHCP client-server scenario. DHCP client-server scenario. C compiler. You writing assignment 2

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

STUDY OF SOCKET PROGRAMMING

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Assignment 1: tcpbridge

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Socket Programming TCP UDP

Network Socket Programming - 3 BUPT/QMUL

31 ChamSys Remote Protocol Commands

CLIENT-SIDE PROGRAMMING

Linux Kernel Application Interface

Lecture 10 Overview!

Category: Informational J. Bound J. McCann Hewlett-Packard W. Stevens February 2003

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Cellular Framework. Application Note. Renesas Synergy Platform. Introduction. Required Resources. Prerequisites and Intended Audience

Transcription:

Network Programming Dr. Thaier Hayajneh Computer Engineering Department Multicasting g( (Chapter 21) Outline Sending and Receiving Messages Multicasting on a LAN Multicasting on a WAN Multicast Issues Examples Multicasting II 1 2 Sending & Receiving Multicast Messages Receiving Multicast Messages Create a UDP socket Bind it to a UDP port, e.g., 123 All processes must bind to the same port in order to receive the multicast messages Join a multicast group address Use recv or recvfrom to read the messages Sending Multicast Messages You may use the same socket t( (you used dfor receiving) i )for sending multicast messages or you can use any other UDP socket (it does not have to join any multicast group) 3 Multicast on a LAN 1/4 Receiving application creates a UDP socket, binds to port 123 and joins multicast group 224.0.1.1 IPv4 layers saves the information internally and tells appropriate datalink to receive Ethernet frames destined to 01:00:5E:00:01:01 Sending applications creates a UDP socket and sends a datagram to 224.0.1.1, port 123 Ethernet frame contains destination Ethernet address, destination IP address, and destination port A host on the LAN that did not express interest in receiving multicast from that group will ignore such datagram Destination Ethernet address does not match the interface address Destination Ethernet address is not the Ethernet broadcast address The interface has not been told to receive any group addresses 4

Multicast on a LAN 2/4 Ethernet frame received by datalink of receiver based on imperfect filtering (When interface told to receive frames destined to one specific Ethernet multicast address, it can receive frames destined to other Ethernet multicast addresses) Ethernet interface cards apply a hash function to group address, calculating a value between 0 and 511. This information turns on a bit in a 512-bit array Small size bit-array implies receiving unwanted frames (old cards) Some network cards provide perfect filtering Some network cards have no multicast filtering at all (multicast promiscuous mode) Packet passed to IP layer (IP layer compares group address against all multicast addresses that applications on this host have joined perfect filtering) Packet passed to UDP layer, which passes it to socket bound to port 123 5 Multicast on a LAN 3/4 Some Other scenarios A host running an application that has joined 225.0.1.1 Ethernet address 01:00:5E:00:01:01. Packet will be discarded by perfect filtering in IP layer A host running an application that has joined some multicast group which the Ethernet address produces the same hash value as 01:00:5E:00:01:01. 00 00 Packet will be discarded d d by datalink layer or by IP layer A packet destined to the same group, but a different port. Accepted by IP layer, but discarded by UDP layer (no socket has bound the different port) 6 Multicast on a LAN 4/4 Multicast on a WAN A program started on five hosts belonging to different LANs Multicast routers communicate with neighbor routers using a multicast routing protocol (MRP) When a process on a host joins a multicast group, that host sends an IGMP message to any attached multicast routers, which in turn exchange this information using MRP with neighbor routers When a sender sends a multicast message, multicast routing information is used to direct the message H1 S MR1 MR5 MR2 MR3 MR4 H4 H5 H2 H3 7 8

Some Multicast Issues Time To Live Set TTL for outgoing multicast datagrams (default is 1 local subnet) Loopback mode Enable or disable local loopback of multicast datagrams By default loopback is enabled A copy of each multicast datagram sent by a process on the host will also be looped back and processed as a received datagram by that host Port Reuse Allow the same multicast application to have several instances running on the same host In Java, Port reuse is enabled by default, in C it is not 9 Socket Options 1/2 Various attributes that are used to determine the behavior of sockets (see chapter 7) #include <sys/socket.h> int getsockopt (int sockfd, int level, int optname, void * optval, socklen_t *optlen); int setsockopt (int sockfd, int level, int optname, const void * optval, socklen_t optlen); Both return 0 if OK, -1 on error sockfd: an open socket descriptor level: code in the system that interprets the option (general socket code, or protocol-specific code) (SOL_SOCKET, IPPROTO_IP, IPPROTO_IPv6, IPPROTO_TCP are examples) optname: see page 193-figure 7.1, and page 194-figure 7.2 10 Socket Options 2/2 Some socket options examples (see table on page 193 and 194) For multicast socket options see section 21.6 on page 559 For multicast group membership socket options, see page 560 Socket Level SO_ SNDBUF, SO_ RCVBUF, SO_ KEEPALIVE, SO_BROADCAST, SO_REUSEADDR, SO_RESUEPORT IP Level IP_TTL, IPMULTICAST_IF, IPMUTLICAST_TTL, IP_MULTICAST_LOOP, LOOP, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP TCP Level TCP_KEEPALIVE, TCP_MAXSEG, TCP_NODELAY 11 Add Membership Socket Option 1/2 Option: IP_ADD_MEMBERSHIP Parameter: Multicast address structure Operation Supports JoinHostGroup of RFC 1112 - allows a host s interface to join a multicast group Required to receive multicast datagrams Not required to send multicast datagrams Each interface can be in multiple groups Multiple interfaces can be in the same group Causes host to send IGMP report if this is a new group address for this host Tells network adapter multicast group address 12

Add Membership Socket Option 2/2 Example call to setsockopt(): setsockopt( size*/ ); sock, /* socket */ IPPROTO_IP, /* level */ IP_ADD_MEMBERSHIP, /*option */ (char *) &mreq, /* argument */ sizeof(mreq) /* argument Multicast Address Structure specifies the multicast group address and the interface Interface specified as an IP address INADDR_ANYANY specifies use of the default multicast t interface struct ip_mreq struct in_addr imr_multiaddr; multiaddr; // group struct in_addr imr_interface; // interface char group[]= 234.5.6.7 ; mreq.imr_multiaddr.s_addr = inet_addr(group); mreq.imr_interface.s_addr = INADDR_ANY; 13 14 Reusing Port Numbers 1/2 What if you want to have multiple l sockets on the same host listen to the same multicast group? Need to bind the same port number to all sockets This will cause an error when bind is called for the second and later sockets unless socket has been set to reuse address Set SO_REUSEADDR socket option allows completely duplicate bindings A bind on an IP address and a port, when that same IP address and port are already bound to another socket (only for UDP sockets for multicast) OptValue = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &OptValue, sizeof(optvalue)); 15 Reusing Port Numbers 2/2 SO_REUSEPORT Can use SO_REUSEPORT socket option which was introduced in 4.4 BSD Allows completely duplicate bindings, only if each socket that wants to bind the same IP address and port specify this socket option Not supported by all systems SO_ REUSEADDR considered equivalent to SO_REUSEPORT if the IP address being bound is a multicast address Conclusion When writing a multicast application that can run multiple times on the same host at the same time, set SO_REUSEADDR option and bind group s multicast address as the local IP address 16

Drop Membership Socket Option 1/2 Option: IP_DROP_MEMBERSHIP Parameter: Multicast address structure Operation Supports LeaveHostGroup of RFC 1112- allows host to leave a multicast group Host s TCP/IP implementation maintains a counter for each group address Incremented for IP_ADD_MEMBERSHIP Decremented for IP_DROP_MEMBERSHIP If count reaches zero Tells adapter to drop multicast address Won t report group address for IGMP query When a socket is closed, membership dropped automatically 17 Drop Membership Socket Option 2/2 Drop membership socket option Need to set group address and interface in ip_ mreq structure (same values as used with IP_ADD_MEMBERSHIP) Example call to setsockopt(): setsockopt( sock, /* socket */ IPPROTO_IP, /* level */ IP_DROP_MEMBERSHIP, /* option */ (char *) &mreq, /* argument */ sizeof(mreq) /* argument size */ ); 18 Receiving Multicast Data Create a standard SOCK_DGRAM socket Set SO_REUSEADDR option for socket Bind address to socket Specify IP address as multicast address Specify port Set IP_ADD_MEMBERSHIP option for socket Specify host group address After these steps completed successfully, receive multicast data for specified group address and port using recvfrom() Drop group membership when finished using IP_DROP_MEMBERSHIP option 19 Sending Multicast Data Use standard SOCK_DGRAM socket Sending alone does not require group membership To send multicast datagrams: Use sendto() to send to appropriate group address and port number, or Use connect() to set group address and port and then use send() Concerns (controlled with socket options) Interface used to send: IP_MULTICAST_IF (relevant to hosts with multiple interfaces) Extent of multicast: IP_MULTICAST_TTL Receiving own data: IP_MULTICAST_LOOP 20

Time to Live Socket Option 1/2 Option: IP_MULTICAST_TTL Parameter: TTL value (int) Operation Controls the time-to-live (TTL) value that IP will use for multicast datagrams Default TTL is 1 multicast datagrams will not leave the local network To send multicast datagrams beyond the local network TTL must be greater than 1, and Intermediate routers must support multicast Group address 224.0.0.0 224.0.0.255 not routed, regardless of TTL value A TTL = 0 will confine to local host 21 Time to Live Socket Option 2/2 int ttl = 5; setsockopt( p( sock, /* socket */ IPPROTO_IP, /* level */ IP_MULTICAST_TTL, TTL /* option */ (char *) &ttl, /* argument */ sizeof(ttl) /*argument size*/ ); 22 Multicast Loopback Socket Option 1/2 Option: IP_MULTICAST_LOOP Parameter: Boolean (TRUE to enable) Operation If enabled (default), socket will receive a copy of multicast datagrams that were sent on that socket Even if disabled, host with two interfaces may receive a copy on the other interface(s) This is an internal loopback performed at the IP layer Multicast Loopback Socket Option 2/2 BOOL opt = FALSE; setsockopt( sock, /* socket */ IPPROTO_IP, /* level */ IP_MULTICAST_LOOP, /* option */ (char *) &opt, /* argument */ sizeof(opt) /* argument size */ ); 23 24

Textbook Multicast Helper Functions See section 21.7 on page 565 int mcast_join (int sockfd, const struct sockaddr*grp, socklen_t grplen, const char* ifname, u_int ifindex); ) int mcast_leave(int sockfd, const struct sockaddr *grp, socklen_t grplen); int mcast_set_loop (int sockfd, int flag) int mcast_set_ttl set ttl (int sockfd, int ttl); //All Above return 0 if OK, -1 on error int mcast get_loop (int sockfd); int mcast_get_ttl (int sockfd); //return value is OK, -1 on error 25 Example for Sending and Receiving Section 21.10 page 575 Function udp_client introduced in 11.14 on page 334 Creates an unconnected UDP socket Return value is the socket descriptor A program to send and receive multicast t datagrams Send datagram to a specific group every five seconds (datagram contains sender s hostname and process ID) An infinite loop that joins the multicast group to which the sending part is sending and prints every received datagram Create a UDP socket then set multicast socket options for address reuse, joining the group, and setting loopback See mcast/main.c, c mcast/send.c, c and mcast/recv.cc 26 Sockets Timeout Section 14.2 page 381 There are three ways to place a timeout on an I/O operation involving a socket. 1. Call alarm which h generates the SIGALARM signal when the specified time has expired. 2. Block waiting for I/O in select, which has a time limit build-in 3. Use the newer SO_RCVTIMEO and SO_SNDTIMEO SNDTIMEO socket options. 27 Connect with a Timeout Using SIGALRM (figure 13.1) #include static void "unp.h" connect_alarm(int); int connect_timeo(int sockfd, const SA *saptr, socklen_t salen, int nsec) Sigfunc *sigfunc; int n; sigfunc = Signal(SIGALRM, connect_alarm); if (alarm(nsec) (!= 0) err_msg("connect_timeo: alarm was already set"); if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0) close(sockfd); if (errno == EINTR) errno = ETIMEDOUT; alarm(0); /* turn off the alarm */ Signal(SIGALRM, sigfunc); /* restore previous signal handler */ return(n); static void connect_alarm(int signo) return; /* just interrupt the connect() */ 28

recvfrom with a Timeout Using SIGALRM (figure 13.2) #include "unp.h" static void sig_alrm(int); void dg_cli(file *fp, int sockfd, const SA *pservaddr, socklen_t servlen) int n; char sendline[maxline], recvline[maxline + 1]; Signal(SIGALRM, sig_alrm); while (Fgets(sendline, MAXLINE, fp)!= NULL) Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); alarm(5); if ( (n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) < 0) if (errno == EINTR) fprintf(stderr, "socket timeout\n"); else err_sys("recvfrom error"); else alarm(0); recvline[n] = 0; /* null terminate */ Fputs(recvline, stdout); static void sig_alrm(int signo) return; /* just interrupt the recvfrom() */ 29 #include recvfrom with a Timeout Using select (figure 13.3) "unp.h" int readable_timeo(int fd, int sec) fd_set rset; struct timeval tv; FD_ZERO(&rset); FD_SET(fd, &rset); tv.tv_sec = sec; tv.tv_usec = 0; return(select(fd+1, &rset, NULL, NULL, &tv)); /* 4> 0 if descriptor is readable */ 30