blob: c25cb1a57ccf31d302968371785b880c4ab1d8ed [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
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 Gaonkereb2b24b2016-03-01 14:04:45 -080016import threading
17import sys
18import os
19import time
20import monotonic
21from scapy.all import *
22
23class McastTraffic(threading.Thread):
Chetan Gaonker4b853002016-03-28 15:17:28 -070024 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 Gaonkereb2b24b2016-03-01 14:04:45 -080027 SEND_STATE = 1
28 RECV_STATE = 2
Chetan Gaonker4b853002016-03-28 15:17:28 -070029
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 Gaonkereb2b24b2016-03-01 14:04:45 -080032 threading.Thread.__init__(self)
33 self.addrs = addrs
34 self.iface = iface
Chetan Gaonker4b853002016-03-28 15:17:28 -070035 self.dst_mac = dst_mac
36 self.src_mac = src_mac
37 self.src_ip = src_ip
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080038 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