o
    B]                      @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ dgZG dd deZdS )    N)str2octs)MsgAndPduDispatcher)SnmpV1MessageProcessingModelSnmpV2cMessageProcessingModel)SnmpV3MessageProcessingModel)SnmpV1SecurityModelSnmpV2cSecurityModel)SnmpUSMSecurityModel)rfc3415void)observer)debug)error
SnmpEnginec                   @   sj   e Zd ZdZ		dddZdd Zdd	 Zd
d ZdddZdddZ	dd Z
dd Zdd Zdd ZdS )r   ar  Creates SNMP engine object.

    SNMP engine object is central in SNMP v3 architecture. It is an umbrella
    object that coordinates interactions between all parts of SNMP v3 system.
    See :RFC:`3412#section-2.1` (where it is termed *The Dispatcher*).

    With PySNMP design, `SnmpEngine` is the only stateful object, all SNMP
    v3 operations require an instance of SNMP engine. Users do not normally
    request services directly from `SnmpEngine`, but pass it around to
    other PySNMP interfaces.

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

    Parameters
    ----------
    snmpEngineID : :py:class:`~pysnmp.proto.rfc1902.OctetString`
        Unique and unambiguous identifier of an SNMP engine.
        If not given, `snmpEngineID` is autogenerated and stored on
        the filesystem. See :RFC:`3411#section-3.1.1`  for details.

    Examples
    --------
    >>> SnmpEngine()
    SnmpEngine(snmpEngineID=OctetString(hexValue='0x80004fb80567726f6d6d69742'))
    >>>

    N  c                 C   s  i | _ t | _|d u rt | _n|| _tjt tjt tjt i| _	t
jt
 tjt tjt i| _tjjt tjjt i| _d | _| jjd u rOtd| jjjdd\}|j||_| jjjdd\}| jd7  _| jjjdd\}|d u r|j| _d S |j||_|j| _tjtj@ otd| j    t!j"#t$% d| j  }tjtj@ otd	|  t!j"&|szt!'| W n
 t(y   Y d S w t!j"#|d
}z|jt)|* |_W n	 t+y   Y nw z	| jd7  _W n t+y
   |jd|_Y nw z t$j,|d\}	}
t!-|	t.|j   t!/|	 t01|
| W n! t+yL   tjtj@ oHtdt23 d   Y d S  Y d S w tjtj@ o]td|j    d S )Nz&MIB instrumentation does not yet existz__SNMP-FRAMEWORK-MIBsnmpEngineMaxMessageSizesnmpEngineBoots   snmpEngineIDz+SnmpEngine: using custom SNMP Engine ID: %sZ__pysnmpz*SnmpEngine: using persistent directory: %sZboots)dirz2SnmpEngine: could not stored SNMP Engine Boots: %sz(SnmpEngine: stored SNMP Engine Boots: %s)4cacher   ZMetaObserverr   msgAndPduDspr   ZmessageProcessingModelIDr   r   messageProcessingSubsystemsr   ZsecurityModelIDr   r	   securityModelsr   ZVacmZaccessModelIDr
   ZaccessControlModeltransportDispatchermibInstrumControllerr   PySnmpError
mibBuilderZimportSymbolsZsyntaxZcloner   r   loggerZflagAppZprettyPrintospathjointempfileZ
gettempdirexistsmakedirsOSErroropenread	ExceptionZmkstempwriter   closeshutilmovesysexc_info)selfr   ZmaxMessageSizer   r   r   ZorigSnmpEngineIDZpersistentPathffdfn r3   6/usr/lib/python3/dist-packages/pysnmp/entity/engine.py__init__8   s   

	

 zSnmpEngine.__init__c                 C   s   d| j j| jf S )Nz%s(snmpEngineID=%r))	__class____name__r   r/   r3   r3   r4   __repr__   s   zSnmpEngine.__repr__c                 C   s   | j | ||| d S N)r   ZreceiveMessage)r/   r   ZtransportDomainZtransportAddressZwholeMsgr3   r3   r4   Z__receiveMessageCbFun   s   z SnmpEngine.__receiveMessageCbFunc                 C   sJ   | j | | | j D ]}|| | q| j D ]}|| | qd S r:   )r   ZreceiveTimerTickr   valuesr   )r/   ZtimeNowZ	mpHandlerZ	smHandlerr3   r3   r4   Z__receiveTimerTickCbFun   s   z"SnmpEngine.__receiveTimerTickCbFunc                 C   sP   | j d ur| j |urtd|| j| | j d u r&|| j || _ d S d S )Nz'Transport dispatcher already registered)r   r   r   ZregisterRecvCbFun _SnmpEngine__receiveMessageCbFunZregisterTimerCbFun"_SnmpEngine__receiveTimerTickCbFun)r/   r   recvIdr3   r3   r4   registerTransportDispatcher   s   


z&SnmpEngine.registerTransportDispatcherc                 C   s4   | j d u r
td| j | | j   d | _ d S )Nz#Transport dispatcher not registered)r   r   r   ZunregisterRecvCbFunZunregisterTimerCbFun)r/   r>   r3   r3   r4   unregisterTransportDispatcher   s   


z(SnmpEngine.unregisterTransportDispatcherc                 C   s
   | j jjS r:   )r   r   r   r8   r3   r3   r4   getMibBuilder   s   
zSnmpEngine.getMibBuilderc                    s"   | j t fdd D  d S )Nc                    s   g | ]
}d |  | fqS )__%sr3   ).0kkwargsr3   r4   
<listcomp>   s    z-SnmpEngine.setUserContext.<locals>.<listcomp>)r   updatedict)r/   rF   r3   rE   r4   setUserContext   s   zSnmpEngine.setUserContextc                 C   s   | j d| S NrB   )r   getr/   argr3   r3   r4   getUserContext   s   zSnmpEngine.getUserContextc                 C   s(   z	| j d| = W d S  ty   Y d S w rK   )r   KeyErrorrM   r3   r3   r4   delUserContext   s
   zSnmpEngine.delUserContext)Nr   Nr:   )r7   
__module____qualname____doc__r5   r9   r<   r=   r?   r@   rA   rJ   rO   rQ   r3   r3   r3   r4   r      s    
X

	)r   r+   r-   r"   Zpyasn1.compat.octetsr   Zpysnmp.proto.rfc3412r   Zpysnmp.proto.mpmod.rfc2576r   r   Zpysnmp.proto.mpmod.rfc3412r   Zpysnmp.proto.secmod.rfc2576r   r   Zpysnmp.proto.secmod.rfc3414r	   Zpysnmp.proto.acmodr
   r   Zpysnmp.entityr   Zpysnmpr   r   __all__objectr   r3   r3   r3   r4   <module>   s    