File: dispatch.py

package info (click to toggle)
python-pysnmp4 7.1.22-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,504 kB
  • sloc: python: 33,673; makefile: 169; javascript: 4
file content (231 lines) | stat: -rw-r--r-- 7,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
# License: https://www.pysnmp.com/pysnmp/license.html
#
import warnings
from time import time
from typing import Any

from pyasn1.codec.ber import decoder, encoder
from pysnmp import debug
from pysnmp.carrier.base import AbstractTransportDispatcher
from pysnmp.entity.engine import SnmpEngine
from pysnmp.hlapi.transport import AbstractTransportTarget
from pysnmp.proto import api
from pysnmp.proto import errind, error
from pysnmp.proto.api import verdec

__all__ = []


class AbstractSnmpDispatcher:
    """Creates SNMP message dispatcher object.

    `SnmpDispatcher` object manages send and receives SNMP PDU
    messages through underlying transport dispatcher and dispatches
    them to the callers.

    `SnmpDispatcher` is the only stateful object, all `hlapi.v1arch` SNMP
    operations require an instance of `SnmpDispatcher`. Users do not normally
    request services directly from `SnmpDispather`, but pass it around to
    other `hlapi.v1arch` interfaces.

    It is possible to run multiple instances of `SnmpDispatcher` in the
    application. In a multithreaded environment, each thread that
    works with SNMP must have its own `SnmpDispatcher` instance.
    """

    PROTO_DISPATCHER = None
    transport_dispatcher: AbstractTransportDispatcher
    cache: dict[str, Any]

    def __init__(self, transportDispatcher: AbstractTransportDispatcher = None):  # type: ignore
        if transportDispatcher:
            self.transport_dispatcher = transportDispatcher

        else:
            self.transport_dispatcher = self.PROTO_DISPATCHER()

        self._automaticDispatcher = transportDispatcher is not self.transport_dispatcher
        self._configuredTransports = set()

        self._pendingReqs = {}

        self.transport_dispatcher.register_recv_callback(self._recv_callback)
        self.transport_dispatcher.register_timer_callback(self._timer_callback)

        self.cache = {}

    def __repr__(self):
        return f"{self.__class__.__name__}(transportDispatcher={self.transport_dispatcher})"

    def close(self):
        self.transport_dispatcher.unregister_recv_callback()
        self.transport_dispatcher.unregister_timer_callback()
        if self._automaticDispatcher:
            self.transport_dispatcher.close_dispatcher()

        for requestId, stateInfo in self._pendingReqs.items():
            cbFun = stateInfo["cbFun"]
            cbCtx = stateInfo["cbCtx"]

            if cbFun:
                cbFun(self, "Request #%d terminated" % requestId, None, cbCtx)

        self._pendingReqs.clear()

    def send_pdu(
        self,
        authData,
        transportTarget: AbstractTransportTarget,
        reqPdu,
        cbFun=None,
        cbCtx=None,
    ):
        if (
            self._automaticDispatcher
            and transportTarget.TRANSPORT_DOMAIN not in self._configuredTransports
        ):
            self.transport_dispatcher.register_transport(
                transportTarget.TRANSPORT_DOMAIN,
                transportTarget.PROTO_TRANSPORT().open_client_mode(
                    transportTarget.iface
                ),
            )
            self._configuredTransports.add(transportTarget.TRANSPORT_DOMAIN)

        pMod = api.PROTOCOL_MODULES[authData.mpModel]

        reqMsg = pMod.Message()
        pMod.apiMessage.set_defaults(reqMsg)
        pMod.apiMessage.set_community(reqMsg, authData.communityName)
        pMod.apiMessage.set_pdu(reqMsg, reqPdu)

        outgoingMsg = encoder.encode(reqMsg)

        requestId = pMod.apiPDU.get_request_id(reqPdu)

        self._pendingReqs[requestId] = dict(
            outgoingMsg=outgoingMsg,
            transportTarget=transportTarget,
            cbFun=cbFun,
            cbCtx=cbCtx,
            timestamp=time() + transportTarget.timeout,
            retries=0,
        )

        self.transport_dispatcher.send_message(
            outgoingMsg,
            transportTarget.TRANSPORT_DOMAIN,
            transportTarget.transport_address,
        )

        if reqPdu.__class__ is getattr(
            pMod, "SNMPv2TrapPDU", None
        ) or reqPdu.__class__ is getattr(pMod, "TrapPDU", None):
            return requestId

        self.transport_dispatcher.job_started(id(self))

        return requestId

    def _recv_callback(
        self, snmpEngine: SnmpEngine, transportDomain, transportAddress, wholeMsg
    ):
        try:
            mpModel = verdec.decode_message_version(wholeMsg)

        except error.ProtocolError:
            return  # n.b the whole buffer gets dropped

        debug.logger & debug.FLAG_DSP and debug.logger(
            "receiveMessage: msgVersion %s, msg decoded" % mpModel
        )

        pMod = api.PROTOCOL_MODULES[mpModel]

        while wholeMsg:
            rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=pMod.Message())
            rspPdu = pMod.apiMessage.get_pdu(rspMsg)

            requestId = pMod.apiPDU.get_request_id(rspPdu)

            try:
                stateInfo = self._pendingReqs.pop(requestId)

            except KeyError:
                continue

            self.transport_dispatcher.job_finished(id(self))

            cbFun = stateInfo["cbFun"]
            cbCtx = stateInfo["cbCtx"]

            if cbFun:
                cbFun(self, requestId, None, rspPdu, cbCtx)

        return wholeMsg

    def _timer_callback(self, timeNow):
        for requestId, stateInfo in tuple(self._pendingReqs.items()):
            if stateInfo["timestamp"] > timeNow:
                continue

            retries = stateInfo["retries"]
            transportTarget: AbstractTransportTarget = stateInfo["transportTarget"]

            if retries == transportTarget.retries:
                cbFun = stateInfo["cbFun"]
                cbCtx = stateInfo["cbCtx"]

                if cbFun:
                    del self._pendingReqs[requestId]
                    cbFun(
                        self,
                        requestId,
                        errind.requestTimedOut,
                        None,
                        cbCtx,
                    )
                    self.transport_dispatcher.job_finished(id(self))
                    continue

            stateInfo["retries"] += 1
            stateInfo["timestamp"] = timeNow + transportTarget.timeout

            outgoingMsg = stateInfo["outgoingMsg"]

            self.transport_dispatcher.send_message(
                outgoingMsg,
                transportTarget.TRANSPORT_DOMAIN,
                transportTarget.transport_address,
            )

    # compatibility with legacy code
    # Old to new attribute mapping
    deprecated_attributes = {
        "transportDispatcher": "transport_dispatcher",
        "sendPdu": "send_pdu",
    }

    def __getattr__(self, attr: str):
        if new_attr := self.deprecated_attributes.get(attr):
            warnings.warn(
                f"{attr} is deprecated. Please use {new_attr} instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            return getattr(self, new_attr)
        raise AttributeError(
            f"'{self.__class__.__name__}' object has no attribute '{attr}'"
        )

    def __enter__(self):
        """Open the SNMP dispatcher."""
        return self

    def __exit__(self, *args, **kwargs):
        """Close the SNMP dispatcher."""
        self.close()