o
    B]7E                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZmZ d d	lmZ d d
lmZ edZG dd deZdd ZdddedfddZee_dS )    N)null)config)rfc2576)rfc3411)v2c)errinderror)viewrfc1902)nextid)debugic                   @   s@   e Zd ZdZdd Zdd Z	dddZd	d
 Z	dddZdS )NotificationOriginator   c                 K   s$   i | _ i | _|dd | _|| _d S )NsnmpContext)$_NotificationOriginator__pendingReqs-_NotificationOriginator__pendingNotificationspopr    _NotificationOriginator__options)selfoptions r   >/usr/lib/python3/dist-packages/pysnmp/entity/rfc3413/ntforg.py__init__   s   
zNotificationOriginator.__init__c                  C   s  |\}}}|| j vrtd| | j |\}}}}}}}}}}}}}|jt|  |
r<tjtj	@ o>td|||
f  |
d }|t
jt
jfv rR|d7 }d}nd}|d7 }||kse|| jddkrtjtj	@ otd|||f  ||||d | d S  ||||d | d S t|d	 |j  }|dkrt|}d}n|}d}z|j|||||||||||d
|| j|||f}W n8 tjy   t d }
tjtj	@ otd||
f  ||||
d d | Y d S  ||||
d d | Y d S w |jt|  tjtj	@ o'td|||||f  |||||||||||||f| j |< d S  |||||||||||||f| j |< d S |dkrGt|	|}	|||d |	| d S )NzMissing sendPduHandle %szOprocessResponsePdu: sendRequestHandle %s, sendPduHandle %s statusInformation %serrorIndication   r   ZdiscoveryRetries   zRprocessResponsePdu: sendRequestHandle %s, sendPduHandle %s retry count %d exceededd   TzCprocessResponsePdu: sendRequestHandle %s: sendPdu() failed with %r zVprocessResponsePdu: sendRequestHandle %s, sendPduHandle %s, timeout %d, retry %d of %d)r   r   ProtocolErrorr   transportDispatcherZjobFinishedidr   loggerflagAppr   ZnotInTimeWindowZunknownEngineIDr   getfloatgetTimerResolutionr   v2ToV1msgAndPduDspsendPduprocessResponsePduStatusInformationsysexc_info
jobStartedZv1ToV2) r   
snmpEnginemessageProcessingModelsecurityModelsecurityNamesecurityLevelcontextEngineIdcontextName
pduVersionZPDUstatusInformationsendPduHandleZcbInfosendRequestHandlecbFuncbCtxZorigTransportDomainZorigTransportAddressZorigMessageProcessingModelZorigSecurityModelZorigSecurityNameZorigSecurityLevelZorigContextEngineIdZorigContextNameZorigPduZorigTimeoutZorigRetryCountZorigRetriesZorigDiscoveryRetriesr   timeoutInTicksreqPDUr   r   r   r(      s   






	



z)NotificationOriginator.processResponsePduNc                 C   sr  t ||\}}	}
}}t ||\}}}}|dkr!t|}d}n|}d}|jtjv rt|
d |j	
  }t }|j|||	||||||||d|| j|||f}tjtj@ o|td||
f  ||	||||||||
|ddf| j|< |j	t|  |S  ||	||||||||
|ddf| j|< |j	t|  |S |j|||	||||||||d d }tjtj@ otd |S )Nr   r   r   Tz%sendPdu: sendPduHandle %s, timeout %dFzsendPdu: message sent)r   getTargetAddrgetTargetParamsr   r%   ZtagSetr   ZconfirmedClassPDUsr#   r   r$   getNextHandler&   r'   r(   r   r    r!   r   r,   r   )r   r-   Z
targetNamer2   r3   pdur8   r9   transportDomaintransportAddresstimeout
retryCountparamsr.   r/   r0   r1   r;   r4   r:   r7   r6   r   r   r   r'   z   s^   





zNotificationOriginator.sendPduc              	   C   s   |\}}}| j | | tjtj@ o td|||| j | f  | j | s_tjtj@ o4td||f  | j |= |||||rEtj|pFd|rPtjj|ddpQd|rYtj	|pZd| d S d S )NzmprocessResponseVarBinds: notificationHandle %s, sendRequestHandle %s, errorIndication %s, pending requests %szQprocessResponseVarBinds: notificationHandle %s, sendRequestHandle %s -- completedr   T)Z
muteErrorsr   )
r   remover   r    r!   r   apiPDUZgetErrorStatusZgetErrorIndexZgetVarBinds)r   r-   r7   r   r?   r9   notificationHandler8   r   r   r   processResponseVarBinds   s.   

z.NotificationOriginator.processResponseVarBindsr   c                 C   s2  t jt j@ ot d||pd||f  |r#|jjjdd\}||}t||\}	}
t	 }t jt j@ o=t d||	|
f  dd |D }|jjjdd	d
\}}t
t|D ]E}|r|| d | kr|d d | krv|| |d< n|d||  ||= |d d | kr|dt| |  f qWt|dk s|d d | kr|dt| | f d}t jt j@ ot d|f  t||	D ]0}t||\}}}}}t||\}}}}t jt j@ ot d||	|||||f  |D ]U\}}||j|jfv rq z"|j| j ||||d|| t jt j@ o.t d||f  W q  tjyU   t jt j@ oOt d||f  Y   d S  Y   d S w |
dkr`t }n|
dkrjt }ntd|
tj| tj || z| !|||||| j"|||f}W nJ tjy   t#$ d }t jt j@ ot d||f  || j%vs| j%| s|| j%v r| j%|= |r||||d ddd| | Y   S w t jt j@ ot d|||f  |
dkr|| j%vrt& | j%|< | j%| '| qt jt j@ ot d||f  |S  |S )NzVsendVarBinds: notificationTarget %s, contextEngineId %s, contextName "%s", varBinds %sz	<default>zSNMP-FRAMEWORK-MIBZSnmpAdminStringz@sendVarBinds: notificationHandle %s, notifyTag %s, notifyType %sc                 S   s   g | ]\}}t ||fqS r   )r   ObjectIdentifier).0xyr   r   r   
<listcomp>   s    z7NotificationOriginator.sendVarBinds.<locals>.<listcomp>z__SNMPv2-MIBsnmpTrapOID	sysUpTimer      r   zsendVarBinds: final varBinds %szsendVarBinds: notificationHandle %s, notifyTag %s yields: transportDomain %s, transportAddress %r, securityModel %s, securityName %s, securityLevel %sZnotifyz6sendVarBinds: ACL succeeded for OID %s securityName %szRsendVarBinds: ACL denied access for OID %s securityName %s, droppping notificationzUnknown notify-type %rz<sendVarBinds: sendRequestHandle %s: sendPdu() failed with %rr   r   zEsendVarBinds: notificationHandle %s, sendRequestHandle %s, timeout %dzOsendVarBinds: notificationHandle %s, sendRequestHandle %s, notification(s) sent)(r   r    r!   r&   mibInstrumControllerZ
mibBuilderZimportSymbolsr   ZgetNotificationInfor>   rangelenZgetNameinsertr   rI   Z	getSyntaxZcloneZgetTargetNamesr<   r=   nameZaccessControlModelacmIDZisAccessAllowedr   r)   ZSNMPv2TrapPDUZInformRequestPDUr   rF   ZsetDefaultsZsetVarBindsr'   rH   r*   r+   r   setadd)r   r-   notificationTargetr2   r3   varBindsr8   r9   Z(_NotificationOriginator__SnmpAdminStringZ	notifyTagZ
notifyTyperG   rN   rO   idxr7   ZtargetAddrNamer@   rA   rB   rC   rD   r.   r/   r0   r1   ZvarNameZvarValr?   r5   r   r   r   sendVarBinds   s  

 
	








z#NotificationOriginator.sendVarBinds)NN)r   NN)	__name__
__module____qualname__rW   r   r(   r'   rH   r]   r   r   r   r   r      s    ]
8r   c                 C   sB   |\}}z||||||| W d S  t y    |||| Y d S w )N)	TypeError)r-   r7   r   ZerrorStatusZ
errorIndexr[   r9   r8   r   r   r   _sendNotificationCbFunU  s   
rb   r   c	              
   C   s   | j d u r
td|d}	|	st| }	|j|	d t|t	t
fr4|r4t|d tr4tj| }nt|}tj||d}
|
|	 | j |}|
d d ||
dd   }
| ||| j j||
t
| t||fS )NzSNMP context not specifiedmibViewController)rc   r   )instanceIndexr   )r   r   r   ZgetUserContextr	   ZMibViewControllerZgetMibBuilderZsetUserContext
isinstancetupleliststrr
   ZObjectIdentityZNotificationTypeZresolveWithMibZgetMibInstrumZreadVarsr]   r2   rb   )r   r-   rZ   ZnotificationNameZadditionalVarBindsr8   r9   r3   rd   rc   r[   rR   r   r   r   _sendNotificationb  s0   




ri   )r*   Zpyasn1.compat.octetsr   Zpysnmp.entity.rfc3413r   Zpysnmp.proto.proxyr   Zpysnmp.protor   Zpysnmp.proto.apir   r   r   Z
pysnmp.smir	   r
   Zpysnmpr   r   ZIntegerr>   objectr   rb   ri   ZsendNotificationr   r   r   r   <module>   s&   
  B

)