Writes a single packet to the pcap file. :param packet: Packet, or bytes for a single packet :type packet: scapy.packet.Packet or bytes :param sec: time the packet was captured, in seconds since epoch. If not supplied, defaults to now. :t
(self,
packet, # type: Union[bytes, Packet]
sec=None, # type: Optional[float]
usec=None, # type: Optional[int]
caplen=None, # type: Optional[int]
wirelen=None, # type: Optional[int]
)
| 2138 | self._write_header(pkt) |
| 2139 | |
| 2140 | def write_packet(self, |
| 2141 | packet, # type: Union[bytes, Packet] |
| 2142 | sec=None, # type: Optional[float] |
| 2143 | usec=None, # type: Optional[int] |
| 2144 | caplen=None, # type: Optional[int] |
| 2145 | wirelen=None, # type: Optional[int] |
| 2146 | ): |
| 2147 | # type: (...) -> None |
| 2148 | """ |
| 2149 | Writes a single packet to the pcap file. |
| 2150 | |
| 2151 | :param packet: Packet, or bytes for a single packet |
| 2152 | :type packet: scapy.packet.Packet or bytes |
| 2153 | :param sec: time the packet was captured, in seconds since epoch. If |
| 2154 | not supplied, defaults to now. |
| 2155 | :type sec: float |
| 2156 | :param usec: If ``nano=True``, then number of nanoseconds after the |
| 2157 | second that the packet was captured. If ``nano=False``, |
| 2158 | then the number of microseconds after the second the |
| 2159 | packet was captured. If ``sec`` is not specified, |
| 2160 | this value is ignored. |
| 2161 | :type usec: int or long |
| 2162 | :param caplen: The length of the packet in the capture file. If not |
| 2163 | specified, uses ``len(raw(packet))``. |
| 2164 | :type caplen: int |
| 2165 | :param wirelen: The length of the packet on the wire. If not |
| 2166 | specified, tries ``packet.wirelen``, otherwise uses |
| 2167 | ``caplen``. |
| 2168 | :type wirelen: int |
| 2169 | :return: None |
| 2170 | :rtype: None |
| 2171 | """ |
| 2172 | f_sec, usec = self._get_time(packet, sec, usec) |
| 2173 | |
| 2174 | rawpkt = bytes_encode(packet) |
| 2175 | caplen = len(rawpkt) if caplen is None else caplen |
| 2176 | |
| 2177 | if wirelen is None: |
| 2178 | if hasattr(packet, "wirelen"): |
| 2179 | wirelen = packet.wirelen |
| 2180 | if wirelen is None: |
| 2181 | wirelen = caplen |
| 2182 | |
| 2183 | comments = getattr(packet, "comments", None) |
| 2184 | ifname = getattr(packet, "sniffed_on", None) |
| 2185 | direction = getattr(packet, "direction", None) |
| 2186 | if not isinstance(packet, bytes): |
| 2187 | linktype: int = conf.l2types.layer2num[ |
| 2188 | packet.__class__ |
| 2189 | ] |
| 2190 | else: |
| 2191 | linktype = self.linktype |
| 2192 | if ifname is not None: |
| 2193 | ifname = str(ifname).encode('utf-8') |
| 2194 | self._write_packet( |
| 2195 | rawpkt, |
| 2196 | sec=f_sec, usec=usec, |
| 2197 | caplen=caplen, wirelen=wirelen, |
no test coverage detected