-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.py
executable file
·79 lines (71 loc) · 2.23 KB
/
request.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
#!/usr/bin/env python
import argparse
import sys
import socket
import random
import struct
from scapy.all import sniff, sendp, srp1, send, get_if_list, get_if_hwaddr, bind_layers
from scapy.all import Packet, Raw
from scapy.all import Ether, IP, UDP
from scapy.fields import *
import readline
def get_if():
ifs=get_if_list()
iface=None
for i in get_if_list():
if "eth0" in i:
iface=i
break;
if not iface:
print "Cannot find eth0 interface"
exit(1)
return iface
def handle_pkt(pkt, iface):
print "got a packet"
pkt.show2()
sys.stdout.flush()
class AtomicCommit(Packet):
fields_desc = [ BitField("dest_port", 0, 9),
BitField("request_number", 0, 16),
BitField("request_type", 0, 1),
BitField("vote", 0, 1),
BitField("resp", 0, 2),
BitField("state", 0, 3),
BitField("key", 1, 8),]
bind_layers(Ether, AtomicCommit, type=0x1313)
def main():
iface = get_if()
addr = socket.gethostbyname("10.0.1.4")
typ = -1;
data = ""
if len(sys.argv) < 4:
print "Invalid request. Usage: %s <get|set> <id> <dest> <key> [data]" % sys.argv[0]
exit(1)
if sys.argv[1] == "get":
typ = 0
elif sys.argv[1] == "set":
if len(sys.argv) < 5:
print "Invalid request. Usage: %s set <id> <dest> <key> <data>" % sys.argv[0]
exit(1)
typ = 1
data = sys.argv[4]
else:
print "Invalid request: should start with 'get' or 'set'"
exit(1)
dest = int(sys.argv[2])
key = int(sys.argv[3])
print "sending on interface %s to %s" % (iface, str(addr))
port = addr.split(".")[3]
pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff');
pkt = pkt / AtomicCommit(dest_port=dest, request_type=typ, key=key,
state=(4 if sys.argv[1] == "get" else 0))
if sys.argv[1] == "set":
pkt = pkt / Raw(data)
pkt.show2()
if sys.argv[1] == "set":
sendp(pkt, iface=iface, verbose=False)
elif sys.argv[1] == "get":
response = srp1(pkt, iface=iface, timeout=2)
handle_pkt(response, iface)
if __name__ == '__main__':
main()