Example #1
0
    def runProxy(self):
        size = 5120
        running_inputList = [self.sut_socket]
        running_outputList = []
        global output
        global outFlag
        global PROTOCOLMESSAGE

        sut_address = (self.sut_ip, self.sut_port)

        running = 1
        sut_diam_conn_socket = 1

        while running:
            try:
                if PROTOCOLMESSAGE:
                    if self.type_of_proxy == "diameter":
                        igniteLogger.logger.info(
                            "Diameter data from Test-Case being sent to SUT : "
                            f"{PROTOCOLMESSAGE}")
                        # Encode the message
                        length, e_data = diameterEncoder.encodeDiameter(
                            PROTOCOLMESSAGE["diameter"]["msg"])
                        PROTOCOLMESSAGE = None
                        igniteLogger.logger.info("Encoded Diameter data: "
                                                 f"{e_data[0:length]}")
                        sut_diam_conn_socket.send(e_data[0:length])

                    elif self.type_of_proxy == "gtp":
                        # Encode the message
                        igniteLogger.logger.info(
                            "GTP data from Test-Case being sent to SUT : "
                            f"{PROTOCOLMESSAGE}")
                        e_data, length = gtpEncoder.encodeGTP(
                            PROTOCOLMESSAGE["gtpv2"]["msg"], ieLocation)
                        PROTOCOLMESSAGE = None
                        igniteLogger.logger.info("Encoded GTP data: "
                                                 f"{e_data[0:length]}")
                        self.sut_socket.sendto(e_data[0:length], sut_address)

                    elif self.type_of_proxy == "s1ap":
                        # Encode the message
                        igniteLogger.logger.info(
                            "S1AP data from Test-Case being sent to SUT : "
                            f"{PROTOCOLMESSAGE}")
                        nas_data = PROTOCOLMESSAGE["NAS-MESSAGE"]
                        e_data = s1apEncoder.s1apEncoding(
                            PROTOCOLMESSAGE, asn1_obj_encoder, nas_data)
                        PROTOCOLMESSAGE = None
                        e_byte = bytearray.fromhex(e_data)
                        igniteLogger.logger.info("Encoded S1AP data: "
                                                 f"{e_byte}")
                        self.sut_socket.send(e_byte)

                input_ready, output_ready, except_ready = select.select(
                    running_inputList, running_outputList, [], 0.0005)
                if input_ready:
                    print("input ready:", input_ready)
                    for sock in input_ready:
                        if sock == self.sut_socket:
                            if self.type_of_proxy == "diameter":
                                print(self.sut_socket)
                                sut_diam_conn_socket, sut_address = self.sut_socket.accept(
                                )
                                sut_diam_conn_socket.setblocking(0)
                                running_inputList.append(sut_diam_conn_socket)

                            elif (self.type_of_proxy
                                  == "gtp") or (self.type_of_proxy == "s1ap"):
                                sut_data = self.sut_socket.recv(size)
                                if sut_data:
                                    if self.type_of_proxy == "s1ap":
                                        igniteLogger.logger.info(
                                            "S1AP Data from SUT to Test-Case: "
                                            f"{hexlify(sut_data).decode('utf-8')}"
                                        )
                                        decoded_msg = s1apDecoder.Decoding(
                                            hexlify(sut_data).decode("utf-8"),
                                            asn1_obj_decoder)
                                        msg = list(decoded_msg)
                                        decoded_msg = {}
                                        decoded_msg['S1AP-PDU'] = msg
                                        igniteLogger.logger.info(
                                            "Decoded S1AP data: "
                                            f"{decoded_msg}")

                                    elif self.type_of_proxy == "gtp":
                                        igniteLogger.logger.info(
                                            "GTP Data from SUT to Test-Case: "
                                            f"{hexlify(sut_data).decode('utf-8')}"
                                        )
                                        decoded_msg, length = gtpDecoder.decodeGTP(
                                            sut_data, self.ieLocation)
                                        igniteLogger.logger.info(
                                            "Decoded GTP data: "
                                            f"{decoded_msg}")

                                    msg_buf = json.dumps(decoded_msg)
                                    output.append(msg_buf)
                                    outFlag = True

                        elif sock == sut_diam_conn_socket:
                            data = sut_diam_conn_socket.recv(size)
                            if data:
                                length, msg = diameterDecoder.decodeDiameter(
                                    data)
                                if du.get(msg, "diameter",
                                          "command-code") == 257:
                                    igniteLogger.logger.info(
                                        "Received CER Diameter Packet with command-code : 257"
                                    )
                                    igniteLogger.logger.info(
                                        "The data from diamSocket : "
                                        f"{hexlify(data)}")
                                    igniteLogger.logger.info(
                                        f"The data to diamSocket : f{msg}")
                                    igniteLogger.logger.info(
                                        "Send CEA Diameter Packet with command-code: 257"
                                    )
                                    msg_heirarchy, msg_data = igniteCommonUtil.loadMessageData(
                                        "../../../Test/MessageTemplates/Diameter/cea.json"
                                    )
                                    origin_state_id = du.get(
                                        msg, "diameter", "origin-state-id")
                                    hbh = du.get(msg, "diameter",
                                                 "hop-by-hop-identifier")
                                    ete = du.get(msg, "diameter",
                                                 "end-to-end-identifier")
                                    msg_data = du.replace(
                                        msg_data, "diameter",
                                        "hop-by-hop-identifier", hbh)
                                    msg_data = du.replace(
                                        msg_data, "diameter",
                                        "end-to-end-identifier", ete)
                                    msg_data = du.replace(
                                        msg_data, "diameter",
                                        "origin-state-id", origin_state_id)
                                    msg_data = du.replace(
                                        msg_data, "diameter",
                                        "host-ip-address", self.ignite_ip)
                                    msg_data = du.replace(
                                        msg_data, "diameter", "origin-host",
                                        config_file["diameter"]["ignite_host"])
                                    msg_data = du.replace(
                                        msg_data, "diameter", "origin-realm",
                                        config_file["diameter"]
                                        ["ignite_realm"])
                                    igniteLogger.logger.info("The msg_data : "
                                                             f"{msg_data}")
                                    self.sendDiamDataToSut(
                                        sut_diam_conn_socket, msg_data)

                                elif du.get(msg, "diameter",
                                            "command-code") == 280:
                                    igniteLogger.logger.info(
                                        "Received CER Diameter Packet with command-code : 280"
                                    )
                                    igniteLogger.logger.info(
                                        "The data from diamSocket : "
                                        f"{hexlify(data)}")
                                    igniteLogger.logger.info(
                                        f"The data to diamSocket : f{msg}")
                                    igniteLogger.logger.info(
                                        "Send CEA Diameter Packet with command-code: 280"
                                    )
                                    msg_heirarchy, msg_data = igniteCommonUtil.loadMessageData(
                                        "../../../Test/MessageTemplates/Diameter/dwa.json"
                                    )
                                    hbh = du.get(msg, "diameter",
                                                 "hop-by-hop-identifier")
                                    ete = du.get(msg, "diameter",
                                                 "end-to-end-identifier")
                                    msg_data = du.replace(
                                        msg_data, "diameter",
                                        "hop-by-hop-identifier", hbh)
                                    msg_data = du.replace(
                                        msg_data, "diameter",
                                        "end-to-end-identifier", ete)
                                    msg_data = du.replace(
                                        msg_data, "diameter", "origin-host",
                                        config_file["diameter"]["ignite_host"])
                                    msg_data = du.replace(
                                        msg_data, "diameter", "origin-realm",
                                        config_file["diameter"]
                                        ["ignite_realm"])
                                    igniteLogger.logger.info("The msg_data : "
                                                             f"{msg_data}")
                                    self.sendDiamDataToSut(
                                        sut_diam_conn_socket, msg_data)

                                else:
                                    igniteLogger.logger.info(
                                        "Diameter Data from SUT sent to Test-Case: "
                                        f"{hexlify(data)}")
                                    length, decoded_msg = diameterDecoder.decodeDiameter(
                                        igniteCommonUtil.
                                        convertBytesToIntArray(hexlify(data)))
                                    data = None
                                    igniteLogger.logger.info(
                                        "Decoded Diameter Data: "
                                        f"{decoded_msg}")
                                    msg_buf = json.dumps(decoded_msg)
                                    output.append(msg_buf)
                                    outFlag = True

                else:
                    continue

            except Exception as e:
                if PROTOCOLMESSAGE != None:
                    PROTOCOLMESSAGE = None
                if sut_diam_conn_socket in running_inputList:
                    running_inputList.remove(sut_diam_conn_socket)
                    sut_diam_conn_socket.close()
                igniteLogger.logger.info("Printing exception : " f"{e}")

            except KeyboardInterrupt:
                igniteLogger.logger.error(
                    "keyboard interrupt, closing Sockets")
                self.sut_socket.close()
            except:
                continue
Example #2
0
 def sendDiamDataToSut(self, sut_diam_conn_socket, msg_data):
     length, e_data = diameterEncoder.encodeDiameter(
         msg_data["diameter"]["msg"])
     sut_diam_conn_socket.send(e_data[0:length])