o
    àý°jt  ã                   @   sÆ   d dl mZ d dl m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
 G dd„ dƒZG d	d
„ d
ƒZdd„ Zedkraeejƒ eejƒdkrXedejd   ƒ e d¡ eejd ƒ dS dS )é    )Údivision)Úprint_functionN)Úopen_offline)Úversion)Ú
EthDecoderÚLinuxSLLDecoderc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú
ConnectionzõThis class can be used as a key in a dictionary to select a connection
    given a pair of peers. Two connections are considered the same if both
    peers are equal, despite the order in which they were passed to the
    class constructor.
    c                 C   s   || _ || _dS )z·This constructor takes two tuples, one for each peer. The first
        element in each tuple is the IP address as a string, and the
        second is the port as an integer.
        N©Úp1Úp2)Úselfr
   r   © r   ú…/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/../../../bin/split.pyÚ__init__,   s   
zConnection.__init__c                 C   s(   d| j d | j d | jd | jd f S )zoUtility function that returns a filename composed by the IP
        addresses and ports of both peers.
        z%s.%d-%s.%d.pcapr   é   r	   ©r   r   r   r   ÚgetFilename5   s   (zConnection.getFilenamec                 C   s8   | j |j kr| j|jks| j |jkr| j|j krdS dS )Nr   éÿÿÿÿr	   )r   Úotherr   r   r   Ú__cmp__;   s   zConnection.__cmp__c                 C   s8   t | jd ƒt | jd ƒA t | jd ƒA t | jd ƒA S )Nr   r   )Úhashr
   r   r   r   r   r   Ú__hash__B   s
   ÿÿzConnection.__hash__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   %   s    	r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚDecoderc                 C   sL   |  ¡ }tj|krtƒ | _ntj|krtƒ | _ntd| ƒ‚|| _i | _	d S )NzDatalink type not supported: )
ÚdatalinkÚpcapyÚ
DLT_EN10MBr   ÚdecoderÚDLT_LINUX_SLLr   Ú	ExceptionÚpcapÚconnections)r   ÚpcapObjr   r   r   r   r   H   s   




zDecoder.__init__c                 C   s   | j  d| j¡ d S )Nr   )r#   ÚloopÚpacketHandlerr   r   r   r   ÚstartU   s   zDecoder.startc                 C   sØ   | j  |¡}| ¡ }| ¡ }| ¡ | ¡ f}| ¡ | ¡ f}t||ƒ}d|j|j	f | j
vr[| ¡ }	td|	ƒ z| j |	¡}
W n tjyO   td|	ƒ Y dS w |
| j
d|j|j	f < | j
d|j|j	f   ||¡ dS )a‹  Handles an incoming pcap packet. This method only knows how
        to recognize TCP/IP connections.
        Be sure that only TCP packets are passed onto this handler (or
        fix the code to ignore the others).

        Setting r"ip proto 	cp" as part of the pcap filter expression
        suffices, and there shouldn't be any problem combining that with
        other expressions.
        z%s%sz%Found a new connection, storing into:zCan't write packet to:N)r    ÚdecodeÚchildÚ
get_ip_srcÚget_th_sportÚ
get_ip_dstÚget_th_dportr   r
   r   r$   r   Úprintr#   Ú	dump_openr   Ú	PcapErrorÚdump)r   ÚhdrÚdataÚpÚipÚtcpÚsrcÚdstÚconÚfnÚdumperr   r   r   r'   Z   s"   


þ"zDecoder.packetHandlerN)r   r   r   r   r(   r'   r   r   r   r   r   G   s    r   c                 C   s6   t | ƒ}| d¡ td| | ¡ f ƒ t|ƒ ¡  d S )Nzip proto \tcpzReading from %s: linktype=%d)r   Ú	setfilterr/   r   r   r(   )Úfilenamer5   r   r   r   Úmain�   s   
r?   Ú__main__r   zUsage: %s <filename>)Ú
__future__r   r   Úsysr   r   Úimpacketr   Úimpacket.ImpactDecoderr   r   r   r   r?   r   r/   ÚDEPRECATION_WARNING_BANNERÚlenÚargvÚexitr   r   r   r   Ú<module>   s"   ":

ú