]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/pim-basic/mcast-rx.py
Merge pull request #3899 from ton31337/fix/remove_private_as_with_local_as
[mirror_frr.git] / tests / topotests / pim-basic / mcast-rx.py
1 #!/usr/bin/env python
2 #
3 # mcast-rx.py
4 #
5 # Copyright (c) 2018 Cumulus Networks, Inc.
6 #
7 # Permission to use, copy, modify, and/or distribute this software
8 # for any purpose with or without fee is hereby granted, provided
9 # that the above copyright notice and this permission notice appear
10 # in all copies.
11 #
12 # THE SOFTWARE IS PROVIDED "AS IS" AND Cumulus Networks DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
16 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
19 # OF THIS SOFTWARE.
20 #
21 """
22 Subscribe to a multicast group so that the kernel sends an IGMP JOIN
23 for the multicast group we subscribed to.
24 """
25
26 import argparse
27 import logging
28 import re
29 import os
30 import socket
31 import subprocess
32 import struct
33 import sys
34 import time
35
36
37 def ifname_to_ifindex(ifname):
38 output = subprocess.check_output("ip link show %s" % ifname, shell=True)
39 first_line = output.split('\n')[0]
40 re_index = re.search('^(\d+):', first_line)
41
42 if re_index:
43 return int(re_index.group(1))
44
45 log.error("Could not parse the ifindex for %s out of\n%s" % (ifname, first_line))
46 return None
47
48
49 # Thou shalt be root
50 if os.geteuid() != 0:
51 sys.stderr.write('ERROR: You must have root privileges\n')
52 sys.exit(1)
53
54
55 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)5s: %(message)s')
56
57 # Color the errors and warnings in red
58 logging.addLevelName(logging.ERROR, "\033[91m %s\033[0m" % logging.getLevelName(logging.ERROR))
59 logging.addLevelName(logging.WARNING, "\033[91m%s\033[0m" % logging.getLevelName(logging.WARNING))
60 log = logging.getLogger(__name__)
61
62 parser = argparse.ArgumentParser(description='Multicast RX utility',
63 version='1.0.0')
64 parser.add_argument('group', help='Multicast IP')
65 parser.add_argument('ifname', help='Interface name')
66 parser.add_argument('--port', help='UDP port', default=1000)
67 parser.add_argument('--sleep', help='Time to sleep before we stop waiting',
68 default = 5)
69 args = parser.parse_args()
70
71 # Create the datagram socket
72 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
73 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
74 sock.bind((args.group, args.port))
75
76 newpid = os.fork()
77
78 if newpid == 0:
79 ifindex = ifname_to_ifindex(args.ifname)
80 mreq = struct.pack("=4sLL", socket.inet_aton(args.group), socket.INADDR_ANY, ifindex)
81 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
82 time.sleep(float(args.sleep))
83 sock.close()