alshabib | 9c19c52 | 2017-01-11 15:24:48 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2017 the original author or authors. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | from igmpv3 import IGMPv3, IGMP_TYPE_V3_MEMBERSHIP_REPORT, IGMP_V3_GR_TYPE_EXCLUDE, IGMPv3gr, IGMP_V3_GR_TYPE_INCLUDE |
| 19 | from scapy.data import ETH_P_IP |
| 20 | from scapy.layers.inet import IP |
| 21 | from scapy.layers.l2 import Ether |
| 22 | from scapy.sendrecv import sendp |
| 23 | import argparse |
| 24 | |
| 25 | """ |
| 26 | Send an igmp join |
| 27 | """ |
| 28 | |
| 29 | IGMP_ETH = Ether(type=ETH_P_IP) |
| 30 | IGMP_IP = IP(dst='224.0.0.22') |
| 31 | |
| 32 | def parse_args(): |
| 33 | parser = argparse.ArgumentParser() |
| 34 | |
| 35 | _help = ('Emit a join message') |
| 36 | parser.add_argument('-j', '--join', |
| 37 | dest='join', |
| 38 | action='store_true', |
| 39 | default=False, |
| 40 | help=_help) |
| 41 | |
| 42 | _help = ('Emit a leave message') |
| 43 | parser.add_argument('-l', '--leave', |
| 44 | dest='join', |
| 45 | action='store_false', |
| 46 | default=False, |
| 47 | help=_help) |
| 48 | |
| 49 | _help = ('Group address to use') |
| 50 | parser.add_argument('-m', '--mcaddr', |
| 51 | dest='mcaddr', |
| 52 | action='store', |
| 53 | default='229.10.20.30', |
| 54 | help = _help) |
| 55 | |
| 56 | _help = ('Interface to use') |
| 57 | parser.add_argument('-i', '--iface', |
| 58 | dest='iface', |
| 59 | action='store', |
| 60 | default='eth0', |
| 61 | help = _help) |
| 62 | |
| 63 | return parser.parse_args() |
| 64 | |
| 65 | |
| 66 | def send(igmp): |
| 67 | ip_pkt = IGMP_ETH/IGMP_IP |
| 68 | pkt = ip_pkt/igmp |
| 69 | IGMPv3.fixup(pkt) |
| 70 | |
| 71 | sendp(pkt, iface=args.iface) |
| 72 | |
| 73 | def send_join(args): |
| 74 | igmp = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.22") |
| 75 | igmp.grps = [IGMPv3gr( rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=args.mcaddr)] |
| 76 | |
| 77 | send(igmp) |
| 78 | |
| 79 | |
| 80 | def send_leave(args): |
| 81 | |
alshabib | 5e493bb | 2017-01-11 18:21:19 -0600 | [diff] [blame] | 82 | igmp = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.22") |
alshabib | 9c19c52 | 2017-01-11 15:24:48 -0600 | [diff] [blame] | 83 | igmp.grps = [IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=args.mcaddr)] |
| 84 | |
| 85 | send(igmp) |
| 86 | |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | args = parse_args() |
| 90 | |
| 91 | if args.join: |
| 92 | send_join(args) |
| 93 | else: |
| 94 | send_leave(args) |