def listen(self, host="localhost", port="IP"): self.host = host self.port = self.port if len(port) == 0 else int( port.encode('utf-8').hex(), 16) self.SOCKET.bind((self.host, self.port)) self.SOCKET.listen(1) print('[SERVER]: Listening in port %s' % str(self.port)) logging.info('[SERVER]: Listening in port %s' % str(self.port)) connection, address = self.SOCKET.accept() logging.info('[SERVER] Connection initialized between tcp sockets.') while True: data = connection.recv(1480) words, body = self.get_words(data) if len(words) == 0: break # el destino es el server src_port = int("IP".encode('utf-8').hex(), 16) dst_port = int(words[0][0:4], base=16) # el origen es el cliente self.SEQ_NUMBER = int(words[1], base=16) self.ACK_NUMBER = int(words[2], base=16) self.FILE_LEN = int(words[3][4:8], base=16) if self.SEQ_NUMBER == 1: sgm = Segment(src_port, dst_port) header_syn_ack = sgm.build_segment(seq=self.SEQ_NUMBER, ack=0x2) logging.info('[Server Recv] SYN | [Server Send] SYN-ACK') print('[Server Recv] SYN | [Server Send] SYN-ACK', self.SEQ_NUMBER, self.ACK_NUMBER) connection.sendall(header_syn_ack) pass elif self.SEQ_NUMBER == 2 and self.ACK_NUMBER == 2: logging.info('[Server Recv] ACK | CONNECTION STABLISHED') print('[Server Recv] ACK | CONNECTION STABLISHED', self.SEQ_NUMBER, self.ACK_NUMBER) pass elif self.SEQ_NUMBER == 0x32 and len(self.FILE_NAME) == 0: logging.info('[Server Recv] PSH-ACK | [Server Send] ACK') print('[Server Recv] PSH-ACK | [Server Send] ACK', self.SEQ_NUMBER, self.ACK_NUMBER) if self.FILE_LEN != 0: self.FILE_NAME = body[0:self.FILE_LEN] pass sgm = Segment(src_port, dst_port) segmento_ack = sgm.build_segment(ack=0x33) connection.sendall(segmento_ack) pass elif self.SEQ_NUMBER >= 0x32: if self.FILE_LEN != 0 and len(body) != 0: if self.FILE_LEN == 700: self.FILE += body pass elif self.FILE_LEN < 700: self.FILE += body[0:self.FILE_LEN] pass pass logging.info('[Server Recv] PSH-ACK | [Server Send] ACK') print('[Server Recv] PSH-ACK | [Server Send] ACK', self.SEQ_NUMBER, self.ACK_NUMBER) sgm = Segment(src_port, dst_port) segmento_ack = sgm.build_segment(ack=self.SEQ_NUMBER + 1) connection.sendall(segmento_ack) pass elif self.SEQ_NUMBER == 0x14: logging.info('[Server Recv] FIN | [Server Send] FIN-ACK') print('[Server Recv] FIN | [Server Send] FIN-ACK', self.SEQ_NUMBER, self.ACK_NUMBER) sgm = Segment(src_port, dst_port) segmento = sgm.build_segment(seq=0x14, ack=0x15) connection.sendall(segmento) pass if len(self.FILE_NAME) == 0: self.SOCKET.close() return pass print(self.FILE) self.FILE_NAME = bytes.fromhex(self.FILE_NAME).decode('utf-8') logging.info('[SERVER] File recv: %s' % self.FILE_NAME) path = './rcp/' + self.FILE_NAME new_file = open(path, mode='wb+') new_file.write(bytes.fromhex(self.FILE)) new_file.close() logging.info('[SERVER] The file was created, check it.') self.SOCKET.close()
def start(self, path='500B.jpg', host='localhost', port='19509'): self.PATH = path self.HOST = host self.PORT = int(port) # Split the data with open('./img/' + self.PATH, 'rb') as file: content = file.read() file_data = content.hex() file_data = self.split_data(file_data) pass try: self.SOCKET.connect((self.HOST, self.PORT)) logging.info('Connection started between tcp sockets') print('Connection started between tcp sockets') print('%s %s' % (self.HOST, self.PORT)) except: logging.error('Connection refused') print('Refused') src_port = int('IP'.encode('utf-8').hex(), base=16) dst_port = 0x0 try: while True: if self.n_data_transmmited == len(file_data): self.SEQ_NUMBER = 0x14 pass if self.SEQ_NUMBER == 1 and self.ACK_NUMBER == 0: # start 3 way handshake sgm = Segment(src_port=0x0, dst_port=self.PORT) header_syn = sgm.build_segment(seq=0x1) logging.basicConfig( format= "%(asctime)s [CLIENT] - %(levelname)s - %(message)s", filename="client.log", filemode='w+') logging.info('[Client] Send SYN') print('[Client] Send SYN') self.SOCKET.sendall(header_syn) pass elif self.SEQ_NUMBER == 1 and self.ACK_NUMBER == 2: sgm = Segment(src_port=src_port, dst_port=dst_port) header_ack = sgm.build_segment(seq=0x2, ack=0x2) self.SOCKET.sendall(header_ack) # Send file name filename = str.encode(self.PATH).hex() logging.basicConfig( format= "%(asctime)s [CLIENT] - %(levelname)s - %(message)s", filename="client.log", filemode='w+') logging.info( '[Client Recv] SYN-ACK | [Client Send] ACK | [Client Send] PSH-ACK' ) print( '[Client Recv] SYN-ACK | [Client Send] ACK | [Client Send] PSH-ACK' ) sgm = Segment(src_port, dst_port, data=filename) segmento = sgm.build_segment(seq=0x32) self.SOCKET.sendall(segmento) pass elif self.SEQ_NUMBER == 0x14 and self.ACK_NUMBER >= 0x32: logging.basicConfig( format= "%(asctime)s [CLIENT] - %(levelname)s - %(message)s", filename="client.log", filemode='w+') logging.info('[Client Recv] ACK | [Client Send] FIN') print('[Client Recv] ACK | [Client Send] FIN') sgm = Segment(src_port, dst_port) segmento = sgm.build_segment(seq=self.SEQ_NUMBER) self.SOCKET.sendall(segmento) pass elif self.ACK_NUMBER == 0x15 and self.SEQ_NUMBER == 0x14: logging.basicConfig( format= "%(asctime)s [CLIENT] - %(levelname)s - %(message)s", filename="client.log", filemode='w+') logging.info( '[Client Recv] FIN-ACK | [Client Send] ACK | CONNECTION FINISHED' ) print( '[Client Recv] FIN-ACK | [Client Send] ACK | CONNECTION FINISHED' ) sgm = Segment(src_port, dst_port) segmento = sgm.build_segment(seq=self.ACK_NUMBER) self.SOCKET.sendall(segmento) break pass elif self.ACK_NUMBER >= 0x33: logging.info('[Client Recv] ACK | [Client Send] PSH-ACK') sgm = Segment(src_port, dst_port, data=file_data[self.n_data_transmmited]) segmento = sgm.build_segment( seq=self.ACK_NUMBER + len(file_data[self.n_data_transmmited])) self.SOCKET.sendall(segmento) self.n_data_transmmited += 1 pass data = self.SOCKET.recv(1280) words = self.get_words(data) if len(words) == 0: break src_port = int(words[0][4:8], base=16) dst_port = int(words[0][0:4], base=16) self.SEQ_NUMBER = int(words[1], base=16) self.ACK_NUMBER = int(words[2], base=16) pass except: logging.error('Exception error') self.SOCKET.close() return