-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraft_definitions.py
201 lines (150 loc) · 5.84 KB
/
raft_definitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from scapy.all import *
from scapy.layers.inet import UDP, IP
import random
from scapy.layers.l2 import Ether
class Raft(Packet):
name = "RaftPacket "
fields_desc = [
XShortField("sourceID", 0x0), # defined as 'field(name, default_value)'
XShortField("destinationID", 0x0),
XShortField("logIndex", 0x0),
XShortField("currentTerm", 0x0),
XIntField("data", 0x0),
XByteField("committed", 0x0),
XByteField("messageType", 0x0),
XByteField("voted", 0x0)
]
# GLOBALS
STATUSES = {'follower': 0, 'candidate': 1, 'leader': 2}
NODES_IPS = ['10.0.1.1', '10.0.2.2', '10.0.3.3']
RANDOM_TIMEOUT = {'min': 1500, 'max': 2000} # min max values in ms
REQUEST_TIMEOUT = 200 # in ms
MAX_LOG_WAIT = 500
HEARTBEAT_TIME = 500
RECOVER_TIME = 1000 # time in which the leader sends periodically recover messages for the nodes that were down
raft_protocol_dstport = 0x9998 # raft protocol identifier on port 9998 see parser.p4
raft_protocol_vote_port = 0x9997 # 39319
raft_protocol_heartbeats_port = 0x9996 # 39318
raft_protocol_client_request_port = 0x9994
raft_protocol_ports_list = [
raft_protocol_vote_port,
raft_protocol_heartbeats_port,
raft_protocol_dstport
]
bind_layers(UDP, Raft, dport=raft_protocol_dstport)
bind_layers(UDP, Raft, dport=raft_protocol_heartbeats_port)
bind_layers(UDP, Raft, dport=raft_protocol_vote_port)
COMMANDS = {
'HeartBeatRequest': 0x1,
'AppendEntries': 0x2,
'HeartBeatResponse': 0x3,
'RequestVote': 0x4,
'ResponseVote': 0x5,
'CommitValue': 0x6,
'AppendEntriesReply': 0x7,
'RecoverEntries': 0x8
}
# END OF GLOBALS
def raft_packet(sourceID,
destinationID,
logIndex,
currentTerm,
data,
srcIP,
dstIP,
messageType,
committed=0x0,
voted=0x0):
"""helper method to craft a Raft packet"""
custom_mac = "08:00:27:10:a8:80" # don't care. The switch will replace it automatically
eth = Ether(dst=custom_mac)
ip = IP(src=srcIP, dst=dstIP)
udp = UDP(sport=raft_protocol_dstport, dport=raft_protocol_dstport)
_packet = eth / ip / udp / Raft(sourceID=sourceID,
destinationID=destinationID,
logIndex=logIndex,
currentTerm=currentTerm,
data=data,
committed=committed,
messageType=messageType,
voted=voted)
return _packet
def send_raft_vote_request(nodeIP, message):
message[UDP].dport = raft_protocol_vote_port
reply = None
_thread = AsyncSniffer(
filter="ip src host {}".format(nodeIP),
lfilter=is_raft_packet_vote_response,
count=1,
)
_thread.start()
sendp(message, iface=conf.iface, count=1, return_packets=False, verbose=False)
_thread.join()
if len(_thread.results) != 0:
reply = _thread.results.pop()
return reply
def send_raft_heartbeat(nodeIP, message):
message[UDP].dport = raft_protocol_heartbeats_port
reply = None
_thread = AsyncSniffer(
filter="ip src host {}".format(nodeIP),
lfilter=is_raft_packet_heartbeat_response,
count=1,
timeout=REQUEST_TIMEOUT // 1000
)
_thread.start()
sendp(message, iface=conf.iface, count=1, return_packets=False, verbose=False)
_thread.join()
if len(_thread.results) != 0:
reply = _thread.results.pop()
return reply
def send_raft_heartbeat_with_log(nodeIP, message):
message[UDP].dport = raft_protocol_heartbeats_port
reply = None
_thread = AsyncSniffer(
filter="ip src host {}".format(nodeIP),
lfilter=is_raft_packet_heartbeat_with_log_response,
count=1,
timeout=1 # in seconds. Achtung! this gave me quite the headache to find this bug
)
_thread.start()
time.sleep(0.1)
sendp(message, iface=conf.iface, count=1, return_packets=False, verbose=False)
_thread.join()
if len(_thread.results) != 0:
reply = _thread.results.pop()
return reply
def send_no_reply(message):
sendp(message, iface=conf.iface, count=1, return_packets=False, verbose=False)
def is_raft_packet_vote_response(_packet):
if _packet.haslayer(IP):
if not _packet[IP].proto == 'icmp':
if _packet.haslayer(UDP):
# if _packet[UDP].dport == raft_protocol_dstport:
if _packet[UDP].dport in raft_protocol_ports_list:
if _packet.haslayer(Raft):
if _packet[Raft].messageType == COMMANDS['ResponseVote']:
return True
return False
def is_raft_packet_heartbeat_response(_packet):
if _packet.haslayer(IP):
if not _packet[IP].proto == 'icmp':
if _packet.haslayer(UDP):
# if _packet[UDP].dport == raft_protocol_dstport:
if _packet[UDP].dport in raft_protocol_ports_list:
if _packet.haslayer(Raft):
if _packet[Raft].messageType == COMMANDS['HeartBeatResponse']:
return True
return False
def is_raft_packet_heartbeat_with_log_response(_packet):
if _packet.haslayer(IP):
if not _packet[IP].proto == 'icmp':
if _packet.haslayer(UDP):
if _packet[UDP].dport == raft_protocol_dstport:
# if _packet[UDP].dport == raft_protocol_log_replication_port:
if _packet.haslayer(Raft):
if _packet[Raft].messageType == COMMANDS['AppendEntriesReply']:
return True
return False
def raft_timeout():
return random.randrange(RANDOM_TIMEOUT['min'], RANDOM_TIMEOUT['max']) / 1000