Multicast

Sat 22 February 2014

In tech.

Client

To test a multicast flow we need to register to the multicast adress et get the data. We can use tools like nemesis or emcast but they are not installed everywhere and they are a bit complex to use.

A solution is to use a simple python script to register and display or save the data. Just copy and past the following content to a new file named mcast.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python
##
## Multicast client
##

import sys, struct, string
from socket import *

def receiver(group, port, filename):
    s = openmcastsock(group, port)
    print("Socket open")


    if filename != "":
        fd = open(filename, 'wb')
    else:
        fd = sys.stdout

    while 1:
        data, sender = s.recvfrom(1500)
        fd.write(data)
        fd.flush()

def openmcastsock(group, port):
    s = socket(AF_INET, SOCK_DGRAM)
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    s.bind((group, port))

    group = gethostbyname(group)

    bytes = map(int, string.split(group, "."))
    grpaddr = 0
    for byte in bytes: grpaddr = (grpaddr << 8) | byte

    ifaddr = INADDR_ANY
    mreq = struct.pack('ll', htonl(grpaddr), htonl(ifaddr))

    s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
    return s

if __name__=="__main__":
    filename = ""
    if len(sys.argv) < 3:
        sys.exit("Usage : %s <group_addr> <port> [<file>]" % sys.argv[0])
    if len(sys.argv) > 3:
        filename = sys.argv[3]
    try:
        receiver(sys.argv[1], int(sys.argv[2]), filename)
    except ValueError:
        sys.exit("Error: Port must be a number.")

Add a route to your multicast IP (or the whole range) via the right interface

ip route add 224.0.0.0/4 dev eth0

And then start the script. It register to the multicast group and then display or save the data.

python mcast.py 230.10.0.6 2008
python mcast.py 230.10.0.6 2008 flow.mp3