Chetan Gaonker | cb122cc | 2016-05-10 10:58:34 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2016-present Ciena Corporation |
| 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 | # |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 17 | import threading |
| 18 | import sys |
| 19 | import os |
| 20 | import time |
| 21 | import monotonic |
| 22 | from scapy.all import * |
| 23 | |
| 24 | class McastTraffic(threading.Thread): |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 25 | DST_MAC_DEFAULT = '01:00:5e:00:01:01' |
| 26 | SRC_MAC_DEFAULT = '02:88:b4:e4:90:77' |
| 27 | SRC_IP_DEFAULT = '1.2.3.4' |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 28 | SEND_STATE = 1 |
| 29 | RECV_STATE = 2 |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 30 | |
| 31 | def __init__(self, addrs, iface = 'eth0', dst_mac = DST_MAC_DEFAULT, src_mac = SRC_MAC_DEFAULT, |
| 32 | src_ip = SRC_IP_DEFAULT, cb = None, arg = None): |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 33 | threading.Thread.__init__(self) |
| 34 | self.addrs = addrs |
| 35 | self.iface = iface |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 36 | self.dst_mac = dst_mac |
| 37 | self.src_mac = src_mac |
| 38 | self.src_ip = src_ip |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 39 | self.cb = cb |
| 40 | self.arg = arg |
| 41 | self.state = self.SEND_STATE | self.RECV_STATE |
| 42 | |
| 43 | def run(self): |
| 44 | eth = Ether(dst = self.dst_mac, src = self.src_mac) |
| 45 | while self.state & self.SEND_STATE: |
| 46 | for addr in self.addrs: |
| 47 | #data = repr(time.time()) |
| 48 | data = repr(monotonic.monotonic()) |
| 49 | ip = IP(dst = addr, src = self.src_ip) |
| 50 | sendp(eth/ip/data, iface = self.iface) |
| 51 | if self.cb: |
| 52 | self.cb(self.arg) |
| 53 | |
| 54 | def stop(self): |
| 55 | self.state = 0 |
| 56 | |
| 57 | def stopReceives(self): |
| 58 | self.state &= ~self.RECV_STATE |
| 59 | |
| 60 | def stopSends(self): |
| 61 | self.state &= ~self.SEND_STATE |
| 62 | |
| 63 | def isRecvStopped(self): |
| 64 | return False if self.state & self.RECV_STATE else True |
| 65 | |
| 66 | def isSendStopped(self): |
| 67 | return False if self.state & self.SEND_STATE else True |
| 68 | |