Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
B

Šä£	»O“‚ã	@s€dZddlmZmZddlZddlmZmZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddlmZddlmZddlmZdd	lmZmZmZdd
lmZddlmZmZmZmZm Z ddl!m"Z"m#Z#m$Z$dd
lm%Z%m&Z&m'Z'ddl(m)Z)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0ddl1m2Z2m3Z3ddl
m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=ddlm>Z>ddl!m?Z?ej@dkrˆddlAZBGdd„dƒZCdZDe<e;e0e0fZEGdd„deFƒZGdd„ZHdPdd„ZIdQd d!„ZJejKjLdRd"d#„ƒZMejKjLdSd$d%„ƒZNejKjLdTd&d'„ƒZOd(d)„ZPejKjLdUd*d+„ƒZQd,d-„ZRejKjLdVd.d/„ƒZSejKjLdddddefd0d1„ƒZTejKjLd2d3„ƒZUx0eTeUeQeSgD] ZVeVjdk	r|eVjeD7_q|Wd4d5„d6d5„d7dddd7fd8d9„ZWejKjLd:d;„ƒZXejKjLd<d=„ƒZYdWd>d?„ZZejKjLdXd@dA„ƒZ[ejKjLdYdBdC„ƒZ\ejKjLdZdDdE„ƒZ]ejKjLd[dFdG„ƒZ^GdHdI„dIeFƒZ_ejKjLdJdK„ƒZ`e_je`_ejKjLd\dLdM„ƒZaejKjLdNdO„ƒZbdS)]z(
Functions to send and receive packets.
é)Úabsolute_importÚprint_functionN)ÚThreadÚEvent)Ú	plain_str)Ú	ETH_P_ALL)Úconf)Úwarning)Únetwork_nameÚ
resolve_ifaceÚNetworkInterface)ÚPacket)Ú
get_temp_fileÚtcpdumpÚwrpcapÚContextManagerSubprocessÚ
PcapReader)Ú
PacketListÚQueryAnswerÚ
SndRcvList)Úlog_runtimeÚlog_interactiveÚScapy_Exception)ÚGenÚSetGen)Úsix)Úmap)ÚDefaultSession)ÚSuperSocketÚ
IterSocket)
ÚAnyÚCallableÚDictÚIteratorÚListÚOptionalÚTupleÚTypeÚUnionÚcast)Ú_GlobInterfaceType)Ú_PacketIterablec@s.eZdZegdƒZegdƒZegdƒZdZdS)ÚdebugÚReceivedÚSentÚMatchedN)	Ú__name__Ú
__module__Ú__qualname__rÚrecvÚsentrÚmatchZ
crashed_on©r6r6úŒ/build/wlanpi-profiler-8o1rr4/wlanpi-profiler-1.0.9/debian/wlanpi-profiler/opt/wlanpi-profiler/lib/python3.7/site-packages/scapy/sendrecv.pyr,Ds


r,a

    :param pks: SuperSocket instance to send/receive packets
    :param pkt: the packet to send
    :param rcv_pks: if set, will be used instead of pks to receive packets.
        packets will still be sent through pks
    :param nofilter: put 1 to avoid use of BPF filters
    :param retry: if positive, how many times to resend unanswered packets
        if negative, how many times to retry when no more packets
        are answered
    :param timeout: how much time to wait after the last packet has been sent
    :param verbose: set verbosity level
    :param multi: whether to accept multiple answers for the same stimulus
    :param prebuild: pre-build the packets before starting to send them.
        Automatically enabled when a generator is passed as the packet
    c@s:eZdZdZddd„Zdd„Zd	d
„Zdd„Zd
d„ZdS)Ú
SndRcvHandleraÝ
    Util to send/receive packets, used by sr*().
    Do not use directly.

    This matches the requests and answers.

    Notes::
      - threaded mode: enabling threaded mode will likely
        break packet timestamps, but might result in a speedup
        when sending a big amount of packets. Disabled by default
      - DEVS: store the outgoing timestamp right BEFORE sending the packet
        to avoid races that could result in negative latency. We aren't Stadia
    NrFcCs¢|dkrtj}tjr8tgdƒt_tgdƒt_tgdƒt_d|_	g|_
||_|	pP||_||_
||_||_||_||_|
|_|rŽ||_|d|_nLt|tjƒsž|
r¶t|ƒ|_t|jƒ|_n$t|tƒsÈt|ƒn||_|j ¡|_|dkrî|}}nd}|dk	r|dkrd|_xð|dkrüi|_|s*|rft|jd}|  d¡| !|j"¡|r\|dƒ| #¡n| !|j¡|r–dd	„t$j%t& '|j¡ŽDƒ}ntt$j%t& '|j¡Žƒ}|rØt|ƒdkrØt|ƒt|jƒkrØ|}||_t|jƒdkrðP|d8}qWtjr.t|dd…dƒt_t|j
dd…ƒt_|rZx$|j
D]\}}t(|d
ƒr<|`)q<W|r†t*d|j	t|j
ƒt|j
ƒ|jfƒt|j
ƒ|_+t|dƒ|_,dS)
Nr-r.r/r)ÚtargetTécSsg|]}t|dƒs|‘qS)Ú	_answered)Úhasattr)Ú.0Úpr6r6r7ú
<listcomp>Àsz*SndRcvHandler.__init__.<locals>.<listcomp>r;z:
Received %i packets, got %i answers, remaining %i packetsZ
Unanswered)-rÚverbÚdebug_matchrr,r3r4rr5ÚnbrecvÚansÚpksÚrcv_pksÚinterÚverboseÚchainCCÚmultiÚtimeoutÚsessionÚtobesentÚnotansÚ
isinstanceÚtypesÚ
GeneratorTypeÚlistÚlenrrÚ__iterlen__ÚhsentrÚ_sndrcv_sndÚ	setDaemonÚ_sndrcv_rcvÚstartÚjoinÚ	itertoolsÚchainrÚ
itervaluesr<r;ÚprintÚ
ans_resultÚunans_result)ÚselfrDÚpktrJrFrGrHÚretryrIrEZprebuildÚ_floodZthreadedrKZautostopZ
snd_threadÚremainZsndÚ_r6r6r7Ú__init__qs~






"zSndRcvHandler.__init__cCs|j|jfS)N)r^r_)r`r6r6r7ÚresultsäszSndRcvHandler.resultscCs¦yp|jrtdƒd}xF|jD]<}|j | ¡g¡ |¡|j |¡t	 
|j¡|d7}qW|jrntd|ƒWn0tk
r„Ynt
k
r t d¡YnXdS)z/Function used in the sending thread of sndrcv()zBegin emission:rr:zFinished sending %i packets.z--- Error sending packetsN)rGr]rLrTÚ
setdefaultÚhashretÚappendrDÚsendÚtimeÚsleeprFÚ
SystemExitÚ	ExceptionrÚ	exception)r`Úir>r6r6r7rUèszSndRcvHandler._sndrcv_sndcCs|dkrdSd}| ¡}||jkr´|j|}x†t|ƒD]z\}}| |¡r6|j t||ƒ¡|jdkrpt 	dd¡d}|j
s||=|jd8_nt|dƒs¨|jd8_d|_
Pq6W|jdkrØ|j
sØ|jrØ|jjdd|s|jdkrôt 	dd	¡|jd7_tjrtj |¡dS)
z.Internal function used to process each packet.NFr:ó*Tr;r)rYó.)rirTÚ	enumerateZanswersrCrjrrGÚosÚwriterIrMr<r;ÚsnifferÚstoprBrrAr,r3)r`ÚrÚokÚhZhlstrqZsentpktr6r6r7Ú_process_packetÿs8





zSndRcvHandler._process_packetcCsTd|_y,tƒ|_|jj|j|jd|j|j|dWntk
rN|jrJ‚YnXdS)z8Function used to receive packets and check their hashretNF)ÚprnrJÚstoreÚ
opened_socketrKÚstarted_callback)	rwÚAsyncSnifferÚ_runr|rJrErKÚKeyboardInterruptrH)r`Úcallbackr6r6r7rW szSndRcvHandler._sndrcv_rcv)NrNFrFNFNFN)	r0r1r2Ú__doc__rfrgrUr|rWr6r6r6r7r8cs 

f!r8cOst||Ž}| ¡S)z§Scapy raw function to send a packet and receive its answer.
    WARNING: This is an internal function. Using sr/srp/sr1/srp is
    more appropriate in many cases.
    )r8rg)ÚargsÚkwargsZsndrcverr6r6r7Úsndrcv3s
rˆFcOsDt|tƒrtj|d}t|tƒs(t|ƒ}|dkr6tj}d}
|dk	rJ|}n|sRd}|r\tƒ}y°xª|rd}xŠ|D]‚}
|r¸t ¡}|rª|t	|
jƒ|}|dkr¸t 
|¡n|t	|
jƒ}| |
¡|rÐ| |
¡|
d7}
|rèt
 dd¡t 
|¡qpW|dkr`|d7}q`WWntk
r"YnX|r6td|
ƒ|r@|SdS)z±
    An internal function used by send/sendp to actually send the packets,
    implement the send logic...

    It will take care of iterating through the different packets
    )ÚloadNréÿÿÿÿr:rsz
Sent %i packets.)rNÚstrrZ	raw_layerrrr@rrlÚfloatrmrkrjrurvrƒr])ÚsÚxrFÚloopÚcountrGÚrealtimeÚreturn_packetsr†ÚkargsÚnZsent_packetsZdt0r>ÚctÚstr6r6r7Ú
__gen_send=sP




r—c


KsX|	dk}t|ptjƒ}|	p.||ƒfd|i|
—Ž}	t|	|||||||d}|rT|	 ¡|S)z(Internal function used by send and sendpNÚiface)rFrrrGr‘r’)rrr˜r—Úclose)
rŽZ_funcrFrr˜rrGr‘r’Úsocketr“Zneed_closingrgr6r6r7Ú_sendzs

r›cKs$t||ƒ}t|dd„fd|i|—ŽS)as
    Send packets at layer 3

    :param x: the packets
    :param inter: time (in s) between two packets (default 0)
    :param loop: send packet indefinetly (default 0)
    :param count: number of packets to send (default None=1)
    :param verbose: verbose mode (default None=conf.verbose)
    :param realtime: check that a packet was sent before sending the next one
    :param return_packets: return the sent packets
    :param socket: the socket to use (default is conf.L3socket(kargs))
    :param iface: the interface to send the packets on
    :param monitor: (not on linux) send in monitor mode
    :returns: None
    cSs| ¡S)N)Úl3socket)r˜r6r6r7Ú<lambda>«ózsend.<locals>.<lambda>r˜)Ú_interface_selectionr›)rŽr˜r“r6r6r7rk“s

rkcKsD|dkr(|dk	r(|dkr(tj |¡d}t|dd„f||dœ|—ŽS)as
    Send packets at layer 2

    :param x: the packets
    :param inter: time (in s) between two packets (default 0)
    :param loop: send packet indefinetly (default 0)
    :param count: number of packets to send (default None=1)
    :param verbose: verbose mode (default None=conf.verbose)
    :param realtime: check that a packet was sent before sending the next one
    :param return_packets: return the sent packets
    :param socket: the socket to use (default is conf.L3socket(kargs))
    :param iface: the interface to send the packets on
    :param monitor: (not on linux) send in monitor mode
    :returns: None
    NrcSs| ¡S)N)Úl2socket)r˜r6r6r7rÌržzsendp.<locals>.<lambda>)r˜rš)rÚrouter›)rŽr˜Ú
iface_hintršr“r6r6r7Úsendp±sr£c		Cs’|dkrtj}tjjdt|ƒg}	|dk	r:|	 d|¡n:|dk	rR|	 d|¡n"|dk	rj|	 d|¡n
|	 d¡|r†|	 d|¡|r”|	 d¡|dk	r¦|	 |¡tƒ}
|	 |
¡t|
|ƒd}t	tjjƒ®yt
j|	t
jt
jd	}Wn@t
k
r
t d
¡Ynptk
r*t |
¡‚YnPX| ¡\}
}|rLt | ¡¡|r`t|
||	ƒ}ntjdkrzt |
 ¡¡WdQRXt |
¡|S)aqSend packets at layer 2 using tcpreplay for performance

    :param pps:  packets per second
    :param mpbs: MBits per second
    :param realtime: use packet's timestamp, bending time with real-time value
    :param loop: number of times to process the packet list
    :param file_cache: cache packets in RAM instead of reading from
        disk at each iteration
    :param iface: output interface
    :param replay_args: List of additional tcpreplay args (List[str])
    :param parse_results: Return a dictionary of information
        outputted by tcpreplay (default=False)
    :returns: stdout, stderr, command used
    Nz
--intf1=%sz--pps=%iz	--mbps=%fz--multiplier=%fz
--topspeedz	--loop=%iz--preload-pcap)ÚstdoutÚstderrzInterrupted by useré)rr˜ÚprogZ	tcpreplayr
rjÚextendrrrÚ
subprocessÚPopenÚPIPErƒrÚinfororuÚunlinkÚcommunicaterr	ÚdecodeÚ_parse_tcpreplay_resultr@)rŽÚppsÚmbpsr‘rÚ
file_cacher˜Zreplay_argsZ
parse_resultsÚargvÚfrgÚcmdr¤r¥r6r6r7Ú	sendpfastÓsL







r·c
sˆyFi}t|ƒ ¡}t|ƒ ¡ d¡}tttftttfttttftftftftftftfdœ	}ddddddœ}d	}d
}	d}
t|	t|i‰x | d¡D]’}| ¡}x„| ¡D]x\}}
| |¡r¨|
 ‡fdd
„|
Dƒ¡}t	 
||¡}x@t|
ƒD]4\}}| ||g¡|}|rè|| 
|d¡ƒ||<qèWq¨Wq’Wd |¡|d<|dd…|d<|Stk
r‚}ztjsf‚t d|¡iSd}~XYnXdS)a‰
    Parse the output of tcpreplay and modify the results_dict to populate output information.  # noqa: E501
    Tested with tcpreplay v3.4.4
    Tested with tcpreplay v4.1.2
    :param stdout: stdout of tcpreplay subprocess call
    :param stderr: stderr of tcpreplay subprocess call
    :param argv: the command used in the subprocess call
    :return: dictionary containing the results
    Ú
)	ÚactualÚratedÚflowsZ	attemptedZ
successfulÚfailedZ	truncatedzretried packets (enozretried packets (eag)ZpacketsÚbytesrl)Zbpsr²r±)r»ZfpsZflow_packetsZnon_flow)Zretried_enobufs)Zretried_eagain)r¹rºr»zretried packets (enozretried packets (eagz([0-9]*\.[0-9]+|[0-9]+)z([0-9]+)z[^0-9]*csg|]}ˆ|‘qSr6r6)r=rŽ)Úr_typesr6r7r?Dsz+_parse_tcpreplay_result.<locals>.<listcomp>r:ú ÚcommandNrŠÚwarningszError parsing output: %s)rÚlowerÚstripÚsplitÚintrŒÚitemsÚ
startswithrYÚreÚsearchrtÚgetÚgrouprorÚinteractiverÚerror)Zstdout_bZstderr_br´rgr¤r¥ÚelementsrIZ	float_regZint_regZany_regÚlineÚeltÚ_typesÚregexÚmatchesrqÚtypÚnameZparse_exceptionr6)r¾r7r°sP


"r°c	Os0tj||||d}t||f|ž|Ž}| ¡|S)z-
    Send and receive packets at layer 3
    )ÚpromiscÚfilterr˜Únofilter)rÚL3socketrˆr™)	rŽrÖr×r˜rØr†r“rÚresultr6r6r7ÚsrTs


rÛcCsH|dkrDyt| ¡ƒ ¡d}Wntk
r8d}YnX|pBtjS|S)zK
    Select the network interface according to the layer 3 destination
    Nr)ÚnextÚ__iter__r¡ÚAttributeErrorrr˜)r˜ÚpacketZiffr6r6r7rŸhs

rŸc
Os\t||ƒ}tj||||d}t||f|ž|Ž\}}	| ¡t|ƒdkrXtt|ddƒSdS)zB
    Send packets at layer 3 and return only the first answer
    )rÖr×rØr˜rr:N)rŸrrÙrˆr™rRr)r
)
rŽrÖr×r˜rØr†r“rrCrer6r6r7Úsr1zs

ràcOsb|dkr |dk	r tj |¡d}t|p*tjƒ}| ¡|||||d}	t|	|f|ž|Ž}
|	 ¡|
S)z-
    Send and receive packets at layer 2
    Nr)rÖr˜r×rØÚtype)rr¡rr˜r rˆr™)rŽrÖr˜r¢r×rØrár†r“rrÚr6r6r7Úsrp‘s
râcOs0t||Ž\}}t|ƒdkr,tt|ddƒSdS)zN
    Send and receive packets at layer 2 and return only the first answer
    rr:N)rârRr)r
)r†r“rCrer6r6r7Úsrp1ªsrãcCs|d ¡S)Nr:)Úsummary)rŽr6r6r7rÁržrcCs| ¡S)N)rä)rŽr6r6r7rÂržr:c	Os²d}d}tj}
|dkrtj}d}g}g}|dkr>td|dƒ}yx|dN}|
j|
jg|}|dk	rx|dkrpP|d8}t ¡}|dkr”tddd||f|	ž|ddd	œ|
—Ž}|t|dƒt|dƒ7}|t|dƒ7}|dkr\|r\t|dƒdkr\d
t|dƒ}td|
 	|¡ddx6|dD]*}t|||ƒƒƒtdt|ƒddq.W|dkrÜ|rÜt|dƒdkrÜdt|dƒ}td|
 
|¡ddx6|dD]*}t|||ƒƒƒtdt|ƒddq®W|dkr|s|std
ttt|dd…ƒƒƒ|r.||d7}||d7}t ¡}|||krFt 
|||¡qFWWntk
rpYnX|r¢|dkr¢t|
 d||d||f¡ƒt|ƒt|ƒfS)Nrr¦ér:z	
send...
r¿)ÚendT)rJrGrHzRECV %i:ú
zfail %i:zrecv:%i  fail:%iz4
Sent %i packets, received %i packets. %3.1f%% hits.gY@)rZcolor_themer@ÚminZevenZoddrlr]rRÚsuccessZfailÚtuplerrmrƒÚnormalrr)ZsrfuncÚpktsr}ZprnfailrFrJrrGr~r†r“r”ryr•ZparityrCZunansÚcolrXÚresÚmsgr>rær6r6r7Ú	__sr_loop¿sb
"" rðcOstt|f|ž|ŽS)z
    Send a packet at layer 3 in loop and print the answer each time
    srloop(pkts, [prn], [inter], [count], ...) --> None
    )rðrÛ)rìr†r“r6r6r7Úsrloops
rñcOstt|f|ž|ŽS)z
    Send a packet at layer 2 in loop and print the answer each time
    srloop(pkts, [prn], [inter], [count], ...) --> None
    )rðrâ)rìr†r“r6r6r7Úsrploop
s
ròc	CsRtƒ}dd„}|||ƒ}t|tƒr*| ¡nt|ƒ}	|	|jg}
t|||||||
dS)zsndrcv equivalent for flooding.css*x$x|D]}| ¡rdS|VqWqWdS)zVInfinite generator that produces the same
        packet until stopevent is triggered.N)Úis_set)rLÚ	stopeventr>r6r6r7Úsend_in_loop's

z!sndrcvflood.<locals>.send_in_loop)rFrGrHrJrc)rrNrrSrRÚsetrˆ)rDrarFrGrHrJrôrõZinfinite_genZ
_flood_lenrcr6r6r7Úsndrcvfloods	


r÷c	Os@t|p
tjƒ}| ¡||||d}t||f|ž|Ž}| ¡|S)a<Flood and receive packets at layer 3

    :param prn:      function applied to packets received
    :param unique:   only consider packets whose print
    :param nofilter: put 1 to avoid use of BPF filters
    :param filter:   provide a BPF filter
    :param iface:    listen answers only on the given interface
    )rÖr×r˜rØ)rrr˜rœr÷r™)	rŽrÖr×r˜rØr†r“rryr6r6r7Úsrflood<s
røc
Osbt|p
tjƒ}| ¡||||d}t||f|ž|Ž\}}	| ¡t|ƒdkr^tt|ddƒSdS)aOFlood and receive packets at layer 3 and return only the first answer

    :param prn:      function applied to packets received
    :param verbose:  set verbosity level
    :param nofilter: put 1 to avoid use of BPF filters
    :param filter:   provide a BPF filter
    :param iface:    listen answers only on the given interface
    )rÖr×rØr˜rr:N)	rrr˜rœr÷r™rRr)r
)
rŽrÖr×r˜rØr†r“rrCrer6r6r7Úsr1floodUsrùc
Os`|dkr |dk	r tj |¡d}t|p*tjƒ}| ¡||||d}t||f|ž|Ž}	| ¡|	S)a<Flood and receive packets at layer 2

    :param prn:      function applied to packets received
    :param unique:   only consider packets whose print
    :param nofilter: put 1 to avoid use of BPF filters
    :param filter:   provide a BPF filter
    :param iface:    listen answers only on the given interface
    Nr)rÖr×r˜rØ)rr¡rr˜r r÷r™)
rŽrÖr×r˜r¢rØr†r“rryr6r6r7Úsrpfloodpsrúc
Osbt|p
tjƒ}| ¡||||d}t||f|ž|Ž\}}	| ¡t|ƒdkr^tt|ddƒSdS)aOFlood and receive packets at layer 2 and return only the first answer

    :param prn:      function applied to packets received
    :param verbose:  set verbosity level
    :param nofilter: put 1 to avoid use of BPF filters
    :param filter:   provide a BPF filter
    :param iface:    listen answers only on the given interface
    )rÖr×rØr˜rr:N)	rrr˜r r÷r™rRr)r
)
rŽrÖr×r˜rØr†r“rrCrer6r6r7Ú	srp1floodŒsrûc@s`eZdZdZdd„Zdd„Zdddd	dddddddddifd
d„Zdd
„Zddd„Zdd„Z	dS)raü

    Sniff packets and return a list of packets.

    Args:
        count: number of packets to capture. 0 means infinity.
        store: whether to store sniffed packets or discard them
        prn: function to apply to each packet. If something is returned, it
             is displayed.
             --Ex: prn = lambda x: x.summary()
        session: a session = a flow decoder used to handle stream of packets.
                 --Ex: session=TCPSession
                 See below for more details.
        filter: BPF filter to apply.
        lfilter: Python function applied to each packet to determine if
                 further action may be done.
                 --Ex: lfilter = lambda x: x.haslayer(Padding)
        offline: PCAP file (or list of PCAP files) to read packets from,
                 instead of sniffing them
        quiet:   when set to True, the process stderr is discarded
                 (default: False).
        timeout: stop sniffing after a given time (default: None).
        L2socket: use the provided L2socket (default: use conf.L2listen).
        opened_socket: provide an object (or a list of objects) ready to use
                      .recv() on.
        stop_filter: Python function applied to each packet to determine if
                     we have to stop the capture after this packet.
                     --Ex: stop_filter = lambda x: x.haslayer(TCP)
        iface: interface or list of interfaces (default: None for sniffing
               on all interfaces).
        monitor: use monitor mode. May not be available on all OS
        started_callback: called as soon as the sniffer starts sniffing
                          (default: None).

    The iface, offline and opened_socket parameters can be either an
    element, a list of elements, or a dict object mapping an element to a
    label (see examples below).

    For more information about the session argument, see
    https://scapy.rtfd.io/en/latest/usage.html#advanced-sniffing-sniffing-sessions

    Examples: synchronous
      >>> sniff(filter="arp")
      >>> sniff(filter="tcp",
      ...       session=IPSession,  # defragment on-the-flow
      ...       prn=lambda x: x.summary())
      >>> sniff(lfilter=lambda pkt: ARP in pkt)
      >>> sniff(iface="eth0", prn=Packet.summary)
      >>> sniff(iface=["eth0", "mon0"],
      ...       prn=lambda pkt: "%s: %s" % (pkt.sniffed_on,
      ...                                   pkt.summary()))
      >>> sniff(iface={"eth0": "Ethernet", "mon0": "Wifi"},
      ...       prn=lambda pkt: "%s: %s" % (pkt.sniffed_on,
      ...                                   pkt.summary()))

    Examples: asynchronous
      >>> t = AsyncSniffer(iface="enp0s3")
      >>> t.start()
      >>> time.sleep(1)
      >>> print("nice weather today")
      >>> t.stop()
    cOs"||_||_d|_d|_d|_dS)NF)r†r‡ÚrunningÚthreadrg)r`r†r‡r6r6r7rfès
zAsyncSniffer.__init__cCs(t|j|j|jdd|_|j d¡dS)Nr)r9r†r‡rÕT)rr‚r†r‡rýrV)r`r6r6r7Ú
_setup_threadñs
zAsyncSniffer._setup_threadrTNFcs"dˆ_t|
tƒs.|
pt}
|
f||dœ|—Ž}
n||
_||
_i}|	dk	r˜t|	tƒrj| dd„t|	ƒDƒ¡n.t|	tƒr| dd„t	 
|	¡Dƒ¡nd||	<|dk	r–ˆ d¡‰t|tƒr¼|g}t|tƒrôt
dd„|Dƒƒrô| ‡‡fd	d„|Dƒ¡n¢t|tƒr"| ‡‡fd
d„t	 
|¡Dƒ¡ntt|tttfƒrjt|ƒ}||ˆdkrN|ntt|ddgˆdˆd
ƒ<n,||tˆdkr~|nt|ddgˆdˆd
ƒ<|r¦|dk	r6t|p²tjƒ}ˆdkrÈ| ¡‰t|tƒrð| ‡‡fdd„|Dƒ¡nFt|tƒr| ‡‡fdd„t	 
|¡Dƒ¡n||ˆft|dœˆ—Ž<tt|ƒƒ}|j‰|j}t
‡fdd„|Dƒƒsntdƒ|s¤ddlm}|ƒ‰d|ˆ<‡‡fdd„}|ˆ_n‡fdd„}|ˆ_d‰y|rÊ|ƒdˆ_ |dk	ræt! !¡|}d}xÔ|rj rÀ|dk	r|t! !¡}|dkrPˆt| "¡ƒ|ƒ}g}xn|D]d}|ˆkrNPy| #¡}WnØt$k
r¢y| %¡Wnt&k
rŽYnX| '|¡w<Yn’t&k
r2}zrd}y| %¡Wn,t&k
rð}zd|}Wdd}~XYnXtd||f|ƒ| '|¡tj(dkr‚w<Wdd}~XYnX|dkrBq<|rV||ƒsVq<|||_)|
 *|¡|
rz|
|ƒs˜d|kr”|
j+kr<nndˆ_ Pq<Wx|D]}||=q¬WqîWWnt,k
rÚYnXdˆ_|	dkrx&|D]}| %¡qòWnˆrˆ %¡|
 -¡ˆ_.dS)NT)r}r~css|]\}}|d|fVqdS)zsocket%dNr6)r=rqrr6r6r7ú	<genexpr>sz$AsyncSniffer._run.<locals>.<genexpr>css|]\}}||fVqdS)Nr6)r=rÚlabelr6r6r7rÿ"sZsocket0r×css|]}t|tƒVqdS)N)rNr‹)r=rÐr6r6r7rÿ.sc	3s8|]0}tˆdkr|nt|ddgˆdˆdƒ|fVqdS)Nz-wú-T)r†ÚfltÚgetfdÚquiet)rr)r=Úfname)rrr6r7rÿ0sc	3s<|]4\}}tˆdkr|nt|ddgˆdˆdƒ|fVqdS)Nz-wrT)r†rrr)rr)r=rr)rrr6r7rÿ:sz-wr)r†rrrc3s&|]}ˆft|dœˆ—Ž|fVqdS))rár˜N)r)r=Úifname)ÚL2socketÚkargr6r7rÿ\sc3s*|]"\}}ˆft|dœˆ—Ž|fVqdS))rár˜N)r)r=rZiflabel)rrr6r7rÿbs)rár˜c3s|]}ˆ|jkVqdS)N)Úselect)r=Úsock)Úselect_funcr6r7rÿoszaWarning: inconsistent socket types ! The used select function will be the one of the first socketr)Ú
ObjectPipeZcontrol_socketcsˆjrˆ d¡dˆ_dS)NF)rürkÚcontinue_sniffr6)Ú
close_piper`r6r7Ústop_cbzs
z"AsyncSniffer._run.<locals>.stop_cbcs
dˆ_dS)NF)r
r6)r`r6r7r‚sz It was closed.z close() failed with '%s'zSocket %s failed with '%s'.r¦F)/rürNrr}r~rQÚupdatertÚdictrÚ	iteritemsrÊr‹Úallr
rrrrrrr˜Zl2listenrrÜÚiterr	Únonblocking_socketr	Zscapy.automatonrrr
rlÚkeysr3ÚEOFErrorr™rorjZdebug_dissectorÚ
sniffed_onZon_packet_receivedrrƒZtoPacketListrg)r`rr~Úofflinerr}ZlfilterrrJrZstop_filterr˜r€rKZsession_kwargsrZ
sniff_socketsZ_main_socketrrrZstoptimerdÚsocketsZdead_socketsrr>ÚexrïZex2r6)rrrrrrr`r7r‚üsî







.
,












zAsyncSniffer._runcCs| ¡|jr|j ¡dS)z!Starts AsyncSniffer in async modeN)rþrýrX)r`r6r6r7rXÍszAsyncSniffer.startcCsR|jrFy| ¡Wntk
r.tdƒ‚YnX|rB| ¡|jSdStdƒ‚dS)z'Stops AsyncSniffer if not in async modez+Unsupported (offline or unsupported socket)Nz
Not started !)rürrÞrrYrg)r`rYr6r6r7rxÔszAsyncSniffer.stopcOs|jr|jj||ŽdS)N)rýrY)r`r†r‡r6r6r7rYåszAsyncSniffer.join)T)
r0r1r2r…rfrþr‚rXrxrYr6r6r6r7r©s*=	D
rcOstƒ}|j||Žtt|jƒS)N)rr‚r)rrg)r†r‡rwr6r6r7ÚsniffësrcsÊx&dD]}||krt d|¡||=qW|fdd„}	|	|dƒ\}
}|	|dƒ\}}||||
i‰i‰|dk	rp|ˆ|<|dk	r€|ˆ|<‡‡fdd	„‰|dkrœˆ}n|‰‡‡fd
d„}t||
|||i|dœ|—ŽS)
aØForward traffic between interfaces if1 and if2, sniff and return
    the exchanged packets.

    :param if1: the interfaces to use (interface names or opened sockets).
    :param if2:
    :param xfrm12: a function to call when forwarding a packet from if1 to
        if2. If it returns True, the packet is forwarded as it. If it
        returns False or None, the packet is discarded. If it returns a
        packet, this packet is forwarded instead of the original packet
        one.
    :param xfrm21: same as xfrm12 for packets forwarded from if2 to if1.

    The other arguments are the same than for the function sniff(),
    except for offline, opened_socket and iface that are ignored.
    See help(sniff) for more.
    )rrr˜z@Argument %s cannot be used in bridge_and_sniff() -- ignoring it.cSsBt|tƒr|d|fS|s0t|p$tjƒ}| ¡}||d|fSdS)Nziface%d)r˜)rNrrrr˜r )r˜rrr6r6r7Ú_init_sockets
z&bridge_and_sniff.<locals>._init_socketr:r¦Nc	sÔyˆ|jpd}Wntk
r&dSX|jˆkryˆ|j|ƒ}Wn,tk
rptjd| ¡|jdddSXt|tƒrŠ|s„dS|}q”|}n|}y| |¡Wn,tk
rÎtjd| ¡|jddYnXdS)NÚzOException in transformation function for packet [%s] received on %s -- droppingT)Úexc_infoz)Cannot forward packet [%s] received on %s)	rÚKeyErrorrorr	rärNÚboolrk)raZsendsockZ_newpktZnewpkt)ÚpeersÚxfrmsr6r7Úprn_send,s.

z"bridge_and_sniff.<locals>.prn_sendcsˆ|ƒˆ|ƒS)Nr6)ra)Úprn_origr$r6r7r}Oszbridge_and_sniff.<locals>.prn)rr})rr	r)Zif1Zif2Zxfrm12Zxfrm21r}rr†r“ÚargrZsckt1Zsckt2r6)r"r%r$r#r7Úbridge_and_sniffös*


r'csd|kr| d¡}n"d|kr0tt| d¡ƒj}ntj}td|ƒdg‰‡fdd„}t||ddœ|—Žtd	ˆdˆdd
kr‚dndfƒd
S)z|Sniff packets and print them calling pkt.summary().
    This tries to replicate what text-wireshark (tshark) would look liker˜rzCapturing on '%s'rcs,tdˆd| ¡fƒˆdd7<dS)Nz%5d	%srr:)r]rä)ra)rqr6r7Ú_cbjsztshark.<locals>._cbF)r}r~z
%d packet%s capturedr:rrN)rÊr)rr˜rr]r)r†r“r˜r(r6)rqr7ÚtsharkXsr))rrNNFF)rrNNNFFN)N)NNN)NNFrFNNF)NNNr)NNNr)rNFN)NNNN)NNNr)NNNNN)NNNr)NNNN)cr…Ú
__future__rrrZÚ	threadingrrrurÈr©rlrOZscapy.compatrZ
scapy.datarZscapy.configrZscapy.errorr	Zscapy.interfacesr
rrZscapy.packetr
Zscapy.utilsrrrrrZscapy.plistrrrrrrZscapy.base_classesrrZ
scapy.modulesrZscapy.modules.six.movesrZscapy.sessionsrZscapy.supersocketrrr r!r"r#r$r%r&r'r(r)r*r+r¡Zscapy.routeZscapyr,Z_DOC_SNDRCV_PARAMSZ_GlobSessionTypeÚobjectr8rˆr—r›ÚcommandsÚregisterrkr£r·r°rÛrŸràrârãZsr_funcrðrñròr÷rørùrúrûrrr'r)r6r6r6r7Ú<module>s0Q
8
?9
9

D\