@@ -100,7 +100,7 @@ def _write_packet(self, packet, sniff_time):
100
100
self ._current_tshark .stdin .write (struct .pack ("IIII" , secs , usecs , len (packet ), len (packet )))
101
101
self ._current_tshark .stdin .write (packet )
102
102
103
- def parse_packet (self , binary_packet , sniff_time = None ):
103
+ def parse_packet (self , binary_packet , sniff_time = None , timeout = DEFAULT_TIMEOUT ):
104
104
"""Parses a single binary packet and returns its parsed version.
105
105
106
106
DOES NOT CLOSE tshark. It must be closed manually by calling close() when you're done
@@ -109,17 +109,17 @@ def parse_packet(self, binary_packet, sniff_time=None):
109
109
"""
110
110
if sniff_time is not None :
111
111
sniff_time = [sniff_time ]
112
- return self .parse_packets ([binary_packet ], sniff_time )[0 ]
112
+ return self .parse_packets ([binary_packet ], sniff_time , timeout )[0 ]
113
113
114
- def parse_packets (self , binary_packets , sniff_times = None ):
114
+ def parse_packets (self , binary_packets , sniff_times = None , timeout = DEFAULT_TIMEOUT ):
115
115
"""Parses binary packets and return a list of parsed packets.
116
116
117
117
DOES NOT CLOSE tshark. It must be closed manually by calling close() when you're done
118
118
working with it.
119
119
"""
120
- return asyncio .get_event_loop ().run_until_complete (self .parse_packets_async (binary_packets , sniff_times ))
120
+ return asyncio .get_event_loop ().run_until_complete (self .parse_packets_async (binary_packets , sniff_times , timeout ))
121
121
122
- async def parse_packets_async (self , binary_packets , sniff_times = None ):
122
+ async def parse_packets_async (self , binary_packets , sniff_times = None , timeout = DEFAULT_TIMEOUT ):
123
123
"""A coroutine which parses binary packets and return a list of parsed packets.
124
124
125
125
DOES NOT CLOSE tshark. It must be closed manually by calling close() when you're done
@@ -138,13 +138,13 @@ def callback(pkt):
138
138
if len (parsed_packets ) == len (binary_packets ):
139
139
raise StopCapture ()
140
140
141
- await self ._get_parsed_packet_from_tshark (callback )
141
+ await self ._get_parsed_packet_from_tshark (callback , timeout )
142
142
return parsed_packets
143
143
144
- async def _get_parsed_packet_from_tshark (self , callback ):
144
+ async def _get_parsed_packet_from_tshark (self , callback , timeout ):
145
145
await self ._current_tshark .stdin .drain ()
146
146
try :
147
- await asyncio .wait_for (self .packets_from_tshark (callback , close_tshark = False ), DEFAULT_TIMEOUT )
147
+ await asyncio .wait_for (self .packets_from_tshark (callback , close_tshark = False ), timeout )
148
148
except asyncio .TimeoutError :
149
149
await self .close_async ()
150
150
raise asyncio .TimeoutError ("Timed out while waiting for tshark to parse packet. "
@@ -155,7 +155,7 @@ async def close_async(self):
155
155
self ._current_tshark = None
156
156
await super (InMemCapture , self ).close_async ()
157
157
158
- def feed_packet (self , binary_packet , linktype = LinkTypes .ETHERNET ):
158
+ def feed_packet (self , binary_packet , linktype = LinkTypes .ETHERNET , timeout = DEFAULT_TIMEOUT ):
159
159
"""
160
160
DEPRECATED. Use parse_packet instead.
161
161
This function adds the packet to the packets list, and also closes and reopens tshark for
@@ -172,12 +172,12 @@ def feed_packet(self, binary_packet, linktype=LinkTypes.ETHERNET):
172
172
"""
173
173
warnings .warn ("Deprecated method. Use InMemCapture.parse_packet() instead." )
174
174
self ._current_linktype = linktype
175
- pkt = self .parse_packet (binary_packet )
175
+ pkt = self .parse_packet (binary_packet , timeout = timeout )
176
176
self .close ()
177
177
self ._packets .append (pkt )
178
178
return pkt
179
179
180
- def feed_packets (self , binary_packets , linktype = LinkTypes .ETHERNET ):
180
+ def feed_packets (self , binary_packets , linktype = LinkTypes .ETHERNET , timeout = DEFAULT_TIMEOUT ):
181
181
"""Gets a list of binary packets, parses them using tshark and returns their parsed values.
182
182
183
183
Keeps the packets in the internal packet list as well.
@@ -186,7 +186,7 @@ def feed_packets(self, binary_packets, linktype=LinkTypes.ETHERNET):
186
186
can be found in the class LinkTypes)
187
187
"""
188
188
self ._current_linktype = linktype
189
- parsed_packets = self .parse_packets (binary_packets )
189
+ parsed_packets = self .parse_packets (binary_packets , timeout = timeout )
190
190
self ._packets .extend (parsed_packets )
191
191
self .close ()
192
192
return parsed_packets
0 commit comments