o
    B]:                     @   s   d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlZd dlmZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZdS )    N)rfc1902rfc1905rfc3411errinderror)v2c)rfc2576)debugc                   @   sx   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZeZ	dd Z
ejjZejjZejjZejjZdd Zdd Zdd ZdS )CommandResponderBase    c                 C   s&   |j |j| j| j || _i | _d S N)msgAndPduDspZregisterContextEngineIdcontextEngineIdpduTypes
processPdusnmpContext"_CommandResponderBase__pendingReqs)self
snmpEnginer   r   r   >/usr/lib/python3/dist-packages/pysnmp/entity/rfc3413/cmdrsp.py__init__   s
   
zCommandResponderBase.__init__c                 C   s   d S r   r   )r   r   stateReferencecontextNamePDUacInfor   r   r   handleMgmtOperation   s   z(CommandResponderBase.handleMgmtOperationc                 C   s$   |j | jj| j d  | _| _d S r   )r   ZunregisterContextEngineIdr   r   r   r   )r   r   r   r   r   close   s   
zCommandResponderBase.closec                 C   s   | j | \}}}}	}
}}}}}}tj|| tj|| tj|| tjtj@ o?td||||f  | 	||| d S  | 	||| d S )NzKsendVarBinds: stateReference %s, errorStatus %s, errorIndex %s, varBinds %s)
r   r   apiPDUZsetErrorStatusZsetErrorIndexZsetVarBindsr	   loggerflagAppsendPdu)r   r   r   errorStatus
errorIndexvarBindsmessageProcessingModelsecurityModelsecurityNamesecurityLevelr   r   
pduVersionr   origPdumaxSizeResponseScopedPDUstatusInformationr   r   r   sendVarBinds%   s    z!CommandResponderBase.sendVarBindsc                 C   s   | j | \}}}}}}	}
}}}}|dkrt||}z|j|||||||	|
|||| W d S  tjyr   tjtj	@ o]td|t
 d f  |jjjdd\}| jd7  _Y d S  |jjjdd\}| jd7  _Y d S w )Nr   z0sendPdu: stateReference %s, statusInformation %s   z__SNMPv2-MIBsnmpSilentDrops)r   r   Zv2ToV1r   ZreturnResponsePdur   StatusInformationr	   r   r    sysexc_infomibInstrumController
mibBuilderimportSymbolssyntax)r   r   r   r   r%   r&   r'   r(   r   r   r)   _r*   r+   r,   r/   r   r   r   r!   :   sH   
zCommandResponderBase.sendPduc                 C   s   || j v r| j |= d S d S r   )r   )r   r   r   r   r   releaseStateInformationa   s   
z,CommandResponderBase.releaseStateInformationc                 C   s  |dkr|	}t |	}	nd }|	jtjvr"|	jtjvr"td|	j tj	
|	}i }||||||||||
|f| j|< tj	|	}d\}}tjtj@ oRtd||f  z| ||||	| j|f W d S  tjjjy   t d }tjtj@ otd||f  d|v r|d |d< |d |d< Y n tjjjy   d	\}}g }Y n tjjjy   d
t d d d }}Y n tjjjy   dt d d d }}Y n tjjjy   dt d d d }}Y n tjjjy   dt d d d }}Y nq tjjjy%   dt d d d }}Y nW tjjjy?   dt d d d }}Y n= tjjjyY   dt d d d }}Y n# tjjjys   dt d d d }}Y n	 tjjjy   dt d d d }}Y n tjjj y   dt d d d }}Y n tjjj!y   dt d d d }}Y n tjjj"y   dt d d d }}Y n tjjj#y   dt d d d }}Y n tjjj$y	   dt d d d }}Y ns tjjj%y"   dt d d d }}Y nZ tjjj&y;   dt d d d }}Y nA tjjj'yT   dt d d d }}Y n( tjjj(yj   dt)|oed}}Y n tjj*y{   | +| Y d S w | ,||||| | +| d S )Nr   zUnexpected PDU class %s)ZnoErrorr   z*processPdu: stateReference %s, varBinds %sr.   z1processPdu: stateReference %s, errorIndication %soidval)ZtooBigr   Z
noSuchNameidxZbadValueZreadOnlyZgenErrZnoAccessZ	wrongTypeZwrongLengthZwrongEncodingZ
wrongValueZ
noCreationZinconsistentValueZresourceUnavailableZcommitFailedZ
undoFailedZauthorizationErrorZnotWritableZinconsistentName)-r   Zv1ToV2tagSetr   ZreadClassPDUsZwriteClassPDUsr   ProtocolErrorr   r   ZgetResponser   getVarBindsr	   r   r    r   #_CommandResponderBase__verifyAccesspysnmpsmiGenErrorr1   r2   ZTooBigErrorZNoSuchNameErrorZBadValueErrorZReadOnlyErrorNoAccessErrorZWrongTypeErrorZWrongLengthErrorZWrongEncodingErrorZWrongValueErrorZNoCreationErrorZInconsistentValueErrorZResourceUnavailableErrorZCommitFailedErrorZUndoFailedErrorAuthorizationErrorNotWritableErrorZInconsistentNameErrorSmiErrorlenZPySnmpErrorr8   r-   )r   r   r%   r&   r'   r(   r   r   r)   r   r+   r   r*   ZrspPDUr,   r$   r"   r#   errorIndicationr   r   r   r   e   s   


Q

        

zCommandResponderBase.processPduc              	   C   s  |}|j d}|d |d |d |d |d  f\}}	}
}}z|j| j |||	|
||| W ny tjy   t	 d }t
jt
j@ oMt
d||f  |d	 }|tjksa|tjksa|tjkrjtjjj||d
|tjkrxtjjj||d
|tjkr|jjjdd\}| jd7  _tjjj|||j|jd|tjkrY dS td| w |dkr|d ur| j| kr| j |krtjjj!||d
d S d S d S d S )Nzrfc3412.receiveMessage:requestr&   r'   r(   r   Zpdur.   z-__verifyAccess: name %s, statusInformation %srH   )namer;   z__SNMP-TARGET-MIBsnmpUnknownContexts)rI   r;   r9   r:   zUnknown ACM error %s)"ZobserverZgetExecutionContextZ	getTagSetZaccessControlModelacmIDZisAccessAllowedr   r0   r1   r2   r	   r   r    r   Z
noSuchViewZnoAccessEntryZnoGroupNamer@   rA   rD   Z
otherErrorrB   ZnoSuchContextr   r3   r4   r5   r6   rI   Z	notInViewr=   _counter64Type_getNextRequestTyperC   )r   rI   r6   r;   ZviewTypeacCtxr   ZexecCtxr&   r'   r(   r   ZpduTyper,   rH   rJ   r   r   r   Z__verifyAccess   s`   







z#CommandResponderBase.__verifyAccessN)__name__
__module____qualname__rK   r   r   r   r   r-   ZsendRspr!   r   GetRequestPDUr<   Z_getRequestTypeGetNextRequestPDUrM   SetRequestPDUZ_setRequestTyper   Z	Counter64rL   r8   r   r?   r   r   r   r   r
      s     "}r
   c                   @      e Zd ZejjfZdd ZdS )GetCommandResponderc           	   
   C   sH   |\}}| j |j}| ||dd|tj|||f | | d S )Nr   )r   getMibInstrumZreadVarsr-   r   r   r>   r8   )	r   r   r   r   r   r   acFunrN   mgmtFunr   r   r   r     s   z'GetCommandResponder.handleMgmtOperationN)rO   rP   rQ   r   rR   r<   r   r   r   r   r   r   rV         
rV   c                   @   rU   )NextCommandResponderc                 C   s   |\}}| j |j}tj|}		 ||	||f}
z| ||dd|
 W n  tjyD   t	
 d d }|
| d |	| d f|	|< Y nw nq| | d S )NTr   r.   r;   )r   rW   readNextVarsr   r   r>   r-   r   r0   r1   r2   r8   )r   r   r   r   r   r   rX   rN   rY   r$   rspVarBindsr;   r   r   r   r   "  s    	z(NextCommandResponder.handleMgmtOperationN)rO   rP   rQ   r   rS   r<   r   r   r   r   r   r   r[     rZ   r[   c                   @   s"   e Zd ZejjfZdZdd ZdS )BulkCommandResponder@   c                 C   sN  |\}}t j|}|dk rd}t j|}	|	dk rd}	t j|}
tt|t|
}t|	}t	t|
| d}|rBt|| j
| }tjtj@ oQtd|||f  | j|j}|rg||
d | ||f}ng }|
| d  }|r|r|||||f || d  }|d8 }|r|stt|r| ||dd| | | d S tjj )Nr   z%handleMgmtOperation: N %d, M %d, R %dr.   )r   Z
apiBulkPDUZgetNonRepeatersZgetMaxRepetitionsr   r>   minintrG   maxmaxVarBindsr	   r   r    r   rW   r\   extendr-   r8   r@   rA   r   rF   )r   r   r   r   r   r   rX   rN   ZnonRepeatersZmaxRepetitionsZreqVarBindsNMRrY   r]   r$   r   r   r   r   9  s:   z(BulkCommandResponder.handleMgmtOperationN)	rO   rP   rQ   r   ZGetBulkRequestPDUr<   r   rc   r   r   r   r   r   r^   4  s    
r^   c                   @   rU   )SetCommandResponderc           
   
   C   s   |\}}| j |j}z| ||dd|tj|||f | | W d S  tj	j
jtj	j
jfyE   tj	j
 }	|	t d  |	w )Nr   r.   )r   rW   Z	writeVarsr-   r   r   r>   r8   r@   rA   r   ZNoSuchObjectErrorZNoSuchInstanceErrorrE   updater1   r2   )
r   r   r   r   r   r   rX   rN   rY   er   r   r   r   f  s    

z'SetCommandResponder.handleMgmtOperationN)rO   rP   rQ   r   rT   r<   r   r   r   r   r   r   rh   b  rZ   rh   )r1   Zpysnmp.protor   r   r   r   r   Zpysnmp.proto.apir   Zpysnmp.proto.proxyr   Zpysnmp.smi.errorr@   r	   objectr
   rV   r[   r^   rh   r   r   r   r   <module>   s     .