def _unittest():
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(name)s %(levelname)s %(message)s')

    from diameter.node import Capability, NodeSettings
    from diameter import ProtocolConstants, Message
    cap = Capability()
    cap.addAuthApp(ProtocolConstants.DIAMETER_APPLICATION_NASREQ)
    settings = NodeSettings("isjsys.int.i1.dk", "i1.dk", 1, cap, 3868,
                            "pythondiameter", 1)

    ssc = SimpleSyncClient(settings, [])
    ssc.start()

    msg = Message()
    msg.hdr.application_id = ProtocolConstants.DIAMETER_APPLICATION_ACCOUNTING
    msg.hdr.command_code = ProtocolConstants.DIAMETER_COMMAND_ACCOUNTING
    msg.hdr.setRequest(True)
    msg.hdr.setProxiable(True)

    answer = ssc.sendRequest(msg)
    assert not answer

    ssc.stop()
    del ssc
Beispiel #2
0
    def do_request(self,
                   message: Message,
                   timeout: Optional[int] = None) -> None:
        """
        Send request message to the client.
        Before sending additional message preparing required.
        """
        deferred = Deferred()
        timeout = timeout or self.request_timeout

        def on_timeout(failure, timeout_):
            logger.error(
                'Request timed out, no response was received '
                'in {timeout} sec',
                timeout=timeout_)
            del self._pending_requests[self._hop_by_hop]

        # noinspection PyTypeChecker
        deferred.addTimeout(timeout, reactor, on_timeout)

        message.hdr.hopbyhop = self._hop_by_hop
        self._hop_by_hop += 1

        request = message.create_request()
        self.transport.write(request)

        self._pending_requests[self._hop_by_hop] = PendingRequest(
            message, deferred)
Beispiel #3
0
 def dataReceived(self, data: bytes):
     self._buff += data
     try:
         while True:
             message = Message(self._dictionary)
             try:
                 message.decode(self._buff)
             except Exception:
                 # Message has been collected in the buffer, but not entirely.
                 # So, end this event handler, and wait for another
                 # data event, with the rest of the message.
                 return
             # noinspection PyAttributeOutsideInit
             self._buff = self._buff[message.hdr.len:]
             self.on_message(message)
     except Exception:
         self.on_error()
Beispiel #4
0
 def do_response(self, message: Message) -> None:
     """
     Send response message to the client.
     Before sending additional message preparing required.
     So, `self.create_response` method do this work.
     """
     logger.debug("Sending response: {message}", message=message)
     response = message.create_answer()
     self.transport.write(response)
Beispiel #5
0
 def test_Message(self):
     m1 = Message()
     assert len(m1)==0
     
     p = xdrlib.Packer()
     m1.encode(p)
     e_sz = len(p.get_buffer())
     assert len(p.get_buffer())==20
     u = xdrlib.Unpacker(p.get_buffer())
     m2 = Message()
     assert m2.decode(u,e_sz)==Message.decode_status_decoded
     
     a = AVP(1,b"hello")
     m1.append(a)
     assert len(m1)==1
     
     p = xdrlib.Packer()
     m1.encode(p)
     e_sz = len(p.get_buffer())
     assert len(p.get_buffer())==36
     
     u = xdrlib.Unpacker(p.get_buffer())
     m2 = Message()
     assert m2.decode(u,e_sz)==Message.decode_status_decoded
     
     #test container+iteration
     m3 = Message()
     m3.append(AVP(1,"user1"))
     m3.append(AVP(1,"user2"))
     assert len(m3)==2
     count=0
     for a in m3:
         count += 1
     assert count==2
     
     #test subset
     m4 = Message()
     m4.append(AVP(1,"user1"))
     m4.append(AVP(1,"user2"))
     m4.append(AVP(2,"foo1"))
     m4.append(AVP(1,"user3"))
     m4.append(AVP(2,"foo1"))
     assert len(m4)==5
     count=0
     for a in m4.subset(1):
         count += 1
     assert count==3
     count=0
     for a in m4.subset(2):
         count += 1
     assert count==2
     count=0
     for a in m4.subset(117):
         count += 1
     assert count==0
     
     #find
     m5 = Message()
     m5.append(AVP(1,"user1"))
     m5.append(AVP(2,"foo1"))
     assert m5.find(1)
     assert m5.find(2)
     assert not m5.find(117)
     
     #decode raw (good)
     raw = binascii.a2b_hex("0100003000000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f31")
     u = xdrlib.Unpacker(raw)
     m6 = Message()
     assert m6.decode(u,len(raw))==Message.decode_status_decoded
     
     #decode raw (short)
     raw = binascii.a2b_hex("0100003000000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f")
     u = xdrlib.Unpacker(raw)
     m7 = Message()
     assert m7.decode(u,len(raw))==Message.decode_status_not_enough
     
     #decode raw (garbage)
     raw = binascii.a2b_hex("0100002900000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f")
     u = xdrlib.Unpacker(raw)
     m7 = Message()
     assert m7.decode(u,len(raw))==Message.decode_status_garbage
     
     #decode raw (garbage) (NUL bytes)
     raw = binascii.a2b_hex("0100000000000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f")
     u = xdrlib.Unpacker(raw)
     m7 = Message()
     assert m7.decode(u,len(raw))==Message.decode_status_garbage
Beispiel #6
0
realm = "our.diameter"


cap = Capability()
cap.addAuthApp(ProtocolConstants.DIAMETER_APPLICATION_COMMON)
cap.addAcctApp(ProtocolConstants.DIAMETER_APPLICATION_COMMON)

cap.addAuthApp(ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL)
cap.addAcctApp(ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL)
settings = NodeSettings(host_id, realm, 9999, cap, 0, "cc_test_client", 1)

ssc = SimpleSyncClient(settings,[Peer(peer,port)])
ssc.start()
ssc.waitForConnection(timeout=3)

req = Message()
# < Diameter Header: 272, REQ, PXY >
req.hdr.command_code = ProtocolConstants.DIAMETER_COMMAND_CC
req.hdr.application_id = ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL
req.hdr.setRequest(True)
req.hdr.setProxiable(True)

# < Session-Id >
req.append(AVP(ProtocolConstants.DI_SESSION_ID,ssc.node.makeNewSessionId()))

# { Origin-Host }
# { Origin-Realm }
ssc.node.addOurHostAndRealm(req)

# { Destination-Realm }
req.append(AVP_UTF8String(ProtocolConstants.DI_DESTINATION_REALM,"example.net"))
Beispiel #7
0
 def test_Utils(self):        
     #Test setMandatory
     avps = []
     avps.append(AVP(1,"*****@*****.**"))
     avps.append(AVP(2,"*****@*****.**"))
     avps.append(AVP(999,"*****@*****.**"))
     avps.append(AVP(1,"*****@*****.**",-1))
     Utils.setMandatory(avps,[1,2])
     assert avps[0].isMandatory()
     assert avps[1].isMandatory()
     assert not avps[2].isMandatory()
     assert not avps[3].isMandatory()
     
     msg = Message()
     msg.append(AVP(1,"*****@*****.**"))
     msg.append(AVP(2,"*****@*****.**"))
     msg.append(AVP(999,"*****@*****.**"))
     msg.append(AVP(1,"*****@*****.**",-1))
     Utils.setMandatory(msg,[1,2])
     assert msg[0].isMandatory()
     assert msg[1].isMandatory()
     assert not msg[2].isMandatory()
     assert not msg[3].isMandatory()
     
     avps = []
     avps.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"*****@*****.**"))
     avps.append(AVP(999,"*****@*****.**"))
     Utils.setMandatory_RFC3588(avps)
     assert avps[0].isMandatory()
     assert not avps[1].isMandatory()
     
     #test copyProxyInfo
     src_msg = Message()
     src_msg.append(AVP(1,"*****@*****.**"))
     src_msg.append(AVP_OctetString(ProtocolConstants.DI_PROXY_INFO,"fnox"))
     src_msg.append(AVP(999,"*****@*****.**",-1))
     src_msg.append(AVP_OctetString(ProtocolConstants.DI_PROXY_INFO,"mox"))
     dst_msg = Message()
     Utils.copyProxyInfo(src_msg,dst_msg)
     assert len(dst_msg)==2
     assert src_msg[1].code==dst_msg[0].code
     assert src_msg[1].payload==dst_msg[0].payload
     assert src_msg[3].code==dst_msg[1].code
     assert src_msg[3].payload==dst_msg[1].payload
     
     #Test the ABNF checker
     #create a conforming DPR
     msg = Message()
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd"))
     rc = Utils.checkABNF(msg,Utils.abnf_dpr)
     assert not rc
     #create a DPR with a missing AVP
     msg = Message()
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     #msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd"))
     rc = Utils.checkABNF(msg,Utils.abnf_dpr)
     assert rc
     assert rc[1]==ProtocolConstants.DIAMETER_RESULT_MISSING_AVP
     #create a DPR with a duplicated AVP
     msg = Message()
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd"))
     rc = Utils.checkABNF(msg,Utils.abnf_dpr)
     assert rc
     assert rc[1]==ProtocolConstants.DIAMETER_RESULT_AVP_OCCURS_TOO_MANY_TIMES
     assert rc[0]
     #create a DPR with a an extra AVP
     msg = Message()
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd"))
     msg.append(AVP(999,"A value"))
     rc = Utils.checkABNF(msg,Utils.abnf_dpr)
     assert rc
     assert rc[1]==ProtocolConstants.DIAMETER_RESULT_AVP_NOT_ALLOWED
     assert rc[0]
     #create a conforming ASR 
     msg = Message()
     msg.append(AVP(ProtocolConstants.DI_SESSION_ID,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DESTINATION_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DESTINATION_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_AUTH_APPLICATION_ID,"A value"))
     rc = Utils.checkABNF(msg,Utils.abnf_asr)
     assert not rc
     #create a ASR with session-id the wrong palce
     msg = Message()
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_SESSION_ID,"A value"))
     msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DESTINATION_REALM,"A value"))
     msg.append(AVP(ProtocolConstants.DI_DESTINATION_HOST,"A value"))
     msg.append(AVP(ProtocolConstants.DI_AUTH_APPLICATION_ID,"A value"))
     rc = Utils.checkABNF(msg,Utils.abnf_asr)
     assert rc
     assert rc[0]
Beispiel #8
0
 def init_request(self, command: str, app_id: str) -> Message:
     """Init creation request message."""
     message = Message(self._dictionary)
     message.new(command, app_id)
     return message