
    hU$                        d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZ dZdZdZd	Zd
ZdZdZdZdZddddddddddddddZd Zd Z G d d e          Z G d! d"e          Z G d# d$e          Z G d% d&e          Z G d' d(e          Z G d) d*e          Z  G d+ d,e          Z! G d- d.e          Z" eee"d/0            eee"d/1            eee"d/d/2            ee"e!e3            ee"e e3            ee"ee3            ee"ee3            ee"ee3            ee"ee3            ee"ee3           d4S )5a}  HICP

Support for HICP (Host IP Control Protocol).

This protocol is used by HMS Anybus software for device discovery and
configuration.

Note : As the specification is not public, this layer was built based on the
Wireshark dissector and HMS's HICP DLL. It was tested with a Anybus X-gateway
device. Therefore, this implementation may differ from what is written in the
standard.
    )match)Packetbind_layersbind_bottom_up)StrFieldMACFieldIPField	ByteFieldRawVal)UDPs   Module scans   Module scan responses	   Configures   Reconfigureds   Invalid Configurations   Invalid Passwords   Winks   Starts   StopzProtocol versionzFB typezModule versionMACzNew passwordPSWDIPSNGWDHCPHNDNS1DNS2)protocol_versionfieldbus_typemodule_versionmac_addressnew_passwordpassword
ip_addresssubnet_maskgateway_addressdhcphostnamedns1dns2c                 .    |                      dd          S )N:-replacexs    V/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/contrib/hicp.py<lambda>r+   ;   s    !))C--     c                 .    |                      dd          S )Nr%   r$   r&   r(   s    r*   r+   r+   <   s    		#s++ r,   c                      e Zd ZdZ edd           edd           edd           edd           ed	d
           edd           edd           edd           edd           edd           edd          gZd Z	d Z
dS )HICPConfigurezConfigure requesttargetff:ff:ff:ff:ff:ffr    r   r   255.255.255.255r   255.255.255.0r   0.0.0.0r   OFFr    r!   r"   paddingr   c                 F   d                     t                              d          t          | j                            g}| j        dd          D ]}|j        t          v rt          | |j                  }t          |t                    r|                    d          }|j        dv r|s[t          |j                 }|j        dk    rd}|                    d                     ||                     d                    |                              d          d	z   |z   S )
Nz	{0}: {1};utf-8   )r   r   r   Password
{0} = {1};r2       )formatCMD_CONFIGUREdecodeFROM_MACFIELDr0   fields_descnameKEYSgetattr
isinstancebytesappendjoinencode)selfppayfieldvaluekeys         r*   
post_buildzHICPConfigure.post_buildS   s    4 4W = = -dk : :< < =%abb) 	: 	:EzT!!ej11eU++ 2!LL11E:!===e=5:&:++$C,,S%88999wwqzz  ))G3c99r,   c                    t          d|                    d                    }|r't          |                    d                    | _        |t          | j                  dz   d          }|                    d          D ]}d |                    d          D             t                    dk    sd         s=d	                             d          d	<   d	         t                                          v rBfd
t          	                                D             d	         }t          | |d                    d S )Nz.*: ([^;]+);r9   r:         ;c                 ^    g | ]*}|                                                     d d          +S r=   r,   stripr'   .0r)   s     r*   
<listcomp>z,HICPConfigure.do_dissect.<locals>.<listcomp>j   0    KKKa!''))##GS11KKKr,      =   r   c                 2    g | ]\  }}|d          k    |S r    rZ   r)   ykvs      r*   r[   z,HICPConfigure.do_dissect.<locals>.<listcomp>o   &    BBBtq!qBqEzzzzzr,   )r   r@   TO_MACFIELDgroupr0   lensplitrD   valuesitemssetattr)rK   sresargrN   rd   s        @r*   
do_dissectzHICPConfigure.do_dissectd   s%   NAHHW$5$566 	4%ciill33DKc$+"##$774== 	, 	,CKK399T??KKKB2ww!||2a5|qELL))BqE!u%%BBBBtzz||BBB1EeRU+++	, 	,r,   N)__name__
__module____qualname__rC   r   r   r	   r
   rB   rQ   rp   ra   r,   r*   r/   r/   C   s        D.//R  $$/00//!9--R  	""	""	)QK: : :", , , , ,r,   r/   c                   8    e Zd ZdZ edd          gZd Zd ZdS )HICPReconfiguredReconfiguredsourcer1   c                     d                     t                              d          t          | j                            }|                    d          dz   |z   S Nz{0}: {1}r9   r=   )r>   CMD_RECONFIGUREDr@   rA   rw   rJ   rK   rL   rM   s      r*   rQ   zHICPReconfigured.post_buildy   sQ    .55g>>+DK88: :xx  7*S00r,   c                     t          d|                    d                    }|r't          |                    d                    | _        d S Nz.*: ([a-fA-F0-9\-\:]+)r9   r:   r   r@   rf   rg   rw   rK   rm   rn   s      r*   rp   zHICPReconfigured.do_dissect~   D    -qxx/@/@AA 	4%ciill33DKtr,   Nrq   rr   rs   rC   r   rB   rQ   rp   ra   r,   r*   ru   ru   s   sN        D.//K1 1 1
    r,   ru   c                   8    e Zd ZdZ edd          gZd Zd ZdS )HICPInvalidConfigurationzInvalid configurationrw   r1   c                     d                     t                              d          t          | j                            }|                    d          dz   |z   S ry   )r>   CMD_INVALIDCONFr@   rA   rw   rJ   r{   s      r*   rQ   z#HICPInvalidConfiguration.post_build   sP    o44W==+DK88: :xx  7*S00r,   c                     t          d|                    d                    }|r't          |                    d                    | _        d S r}   r~   r   s      r*   rp   z#HICPInvalidConfiguration.do_dissect   r   r,   Nr   ra   r,   r*   r   r      sN        "D.//K1 1 1
    r,   r   c                   8    e Zd ZdZ edd          gZd Zd ZdS )HICPInvalidPasswordzInvalid passwordrw   r1   c                     d                     t                              d          t          | j                            }|                    d          dz   |z   S ry   )r>   CMD_INVALIDPWDr@   rA   rw   rJ   r{   s      r*   rQ   zHICPInvalidPassword.post_build   sP    n33G<<+DK88: :xx  7*S00r,   c                     t          d|                    d                    }|r't          |                    d                    | _        d S r}   r~   r   s      r*   rp   zHICPInvalidPassword.do_dissect   r   r,   Nr   ra   r,   r*   r   r      sN        D.//K1 1 1
    r,   r   c                   N    e Zd ZdZ edd           edd          gZd Zd ZdS )	HICPWinkWinkr0   r1   r7   r   c                     d                     t          | j                  t                              d                                                    }|                    d          dz   |z   S )NzTo: {0};{1};r9   r=   )r>   rA   r0   CMD_WINKr@   upperrJ   r{   s      r*   rQ   zHICPWink.post_build   s\    !!-"<"<"*//'":":"@"@"B"BD Dxx  7*S00r,   c                     t          d|                    d                    }|r)t          |                    d                    | _        d S d S )Nz^To: ([^;]+);r9   r:   )r   r@   rf   rg   r0   r   s      r*   rp   zHICPWink.do_dissect   sL    OQXXg%6%677 	4%ciill33DKKK	4 	4r,   N)	rq   rr   rs   rC   r   r
   rB   rQ   rp   ra   r,   r*   r   r      s]        D.//	)QK
1 1 1
4 4 4 4 4r,   r   c                   @   e Zd ZdZ edd           edd           edd           edd           ed	d
           edd           edd           edd           edd           edd           edd           edd           edd          gZd Z	d Z
dS )HICPModuleScanResponsezModule scan responser   z1.00r   r2   r   r   r1   r   r3   r   r4   r   r5   r   r6   r   r    r!   r"   r7   r   c                    g }| j         D ]}|j        t          v rxt          | |j                  }t	          |t
                    r|                    d          }|                    d                    t          |j                 |                     d	                    |          
                    d          dz   |z   S )Nr9   r<   r2   r=   )rB   rC   rD   rE   rF   rG   r@   rH   r>   rI   rJ   )rK   rL   rM   rN   rO   s        r*   rQ   z!HICPModuleScanResponse.post_build   s    % 	G 	GEzT!!ej11eU++ 2!LL11E,,T%*-=uEEFFFwwqzz  ))G3c99r,   c                    |                     d          D ]}d |                     d          D             t                    dk    sd         s=d                             d          d<   d         t                                          v rsfdt                                          D             d         }|d	k    r+t          d                             d                    d<   t          | |d                    d S )
NrT   c                 ^    g | ]*}|                                                     d d          +S rV   rW   rY   s     r*   r[   z5HICPModuleScanResponse.do_dissect.<locals>.<listcomp>   r\   r,   r]   r^   r:   r   r9   c                 2    g | ]\  }}|d          k    |S r`   ra   rb   s      r*   r[   z5HICPModuleScanResponse.do_dissect.<locals>.<listcomp>   re   r,   r   )ri   rh   r@   rD   rj   rk   rf   rl   )rK   rm   ro   rN   rd   s       @r*   rp   z!HICPModuleScanResponse.do_dissect   s    774== 		, 		,CKK399T??KKKB2ww!||2a5|qELL))BqE!u%%BBBBtzz||BBB1EM))'1W(=(=>>BqEeRU+++		, 		,r,   N)rq   rr   rs   rC   r   r   r	   r
   rB   rQ   rp   ra   r,   r*   r   r      s       !D#V,,"%%!2&& 344/00//!9--U##R  	""	""	)QK : : :
, 
, 
, 
, 
,r,   r   c                   N    e Zd ZdZ ede           edd          gZd Zd Z	dS )HICPModuleScanzModule scan requesthicp_commandr7   r   c                     t          |          t          t                    k    rD|d t          t                             | _        |t          t                    d          | _        d S t	          |          | _        d S N)rh   CMD_MODULESCANr   r7   r   )rK   rm   s     r*   rp   zHICPModuleScan.do_dissect   se    q66C'''' !"63~#6#6"6 7DS00112DLLL!!99DLLLr,   c                 0    |                                 |z   S r   )r   r{   s      r*   rQ   zHICPModuleScan.post_build   s    wwyy3r,   N)
rq   rr   rs   rC   r   r   r
   rB   rp   rQ   ra   r,   r*   r   r      s\         D00	)QK
% % %    r,   r   c                   8    e Zd Zd Z edd          gZd Zd ZdS )HICPr   r2   c                 *   t           t          t          t          t          fD ]=}|d t          |                   |k    r || _        |t          |          d          c S >|d t          d                   dk    rt          | _        nt          | _        |S )NzTo:s   To:)	r   r?   rz   r   r   rh   r   r   CMD_MSRESPONSE)rK   rm   cmds      r*   rp   zHICP.do_dissect   s    "M3C#^5 	$ 	$C#c((|s""$'!S|### # [c%jj[>V## (D .Dr,   c                 D    |t          | j                  d          }||z   S r   )rh   r   r{   s      r*   rQ   zHICP.post_build  s&    c$#$$%%&3wr,   N)rq   rr   rs   rC   r   rB   rp   rQ   ra   r,   r*   r   r      sM        D$$K
 
 
    r,   r   i  )dport)sport)r   r   )r   N)#__doc__rer   scapy.packetr   r   r   scapy.fieldsr   r   r	   r
   r   scapy.layers.inetr   r   r   r?   rz   r   r   r   	CMD_STARTCMD_STOPrD   rA   rf   r/   ru   r   r   r   r   r   r   ra   r,   r*   <module>r      s;   "       < < < < < < < < < < G G G G G G G G G G G G G G ! ! ! ! ! !  (" *$	 +&" " .-++-, -, -, -, -,F -, -, -,`    v   $    v   $    &   $4 4 4 4 4v 4 4 4$&, &, &, &, &,V &, &, &,R    V   $    6   . sD % % % % sD % % % % CT . . . . D.~ > > > > D(~ F F F F D( 2 2 2 2 D-m < < < < D"1A B B B B D* I I I I D%N C C C C C Cr,   