def finish_connection(deserialize, address): """Finish the connection, this function is called when a FIN arrives. Sends a ACK and FIN packet and expects an ACK later on Once the ACK has arrived we create log and exit """ new_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) seq_num = deserialize.get_seq_num() ack = packet("ACK", 1, seq_num + 1) fin = packet("FIN", 1, seq_num + 1) serialize_ack = serialize_packet(ack) new_socket.sendto(serialize_ack, address) add_to_log(serialize_ack, "snd") time.sleep(2) serialize_fin = serialize_packet(fin) new_socket.sendto(serialize_fin, address) add_to_log(serialize_fin, "snd") while True: data, address = receiver_socket.recvfrom(4000) add_to_log(data, "rcv") deserialize = deserialize_packet(data) convert_buffer_to_file() break create_log() print("FINISHED TEARDOWN") sys.exit(0)
def process_packet(pkt): """Return the response packet based on sender packet (pkt). Return nothing if packet is corrupt or if already in buffer Otherwise we find the appropriate ack num and return a packet """ global expected_seq_num global CONNECTION_STATE global receiver_buffer seq_num = pkt.get_seq_num() pkt_type = pkt.get_packet_type() if pkt_type == "SYN": new_packet = packet("SYNACK", 0, seq_num + 1) return new_packet elif pkt_type == "ACK": print("HANDSHAKE COMPLETE") CONNECTION_STATE = "OPEN" expected_seq_num = 1 return None # all DATA packets go here else: if (pkt.get_corrupt() == 1): overall_logger.increment_field("bit_errors") return None elif (pkt.seq_num in receiver_buffer.keys()): return None else: add_to_buffer(pkt) # print("ADDED TO BUFFER", receiver_buffer.keys()) update_expected_seq_num(pkt) # print ("CUMULATIIVE ACK =", expected_seq_num) new_packet = packet("ACK", 1, expected_seq_num) return new_packet
def communicate(data_sock, ack_sock): exp_seq_num = 0 while True: # data arrived udp_array, addr = data_sock.recvfrom(512) # parse bytes array into packet udp_packet = MyPacket.packet.parse_udp_data(udp_array) packet_type, seq_num, data = udp_packet.type, udp_packet.seq_num, udp_packet.data if packet_type == 1: log_seq_num(seq_num) # sequence number matches if exp_seq_num == seq_num: record_data(data) # send an ACK back with the same sequence number ack_packet = MyPacket.packet(0, seq_num, '').get_udp_data() ack_sock.sendmsg([ack_packet], [], 0, (EMULATOR_NAME, ACK_PORT)) # increment and modulo expected sequence number exp_seq_num = (exp_seq_num + 1) % SEQ_NUM_MODULO else: # send an ACK back with the sequence number equal to most recently received in-order packet ack_packet = MyPacket.packet(0, exp_seq_num - 1, '').get_udp_data() ack_sock.sendmsg([ack_packet], [], 0, (EMULATOR_NAME, ACK_PORT)) # receive EOT, then send back EOT then exit if packet_type == 2: eot_packet = MyPacket.packet(2, exp_seq_num, '').get_udp_data() ack_sock.sendmsg([eot_packet], [], 0, (EMULATOR_NAME, ACK_PORT)) return
def packet_iterator(): p = packet.packet() p2 = packet.packet() return p.packet_id == p2.packet_id - 1
def run(self): self.connected = True print "Connection %d started" % self.connection_id constant = constants() try: recv_data = self.client.recv(self.buffer_size) try: recv_packet = packet(recv_data) except packet_exception as e: self.master.debug("[%d] Invalid Packet, closing connection" % self.connection_id) self.connected = False print "packet type is ",recv_packet.packet_type if recv_packet.packet_type == constant.JOIN: doc = recv_packet.doc_name print doc, "this is the doc... 1" print doc_index; if doc in doc_index.keys(): send_packet = packet() send_packet.packet_type = constant.Dummy send_packet.list_of_ip = doc_index[doc] self.__send(send_packet) doc_index[doc].append([recv_packet.my_host_name,recv_packet.my_port_name]); else: doc = recv_packet.doc_name print doc, "this is the doc... 2" print doc_index; print "i am in else...",recv_packet.my_host_name,recv_packet.my_port_name print "doc_index",doc_index doc_index[doc] = list([[recv_packet.my_host_name,recv_packet.my_port_name]]); print "doc_index",doc_index send_packet = packet() send_packet.packet_type = constant.NewFile self.__send(send_packet) print "client want to join " + doc except socket.timeout: #timeout hit print "[%d] Client timed out, terminating thread" % self.connection_id self.connected = False except socket.error as e: self.master.debug("socket.error (%d, %s)" % (e.errno, e.strerror)) if e.errno == 10054: print "Connection closed by the client..." self.connected = False #little nap for the cpu time.sleep(0.3) self.terminate()
def right_write_action(self): if self.connection_thread is not None: #WORKSPACE-03 self.append_log("Asking server for right to write.") q_packet = packet() q_packet.packet_type = packet.Right self.connection_thread.send_packet(q_packet)
def teardown_connection(): """Initiate the closing of the connection.""" global last_ack_received global CONNECTION_STATE global log pkt = packet("FIN", last_ack_received, 1) serialize = serialize_packet(pkt) sender_socket.sendto(serialize, hostport) add_to_log(serialize, "snd") while True: try: received, server = sender_socket.recvfrom(4096) add_to_log(received, "rcv") except: print("teardown failed") if received: rcv = deserialize_packet(received) if rcv.get_packet_type() == "FIN": send_ack(rcv.get_ack_num(), 2) print('TEARDOWN COMPLETE') create_log() exit(0) break
def magPacket(): p = packet.packet() p.addField( "address" , 2 , packet.UINT , BROADCAST_ADDR ) p.addField( "amType" , 1 , packet.UINT , MAG_AM_TYPE ) p.addField( "groupID" , 1 , packet.UINT , GROUP_ID ) p.addField( "length" , 1 , packet.UINT , MAG_PAYLOAD_LENGTH ) p.addField( "magSum" , 4 , packet.UINT , 1 ) p.addField( "magXSum88" , 4 , packet.INT ) p.addField( "magYSum88" , 4 , packet.INT ) p.addField( "magNumReporting" , 1 , packet.UINT , 1 ) p.addField( "hopsLeft" , 1 , packet.UINT , 0 ) p.addField( "originAddress" , 2 , packet.UINT ) p.addField( "originSequence" , 1 , packet.UINT ) p.addField( "protocol" , 1 , packet.UINT ) p.addField( "zeroPadding0" , 4 , packet.UINT , 0 ) p.addField( "zeroPadding1" , 4 , packet.UINT , 0 ) p.addField( "zeroPadding2" , 3 , packet.UINT , 0 ) p.addField( "crc" , 2 , packet.UINT , 1 ) return p
def __workspace_update(self, diff): for c in self.master.threads: if not c.connection_id == self.connection_id: p = packet() p.packet_type = packet.WorkspaceUpdate p = diff.fill_packet(p) c.queued_packets.append(p)
def send_ack(seq_num, ack_num): """Send an ACK packet.""" pkt = packet("ACK", seq_num, ack_num) sender_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) serialize = serialize_packet(pkt) sender_socket.sendto(serialize, hostport) add_to_log(serialize, "snd")
def encapsulate_data(data, dictionary, count): """Create a packet and put payload in then add packet to dictionary.""" seq_num = count * MSS + 1 ack_num = 1 # always 1? pkt = packet("DATA", seq_num, ack_num) pkt.add_payload(data) dictionary[seq_num] = pkt
def __write_right_update(self): for c in self.master.threads: if not c.connection_id == self.connection_id: p = packet() p.packet_type = packet.WriteUpdate p.put_field('id', self.user_id) c.queued_packets.append(p)
def send_message(self, message, excludes=[]): for c in self.threads: if c.connection_id not in excludes: p = packet.packet() p.packet_type = packet.packet.Message p.put_field("message", message) c.queued_packets.append(p)
def builder(): print(interface.print_stuff.banner()) number_to_send = 0 t = packet.packet() meth = input("GET or POST: ") t.methoud = meth print(interface.print_stuff.packet_builder()) print("pick useragent") time.sleep(3) ua = useragent.agent() ua.options() ua.pick_option() ua.agentviewer() ua.pick_agent() t.user_agent = ua.agent por = input("port (default 443): ") t.port = por ur = input('url (if port 443 add https:// ): ') t.url = ur ti = input('Timeout: ') t.timeout = int(ti) da = input('Data if POST: ') t.data = "" t.build()
def client_accept(client_handle_socket, app_name): try: while True: private_socket = None (client_socket, client_address) = client_handle_socket.accept() logging.info(str(client_address) + ' Connected') logging.debug('accept client: ' + str(client_address)) c_address = json.loads(json.dumps(client_address)) client_address_element = {'client_address': c_address, 'client_socket': client_socket} CLIENT_ADDRESS_TABLE.append(client_address_element) # private_socket_element = {'app_name': app_name, 'private_socket': private_socket} # PRIVATE_SOCKET_TABLE.append(private_socket_element) for element in PRIVATE_SOCKET_TABLE: logging.debug(element) if app_name == element['app_name']: private_socket = element['private_socket'] build_connect_packet = json.dumps(packet.packet(op_enum.OP_BUILD_CONNECTION, op_enum.DES_BUILD_CONNECTION, '', app_name, client_address)) if private_socket != None: core_transmit.send_operation(private_socket, build_connect_packet) client_to_private_thread = Thread(target=client_to_private, args=(client_socket, client_address, private_socket, app_name), daemon=False, name=app_name + str(client_address)) client_to_private_thread.start() except Exception as err: logging.debug(err) client_handle_socket.shutdown(socket.SHUT_RDWR) client_handle_socket.close()
def sendData(self, data): """ Send data over the enlace interface """ construtor = packet.packet() max_pkt = self.max_pkt qtdpartes = math.ceil(len(data)/max_pkt) atual = 1 x = 0 y = max_pkt while atual <= qtdpartes: data_cortada = data[x:y] pacote = construtor.buildPacket(len(data_cortada), data_cortada, 3, atual, qtdpartes, 0, 0) #constroi pacote falso crc_head = self.CRC(pacote[0:5]) #calcula crc pro pacote falso crc_payload = self.CRC(data_cortada) pacote_final = construtor.buildPacket(len(data_cortada), data_cortada, 3, atual, qtdpartes, crc_head, crc_payload) time.sleep(0.05) self.tx.sendBuffer(pacote_final) #envia pacote verdadeiro tmp= self.getData(0.5) if tmp[2] == 1: #10 é valor do timeout print("Recebi o Ack, vou enviar o proximo pacote") atual += 1 x += max_pkt y += max_pkt print("Checagem pacote: "+str(len(pacote_final))) if tmp[2] == 2: print("Recebi um nAck, vou reenviar o ultimo pacote")
def cRoutePacket(): p = packet.packet() p.addField( "address" , 2 , packet.UINT , BROADCAST_ADDR ) p.addField( "amType" , 1 , packet.UINT , C_ROUTE_AM_TYPE ) p.addField( "groupID" , 1 , packet.UINT , GROUP_ID ) p.addField( "length" , 1 , packet.UINT , C_ROUTE_PAYLOAD_LENGTH ) p.addField( "type" , 1 , packet.UINT , C_ROUTE_TYPE ) p.addField( "dest" , 1 , packet.UINT ) p.addField( "crumb" , 2 , packet.UINT ) p.addField( "len" , 1 , packet.UINT , C_ROUTE_LEN ) p.addField( "magSum" , 4 , packet.UINT , 1 ) p.addField( "magXSum88" , 4 , packet.INT ) p.addField( "magYSum88" , 4 , packet.INT ) p.addField( "magNumReporting" , 1 , packet.UINT , 1 ) p.addField( "originAddress" , 2 , packet.UINT ) p.addField( "originSequence" , 1 , packet.UINT ) p.addField( "protocol" , 1 , packet.UINT , C_ROUTE_PROTOCOL ) p.addField( "zeroPadding0" , 4 , packet.UINT , 0 ) p.addField( "zeroPadding1" , 3 , packet.UINT , 0 ) p.addField( "crc" , 2 , packet.UINT , 1 ) return p
def private_to_public(private_app_socket, client_address, app_name): try: public_socket = None for element in PUBLIC_SOCKET_TABLE: if app_name == element['app_name']: public_socket = element['public_socket'] while True: msg = core_transmit.get_data(private_app_socket) if msg == '': logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') logging.debug( 'DETECT \'\'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') break # After receive data data_packet = packet.packet(op_enum.OP_TRANSMIT_DATA, op_enum.DES_TRANSMIT_DATA, msg, app_name, client_address) data_packet_json = json.dumps(data_packet) logging.debug('generate: ' + str(data_packet_json)) core_transmit.send_operation(public_socket, data_packet_json) except Exception as err: logging.debug(err) finally: private_app_socket.shutdown(socket.SHUT_RDWR) private_app_socket.close() disconn_packet = packet.packet(op_enum.OP_DISCONNECTED, op_enum.DES_DISCONNECTED, '', app_name, client_address) disconn_json = json.dumps(disconn_packet) logging.debug('generate: ' + str(disconn_json)) core_transmit.send_operation(public_socket, disconn_json) for element in PRIVATE_SOCKET_TABLE: if client_address == element['client_address']: PRIVATE_SOCKET_TABLE.remove(element)
def register_app(app_name=None, app_address=None, app_port=None, public_server_port=None): if app_name == None or app_address == None or app_port == None or public_server_port == None: return 'url must be \"/register_app/<app_name>/<app_address>/<app_port>/<public_server_port>\"' for app in REGISTERED_APPS: if app_name == app['app_name']: return 'This app name has been registered.' if app_address == app['app_address'] and app_port == app['app_port']: return 'This port has been registered.' register_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) register_socket.connect((PUBLIC_SERVER_ADDRESS, OP_PORT)) register_operation = packet.packet(op_enum.OP_REGISTER_APP, op_enum.DES_REGISTER_APP, str(public_server_port), app_name, None) core_transmit.send_operation(register_socket, json.dumps(register_operation)) public_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) public_socket.connect((PUBLIC_SERVER_ADDRESS, PROXY_PORT)) public_socket_element = { 'public_socket': public_socket, 'app_name': app_name } PUBLIC_SOCKET_TABLE.append(public_socket_element) app_get_op_thread = Thread(target=get_operation, args=(public_socket, ), daemon=False, name='get_operation:' + str(app_name)) app_get_op_thread.start() register_app = {} register_app['app_name'] = app_name register_app['app_address'] = app_address register_app['app_port'] = app_port register_app['public_port'] = public_server_port register_app['public_socket'] = public_socket waiting_register_app = { 'register_app_name': register_app['app_name'], 'status': None, 'msg': '' } REGISTER_IN_WAIT.append(waiting_register_app) waitcount = 200 while waiting_register_app['status'] == None: time.sleep(0.1) waitcount -= 1 if waitcount < 0: waiting_register_app['status'] = False waiting_register_app['msg'] = 'time out' # logging.debug('waiting for register') logging.debug(waiting_register_app) if waiting_register_app['status'] == True: REGISTER_IN_WAIT.remove(waiting_register_app) elif waiting_register_app['status'] == False: REGISTER_IN_WAIT.remove(waiting_register_app) return 'Register failed because of \"' + waiting_register_app[ 'msg'] + '\"' REGISTERED_APPS.append(register_app) return 'Register success'
def receive_message(name, ID): "Send message procedure (default)" global thread_count print("Waiting for client (Thread %s)" % ID) data, addr = sock.recvfrom(1024) ack_sock = socket(AF_INET, SOCK_DGRAM) ack_sock.connect((addr[0], addr[1])) port_used = port_list[0] port_list.remove(port_used) ack_sock.send(str(port_used).encode()) server_sock = socket(AF_INET, SOCK_DGRAM) server_sock.bind((UDP_IP, port_used)) filename = data.decode() with open('received_file/' + filename, 'wb') as f: print('receiving data packet... (Thread %s)' % ID) while True: byte_packet, addr = server_sock.recvfrom(MAX_PACKET_LENGTH) data_packet = packet.decode_packet(byte_packet) type = data_packet.get_type() data = data_packet.get_data() checksum = get_checksum(data) ori_checksum = data_packet.get_checksum() if (checksum == ori_checksum): if (type == FIN): print("File received successfully (Thread %s)" % ID) ack = packet.packet(FIN_ACK, data_packet.get_id(), data_packet.get_seq_number(), ori_checksum) ack_sock.send(ack.get_packet_byte()) f.write(data) break else: ack = packet.packet(ACK, data_packet.get_id(), data_packet.get_seq_number(), ori_checksum) ack_sock.send(ack.get_packet_byte()) f.write(data) port_list.append(port_used) id_list.append(ID) thread_count -= 1
def chars_parentheses(): p = packet.packet() p.fields['var1'] = 'test var1 )' p.fields['var2'] = "This data (should,) be included !" txt = p.to_string() p2 = packet.packet(txt) txt2 = p2.to_string() print "Packet 1 :" print "\t%s" % txt print "Packet 2 :" print "\t%s" % txt2 return txt == txt2
def __init__(self, path, location): self.location = location self.path = path if self.location.split("/")[-1] == 'download_source': self.path, self.location = self.location, self.path self.repo = svn.SVN(self.location) self.repo_structure = packet.packet(self.path, self.path, ignore=[".svn"]) logger.info("path - {p}, location - {l}".format(p=self.path,l=self.location))
def move_pincosecond(self): #print(self.count) self.packets.insert(0, packet(self.count)) self.move_packets() self.catch_packets() self.remove_caught_packets() self.count += 1 self.move_layers_scanners() return self.check_for_not_caught()
def close(self): if self.connected: send_packet = packet() send_packet.packet_type = packet.Closing self.__send(send_packet) self.connected = False self.__disconnected()
def receive(self): while True: try: (data, addr) = self.socket.recvfrom(4096) except: break try: self.processdata(packet(data, addr)) except: debug.exc(self)
def receive(self): while True: try: (data, addr) = self.socket.recvfrom(4096) except: break try: self.processdata(packet(data, addr)) except: debug.exc(self)
def get_workspace_packet(self): """ Generating a packet with all the text data :return: Forged packet """ p = packet() p.packet_type = packet.Workspace p.fields['length'] = str(len(self.__data)) p.fields['content'] = self.__data return p
def handshake(): """Handshake Function. Creates an initial SYN packet and sends it Establishes connection when complete """ global CONNECTION_STATE initial_pkt = packet("SYN", 0, 0) send_syn(initial_pkt) CONNECTION_STATE = "ESTABLISHED" print("HANDSHAKE COMPLETE")
def initializeMessage(self, event_queue, links, time): # Initialize the message message = packet(1, self.dest, self.src, 'message', 64, self.dest) # Append the flow destination to the route since that is where it starts message.route.append(self.dest.id) # Choose the start time as the inputted time message.start_time = time # Find the last link last_link = self.findLastLink(links) # Add the data to buffer for the last_link last_link.addToBuffer(event_queue, time, message, self)
def initializePackets(self, data_packet_size): #get the number of packets needed based on the data amount packet_amount = (self.amt * 1000000) / data_packet_size #fill the packets of the flow, setting the current route as just the source self.packets = [0] * packet_amount for i in range(packet_amount): self.packets[i] = packet(i + 1, self.src, self.dest, 'packet', data_packet_size, self.src) self.packets[i].route.append(self.src.id)
def chars_escape_comma(): p = packet.packet() p.fields['var1'] = "hello" p.fields['var2'] = 'josh,allo,yes' txt = p.to_string() p2 = packet.packet(txt) txt2 = p2.to_string() print "Packet 1 :" print "\t%s" % txt print "-----------" #print_packet(p) print "Packet 2 :" print "\t%s" % txt2 print "-----------" #print_packet(p2) return txt == txt2
def __init__(self, name): """ Initializes the enlace class """ self.fisica = fisica(name) self.rx = RX(self.fisica) self.tx = TX(self.fisica) self.connected = False construtor = packet.packet() nada = bytearray([]) self.SYN = construtor.buildPacket(0, nada, 0, 0, 0, 0, 0) self.ACK = construtor.buildPacket(0, nada, 1, 0, 0, 0, 0) self.nACK = construtor.buildPacket(0, nada, 2, 0, 0, 0, 0)
def register_app(app_name=None, bind_port=None): app_name_failed_op = None if app_name in BIND_APP: app_name_failed_op = json.dumps(packet.packet(op_enum.OP_REGISTER_FAILED, op_enum.DES_REGISTER_FAILED, 'APP \"' + app_name + '\" has already exist', app_name, None)) if app_name_failed_op == None: BIND_APP[app_name] = bind_port try: (private_socket, private_address) = PROXY_SOCKET.accept() logging.debug('accept address: ' + str(private_address)) private_socket_element = {'app_name': app_name, 'private_socket': private_socket} PRIVATE_SOCKET_TABLE.append(private_socket_element) get_op_thread = Thread(target=get_operation, args=(private_socket,), daemon=False, name='get_operation: ' + app_name) get_op_thread.start() if app_name_failed_op != None: core_transmit.send_operation(private_socket, app_name_failed_op) private_socket.shutdown(socket.SHUT_RDWR) private_socket.close() # If register failed, do not start client_handle_socket client_handle_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_handle_socket.bind((PROXY_ADDRESS, bind_port)) client_handle_socket.listen(20) client_handle_element = {'app_name': app_name, 'client_handle_socket': client_handle_socket} CLIENT_HANDLE_TABLE.append(client_handle_element) logging.debug(BIND_APP) logging.debug(PRIVATE_SOCKET_TABLE) client_accept_thread = Thread(target=client_accept, args=(client_handle_socket, app_name, ), daemon=False, name='client_accept_thread: ' + app_name) client_accept_thread.start() success_op = json.dumps(packet.packet(op_enum.OP_REGISTER_SUCCESS, op_enum.DES_REGISTER_SUCCESS, 'APP \"' + app_name + '\" created', app_name, None)) core_transmit.send_operation(private_socket, success_op) except Exception as err: failed_op = json.dumps(packet.packet(op_enum.OP_REGISTER_FAILED, op_enum.DES_REGISTER_FAILED, str(err), app_name, None)) core_transmit.send_operation(private_socket, failed_op) del BIND_APP[app_name] private_socket.shutdown(socket.SHUT_RDWR) private_socket.close() client_handle_socket.close()
def client_to_private(c_sock, c_address, pri_sock, app_name): try: while True: msg = core_transmit.get_data(c_sock) if msg == '': break # After receive data data_packet = packet.packet(op_enum.OP_TRANSMIT_DATA, op_enum.DES_TRANSMIT_DATA, msg, app_name, c_address) data_packet_json = json.dumps(data_packet) logging.debug('generate: ' + str(data_packet_json)) core_transmit.send_operation(pri_sock, data_packet_json) except Exception as err: logging.debug(err) finally: logging.info('Client disconnected') c_sock.shutdown(socket.SHUT_RDWR) c_sock.close() disconn_packet = packet.packet(op_enum.OP_DISCONNECTED, op_enum.DES_DISCONNECTED, '', app_name, c_address) disconn_json = json.dumps(disconn_packet) logging.debug('generate: ' + str(disconn_json)) core_transmit.send_operation(pri_sock, disconn_json) for element in CLIENT_ADDRESS_TABLE: if c_address == element['client_address']: CLIENT_ADDRESS_TABLE.remove(element)
def unregister_app(app_name=None): if app_name == None: return 'url must be \"unregister_app/<app_name>\"' for app in REGISTERED_APPS: if app_name == app['app_name']: public_socket = app['public_socket'] public_socket_address = public_socket.getsockname() unregister_op = json.dumps( packet.packet(op_enum.OP_UNREGISTER_APP, op_enum.DES_UNREGISTER_APP, str(public_socket_address), app_name, None)) core_transmit.send_operation(public_socket, unregister_op) REGISTERED_APPS.remove(app) return 'Unregister success' return 'There is no app named \"' + app_name + '\"'
def next_inline(self): if self.master.access_write is None: print "next inline process" self.master.access_write = self.connection_id send_packet = packet() send_packet.packet_type = packet.Right access = True send_packet.put_field("can_write", str(access)) send_packet.put_field("is_waiting", False) self.queued_packets.append(send_packet) print "Next inline packet: " + send_packet.to_string() self.__write_right_update() else: print "handle connection, next inline case not implemented "
def run(self): """The generator function used in simulations. """ yield self.env.timeout(self.start_time) while self.env.now < self.stop_time: self.packets_sent += 1 pkt = packet(time=self.env.now, size=self.sdist(), id=self.packets_sent, src=self.id,\ flow_id=self.flow_id, dst=self.dest_id, priority=self.priority, type=self.type) pkt.data = self.data self.S_1.put(pkt) # wait for next transmission yield self.env.timeout(self.adist())
def do_send(self, line): """Send data to the serial host, commands are: """ packet_to_send = packet(line) try: self.ser.open() self.ser.write(packet_to_send.get_packet()) response = self.ser.read(2) #ack should be 2 bytes if check_response(response): self.er.close() else: self.er.close() raise Exception("ACK not reveived") except serial.SerialException as e: sys.stderr.write('Could not open serial port {}: {}\n'.format( self.ser.name, e)) return
def newUser(socket): """newUser will handle the creation of new accounts in the database. """ #ask user for input regarding username and password, hash the password, generate public and private keys, then create # a packet object, pickle it (serialize it) and send it to the server #Generate the private key privateKey = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()) #extract the public key from the private key object publicKey = privateKey.public_key() #serialize and store the private key pem = privateKey.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) with open('private_key.pem', 'wb') as f: f.write(pem) #serialize and store the public key pem = publicKey.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo) with open('public_key.pem', 'wb') as f: f.write(pem) #Generate a salt of 64 bytes for the password from the os's crypto-random store salt = os.urandom(64) #**TEMP PASSWORD** password = b'badpassword' #hash the password using the user's input and the generated salt hashBrown = hashlib.pbkdf2_hmac('sha256', password, salt, 100000) testPacket = packet() testPacket.prepNewUser("justin", salt, hashBrown, pem) print("User sent to server.") testPacket.command = 1 data = pickle.dumps(testPacket) print(data) socket.send(data)
def sendOperationToAllPeers(self): constant = constants() while 1: if self.set_of_operation == []: time.sleep(1) else: peers = self.peers operation = self.set_of_operation self.set_of_operation = [] print "===================SENDING OPERATIONS TO OTHER PEERS===================== " for op in operation: for peer in peers: print "sending to peer", peer self.connect_peer(peer[0],peer[1]) send_packet = packet() send_packet.packet_type = constant.OperationTransformation send_packet.data = "(" + str(op[0]) + "," + str(op[1]) + "," + str(op[2]) + ")" self.__send(send_packet)
def analise(routerList, argv): data = utills.parseJson(argv) for flow in data["flows"]: for i in routerList: # i é um roteador if (i.ID == flow["target"]): # acha o roteador da vez i.AllHellou( ) #hellou para que cada host saiba quem pe seu vizinho #-----------envio da msg do test flow-------------- for j in i.hostList: # j é um host do roteador i if (j.ID == flow["source"]): for count in range(flow["amount"]): # sleep(flow["start"]) # tempo de espera da simulação dado = packet(flow["source"], flow["destination"], str(count)) #encapsula o pacote j.send(dado.destination, dado) #faz o envio do pacote # j.send(flow["destination"],flow["source"]+str(count)) if (not j.CheckEnergy() ): #verifica se acabou a energia i.AllHellou() break break
def xml_parsing(): tree = etree.parse('../config/NetworkConfiguration') root = tree.getroot() # empty list for stations stations_list = [] for stations in root: # Iteration in every station for station_xml in stations: station_id = int(station_xml.find('stationId').text) #print(station_id) event_list = [] station_obj = None sifs = int(station_xml.find('sifs').text) difs = int(station_xml.find('difs').text) for packets_xml in station_xml.findall('packets'): for current_packet_xml in packets_xml.findall('packet'): new_packet = packet( int(current_packet_xml.find('size').text), station_id, int(current_packet_xml.find('to').text)) event_list.append(event(new_packet)) station_obj = station(station_id, sifs, difs, event_list) stations_list.append(station_obj) return stations_list
def loop(self): while self.connected: try: if self.user_id is None: #must do connection init send_packet = packet() send_packet.packet_type = packet.ConnectionInit self.__send(send_packet) recv_packet = self.__receive() #stamp if recv_packet.stamp > 0: self.lamport.set_stamp(recv_packet) if recv_packet.packet_type == packet.UserIdAssignation: self.user_id = recv_packet.fields['user_id'] self.__user_assigned() text_data = recv_packet.get_field("workspace") self.workspace.set_data(text_data) self.__workspace_received() #print "User Id : %s" % self.user_id elif recv_packet.packet_type == packet.Error: print "Error packet : %s" % recv_packet.fields['message'] else: if len(self.queued_packets) > 0: q_packet = self.queued_packets[0] if q_packet.packet_type == packet.Right or q_packet.packet_type == packet.ReleaseRight: q_packet.stamp = self.lamport.increment() self.__send(q_packet) self.debug("queued packet send %s" % q_packet.to_string()) self.queued_packets.remove(q_packet) else: send_packet = packet() self.__send(send_packet) recv_packet = self.__receive() if recv_packet.packet_type == packet.Ping: self.debug("Ping from server (%d)" % recv_packet.packet_id) elif recv_packet.packet_type == packet.Right: can_write = recv_packet.get_bool('can_write') is_waiting = recv_packet.get_bool('is_waiting') self.can_write = can_write self.is_waiting = is_waiting self.__write_status_changed() elif recv_packet.packet_type == packet.WorkspaceUpdate: diff_data = recv_packet.get_field("diff") diff = workspace_diff.workspace_diff(diff_data) if not diff.is_empty(): self.workspace.apply_diff(diff) self.__workspace_received() elif recv_packet.packet_type == packet.WriteUpdate: user_id = recv_packet.get_field("id") if self.write_update is not None: print user_id self.write_update(user_id) elif recv_packet.packet_type == packet.Message: msg = recv_packet.get_field("message") self.__message_recevied(msg) except socket.error as e: print "Socket error [%s, %s]" % (e.errno, e.strerror) self.connected = False self.__disconnected() time.sleep(0.2)
print"payload is: ", payload crc_recvd = pieces[4] #print"crc received is: ", crc_recvd # ---- start checking for errors ---- # if ("101010101010" in data) == False: # not corrupted isValid = True # valid for the moment - not corrupted print"Expected sequence number is ", correct_seq, ", got sequence number ", int(ack_seq) # -- start error-checking the data -- # if correct_seq != int(ack_seq): # error has occurred - send a nack to request retransmission isValid = False print"Incorrect sequence number - the original packet was dropped" dropped = True nack_packet = packet.packet(bytes(payload), correct_seq, "01") connectionSocket.send(bytes(nack_packet)) # NACK else: # we have the correct sequence num - continue error-checking print"Correct sequence number" # print"CRC received: ", crc_recvd chk = CRC.calc_checksum(bytes(payload)) if chk != crc_recvd: isValid = False # request a resend print"CRC fields do not match" else: isValid = True print"CRC correct" else: # packet corrupt print"Corrupt packet!" isValid = False
def handleNewConnection(): self.peers.append([recv_peer_packet.my_host_name, recv_peer_packet.my_port_name]) send_packet = packet() send_packet.packet_type = constant.Ack self.__peer_send(peer_socket,send_packet)
from packet import packet p = packet() ##################################################### # 网页登录后的cookie # 无法从document.cookie获取(httpOnly) # 仅需auth字段 auth = '' ##################################################### p.setCookie('auth=' + auth) moduleList = p.getModuleList() for module in moduleList: try: moduleId = module['id'] infoList = p.getBookModuleInfo(moduleId=moduleId) for info in infoList: try: cells = info['cells'] for cell in cells: try: courseId = cell['CourseId'] moduleIds = moduleId cellId = cell['Id'] auvideoLength = cell['VideoTimeLong'] videoTimeTotalLong = cell['VideoTimeLong'] print(p.statStuProcessCellLogAndTimeLong(courseId=courseId,moduleIds=moduleIds,cellId=cellId,auvideoLength=auvideoLength,videoTimeTotalLong=videoTimeTotalLong)) except Exception as e: print("cells:", str(e))
def loop(self,my_host_name,my_port_name): constant = constants() print my_host_name,my_port_name while self.connected: try: send_packet = packet() send_packet.packet_type = constant.JOIN send_packet.my_host_name = my_host_name send_packet.my_port_name = my_port_name print "details packet_type",send_packet.packet_type send_packet.doc_name = self.file_name self.__send(send_packet) recv_packet = self.__receive() self.socket.close(); self.bind_socket.bind((my_host_name,my_port_name)) print "creating server at ",my_host_name,my_port_name self.bind_socket.listen(10) thread.start_new_thread(self.sendOperationToAllPeers,()) thread.start_new_thread(self.mergeNewOperations,()) if recv_packet.packet_type == constant.NewFile: #new file created. while 1: peer_socket, peer_address = self.bind_socket.accept() print "connection accepted ....." try: recv_peer_packet = self.__peer_receive(peer_socket) # <packet_type 1000: my_host_name: my_port_name> if recv_peer_packet.packet_type == constant.NewConnection: handleNewConnection() if recv_peer_packet.packet_type == constant.OperationTransformation: self.set_of_operation_to_merge.append(recv_peer_packet.data) if recv_peer_packet.packet_type == constant.getData: print "some peer wants my data workspace.data" send_packet = packet() print "my workspace data before is ", self.workspace.get_data() print "my workspace data is ", self.workspace.get_data() send_packet.data = self.workspace.get_data() self.__peer_send(peer_socket,send_packet) peer_socket.close() except: peer_socket.close() print "error 265" else: print "joining existing file list_of_ip: ",recv_packet.list_of_ip for peer in recv_packet.list_of_ip: print "perr details are: ", peer self.peers.append(peer) self.connect_peer(peer[0],peer[1]) send_packet = packet() send_packet.packet_type = constant.NewConnection send_packet.my_host_name = my_host_name send_packet.my_port_name = my_port_name self.__send(send_packet) recv_peer_ack = self.__receive() peer = recv_packet.list_of_ip[0] self.connect_peer(peer[0],peer[1]) send_packet.packet_type = constant.getData self.__send(send_packet) recv_peer_data = self.__receive() self.workspace.set_data(recv_peer_data.data) print "recv_data ", recv_peer_data.data self.__workspace_received() print "recv_packet data:",recv_peer_data.data while 1: print "waiting to accept....." peer_socket, peer_address = self.bind_socket.accept() print "connection accepted ....." try: recv_peer_packet = self.__peer_receive(peer_socket) # <packet_type 1000: my_host_name: my_port_name> if recv_peer_packet.packet_type == constant.NewConnection: handleNewConnection() if recv_peer_packet.packet_type == constant.OperationTransformation: self.set_of_operation_to_merge.append(recv_peer_packet.data) if recv_peer_packet.packet_type == constant.getData: print "some peer wants my data workspace.data" send_packet = packet() print "my workspace data before is ", self.workspace.get_data() print "my workspace data is ", self.workspace.get_data() send_packet.data = self.workspace.get_data() self.__peer_send(peer_socket,send_packet) peer_socket.close() except: peer_socket.close() print "error 265" except socket.error as e: print "Socket error [%s, %s]" % (e.errno, e.strerror) self.connected = False self.__disconnected() time.sleep(0.2)
def __error(self, message): error_packet = packet() error_packet.packet_type = packet.Error error_packet.fields['message'] = message print "[%d] Sending error packet..." % self.connection_id self.__send(error_packet)
def __receive(self): sock_data = self.socket.recv(self.buffer_size) recv_packet = packet(sock_data) return recv_packet
def __peer_receive(self,peer_socket): data = peer_socket.recv(self.buffer_size) recv_packet = packet(data) return recv_packet
def getData(self, timeout): """ Get n data over the enlace interface Return the byte array and the size of the buffer """ print("Chamei o Get Data") payload = bytearray([]) while True: pacote = self.rx.getPacket(timeout) construtor = packet.packet() data, tipo, atual, total, crc_head, crc_payload = construtor.unpack(pacote) if data != None: print("Atual: ", atual) print("Total: ", total) crc_payload_2 = self.CRC(data) crc_head_2 = self.CRC(pacote[0:5]) if crc_payload_2 == crc_payload and crc_head_2 == crc_head: #comparação de crcs, se der certo, envia Ack payload += data time.sleep(0.05) self.sendCmd(1) print("Recebi o pacote, mandando o Ack") else: time.sleep(0.1) self.sendCmd(2) print("Recebi o pacote corrompido, mandando nAck") last= atual ptotal=total while atual < ptotal: pacote = self.rx.getPacket(timeout) data, tipo, atual, total, crc_head, crc_payload = construtor.unpack(pacote) print("Recebi um pacote tipo: ", tipo) if (tipo!=3): continue print("Atual: ", atual) print("Total: ", total) crc_payload_2 = self.CRC(data) crc_head_2 = self.CRC(pacote[0:5]) if crc_payload_2 == crc_payload and crc_head_2 == crc_head and atual==(last+1): #comparação de crcs, se der certo, envia Ack last=atual payload += data time.sleep(0.1) self.sendCmd(1) print("Recebi o pacote, mandando o Ack") elif(last==atual): print("Recebi um pacote repetido") else: time.sleep(0.05) self.sendCmd(2) print("Last: ", last) print("Atual: ", atual) print("Recebi o pacote corrompido, mandando nAck") return(payload, len(payload), 3) else: return(None, 0, tipo)
except serial.SerialException: print("Device not connected") input = 0 while 1: msgData = raw_input("Enter data:(no more than 2 chars): ")[:2] msgType = raw_input("Select message type:(0 - get,1 - set):")[:1] if (msgData == 'qt'): ser.close() exit() else: out = '' pkt = packet.packet() pkt.setType(msgType) pkt.setData(msgData) pkt.calculateCrc() stringToSend = pkt.packetToString() print stringToSend ser.write(stringToSend) time.sleep(1) while (ser.inWaiting() > 0): out += ser.read(1) if (out != ''): print out
def run(self): self.connected = True print "Connection %d started" % self.connection_id while self.connected: try: if len(self.queued_packets) > 0: queued_packet = self.queued_packets[0] self.__send(queued_packet) self.queued_packets.remove(queued_packet) recv_data = self.client.recv(self.buffer_size) try: recv_packet = packet(recv_data) except packet_exception as e: self.master.debug("[%d] Invalid Packet, closing connection" % self.connection_id) self.connected = False if recv_packet.packet_type == packet.ConnectionInit: if self.user_id is None: self.__generate_user_id() send_packet = packet() send_packet.packet_type = packet.UserIdAssignation send_packet.fields['user_id'] = self.user_id send_packet.fields['workspace'] = self.master.workspace.get_data() self.queued_packets.append(send_packet) if self.master.access_write is not None: c = self.master.get_thread(self.master.access_write) if c is not None: p = packet() p.packet_type = packet.Message p.put_field("message", "User [%s] is now possessing the write token !" % c.user_id) self.queued_packets.append(p) else: p = packet() p.packet_type = packet.Message p.put_field("message", "Nobody is possessing the write token") self.queued_packets.append(p) else: self.__error("User Id already assigned") elif recv_packet.packet_type == packet.Ping: #sending a ping self.master.debug("[%d] Ping from client (%d)" % (self.connection_id, recv_packet.packet_id)) send_packet = packet() self.queued_packets.append(send_packet) elif recv_packet.packet_type == packet.Closing: self.master.debug("[%d] Client is closing connection" % self.connection_id) self.connected = False elif recv_packet.packet_type == packet.Right: self.master.debug("[%d] a client is asking for the right to write, packet id: (%d)" % (self.connection_id, recv_packet.packet_id)) send_packet = packet() send_packet.stamp = self.master.lamport.increment() send_packet.packet_type = packet.Right is_waiting = False if self.master.access_write is None: self.master.access_write = self.connection_id access = True self.__write_right_update() else: self.master.debug("[%d] a client is added to the right to write waiting list, packet id: (%d)" % (self.connection_id, recv_packet.packet_id)) self.master.adding_access_queued(self.connection_id) access = False is_waiting = True send_packet.put_field("can_write", str(access)) send_packet.put_field("is_waiting", str(is_waiting)) self.queued_packets.append(send_packet) elif recv_packet.packet_type == packet.ReleaseRight: if self.master.access_write == self.connection_id: diff_data = recv_packet.get_field("diff") diff = workspace_diff.workspace_diff(diff_data) if not diff.is_empty(): self.master.workspace.apply_diff(diff) self.master.workspace.flush() self.__workspace_update(diff) self.master.access_write = None print "[%d] Releasing write access" % self.connection_id send_packet = packet() send_packet.stamp = self.master.lamport.increment() send_packet.packet_type = packet.Right send_packet.put_field('can_write', str(False)) send_packet.put_field('is_waiting', str(False)) self.__send(send_packet) if len(self.master.access_queued) > 0: print "[%d] Release right someone is waiting for right" % self.connection_id succeeding = self.master.access_queued[0] print "[%d] next one in line is: %d" % (self.connection_id, succeeding) self.master.next_in_queued() else: print "[%d] access_waiting is smaller <= 0" % self.connection_id self.master.send_message('Write token is now free !') elif self.connection_id in self.master.access_queued: print "[%d] Releasing right when in waiting list: %d" % (self.connection_id, self.connection_id) self.master.access_queued.remove(self.connection_id) send_packet = packet() send_packet.packet_type = packet.Right send_packet.put_field("can_write", False) send_packet.put_field("is_waiting", False) self.__send(send_packet) else: self.__error("You don't have the write access !") else: # nothing to send send_packet = packet() self.__send(send_packet) except socket.timeout: #timeout hit print "[%d] Client timed out, terminating thread" % self.connection_id self.connected = False except socket.error as e: self.master.debug("socket.error (%d, %s)" % (e.errno, e.strerror)) if e.errno == 10054: print "Connection closed by the client..." self.connected = False #little nap for the cpu time.sleep(0.3) self.terminate()
def flush(self): fp = open(self.file_path, 'w') fp.write(self.get_data()) fp.close() if __name__ == '__main__': print "Workspace Unit Test" fw = file_workspace('test.txt') #fw.__data = "allo" pack_str = fw.get_workspace_packet().to_string() print "Packet test" p = packet(pack_str) print p.to_string() #clone w2 = fw.clone() w2.insert(3, "aloooooo clone !!") print fw.get_data() print w2.get_data() _diff = fw.diff(w2) w3 = workspace()
def __receive(self): sock_data = self.socket.recv(self.buffer_size) recv_packet = packet(sock_data) self.received_packets.append(recv_packet) return recv_packet
except serial.SerialException: print("Device not connected") input = 0 while 1: msgData = raw_input("Enter data:(no more than 2 chars): ")[:2] msgType = raw_input("Select message type:(0 - get,1 - set):")[:1] if (msgData == 'qt'): ser.close() exit() else: out = '' pkt = packet.packet() pkt.setType(msgType) pkt.setData(msgData) pkt.calculateCrc() stringToSend = pkt.packetToString() print stringToSend ser.write(stringToSend) time.sleep(1) while (ser.inWaiting() > 0): out += ser.read(1) if (out != ''): print out
def release_right(self, diff): p = packet() p.packet_type = packet.ReleaseRight p = diff.fill_packet(p) self.client.queued_packets.append(p)