blob: 0cf1690776927044f0e4c74ab83e18ece10eaf5c [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002#
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 Gaonkereb2b24b2016-03-01 14:04:45 -080017import threading
18import sys
19import os
20import time
21import monotonic
22from scapy.all import *
23
24class McastTraffic(threading.Thread):
Chetan Gaonker4b853002016-03-28 15:17:28 -070025 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 Gaonkereb2b24b2016-03-01 14:04:45 -080028 SEND_STATE = 1
29 RECV_STATE = 2
Chetan Gaonker4b853002016-03-28 15:17:28 -070030
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 Gaonkereb2b24b2016-03-01 14:04:45 -080033 threading.Thread.__init__(self)
34 self.addrs = addrs
35 self.iface = iface
Chetan Gaonker4b853002016-03-28 15:17:28 -070036 self.dst_mac = dst_mac
37 self.src_mac = src_mac
38 self.src_ip = src_ip
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080039 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