def remove_authenticated_client(self, additional_data): addr = additional_data[0] # ICMP specific c = common.lookup_client_pub(self.clients, addr) if c: self.packetselector.delete_client(c) self.rlist.remove(c.get_pipe_r()) common.delete_client_stateless(self.clients, c) return
def communication(self, is_check): sequence = 0 identifier = 0 self.rlist = [self.comms_socket] if not self.serverorclient and self.tunnel: self.rlist = [self.tunnel, self.comms_socket] wlist = [] xlist = [] while not self._stop: try: readable, writable, exceptional = select.select( self.rlist, wlist, xlist, self.timeout) except select.error, e: print e break try: if not readable: if is_check: raise socket.timeout if not self.serverorclient: if self.authenticated: self.ICMP_sequence = (self.ICMP_sequence + 1) % 65536 self.do_dummy_packet(self.ICMP_identifier, self.ICMP_sequence) common.internal_print("DEBUG: Keep alive sent", 0, self.verbosity, common.DEBUG) continue for s in readable: if (s in self.rlist) and not (s is self.comms_socket): message = os.read(s, 4096) while True: if (len(message) < 4) or (message[0:1] != "\x45" ): #Only care about IPv4 break packetlen = struct.unpack( ">H", message[2:4])[0] # IP Total length if packetlen > len(message): message += os.read(s, 4096) readytogo = message[0:packetlen] message = message[packetlen:] if self.serverorclient: c = common.lookup_client_priv( readytogo, self.clients) if c: identifier = c.get_ICMP_sent_identifier() sequence = c.get_ICMP_sent_sequence() if (c.get_ICMP_received_sequence() - sequence) == 0: c.queue_put(readytogo) continue else: if c.queue_length(): c.queue_put(readytogo) i = 0 if (c.get_ICMP_received_sequence() - sequence) < ( c.queue_length()): number_to_get = ( c. get_ICMP_received_sequence( ) - sequence) else: number_to_get = c.queue_length( ) for i in range(number_to_get - 1): readytogo = c.queue_get() self.send( common.DATA_CHANNEL_BYTE, readytogo, ((socket.inet_ntoa( c.get_public_ip_addr( )), c.get_public_src_port()), identifier, (sequence + i + 1), 0)) #?? readytogo = c.queue_get() sequence += i sequence = (sequence + 1) % 65536 c.set_ICMP_sent_sequence(sequence) self.send(common.DATA_CHANNEL_BYTE, readytogo, ((socket.inet_ntoa( c.get_public_ip_addr()), c.get_public_src_port()), identifier, sequence, c.queue_length())) #?? else: common.internal_print( "Client not found, strange?!", 0, self.verbosity, common.DEBUG) continue else: if self.authenticated: self.ICMP_sequence = (self.ICMP_sequence + 1) % 65536 self.send(common.DATA_CHANNEL_BYTE, readytogo, (self.server_tuple, self.ICMP_identifier, self.ICMP_sequence, 0)) #?? sequence = self.ICMP_sequence # del this line identifier = self.ICMP_identifier # del this line else: common.internal_print( "Spoofed packets, strange?!", 0, self.verbosity, common.DEBUG) continue if s is self.comms_socket: message, addr, identifier, sequence, queue_length = self.recv( ) if len(message) == 0: continue c = None if self.serverorclient: self.authenticated = False c = common.lookup_client_pub(self.clients, addr) if c: c.set_ICMP_received_identifier(identifier) # packets does not arrive in order sometime # if higher sequence arrived already, then we # do not modify # 16bit integer MAX could be a bit tricky, a # threshold had to be introduced to make it # fail safe. Hacky but should work. ICMP_THRESHOLD = 100 if (sequence > c.get_ICMP_received_sequence() ) or ((sequence < ICMP_THRESHOLD) and ((sequence + 65536) > c.get_ICMP_received_sequence()) and (c.get_ICMP_received_sequence() > ICMP_THRESHOLD)): c.set_ICMP_received_sequence(sequence) else: if queue_length: common.internal_print( "sending {0} dummy packets".format( queue_length), 0, self.verbosity, common.DEBUG) for i in range(queue_length + 10): self.ICMP_sequence = (self.ICMP_sequence + 1) % 65536 self.do_dummy_packet( self.ICMP_identifier, self.ICMP_sequence) if message[0:len(common.CONTROL_CHANNEL_BYTE )] == common.CONTROL_CHANNEL_BYTE: if self.controlchannel.handle_control_messages( self, message[len(common.CONTROL_CHANNEL_BYTE):], (addr, identifier, sequence, 0)): continue else: self.stop() break if c: self.authenticated = c.get_authenticated() if self.authenticated: try: os.write( self.tunnel, message[len(common.CONTROL_CHANNEL_BYTE):]) except OSError as e: print e except (socket.error, OSError): raise if self.serverorclient: self.comms_socket.close() break except: print "another error" raise
def communication(self, is_check): self.rlist = [self.comms_socket] if not self.serverorclient and self.tunnel: self.rlist = [self.tunnel, self.comms_socket] wlist = [] xlist = [] while not self._stop: try: readable, writable, exceptional = select.select( self.rlist, wlist, xlist, self.timeout) except select.error, e: print e break if (not readable) and is_check: raise socket.timeout try: for s in readable: if (s in self.rlist) and not (s is self.comms_socket): message = os.read(s, 4096) while True: if (len(message) < 4) or (message[0:1] != "\x45" ): #Only care about IPv4 break packetlen = struct.unpack( ">H", message[2:4])[0] # IP Total length if packetlen > len(message): message += os.read(s, 4096) readytogo = message[0:packetlen] message = message[packetlen:] if self.serverorclient: c = common.lookup_client_priv( readytogo, self.clients) if c: self.send(common.DATA_CHANNEL_BYTE, readytogo, ((socket.inet_ntoa( c.get_public_ip_addr()), c.get_public_src_port()))) else: common.internal_print( "Client not found, strange?!", 0, self.verbosity, common.DEBUG) continue else: if self.authenticated: self.send(common.DATA_CHANNEL_BYTE, readytogo, (self.server_tuple)) common.internal_print( "UDP sent: {0}".format(len(readytogo)), 0, self.verbosity, common.DEBUG) if s is self.comms_socket: message, addr = self.recv() if len(message) == 0: continue c = None if self.serverorclient: self.authenticated = False c = common.lookup_client_pub(self.clients, addr) if message[0:len(common.CONTROL_CHANNEL_BYTE )] == common.CONTROL_CHANNEL_BYTE: if self.controlchannel.handle_control_messages( self, message[len(common.CONTROL_CHANNEL_BYTE):], (addr)): continue else: self.stop() break if c: self.authenticated = c.get_authenticated() if self.authenticated: try: os.write( self.tunnel, message[len(common.CONTROL_CHANNEL_BYTE):]) except OSError as e: print e except (socket.error, OSError): raise if self.serverorclient: self.comms_socket.close() break except: print "another error" raise