- 44B1B3516F89AD8B7E7B9769A5C6089E22E9D459D012C1B72232926E0ABBDF42FBF74D2CD38F1344FE29143386C21432A61DC1DF454BF1CBA19ABDD8910C05CF+ 7A172DDFD99E3C419BDCA866441AD222790353CA0EDDCB452B6D607E1998041AC0D61E8841D23E82D7C13E850CF7FF01BBFCBA5D1924169795A100C4674BBCB5blatta/lib/server.py(1 . 4)(1 . 4)
317 VERSION = "9991"
318 VERSION = "9990"
319
320 import os
321 import select
(20 . 12)(20 . 15)
323 from lib.infosec import DUPLICATE_PACKET
324 from lib.infosec import MALFORMED_PACKET
325 from lib.infosec import INVALID_SIGNATURE
326 from lib.infosec import IGNORED
327 from lib.infosec import Infosec
328 from lib.peer import Peer
329 from lib.message import Message
330 from lib.ringbuffer import Ringbuffer
331 from funcs import *
332 from commands import BROADCAST
333 from commands import DIRECT
334 from commands import IGNORE
335 import imp
336 import pprint
337
(156 . 29)(159 . 28)
339 for peer in self.state.get_peers():
340 if peer.get_key() != None:
341 message = self.infosec.unpack(peer, data)
342 error_code = message["error_code"]
343 error_code = message.error_code
344 if(error_code == None):
345 self.print_debug("[%s] -> %s" % (peer.handles[0], message["body"]))
346 self.print_debug("[%s] -> %s" % (peer.handles[0], message.body))
347
348 # we only update the address table if the speaker is same as peer
349
350 try:
351 idx = peer.handles.index(message["speaker"])
352 idx = peer.handles.index(message.speaker)
353 except:
354 idx = None
355
356 if idx != None:
357 self.state.update_address_table({"handle": message["speaker"],
358 self.state.update_address_table({"handle": message.speaker,
359 "address": address[0],
360 "port": address[1]
361 })
362 # send the message to all clients
363 for c in self.clients:
364 # self.clients[c].udp_socket_readable_notification(message)
365 if (self.clients[c].is_addressed_to_me(message["body"])):
366 self.clients[c].message(message["body"])
367 if (self.clients[c].is_addressed_to_me(message.body)):
368 self.clients[c].message(message.body)
369 # send the message to all other peers if it should be propagated
370 if(message["command"] == BROADCAST) and message["bounces"] < MAX_BOUNCES:
371 if(message.command == BROADCAST) and message.bounces < MAX_BOUNCES:
372 self.rebroadcast(peer, message)
373 return
374 elif error_code == STALE_PACKET:
(190 . 34)(192 . 47)
376 elif error_code == MALFORMED_PACKET:
377 self.print_debug("[%s:%d] -> malformed packet: %s" % packet_info)
378 return
379 elif error_code == IGNORED:
380 self.print_debug("[%s:%d] -> ignoring packet: %s" % packet_info)
381 return
382 elif error_code == INVALID_SIGNATURE:
383 pass
384 self.print_debug("[%s:%d] -> martian packet: %s" % packet_info)
385
386 def peer_message(self, message):
387 message["original"] = True
388 if message["command"] == DIRECT:
389 peer = self.state.get_peer_by_handle(message["handle"])
390 message.original = True
391 if message.command == DIRECT:
392 peer = self.state.get_peer_by_handle(message.handle)
393 if peer and (peer.get_key() != None):
394 peer.send(message)
395 else:
396 self.print_debug("Discarding message to unknown handle or handle with no key: %s" % message["handle"])
397 self.print_debug("Discarding message to unknown handle or handle with no key: %s" % message.handle)
398 else:
399 for peer in self.state.get_peers():
400 if peer.get_key() != None:
401 peer.send(message)
402 else:
403 self.print_debug("Discarding message to handle with no key: %s" % message["handle"])
404 self.print_debug("Discarding message to handle with no key: %s" % message.handle)
405
406 def rebroadcast(self, source_peer, message):
407 message["original"] = False
408 message.original = False
409 for peer in self.state.get_peers():
410 if(peer.peer_id != source_peer.peer_id):
411 message["command"] = BROADCAST
412 message["bounces"] = message["bounces"] + 1
413 message.command = BROADCAST
414 message.bounces = message.bounces + 1
415 peer.send(message)
416
417
418 def sendrubbish(self):
419 for peer in self.state.get_peers():
420 for socket in self.clients:
421 self.peer_message(Message({
422 "speaker": self.clients[socket].nickname,
423 "command": IGNORE,
424 "bounces": 0,
425 "body": self.infosec.gen_rubbish_body()
426 }, self))
427
428 def start(self):
429 # Setup UDP first
430 self.udp_server_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
(254 . 7)(269 . 7)
432 [x.socket for x in self.clients.values()
433 if x.write_queue_size() > 0],
434 [],
435 1)
436 .2)
437 for x in inputready:
438 if x == self.udp_server_socket:
439 bytes_address_pair = self.udp_server_socket.recvfrom(PACKET_SIZE)
(275 . 7)(290 . 7)
441 for client in self.clients.values():
442 client.check_aliveness()
443 last_aliveness_check = now
444
445 self.sendrubbish() # Kludge to keep ephemeral port open when NATed
446
447 def create_directory(path):
448 if not os.path.isdir(path):