Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present Ciena Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 16 | import threading |
| 17 | import sys |
| 18 | import os |
| 19 | import time |
| 20 | import monotonic |
| 21 | from scapy.all import * |
| 22 | |
| 23 | class McastTraffic(threading.Thread): |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 24 | DST_MAC_DEFAULT = '01:00:5e:00:01:01' |
| 25 | SRC_MAC_DEFAULT = '02:88:b4:e4:90:77' |
| 26 | SRC_IP_DEFAULT = '1.2.3.4' |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 27 | SEND_STATE = 1 |
| 28 | RECV_STATE = 2 |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 29 | |
| 30 | def __init__(self, addrs, iface = 'eth0', dst_mac = DST_MAC_DEFAULT, src_mac = SRC_MAC_DEFAULT, |
| 31 | src_ip = SRC_IP_DEFAULT, cb = None, arg = None): |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 32 | threading.Thread.__init__(self) |
| 33 | self.addrs = addrs |
| 34 | self.iface = iface |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 35 | self.dst_mac = dst_mac |
| 36 | self.src_mac = src_mac |
| 37 | self.src_ip = src_ip |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 38 | self.cb = cb |
| 39 | self.arg = arg |
| 40 | self.state = self.SEND_STATE | self.RECV_STATE |
| 41 | |
| 42 | def run(self): |
| 43 | eth = Ether(dst = self.dst_mac, src = self.src_mac) |
| 44 | while self.state & self.SEND_STATE: |
| 45 | for addr in self.addrs: |
| 46 | #data = repr(time.time()) |
| 47 | data = repr(monotonic.monotonic()) |
| 48 | ip = IP(dst = addr, src = self.src_ip) |
| 49 | sendp(eth/ip/data, iface = self.iface) |
| 50 | if self.cb: |
| 51 | self.cb(self.arg) |
| 52 | |
| 53 | def stop(self): |
| 54 | self.state = 0 |
| 55 | |
| 56 | def stopReceives(self): |
| 57 | self.state &= ~self.RECV_STATE |
| 58 | |
| 59 | def stopSends(self): |
| 60 | self.state &= ~self.SEND_STATE |
| 61 | |
| 62 | def isRecvStopped(self): |
| 63 | return False if self.state & self.RECV_STATE else True |
| 64 | |
| 65 | def isSendStopped(self): |
| 66 | return False if self.state & self.SEND_STATE else True |
| 67 | |