-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
132 lines (113 loc) · 4.1 KB
/
send.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
import scapy.all as scapy
import sub_logic
import argparse
import sub_logic
import ifcfg
BROADCAST_IP = '10.0.0.255'
ASK_IP_MSG = 10
ASK_OPINION = 1
BASE_RATE = 0.5
ips = []
for name, interface in ifcfg.interfaces().items():
for i in interface['inet4']:
if(i != "127.0.0.1"):
ips.append(i)
my_ip = ips[0]
def send(ip, data):
packet = scapy.IP(dst=ip)/scapy.TCP()/str(data)
scapy.send(packet)
def ping(ip):
packet = scapy.IP(dst=ip)/scapy.ICMP()
s = scapy.sr1(packet, timeout=2, verbose=0)
g = sub_logic.Graph(my_ip)
if s is not None:
o = sub_logic.SubLog(False, {
"r": 1,
"s": 0,
"a": BASE_RATE
})
g.add_direct_nodes(ip, o)
else:
o = sub_logic.SubLog(False, {
"r": 0,
"s": 1,
"a": BASE_RATE
})
g.add_direct_nodes(ip, o)
path = "trust" + str(my_ip) + "/" + str(ip) + ".txt"
trust = g.compute_trust(ip)
with open (path, "a") as f:
f.write(str(trust))
f.write(",")
def send_opinion(ip, opinion, on):
raw_data = str(opinion.opinion_print_bdu()) + "-" + str(on)
packet = scapy.IP(dst=ip)/scapy.TCP()/raw_data
scapy.send(packet)
parser = argparse.ArgumentParser()
parser.add_argument('-dst', '--destination')
parser.add_argument('-i', '--init', action='store_true', help='You can send a broadcast message to obtain the IP adresses of your neighbours')
parser.add_argument('-o', '--opinion_ask', action='store_true', help='This sends a message to ask the opinion of somebody on an ip adresse')
parser.add_argument('-p', '--ping', action='store_true', help='This pings another node')
parser.add_argument('-m', '--message', help="You can send a message to another node using this flag")
parser.add_argument('-t', '--trust', nargs='+', type=float, help='You can send an opinion with this flag (format : -t value_a value_b value_d value_u)')
parser.add_argument('-on', '--opinion_on', help='When you send a trust value, you should precise the trust on who you are sending')
args = parser.parse_args()
#Handle the case the node want to asks for other nodes IP
if args.init:
send(BROADCAST_IP, ASK_IP_MSG)
exit()
#Pings another node
if args.ping:
if args.destination:
dst = args.destination
ping(dst)
exit()
else:
raise ValueError('You should presice with -dst to which node you want to ping')
#Send a string to another node
if args.message:
if args.destination:
dst = args.destination
message = args.message
send(dst, message)
exit()
else:
raise ValueError('You should presice with -dst to which node you want to send the message')
#Ask the opinion on a node to another node
if args.opinion_ask:
if args.destination:
dst = args.destination
if args.opinion_on:
on = args.opinion_on
send(dst, str(ASK_OPINION) + "-" + str(on))
exit()
else:
raise ValueError('You should presice with -on the opinion of which node you want')
else:
raise ValueError('You should presice with -dst to which node you want to ask his opinion')
#Send a trust value to a node
if args.trust:
a = args.trust[0]
b = args.trust[1]
d = args.trust[2]
u = args.trust[3]
if ((u + b + d) == 1):
if args.destination:
dst = args.destination
if args.opinion_on:
on = args.opinion_on
opinion = sub_logic.SubLog(True, {
"a": a,
"b": b,
"d": d,
"u": u
})
send_opinion(dst, opinion, on)
exit()
else:
raise ValueError('You should presice with -on the opinion of which node you are sending')
else:
raise ValueError('You should presice with -dst to which node you want to ask your opinion')
else:
raise ValueError('Sum of b,d and u should be equal to 1')
raise ValueError('Use a flag to determine the data you want to send : ')