def server_mode(options): """"Implements the niping server running mode :param options: option set from the command line :type options: Values """ if not options.host: options.host = "0.0.0.0" sock = socket() try: sock.bind((options.host, options.port)) sock.listen(0) logging.info("") logging.info(datetime.today().ctime()) logging.info("ready for connect from client ...") while True: sc, sockname = sock.accept() client = SAPNIStreamSocket(sc) logging.info("") logging.info(datetime.today().ctime()) logging.info("connect from host '{}', client hdl {} o.k.".format( sockname[0], client.fileno())) try: while True: r = client.recv() client.send(r.payload) except SocketError: pass finally: logging.info("") logging.info(datetime.today().ctime()) logging.info("client hdl {} disconnected ...".format( client.fileno())) except SocketError: logging.error("[*] Connection error") except KeyboardInterrupt: logging.error("[*] Cancelled by the user") finally: sock.shutdown(SHUT_RDWR) sock.close()
def server_mode(options): """"Implements the niping server running mode :param options: option set from the command line :type options: Values """ if not options.host: options.host = "0.0.0.0" sock = socket() try: sock.bind((options.host, options.port)) sock.listen(0) print("") print(datetime.today().ctime()) print("ready for connect from client ...") while True: sc, sockname = sock.accept() client = SAPNIStreamSocket(sc) print("") print(datetime.today().ctime()) print("connect from host '{}', client hdl {} o.k.".format(sockname[0], client.fileno())) try: while True: r = client.recv() client.send(r.payload) except SocketError: pass finally: print("") print(datetime.today().ctime()) print("client hdl {} disconnected ...".format(client.fileno())) except SocketError: print("[*] Connection error") except KeyboardInterrupt: print("[*] Cancelled by the user") finally: sock.shutdown(SHUT_RDWR) sock.close()
def send(self, packet): """Send a packet. If the talk mode in use is native the packet sent is a raw packet. Otherwise, the packet is a NI layer packet in the same way the :class:`SAPNIStreamSocket` works. :param packet: packet to send :type packet: Packet """ # If we're working on native mode and the route was accepted, we don't # need the NI layer anymore. Just use the plain socket inside the # NIStreamSockets. if self.routed and self.talk_mode == 1: return StreamSocket.send(self, packet) # If the route was not accepted yet or we're working on non-native talk # mode, we need the NI layer. return SAPNIStreamSocket.send(self, packet)
class PySAPNIStreamSocketTest(PySAPBaseServerTest): test_port = 8005 test_address = "127.0.0.1" test_string = "TEST" * 10 def test_sapnistreamsocket(self): """Test SAPNIStreamSocket""" self.start_server(self.test_address, self.test_port, SAPNITestHandler) sock = socket.socket() sock.connect((self.test_address, self.test_port)) self.client = SAPNIStreamSocket(sock) packet = self.client.sr(Raw(self.test_string)) packet.decode_payload_as(Raw) self.client.close() self.assertIn(SAPNI, packet) self.assertEqual(packet[SAPNI].length, len(self.test_string)) self.assertEqual(packet.payload.load, self.test_string) self.stop_server() def test_sapnistreamsocket_base_cls(self): """Test SAPNIStreamSocket handling of custom base packet classes""" self.start_server(self.test_address, self.test_port, SAPNITestHandler) class SomeClass(Packet): fields_desc = [StrField("text", None)] sock = socket.socket() sock.connect((self.test_address, self.test_port)) self.client = SAPNIStreamSocket(sock, base_cls=SomeClass) packet = self.client.sr(Raw(self.test_string)) self.client.close() self.assertIn(SAPNI, packet) self.assertIn(SomeClass, packet) self.assertEqual(packet[SAPNI].length, len(self.test_string)) self.assertEqual(packet[SomeClass].text, self.test_string) self.stop_server() def test_sapnistreamsocket_getnisocket(self): """Test SAPNIStreamSocket get nisocket class method""" self.start_server(self.test_address, self.test_port, SAPNITestHandler) self.client = SAPNIStreamSocket.get_nisocket(self.test_address, self.test_port) packet = self.client.sr(Raw(self.test_string)) packet.decode_payload_as(Raw) self.client.close() self.assertIn(SAPNI, packet) self.assertEqual(packet[SAPNI].length, len(self.test_string)) self.assertEqual(packet.payload.load, self.test_string) self.stop_server() def test_sapnistreamsocket_without_keep_alive(self): """Test SAPNIStreamSocket without keep alive""" self.start_server(self.test_address, self.test_port, SAPNITestHandlerKeepAlive) sock = socket.socket() sock.connect((self.test_address, self.test_port)) self.client = SAPNIStreamSocket(sock, keep_alive=False) packet = self.client.sr(Raw(self.test_string)) packet.decode_payload_as(Raw) # We should receive our packet first self.assertIn(SAPNI, packet) self.assertEqual(packet[SAPNI].length, len(self.test_string)) self.assertEqual(packet.payload.load, self.test_string) # Then we should get a we should receive a PING packet = self.client.recv() self.assertIn(SAPNI, packet) self.assertEqual(packet[SAPNI].length, len(SAPNI.SAPNI_PING)) self.assertEqual(packet.payload.load, SAPNI.SAPNI_PING) self.client.close() self.stop_server() def test_sapnistreamsocket_with_keep_alive(self): """Test SAPNIStreamSocket with keep alive""" self.start_server(self.test_address, self.test_port, SAPNITestHandlerKeepAlive) sock = socket.socket() sock.connect((self.test_address, self.test_port)) self.client = SAPNIStreamSocket(sock, keep_alive=True) self.client.send(Raw(self.test_string)) packet = self.client.recv() packet.decode_payload_as(Raw) # We should receive our packet first self.assertIn(SAPNI, packet) self.assertEqual(packet[SAPNI].length, len(self.test_string)) self.assertEqual(packet.payload.load, self.test_string) # Then we should get a connection reset if we try to receive from the server self.assertRaises(socket.error, self.client.recv) self.client.close() self.stop_server() def test_sapnistreamsocket_close(self): """Test SAPNIStreamSocket with a server that closes the connection""" self.start_server(self.test_address, self.test_port, SAPNITestHandlerClose) sock = socket.socket() sock.connect((self.test_address, self.test_port)) self.client = SAPNIStreamSocket(sock, keep_alive=False) with self.assertRaises(socket.error): self.client.sr(Raw(self.test_string)) self.stop_server()