blob: 7ab79a7e0a41699ac6598889fe9506c208802a07 [file] [log] [blame]
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -08001import threading
2import sys
3import os
4import time
5import monotonic
6from scapy.all import *
7
8class McastTraffic(threading.Thread):
Chetan Gaonker4b853002016-03-28 15:17:28 -07009 DST_MAC_DEFAULT = '01:00:5e:00:01:01'
10 SRC_MAC_DEFAULT = '02:88:b4:e4:90:77'
11 SRC_IP_DEFAULT = '1.2.3.4'
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080012 SEND_STATE = 1
13 RECV_STATE = 2
Chetan Gaonker4b853002016-03-28 15:17:28 -070014
15 def __init__(self, addrs, iface = 'eth0', dst_mac = DST_MAC_DEFAULT, src_mac = SRC_MAC_DEFAULT,
16 src_ip = SRC_IP_DEFAULT, cb = None, arg = None):
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080017 threading.Thread.__init__(self)
18 self.addrs = addrs
19 self.iface = iface
Chetan Gaonker4b853002016-03-28 15:17:28 -070020 self.dst_mac = dst_mac
21 self.src_mac = src_mac
22 self.src_ip = src_ip
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080023 self.cb = cb
24 self.arg = arg
25 self.state = self.SEND_STATE | self.RECV_STATE
26
27 def run(self):
28 eth = Ether(dst = self.dst_mac, src = self.src_mac)
29 while self.state & self.SEND_STATE:
30 for addr in self.addrs:
31 #data = repr(time.time())
32 data = repr(monotonic.monotonic())
33 ip = IP(dst = addr, src = self.src_ip)
34 sendp(eth/ip/data, iface = self.iface)
35 if self.cb:
36 self.cb(self.arg)
37
38 def stop(self):
39 self.state = 0
40
41 def stopReceives(self):
42 self.state &= ~self.RECV_STATE
43
44 def stopSends(self):
45 self.state &= ~self.SEND_STATE
46
47 def isRecvStopped(self):
48 return False if self.state & self.RECV_STATE else True
49
50 def isSendStopped(self):
51 return False if self.state & self.SEND_STATE else True
52