]> git.proxmox.com Git - mirror_frr.git/blame - tests/topotests/pim_basic/mcast-rx.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / pim_basic / mcast-rx.py
CommitLineData
f1f0bd09 1#!/usr/bin/env python3
acddc0ed 2# SPDX-License-Identifier: ISC
86439e8d
DS
3#
4# mcast-rx.py
5#
6# Copyright (c) 2018 Cumulus Networks, Inc.
7#
86439e8d
DS
8"""
9Subscribe to a multicast group so that the kernel sends an IGMP JOIN
10for the multicast group we subscribed to.
11"""
12
13import argparse
14import logging
15import re
16import os
17import socket
18import subprocess
19import struct
20import sys
21import time
22
23
24def ifname_to_ifindex(ifname):
701a0192 25 output = subprocess.check_output(
26 "ip link show %s" % ifname, shell=True, universal_newlines=True
27 )
787e7624 28 first_line = output.split("\n")[0]
29 re_index = re.search("^(\d+):", first_line)
86439e8d
DS
30
31 if re_index:
32 return int(re_index.group(1))
33
34 log.error("Could not parse the ifindex for %s out of\n%s" % (ifname, first_line))
35 return None
36
37
38# Thou shalt be root
39if os.geteuid() != 0:
787e7624 40 sys.stderr.write("ERROR: You must have root privileges\n")
86439e8d
DS
41 sys.exit(1)
42
43
787e7624 44logging.basicConfig(
45 level=logging.DEBUG, format="%(asctime)s %(levelname)5s: %(message)s"
46)
86439e8d
DS
47
48# Color the errors and warnings in red
787e7624 49logging.addLevelName(
50 logging.ERROR, "\033[91m %s\033[0m" % logging.getLevelName(logging.ERROR)
51)
52logging.addLevelName(
53 logging.WARNING, "\033[91m%s\033[0m" % logging.getLevelName(logging.WARNING)
54)
86439e8d
DS
55log = logging.getLogger(__name__)
56
f635350e
MS
57parser = argparse.ArgumentParser(description="Multicast RX utility")
58
787e7624 59parser.add_argument("group", help="Multicast IP")
60parser.add_argument("ifname", help="Interface name")
61parser.add_argument("--port", help="UDP port", default=1000)
62parser.add_argument("--sleep", help="Time to sleep before we stop waiting", default=5)
86439e8d
DS
63args = parser.parse_args()
64
65# Create the datagram socket
66sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
67sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
68sock.bind((args.group, args.port))
69
70newpid = os.fork()
71
72if newpid == 0:
73 ifindex = ifname_to_ifindex(args.ifname)
787e7624 74 mreq = struct.pack(
75 "=4sLL", socket.inet_aton(args.group), socket.INADDR_ANY, ifindex
76 )
86439e8d
DS
77 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
78 time.sleep(float(args.sleep))
79 sock.close()