Skip to content

Commit cdd522c

Browse files
authored
Merge pull request #456 from MBradbury/inmem-timeout
Allow configuring the tshark timeout in InMemCapture
2 parents ded5b36 + 1b8a0a5 commit cdd522c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/pyshark/capture/inmem_capture.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _write_packet(self, packet, sniff_time):
100100
self._current_tshark.stdin.write(struct.pack("IIII", secs, usecs, len(packet), len(packet)))
101101
self._current_tshark.stdin.write(packet)
102102

103-
def parse_packet(self, binary_packet, sniff_time=None):
103+
def parse_packet(self, binary_packet, sniff_time=None, timeout=DEFAULT_TIMEOUT):
104104
"""Parses a single binary packet and returns its parsed version.
105105
106106
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):
109109
"""
110110
if sniff_time is not None:
111111
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]
113113

114-
def parse_packets(self, binary_packets, sniff_times=None):
114+
def parse_packets(self, binary_packets, sniff_times=None, timeout=DEFAULT_TIMEOUT):
115115
"""Parses binary packets and return a list of parsed packets.
116116
117117
DOES NOT CLOSE tshark. It must be closed manually by calling close() when you're done
118118
working with it.
119119
"""
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))
121121

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):
123123
"""A coroutine which parses binary packets and return a list of parsed packets.
124124
125125
DOES NOT CLOSE tshark. It must be closed manually by calling close() when you're done
@@ -138,13 +138,13 @@ def callback(pkt):
138138
if len(parsed_packets) == len(binary_packets):
139139
raise StopCapture()
140140

141-
await self._get_parsed_packet_from_tshark(callback)
141+
await self._get_parsed_packet_from_tshark(callback, timeout)
142142
return parsed_packets
143143

144-
async def _get_parsed_packet_from_tshark(self, callback):
144+
async def _get_parsed_packet_from_tshark(self, callback, timeout):
145145
await self._current_tshark.stdin.drain()
146146
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)
148148
except asyncio.TimeoutError:
149149
await self.close_async()
150150
raise asyncio.TimeoutError("Timed out while waiting for tshark to parse packet. "
@@ -155,7 +155,7 @@ async def close_async(self):
155155
self._current_tshark = None
156156
await super(InMemCapture, self).close_async()
157157

158-
def feed_packet(self, binary_packet, linktype=LinkTypes.ETHERNET):
158+
def feed_packet(self, binary_packet, linktype=LinkTypes.ETHERNET, timeout=DEFAULT_TIMEOUT):
159159
"""
160160
DEPRECATED. Use parse_packet instead.
161161
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):
172172
"""
173173
warnings.warn("Deprecated method. Use InMemCapture.parse_packet() instead.")
174174
self._current_linktype = linktype
175-
pkt = self.parse_packet(binary_packet)
175+
pkt = self.parse_packet(binary_packet, timeout=timeout)
176176
self.close()
177177
self._packets.append(pkt)
178178
return pkt
179179

180-
def feed_packets(self, binary_packets, linktype=LinkTypes.ETHERNET):
180+
def feed_packets(self, binary_packets, linktype=LinkTypes.ETHERNET, timeout=DEFAULT_TIMEOUT):
181181
"""Gets a list of binary packets, parses them using tshark and returns their parsed values.
182182
183183
Keeps the packets in the internal packet list as well.
@@ -186,7 +186,7 @@ def feed_packets(self, binary_packets, linktype=LinkTypes.ETHERNET):
186186
can be found in the class LinkTypes)
187187
"""
188188
self._current_linktype = linktype
189-
parsed_packets = self.parse_packets(binary_packets)
189+
parsed_packets = self.parse_packets(binary_packets, timeout=timeout)
190190
self._packets.extend(parsed_packets)
191191
self.close()
192192
return parsed_packets

0 commit comments

Comments
 (0)