def tcp_recieveItem(csock, caddr,dispatcher,keys): try: # if caddr[0]=='83.201.26.105': # print 'start-1' SizeStruct = struct.Struct( "!I" ) size_data = csock.recv(4) size, = struct.unpack("!I",size_data) result2 ='' #@Ali start=0 packetSize=5000 dieCounter=0 while len(result2)!=size: if (len(result2)+packetSize) < size: packet=csock.recv(packetSize) # recieve 100 KB result2=result2+packet start=start+len(packet) else: if dieCounter>=100: print 'error: unable to recieve all bytes from '+str(caddr) print 'error: bytes to send = '+str(size) print 'error: bytes recieved = '+str(len(result2)) csock.close() return packet=csock.recv(size-start) dieCounter+=1 result2=result2+packet start=start+len(packet) if len(result2)!=size: time.sleep(1) try: dec_data = crypto.decrypt( keys, result2 ) except: print 'Communication Manager: error in decrypt' print traceback.print_exc() return try: msg = pickle.loads( dec_data ) except: print 'TCP Thread Error: Cant load Pickle' print type(dec_data) print traceback.print_exc() return msg.type = "R" + msg.type[1:] try: msg.fromip = msg.myIP #@Ali except: msg.fromip = (caddr[0],'','') msg.isSSLPacket=False dispatcher.add( msg ) csock.close() except socket.error, e: # A socket error print 'Recieve Socket Error' print socket.error
def udp_serverthread( dispatcher, udpSSock ): udpSSock.bind( ("0.0.0.0", int(myPubaddr[2])) )#@Ali logging.info( "UDP server listening on port {0}...".format( myPubaddr[2] ) )#@Ali f = open(constants.nid_filename, 'r') nodeid = f.readline() f.close() keys = crypto.loadKeyPrivate(nodeid+"_N_") while True: try: data, addr = udpSSock.recvfrom( 30000 ) try: dec_data = crypto.decrypt( keys, data ) #esko: p2p message decryption except: print 'Communication Manager: error in p2p decrypt' print print traceback.print_exc() m = pickle.loads( dec_data ) m.isSSLPacket=False job = Job( "R_P2P_" + m.type, m )#@Ali addr contains the IP and the random port, so the sender attach the MYIP field with IP and its listening ports dispatcher.add( job ) logging.info( "Received datagram from {0}".format( addr ) ) except socket.error, e: # A socket error pass except IOError, e: if e.errno == errno.EPIPE: print "broken pipe"
def udpMsg_handler(self,item,id): try: #print "UDP/SSL handler" dec_data = b64decode(crypto.decrypt( self.keys, item )) m = pickle.loads( dec_data ) m.myIP=(id,str(self.hostSSLport),str(self.hostSSLport)) m.isSSLPacket=True m.isClient=self.isClient job = Job( "R_P2P_" + m.type, m ) #print m self.dispatcher.add( job ) except: print 'Error in upd packet pickle load'
def tcpMsg_handler(self,item,id): try: dec_data = b64decode(crypto.decrypt( self.keys, item )) except: print 'SSL TCP Handler: error in decrypt' print traceback.print_exc() return try: msg = pickle.loads( dec_data ) except: print 'SSL TCP Handler: Cant load Pickle' print traceback.print_exc() return msg.type = "R" + msg.type[1:] msg.fromip = (id,str(self.hostSSLport),str(self.hostSSLport)) msg.myIP=(id,str(self.hostSSLport),str(self.hostSSLport)) msg.isSSLPacket=True self.dispatcher.add( msg )