def activateRelaySocket(port): global ofSwitch hostname = socket.gethostbyname(socket.gethostname()) listenSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listenSock.bind(("0.0.0.0", port)) listenSock.listen(1) data = b'' sdnpwn.message("[Relay Socket] Relay port open on port " + str(port) + "", sdnpwn.NORMAL) while 1: try: conn, addr = listenSock.accept() msgHeader = conn.recv(8) header = Header() header.unpack(msgHeader) sdnpwn.message( "[Relay Socket] Got " + str(header.message_type) + " from " + str(addr), sdnpwn.NORMAL) msgBody = conn.recv(header.length - 8) msgFull = header.pack() + msgBody print(msgFull) ofSwitch.comm_sock.send(msgFull) except Exception as e: sdnpwn.message("[Relay socket] Error handling message", sdnpwn.WARNING) print(e) listenSock.close()
def handlePkt(pkt): if (TCP in pkt and len(pkt[TCP].payload) > 0): try: ofHeader = Header() ofHeader.unpack(bytes(pkt[TCP].payload)[:8]) print("[>] " + str(pkt[IP].src) + " -> ", end='') print("OFv" + str(ofHeader.version), end=' ') print(ofHeader.message_type, end=' -> ') print(str(pkt[IP].dst)) #ofBody = "" #try: ##TODO: Allow for detailed message information to be printed #ofBody = bytes(pkt[TCP].payload)[:(ofHeader.length-8)] #if((ofHeader.message_type & 0xFF) == 6): #ofFeatureReply = FeaturesReply() #ofFeatureReply.unpack(ofBody) #ofHelper.printFeatureReplyDetails(ofFeatureReply) #except Exception as e: #print(e) except: #Not an OF message pass
def unpack_message(buffer): """Unpack the whole buffer, including header pack.""" hdr_size = Header().get_size() hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:] header = Header() header.unpack(hdr_buff) message = new_message_from_header(header) message.unpack(msg_buff) return message
def getResponse(sock): try: ofHeader = Header() replyHeader = sock.recv(8) ofHeader.unpack(replyHeader) replyBody = sock.recv(ofHeader.length - 8) return (ofHeader, replyBody) except Exception as e: if (verbose == True): print("Error: " + str(e)) return None
def _unpack_data(self): if self.data == b'': return BinaryData() # header unpacking header = Header() header_size = header.get_size() header_data = self.data.value[:header_size] header.unpack(header_data) # message unpacking msg = common.utils.new_message_from_header(header) msg_data = self.data.value[header_size:] msg.unpack(msg_data) return msg
def connect(self, controllerIP, port): try: self.comm_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.comm_sock.connect((controllerIP, port)) except Exception as e: sdnpwn.message("Problem connecting to " + controllerIP + ":" + str(port), sdnpwn.ERROR) print return sdnpwn.message("Socket connected. Sending OF Hello...", sdnpwn.SUCCESS) ofHello = Hello() header = Header() self.comm_sock.send(ofHello.pack()) #Send Hello replyHeader = self.comm_sock.recv(8) #Get hello response header & body header.unpack(replyHeader) sdnpwn.message("Got " + str(header.message_type), sdnpwn.NORMAL) #sdnpwn.message("Controller base OF version: " + str(header.version), sdnpwn.VERBOSE) replyBody = self.comm_sock.recv(header.length-8) #Get body but ignore sdnpwn.message("Connected to controller", sdnpwn.SUCCESS) if(self.auto_handle_Messages == True): run = True sdnpwn.message("Handling OpenFlow messages automatically", sdnpwn.NORMAL) while(run): #try: #Get feature request reply = self.comm_sock.recv(8) header.unpack(reply) if(header.length == None): sdnpwn.message("Got bad OF message. Closing.", sdnpwn.WARNING) run = False self.comm_sock.close() else: replyBody = self.comm_sock.recv(header.length-8) try: autohandleOFMessage(self, header, replyBody, self.enable_output) except: sdnpwn.message("Error handling OF message", sdnpwn.WARNING) #except Exception as e: #sdnpwn.message("Socket disconnected", sdnpwn.ERROR) #print(e) #self.comm_sock.close() #break else: return True
def unpack_message(buffer): """Unpack the whole buffer, including header pack. Args: buffer (bytes): Bytes representation of a openflow message. Returns: object: Instance of openflow message. """ hdr_size = Header().get_size() hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:] header = Header() header.unpack(hdr_buff) message = new_message_from_header(header) message.unpack(msg_buff) return message
def unpack_message(buffer): """Unpack the whole buffer, including header pack. Args: buffer (bytes): Bytes representation of a openflow message. Returns: object: Instance of openflow message. """ hdr_size = Header().get_size() hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:] header = Header() header.unpack(hdr_buff) message = new_message_from_header(header) message.unpack(msg_buff) return message
class TestHeader(unittest.TestCase): """Test the message Header.""" def setUp(self): """Setup the TestHeader Class instantiating a HELLO header.""" self.message = Header() self.message.message_type = Type.OFPT_HELLO self.message.xid = 1 self.message.length = 0 def test_size(self): """[Common/Header] - size 8.""" self.assertEqual(self.message.get_size(), 8) @unittest.expectedFailure def test_pack_empty(self): """[Common/Header] - packing empty header.""" self.assertRaises(TypeError, Header().pack()) def test_pack(self): """[Common/Header] - packing Hello.""" packed_header = b'\x01\x00\x00\x00\x00\x00\x00\x01' self.assertEqual(self.message.pack(), packed_header) def test_unpack(self): """[Common/Header] - unpacking Hello.""" filename = os.path.join(os.path.dirname(os.path.realpath('__file__')), 'raw/v0x01/ofpt_hello.dat') f = open(filename, 'rb') self.message.unpack(f.read(8)) self.assertEqual(self.message.length, 8) self.assertEqual(self.message.xid, 1) self.assertEqual(self.message.message_type, Type.OFPT_HELLO) self.assertEqual(self.message.version, 1) f.close() @patch('pyof.v0x01.common.header.randint') def test_random_xid(self, m): """Each Header instantiations without xid should call randint.""" Header(), Header() # noqa self.assertEqual(m.call_count, 2)
def do_handshake(client): """Get a client (socket) and do the handshake of it. This method receives a client (socket) that simulates a switch on the network and do the OpenFlow handshake process with a running controller on the network. Args: client (socket): a socket object connected on the controller. Returns: The client with the handshake process done. """ # -- STEP 1: Sending Hello message client.send(Hello(xid=3).pack()) # -- STEP 2: Whait for Hello response binary_packet = b'' while len(binary_packet) < 8: binary_packet = client.recv(8) header = Header() header.unpack(binary_packet) # -- STEP 3: Wait for features_request message binary_packet = b'' # len() < 8 here because we just expect a Hello as response while len(binary_packet) < 8: binary_packet = client.recv(8) header = Header() header.unpack(binary_packet) # -- STEP 4: Send features_reply to the controller basedir = os.path.dirname(os.path.abspath(__file__)) raw_dir = os.path.join(basedir, 'raw') message = None with open(os.path.join(raw_dir, 'features_reply.cap'), 'rb') as file: message = file.read() client.send(message) return client
def do_handshake(client: socket): """Get a client socket and do the handshake of it. This method receives a client socket that simulates a switch on the network and does the OpenFlow handshake process with a running controller on the network. Args: client (socket): a socket object connected to the controller. Returns: The client with the handshake process done. """ # -- STEP 1: Send Hello message client.send(Hello(xid=3).pack()) # -- STEP 2: Wait for Hello response binary_packet = b'' while len(binary_packet) < 8: binary_packet = client.recv(8) header = Header() header.unpack(binary_packet) # -- STEP 3: Wait for features_request message binary_packet = b'' # len() < 8 here because we just expect a Hello as response while len(binary_packet) < 8: binary_packet = client.recv(8) header = Header() header.unpack(binary_packet) # -- STEP 4: Send features_reply to the controller basedir = os.path.dirname(os.path.abspath(__file__)) raw_dir = os.path.join(basedir, 'raw') message = None with open(os.path.join(raw_dir, 'features_reply.cap'), 'rb') as file: message = file.read() client.send(message) return client
class TestHeader(unittest.TestCase): """Test the message Header.""" def setUp(self): """Setup the TestHeader Class instantiating a HELLO header.""" self.message = Header() self.message.message_type = Type.OFPT_HELLO self.message.xid = 1 self.message.length = 0 def test_size(self): """[Common/Header] - size 8.""" self.assertEqual(self.message.get_size(), 8) @unittest.expectedFailure def test_pack_empty(self): """[Common/Header] - packing empty header.""" self.assertRaises(TypeError, Header().pack()) def test_pack(self): """[Common/Header] - packing Hello.""" packed_header = b'\x01\x00\x00\x00\x00\x00\x00\x01' self.assertEqual(self.message.pack(), packed_header) def test_unpack(self): """[Common/Header] - unpacking Hello.""" filename = os.path.join(os.path.dirname(os.path.realpath('__file__')), 'raw/v0x01/ofpt_hello.dat') f = open(filename, 'rb') self.message.unpack(f.read(8)) self.assertEqual(self.message.length, 8) self.assertEqual(self.message.xid, 1) self.assertEqual(self.message.message_type, Type.OFPT_HELLO) self.assertEqual(self.message.version, 1) f.close()
def _unpack_header(raw_header): header = Header() header.unpack(raw_header) return header