diff --git a/openair-cn/TEST/mme_test_s1_generate_scenario_from_pcap b/openair-cn/TEST/mme_test_s1_generate_scenario_from_pcap index 6a3e61091f2221519e41886cedea339a9ba34bb9..247814f553413eb143c4228c7f884bbb0df99fad 100755 --- a/openair-cn/TEST/mme_test_s1_generate_scenario_from_pcap +++ b/openair-cn/TEST/mme_test_s1_generate_scenario_from_pcap @@ -1,33 +1,5 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -################################################################################ -# OpenAirInterface -# Copyright(c) 1999 - 2014 Eurecom -# -# OpenAirInterface is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# -# OpenAirInterface is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with OpenAirInterface.The full GNU General Public License is -# included in this distribution in the file called "COPYING". If not, -# see <http://www.gnu.org/licenses/>. -# -# Contact Information -# OpenAirInterface Admin: openair_admin@eurecom.fr -# OpenAirInterface Tech : openair_tech@eurecom.fr -# OpenAirInterface Dev : openair4g-devel@eurecom.fr -# -# Address : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE -# -################################################################################ import sys import subprocess import re @@ -36,175 +8,69 @@ import datetime from datetime import date import os, errno import argparse +import tempfile +from xml.dom.minidom import parse, parseString +#from xml.etree.ElementTree import ElementTree - - - +##################### +# program arguments +##################### parser = argparse.ArgumentParser() parser.add_argument("--pcap_file", "-p", type=str,help="input pcap file to be translated") args = parser.parse_args() -pcap_file = args.pcap_file.strip() - -pcap_dissected_file = subprocess.check_output(["tshark", '-V', '-r' , pcap_file], stderr=subprocess.STDOUT) -pcap_hex_file = subprocess.check_output(["tshark", '-x', '-O', 'sctp', '-r', pcap_file]) -pcap_dissected_list = [] - -# split file content in lines -lines = pcap_dissected_file.splitlines() -message = {} -item_index = 0; -item_string = " " -fsm_state="search_frame" -for line in lines: - print ("INPUT LINE: %s " % line) - line = line.strip() - if line != "": - if fsm_state == "search_frame": - if line.startswith("Frame"): - message = {} - partition = line.split(' ',3) - message['frame'] = int(partition[1].strip(':')) - fsm_state = "search_ip" - print("Found Frame %d" % (message['frame'])) - - elif fsm_state == "search_ip": - if line.startswith("Internet Protocol Version"): - fsm_state = "fill_ip" - message['ip_fields'] = 0 - print("Found in Frame %d IP" % (message['frame'])) - - elif fsm_state == "fill_ip": - if line.startswith("Header length"): - partition = line.split(' ',4) - message['ip_header_length'] = int(partition[2]) - message['ip_fields'] += 1 - print("Found in Frame %d IP header length %d" % (message['frame'], int(partition[2]))) - if line.startswith("Total Length"): - partition = line.split(' ',4) - message['ip_total_length'] = int(partition[2]) - message['ip_fields'] += 1 - print("Found in Frame %d IP Total Length %d" % (message['frame'], int(partition[2]))) - if line.startswith("Protocol"): - partition = line.split(' ',4) - if partition[1] != "SCTP": - print("Error did not found in Frame %d SCTP" % (message['frame']))) - fsm_state="search_frame" - continue - else: - message['ip_protocol'] = partition[1] - message['ip_fields'] += 1 - print("Found in Frame %d SCTP" % (message['frame']))) - if message['ip_fields'] == 3: - fsm_state = "search_sctp" - print("in Frame %d searching S1AP" % (message['frame']))) - - elif fsm_state == "search_sctp": - if line.startswith("Internet Protocol Version"): - fsm_state = "fill_sctp" - message['sctp_fields'] = 0 - - elif fsm_state == "fill_sctp": - if line.startswith("Source port"): - partition = line.split(' ',4) - message['Source port'] = int(partition[2]) - message['sctp_fields'] += 1 - elif line.startswith("Destination port"): - partition = line.split(' ',4) - message['Destination port'] = int(partition[2]) - message['sctp_fields'] += 1 - elif line.startswith("Chunk length"): - partition = line.split(' ',4) - message['Chunk length'] = int(partition[2]) - message['sctp_fields'] += 1 - elif line.startswith("Stream Identifier"): - partition = line.split(' ',4) - message['Stream Identifier'] = int(partition[2]) - message['sctp_fields'] += 1 - elif line.startswith("Stream sequence number"): - partition = line.split(' ',4) - message['Stream sequence number'] = int(partition[3]) - message['sctp_fields'] += 1 - elif line.startswith("Chunk padding"): - partition = line.split(' ',4) - message['Chunk padding'] = int(partition[2]) - message['sctp_fields'] += 1 - if message['sctp_fields'] == 6: - fsm_state = "search_s1ap" - - elif fsm_state == "search_s1ap": - if line.startswith("S1 Application Protocol"): - fsm_state = "fill_s1ap" - message['s1ap_fields'] = 0 - print("in Frame %d Filling S1AP" % (message['frame']))) - - elif fsm_state == "fill_s1ap": - if line.startswith("S1AP-PDU:"): - partition = line.split(' ') - message['Pdu'] = partition[1] - message['s1ap_fields'] += 1 - elif line.startswith("procedureCode:"): - partition = line.split(' ') - message['procedureCode:'] = partition[1] - message['s1ap_fields'] += 1 - elif line.startswith("protocolIEs"): - partition = line.split(' ') - message['protocolIEs'] = int(partition[1]) - message['items'] = {} - elif line.startswith("Item"): - partition = line.split(' ') - item_index = partition[1].trim(':') - item_string = partition[2] - message['items'][item_string] = ' ' - print("Found in Frame %d S1AP Item %d %s" % (message['frame'], item_index, item_string))) - if item_string == id-E-RABToBeSetupListCtxtSUReq: - elif item_string == id-E-RABToBeSetupListCtxtSUReq: - elif line.startswith("nAS-PDU"): - partition = line.split(' ') - nas_bytes = partition[1].trim('.') - message['items'].append - print("Found in Frame %d Stream sequence number" % (message['frame'], int(partition[3]))) +##################### +# get xml document from pcap +##################### +orig_pcap_file_name = args.pcap_file.strip() +orig_pdml_string = subprocess.check_output(["tshark", '-T', 'pdml', '-r', orig_pcap_file_name]) - if message['s1ap_fields'] == 6: - pcap_dissected_list.append(message) - fsm_state = "search_frame" +orig_dom = parseString(orig_pdml_string) +#orig_etree = ElementTree() +#orig_etree.parse(orig_pcap_pdml_file.name) -for message in pcap_dissected_list: - print("Message:\n" ) - print(" %s" % (message)) +##################### +# filtering unwanted packets +##################### +#cases = orig_etree.findall(".//proto[@name='sctp']") +packets = orig_dom.getElementsByTagName("packet") +for packet in packets: + found_sctp = False + found_s1ap = False + protos = packet.getElementsByTagName("proto") + for proto in protos: + attrs = proto.attributes + urlnode = attrs['name'] + if urlnode.nodeValue == 'sctp': + found_sctp = True + elif urlnode.nodeValue == 's1ap': + found_s1ap = True + elif urlnode.nodeValue == 'geninfo': + packet.removeChild(proto) + elif urlnode.nodeValue == 'eth': + packet.removeChild(proto) + if found_s1ap == False: + # hopefully it seems to work (remove iterated packet) + packet.parentNode.removeChild(packet) -lines = pcap_hex_file.splitlines() -message = {} -message_index = 0 -fsm_state="search_frame" +##################### +# dom to xml string +##################### +filtered_pdml_string = orig_dom.toxml() +cleaned_pdml_string = "" +##################### +# remove blank lines in xml string +##################### +lines = filtered_pdml_string.splitlines() for line in lines: - print ("INPUT LINE: %s " % line) - line = line.strip() - if line != "": - if fsm_state == "search_frame": - if line.startswith("Frame"): - partition = line.split(' ',3) - fsm_state = "search_s1ap" - - elif fsm_state == "search_s1ap": - if line.startswith("S1 Application Protocol"): - fsm_state = "fill_s1ap" - message = pcap_dissected_list[message_index] - message['s1ap_byte_offset'] = 0 - message['dumped'] = [] - message_index += 1 - - elif fsm_state == "fill_s1ap": - if not line.startswith("Frame"): - bytes = line.split(' ') - byte_index = int(bytes[0],16) - if byte_index == message['s1ap_byte_offset']: - message['s1ap_byte_offset'] += 16 - for byte in bytes: - message['dumped'].append(byte) - message['s1ap_byte_offset'] += 1 - else: - fsm_state="search_frame" - -print (" %s " % ( pcap_dissected_list ) ) + if line[:-1]: + cleaned_pdml_string += line + '\r\n' +#print "'%s'" % cleaned_pdml_string +##################### +# write xml string to pdml file +##################### +out_pdml_file_name = os.path.dirname(orig_pcap_file_name) + os.path.splitext(os.path.basename(orig_pcap_file_name))[0] + '.pdml' +out_file = open(out_pdml_file_name, "w") +out_file.write(cleaned_pdml_string) +out_file.close()