Skip to content

Commit f8e184b

Browse files
committed
fix formatting and remove unused method
1 parent 3c240b4 commit f8e184b

File tree

1 file changed

+36
-41
lines changed

1 file changed

+36
-41
lines changed

src/urh/dev/PCAPNG.py

+36-41
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import os
22
import struct
3-
import time
43
import math
54

65
from urh.util.Logger import logger
76

7+
88
# Refer to PCAPNG spec
99
# https://www.ietf.org/staging/draft-tuexen-opsawg-pcapng-02.html
1010

@@ -14,15 +14,15 @@ def __init__(self):
1414
self.__messages_timestamps = []
1515
self.__filename = ""
1616

17-
def __build_pcapng_shb(self, shb_userappl : str = "", shb_hardware : str = "") -> bytes:
17+
def __build_pcapng_shb(self, shb_userappl: str = "", shb_hardware: str = "") -> bytes:
1818
BLOCKTYPE = 0x0A0D0D0A
1919
HEADERS_BLOCK_LENGTH = 28
2020
MAGIC_NUMBER = 0x1A2B3C4D
21-
VERSION_MAJOR, VERSION_MINOR = 1,0
22-
SECTIONLENGTH = 0xFFFFFFFFFFFFFFFF # -1 => Not specified
21+
VERSION_MAJOR, VERSION_MINOR = 1, 0
22+
SECTIONLENGTH = 0xFFFFFFFFFFFFFFFF # -1 => Not specified
2323

24-
shb_userappl_padded_len = math.ceil(len(shb_userappl)/4) * 4
25-
shb_hardware_padded_len = math.ceil(len(shb_hardware)/4) * 4
24+
shb_userappl_padded_len = math.ceil(len(shb_userappl) / 4) * 4
25+
shb_hardware_padded_len = math.ceil(len(shb_hardware) / 4) * 4
2626

2727
total_block_len = HEADERS_BLOCK_LENGTH
2828
if shb_userappl_padded_len > 0:
@@ -32,11 +32,11 @@ def __build_pcapng_shb(self, shb_userappl : str = "", shb_hardware : str = "") -
3232
total_block_len += shb_hardware_padded_len + 4
3333

3434
shb = struct.pack(">IIIHHQ",
35-
BLOCKTYPE,
36-
total_block_len,
37-
MAGIC_NUMBER,
38-
VERSION_MAJOR, VERSION_MINOR,
39-
SECTIONLENGTH)
35+
BLOCKTYPE,
36+
total_block_len,
37+
MAGIC_NUMBER,
38+
VERSION_MAJOR, VERSION_MINOR,
39+
SECTIONLENGTH)
4040

4141
if shb_userappl != "":
4242
SHB_USERAPPL = 4
@@ -49,57 +49,57 @@ def __build_pcapng_shb(self, shb_userappl : str = "", shb_hardware : str = "") -
4949
strpad = shb_hardware.ljust(shb_hardware_padded_len, "\0")
5050
shb += struct.pack(">HH", SHB_HARDWARE, shb_hardware_padded_len)
5151
shb += bytes(strpad, 'ascii')
52-
53-
shb += struct.pack(">I",total_block_len)
52+
53+
shb += struct.pack(">I", total_block_len)
5454
return shb
5555

5656
def __build_pcapng_idb(self, link_type) -> bytes:
5757
BLOCKTYPE = 0x00000001
5858
BLOCKLENGTH = 20
5959
SNAP_LEN = 0
6060

61-
return struct.pack(">IIHHII",
62-
BLOCKTYPE,
63-
BLOCKLENGTH,
64-
link_type, 0,
65-
SNAP_LEN,
66-
BLOCKLENGTH)
61+
return struct.pack(">IIHHII",
62+
BLOCKTYPE,
63+
BLOCKLENGTH,
64+
link_type, 0,
65+
SNAP_LEN,
66+
BLOCKLENGTH)
6767

68-
def __build_pcapng_epb(self, packet : bytes, timestamp : float) -> bytes:
68+
def __build_pcapng_epb(self, packet: bytes, timestamp: float) -> bytes:
6969
BLOCKTYPE = 0x00000006
7070
BLOCKHEADERLEN = 32
7171
INTERFACE_ID = 0
7272

7373
captured_packet_len = len(packet)
7474
original_packet_len = captured_packet_len
75-
padded_packet_len = math.ceil(captured_packet_len/4) * 4
75+
padded_packet_len = math.ceil(captured_packet_len / 4) * 4
7676
padding_len = padded_packet_len - original_packet_len
7777
padded_packet = packet + bytearray(padding_len)
7878
block_total_length = BLOCKHEADERLEN + padded_packet_len
79-
timestamp_int = int(timestamp *1e6) # Set the proper resolution
79+
timestamp_int = int(timestamp * 1e6) # Set the proper resolution
8080
timestamp_high = timestamp_int >> 32
8181
timestamp_low = timestamp_int & 0x00000000FFFFFFFF
8282

83-
epb = struct.pack(">IIIIIII",
84-
BLOCKTYPE,
85-
block_total_length,
86-
INTERFACE_ID,
87-
timestamp_high,
88-
timestamp_low,
89-
captured_packet_len,
90-
original_packet_len)
83+
epb = struct.pack(">IIIIIII",
84+
BLOCKTYPE,
85+
block_total_length,
86+
INTERFACE_ID,
87+
timestamp_high,
88+
timestamp_low,
89+
captured_packet_len,
90+
original_packet_len)
9191
epb += padded_packet
9292
epb += struct.pack(">I", block_total_length)
9393
return epb
9494

95-
############################################################################################################
96-
# PUBLIC INTERFACES
97-
def create_pcapng_file(self, filename : str, shb_userappl : str = "", shb_hardware : str = "", link_type : int =147) -> bytes:
98-
if (filename == ""):
95+
def create_pcapng_file(self, filename: str, shb_userappl: str = "", shb_hardware: str = "",
96+
link_type: int = 147) -> bytes:
97+
if filename == "":
9998
return
99+
100100
self.__filename = filename
101101

102-
shb_bytes = self.__build_pcapng_shb(shb_userappl,shb_hardware)
102+
shb_bytes = self.__build_pcapng_shb(shb_userappl, shb_hardware)
103103
idb_bytes = self.__build_pcapng_idb(link_type)
104104

105105
if os.path.isfile(self.__filename):
@@ -109,12 +109,7 @@ def create_pcapng_file(self, filename : str, shb_userappl : str = "", shb_hardwa
109109
f.write(shb_bytes)
110110
f.write(idb_bytes)
111111

112-
def pcapng_file_append_packet(self, packet : bytes, timestamp: float):
113-
with open(self.__filename, "ab") as f:
114-
f.write(self.__build_pcapng_epb(packet, timestamp))
115-
116-
def pcapng_file_append_multi_packets(self, packets : list):
112+
def pcapng_file_append_multi_packets(self, packets: list):
117113
with open(self.__filename, "ab") as f:
118114
for packet, timestamp in packets:
119115
f.write(self.__build_pcapng_epb(packet, timestamp))
120-

0 commit comments

Comments
 (0)