def runTest(self): logging = get_logger() logging.info("Running Grp100No80 BadRequestBadLength test") #In Message module at pack time the length is computed #avoid this by using cstruct module logging.info("Sending barrier_request message..") stats_request = message.barrier_request() header=ofp.ofp_header() header.type = ofp.OFPT_BARRIER_REQUEST # normal the header length is 12bytes changed it to 18bytes header.length=5; packed=header.pack()+stats_request.pack() sleep(2) rv=self.controller.message_send(packed) sleep(2) self.assertTrue(rv != -1,"Unable to send the message") logging.info("Waiting for OFPT_ERROR message..") (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_ERROR, timeout=10) self.assertTrue(response is not None, 'Switch did not reply with an error message') self.assertTrue(response.type==ofp.OFPET_BAD_REQUEST, 'Error type is not OFPET_BAD_REQUEST') self.assertTrue(response.code==ofp.OFPBRC_BAD_LEN, 'Error code is not OFPBRC_BAD_LEN got {0}'.format(response.code))
def _of_message_to_object(binary_string): """ Map a binary string to the corresponding class. Appropriately resolves subclasses """ hdr = ofp.ofp_header() hdr.unpack(binary_string) # FIXME: Add error detection if not hdr.type in msg_type_subclassed: return msg_type_to_class_map[hdr.type]() if hdr.type == ofp.OFPT_STATS_REQUEST: sub_hdr = ofp.ofp_stats_request() sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) try: obj = stats_request_to_class_map[sub_hdr.type]() except LookupError: obj = None return obj elif hdr.type == ofp.OFPT_STATS_REPLY: sub_hdr = ofp.ofp_stats_reply() sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) try: obj = stats_reply_to_class_map[sub_hdr.type]() except LookupError: obj = None return obj elif hdr.type == ofp.OFPT_ERROR: sub_hdr = ofp.ofp_error_msg() sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) return error_to_class_map[sub_hdr.type]() else: parse_logger.error("Cannot parse pkt to message") return None
def runTest(self): logging.info("Running Grp100No60 BadRequestBadLength test") # In Message module at pack time the length is computed # avoid this by using cstruct module logging.info("Sending stats_request message..") stats_request = ofp.ofp_stats_request() header = ofp.ofp_header() header.type = ofp.OFPT_STATS_REQUEST # normal the header length is 12bytes changed it to 18bytes header.length = 18 packed = header.pack() + stats_request.pack() rv = self.controller.message_send(packed) self.assertTrue(rv != -1, "Unable to send the message") logging.info("Waiting for OFPT_ERROR message..") (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_ERROR, timeout=5) self.assertTrue(response is not None, "Switch did not reply with expected error messge") self.assertTrue(response.type == ofp.OFPET_BAD_REQUEST, "Error type is not OFPET_BAD_REQUEST") self.assertTrue(response.type == ofp.OFPBRC_BAD_LEN, "Error code is not OFPBRC_BAD_LEN")
def of_header_parse(binary_string, raw=False): """ Parse only the header from an OpenFlow packet Parses the header from a raw OpenFlow packet into a an ofp_header Python class. @param binary_string The packet (string) to be parsed @param raw If true, interpret the packet as an L2 packet. Not yet supported. @return An ofp_header object """ if raw: parse_logger.error("raw packet message parsing not supported") return None hdr = ofp.ofp_header() hdr.unpack(binary_string) return hdr