-
Notifications
You must be signed in to change notification settings - Fork 0
/
anon_net.py
278 lines (235 loc) · 6.71 KB
/
anon_net.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Dissent: Accountable Group Anonymity
Copyright (C) 2010 Yale University
Released under the GNU General Public License version 3:
see the file COPYING for details.
Filename: anon_net.py
Description: Networking utility functions for the
anon protocol implementation.
Author: Henry Corrigan-Gibbs
"""
from __future__ import with_statement
import socket, cPickle, random, struct, tempfile, os
from time import sleep
from logging import debug, info, critical
import threading
class AnonNet:
"""
Maximum number of times a client will try to connect
to a server node.
"""
MAX_ATTEMPTS = 1000
LEN_FORMAT = "!Q"
"""
Big file functions
Files in the bulk protocol may be very large, so we send
them block by block.
"""
@staticmethod
def send_file_to_sock(sock, filename):
debug("Sending file of length: %d" % os.path.getsize(filename))
AnonNet.send_bytes(sock,
struct.pack(AnonNet.LEN_FORMAT,
os.path.getsize(filename)))
blocksize = 4096
with open(filename, 'r') as f:
while True:
bytes = f.read(blocksize)
if(bytes == ''): break
AnonNet.send_bytes(sock, bytes)
debug('Done sending file')
@staticmethod
def recv_file_from_sock(sock):
header_len = struct.calcsize(AnonNet.LEN_FORMAT)
header = AnonNet.recv_bytes(sock, header_len)
(left_to_read,) = struct.unpack(AnonNet.LEN_FORMAT, header)
blocksize = 4096
handle, filename = tempfile.mkstemp()
with open(filename, 'w') as f:
while left_to_read > 0:
try:
newdat = AnonNet.recv_bytes(
sock,
min(left_to_read, blocksize))
except KeyboardInterrupt, SystemExit:
sock.close()
except socket.error, (errno, errstr):
if errno == 35: continue
else: raise
# File is done
if len(newdat) == 0:
break
else:
left_to_read = left_to_read - len(newdat)
f.write(newdat)
return filename
@staticmethod
def recv_file_from_n(sockets):
return AnonNet.threaded_recv_from_n(
sockets,
AnonNet.recv_file_from_sock)
"""
Misc Network
"""
@staticmethod
def send_to_addr(ip, port, msg):
print "Trying to connect to %s:%d (msg len: %d)" % (ip,port, len(msg))
sock = AnonNet.new_client_sock(ip, port)
print "Connected to %s:%d" % (ip,port)
AnonNet.send_to_socket(sock, msg)
sock.close()
print "Closed socket to server"
@staticmethod
def send_to_socket(sock, msg):
""" Snippet inspired by http://www.amk.ca/python/howto/sockets/ """
debug("Sending message of length %d" % len(msg))
AnonNet.send_bytes(sock, struct.pack(AnonNet.LEN_FORMAT, len(msg)))
AnonNet.send_bytes(sock, msg)
@staticmethod
def recv_from_socket(sock):
header_len = struct.calcsize(AnonNet.LEN_FORMAT)
header = AnonNet.recv_bytes(sock, header_len)
(msg_len,) = struct.unpack(AnonNet.LEN_FORMAT, header)
return AnonNet.recv_bytes(sock, msg_len)
@staticmethod
def send_bytes(sock, bytes):
fd = sock.fileno()
totalsent = 0
while totalsent < len(bytes):
try:
sent = os.write(fd, bytes[totalsent:])
except KeyboardInterrupt, SystemExit:
sock.close()
raise
if sent == 0:
raise RuntimeError, "Socket broken"
totalsent = totalsent + sent
return
@staticmethod
def recv_bytes(sock, n_bytes):
fd = sock.fileno()
data = ""
blocksize = 4096
to_read = n_bytes
while to_read > 0:
try:
newdat = os.read(fd, min(blocksize, to_read))
except socket.error, (errno, errstr):
if errno == 35: continue
else: raise
except KeyboardInterrupt, SystemExit:
sock.close()
raise
if len(newdat) == 0:
raise RuntimeError, "Socket closed unexpectedly"
to_read = to_read - len(newdat)
data += newdat
return data
@staticmethod
def recv_once(my_ip, my_port):
print "Setting up server socket at %s:%d" % (my_ip, my_port)
sock_list = AnonNet.new_server_socket_set(my_ip, my_port, 1)
s = sock_list[0]
print "Set up server socket at %s:%d" % (my_ip, my_port)
d = AnonNet.recv_from_socket(s)
print "Closing socket"
s.close()
return d
@staticmethod
def recv_file_once(my_ip, my_port):
s = AnonNet.new_client_sock(my_ip, my_port)
f = AnonNet.recv_file_from_sock(s)
debug("Closing socket")
s.close()
return f
@staticmethod
def new_server_socket(ip, port, n_backlog):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(1)
sock.settimeout(None)
try:
sock.bind((ip, port))
except socket.error, (errno, errstr):
if errno == 48: critical('Leader cannot bind port')
raise
sock.listen(n_backlog)
debug("Socket listening at %s:%d" % (ip, port))
return sock
@staticmethod
def new_client_sock(ip, port):
debug("Trying to connect to (%s, %d)" % (ip, port))
for i in xrange(0, AnonNet.MAX_ATTEMPTS):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((ip, port))
debug(sock.getsockname())
break
except socket.error, (errno, errstr):
if errno == 61 or errno == 22 or errno == 111: # Server not awake yet
if i == AnonNet.MAX_ATTEMPTS - 1:
raise RuntimeError, "Cannot connect to server"
debug("Waiting for server %s:%d..." % (ip,port))
sleep(random.randint(5,10))
sock.close()
else: raise
except KeyboardInterrupt:
sock.close()
raise
return sock
@staticmethod
def recv_from_n(sockets):
return AnonNet.threaded_recv_from_n(
sockets,
AnonNet.recv_from_socket)
"""
Threaded Net functions
"""
@staticmethod
def broadcast_using(sockets, func, arg):
threads = []
""" Only leader can broadcast """
for i in xrange(0, len(sockets)):
t = threading.Thread(
target = func,
args = (sockets[i], arg))
t.start()
threads.append(t)
for t in threads:
t.join()
@staticmethod
def threaded_recv_from_n(sockets, sock_function):
""" Receive a message from N nodes """
debug('Setting up server socket')
""" Set up server connection """
data = [None] * len(sockets)
threads = []
for i in xrange(0, len(sockets)):
t = threading.Thread(
target = AnonNet.read_sock_into_array,
args = (data, i, sock_function, sockets[i]))
t.start()
threads.append(t)
for t in threads:
t.join()
debug("Got %d messages" % len(data))
return data
@staticmethod
def new_server_socket_set(server_ip, server_port, n_clients):
debug('Setting up server socket')
""" Set up server connection """
server_sock = AnonNet.new_server_socket(server_ip, server_port, n_clients)
socks = []
for i in xrange(0, n_clients):
debug("Listening...")
try:
(rem_sock, (rem_ip, rem_port)) = server_sock.accept()
socks.append(rem_sock)
except KeyboardInterrupt:
server_sock.close()
raise
server_sock.close()
debug('Got all messages, closing socket')
return socks
@staticmethod
def read_sock_into_array(data_array, index, sock_function, sock):
data_array[index] = sock_function(sock)