= Multicast = * netstat -g * netstat -aun * iptables -L * iptables -F * http://man7.org/linux/man-pages/man7/ip.7.html * https://www.tldp.org/HOWTO/Multicast-HOWTO.html {{{ All of them are handled via two system calls: setsockopt() (used to pass information to the kernel) and getsockopt() (to retrieve information regarded multicast behavior). socket of the family AF_INET type SOCK_DGRAM For multicast programming, level will always be IPPROTO_IP. optname identifies the option we are setting/getting. IP_ADD_MEMBERSHIP IP_MULTICAST_TTL IP_MULTICAST_IF IP_MULTICAST_TTL, multicast datagrams are sent with a default value of 1, to prevent them to be forwarded beyond the local network. IP_MULTICAST_IF, system administrator specifies the default interface multicast datagrams should be sent from. The programmer can override this and choose a concrete outgoing interface for a given socket with this option. In determining or selecting outgoing interfaces, the following ioctls might be useful: SIOCGIFADDR (to get an interface's address), SIOCGIFCONF (to get the list of all the interfaces) and SIOCGIFFLAGS (to get an interface's flags and, thus, determine whether the interface is multicast capable or not -the IFF_MULTICAST flag-). If the host has more than one interface and the IP_MULTICAST_IF option is not set, multicast transmissions are sent from the default interface tell the kernel which multicast groups you are interested in become a member of that group, you should first fill a ip_mreq imr_multiaddr, holds the group address you want to join. This way, if you are in a multihomed host, you can join the same group in several interfaces. You can always fill this last member with the wildcard address (INADDR_ANY) and then the kernel will deal with the task of choosing the interface. setsockopt (socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); MULTICAST Address | 224.0.0.0 - 239.255.255.255 the range 239.0.0.0 to 239.255.255.255 has been reserved for "administrative scoping" (see section 2.3.1 for information on administrative scoping). multicast traffic is handled at the transport layer with UDP In principle, an application just needs to open a UDP socket and fill with a class D multicast address the destination address where it wants to send data to. TTL thresholds 1 Restricted to the same subnet. Won't be forwarded by a router. When the sending host is Level 2 conformant and is also a member of the group datagrams are being sent to, a copy is looped back by default. Interface selection. Hosts attached to more than one network should provide a way for applications to decide which network interface will be used to output the transmissions. If not specified, the kernel chooses a default one based on system administrator's configuration. With multicast, however, it is necessary to advise the kernel which multicast groups we are interested in. That is, we have to ask the kernel to "join" those multicast groups. You join a group on a particular network interface. If you don't specify a concrete interface, then the kernel will choose it based on its routing tables when datagrams are to be sent. It is also possible that more than one process joins the same multicast group on the same interface. They will all receive the datagrams sent to that group via that interface. }}} == C code publisher == '''publisher.c''' {{{#!highlight c #include #include #include #include #include #include #include #include #define PORT 4242 #define MAXLINE 1024 int main() { int sockfd; char *hello = "Hello from publisher!!!!!!!"; struct sockaddr_in servaddr; sockfd = socket(AF_INET, SOCK_DGRAM, 0 ); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = inet_addr("225.3.2.1"); int n, len; int size = strlen(hello)+1; // include null terminator sendto(sockfd, (const char *)hello, size, MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr)); printf("Message %s sent.\n", hello); close(sockfd); return 0; } }}} == C code subscriber == '''subscriber.c''' {{{#!highlight c #include #include #include #include #include #include #include #include #define PORT 4242 #define MAXLINE 1024 int main() { int sockfd; char buffer[MAXLINE]; struct sockaddr_in servaddr, cliaddr; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&servaddr, 0, sizeof(servaddr)); memset(&cliaddr, 0, sizeof(cliaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = INADDR_ANY; servaddr.sin_port = htons(PORT); struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = inet_addr("225.3.2.1"); mreq.imr_interface.s_addr = INADDR_ANY; int enable=1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int) ); setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int) ); setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq) ); bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)); int len, bytes_received; while(1){ bytes_received = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, ( struct sockaddr *) &cliaddr, &len); printf("RX: %s\n", buffer); } return 0; } }}} == iptables == https://www.ibm.com/developerworks/community/blogs/fe313521-2e95-46f2-817d-44a4f27eba32/entry/configuring_iptables_for_ip_multicast1?lang=en Disabling iptables If your multicast client system doesn't need to be protected by a firewall the easiest way to make a multicast application to work is by disabling iptables or any other firewall service that might be running. That can be done temporarily by flushing all the iptables rules with the following command: # iptables -F /etc/sysconfig/iptables By adding the following line to this file, iptables will allow all incoming multicast packets: -A INPUT -m pkttype --pkt-type multicast -j ACCEPT == python subscriber == '''subscriber.py''' {{{#!highlight python import socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) mreq = socket.inet_aton('225.3.2.1') + socket.inet_aton('0.0.0.0') sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) sock.bind(('0.0.0.0', 4242)) while True: print sock.recvfrom(1024)[0] }}} == python publisher == '''publisher.py''' {{{#!highlight python import socket MULTICAST_GROUP="225.3.2.1" PORT=4242 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.sendto("robot\0", (MULTICAST_GROUP ,PORT )) }}}