
    h^                         d dl mZmZ d dlmZ d dlmZmZmZm	Z	 d dl
mZmZ d dlmZmZ d dlmZmZ d dlmZmZ d Z G d	 d
e          Z eeed dd           dS )    )chborb)warning)ByteEnumField	ByteFieldIPFieldXShortField)IPIPOption_Router_Alert)Ether
getmacbyip)bind_layersPacket)atolchecksumc                 D    t          |           dz	  dz  }|dk    o|dk    S )z<convert dotted quad string to long and check the first octet            )r   )ipFirstOcts     V/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/contrib/igmp.pyisValidMCAddrr      s)    Bxx2~$HO2(c/2    c                       e Zd ZdZd ZdddddZ edde           ed	d
           edd           e	dd          gZ
d Zedd            Zd Zd ZdS )IGMPa  IGMP Message Class for v1 and v2.

    This class is derived from class Packet. You  need call "igmpize()"
    so the packet is transformed according the RFC when sent.
    a=Ether(src="00:01:02:03:04:05")
    b=IP(src="1.2.3.4")
    c=IGMP(type=0x12, gaddr="224.2.3.4")
    x = a/b/c
    x[IGMP].igmpize()
    sendp(a/b/c, iface="en0")

        Parameters:
          type    IGMP type field, 0x11, 0x12, 0x16 or 0x17
          mrcode  Maximum Response time (zero for v1)
          gaddr   Multicast Group Address 224.x.x.x/4

    See RFC2236, Section 2. Introduction for definitions of proper
    IGMPv2 message format   http://www.faqs.org/rfcs/rfc2236.html
    zGroup Membership QueryzVersion 1 - Membership ReportzVersion 2 - Membership ReportzLeave Group)            typer   mrcode   chksumNgaddr0.0.0.0c                     ||z  }| j         Jt          |          }|dd         t          |dz	            z   t          |dz            z   |dd         z   }|S )a  Called implicitly before a packet is sent to compute and place IGMP checksum.

        Parameters:
          self    The instantiation of an IGMP class
          p       The IGMP message in hex in network byte order
          pay     Additional payload for the IGMP message
        N      r      )r%   r   r   )selfppaycks       r   
post_buildzIGMP.post_build7   s]     	
S;!B"1"B!G$s29~~5!""=Ar   c                     |r`t          |          dk    rMddlm} t          |d                   dv r|S t          |d                   dk    rt          |          dk    r|S t          S )Nr+   r   IGMPv3)"   0   1   2   r      )lenscapy.contrib.igmpv3r3   r   r   )cls_pktargskargsr3   s        r   dispatch_hookzIGMP.dispatch_hookE   sr     	CIINN33333347||77747||t##D		Rr   c                    ddl m} t          | d          r| j        r| j        nd}| j        }| j        dvrd| _        t          |t                    rX| j        dk    r6|dk    rd|_	        nt          |          r||_	        nt          d           d	S | j        d
k    rt          |          rd|_	        nX| j        dk    s| j        dk    rt          |          r||_	        n+| j        dv rt          | |          rnt          d           d	S t          d |j        D                       s&|j                            t                                 d|_        |                                 }|                    t&                    r$t)          |j	                  |t&                   _	        t          | |          r|                                  dS )a  Called to explicitly fixup the packet according to the IGMP RFC

        The rules are:
        - General:
        1.  the Max Response time is meaningful only in Membership Queries and should be zero
        - IP:
        1. Send General Group Query to 224.0.0.1 (all systems)
        2. Send Leave Group to 224.0.0.2 (all routers)
        3a.Otherwise send the packet to the group address
        3b.Send reports/joins to the group address
        4. ttl = 1 (RFC 2236, section 2)
        5. send the packet with the router alert IP option (RFC 2236, section 2)
        - Ether:
        1. Recalculate destination

        Returns:
            True    The tuple ether/ip/self passed all check and represents
                    a proper IGMP packet.
            False   One of more validation checks failed and no fields
                    were adjusted.

        The function will examine the IGMP message to assure proper format.
        Corrections will be attempted if possible. The IP header is then properly
        adjusted to ensure correct formatting and assignment. The Ethernet header
        is then adjusted to the proper IGMP packet format.
        r   r2   r&   r'   )r   r5   r   z	224.0.0.1z%Invalid IGMP Group Address detected !Fr!   z	224.0.0.2r   r    )r   r4   r5   r6   r7   zInvalid IGMP Type detected !c              3   @   K   | ]}t          |t                    V  d S N)
isinstancer   ).0xs     r   	<genexpr>zIGMP.igmpize.<locals>.<genexpr>   s-      XXz!%:;;XXXXXXr      T)r:   r3   hasattrr&   
underlayerr"   r#   rC   r
   dstr   r   anyoptionsappendr   ttl
firstlayerhaslayerr   r   encode_maxrespcode)r,   r3   r&   rI   _roots        r   igmpizezIGMP.igmpizeO   s   6 	0/////%dG44RR

_
9L((DKj"%% 	>	T!!Y&&%0JNN"5)) !%*JNNCDDD 59$$-*>*>$!,
9$$$)t*;*;=QVCWCW*;!&
)===*TSYBZBZ=6777uXXZEWXXXXX C"))*?*A*ABBBJNOO%%E~~e$$ >#-jn#=#=e dF## 	&##%%%tr   c                     t          | j        t                    r| j                            d          S |                     d          S )z%Display a summary of the IGMP object.z2IGMP: %IP.src% > %IP.dst% %IGMP.type% %IGMP.gaddr%zIGMP %IGMP.type% %IGMP.gaddr%)rC   rI   r
   sprintf)r,   s    r   	mysummaryzIGMP.mysummary   sA    dor** 	A?**+_```<< ?@@@r   rB   )__name__
__module____qualname____doc__name	igmptypesr   r   r	   r   fields_descr0   classmethodr?   rS   rV    r   r   r   r      s         & D/66$& &I
 !=y999Xr**;x..77I..0K
      [; ; ;zA A A A Ar   r   r)   rG   )fragprotorN   N)scapy.compatr   r   scapy.errorr   scapy.fieldsr   r   r   r	   scapy.layers.inetr
   r   scapy.layers.l2r   r   scapy.packetr   r   scapy.utilsr   r   r   r   r_   r   r   <module>ri      s0   " ! ! ! ! ! ! !       G G G G G G G G G G G G 7 7 7 7 7 7 7 7 - - - - - - - - , , , , , , , , & & & & & & & &3 3 3zA zA zA zA zA6 zA zA zAz B1     r   