Skip to content

Commit f8958ab

Browse files
committed
working on pcapng protocol impl (added IF options)
1 parent 8312727 commit f8958ab

File tree

1 file changed

+281
-1
lines changed

1 file changed

+281
-1
lines changed

pcapkit/protocols/misc/pcapng.py

Lines changed: 281 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import enum
1818
import io
1919
import math
20+
import os
21+
import platform
2022
import re
2123
import sys
2224
import textwrap
@@ -1582,6 +1584,144 @@ def _read_option_if_filter(self, schema: 'Schema_IF_FilterOption', *,
15821584
)
15831585
return option
15841586

1587+
def _read_option_if_os(self, schema: 'Schema_IF_OSOption', *,
1588+
options: 'Option') -> 'Data_IF_OSOption':
1589+
"""Read PCAP-NG ``if_os`` option.
1590+
1591+
Args:
1592+
schema: Parsed option schema.
1593+
options: Parsed PCAP-NG options.
1594+
1595+
Returns:
1596+
Constructed option data.
1597+
1598+
"""
1599+
if self._opt[schema.type] > 0:
1600+
raise ProtocolError(f'PCAP-NG: [if_os] option must be only one, '
1601+
f'but {self._opt[schema.type] + 1} found.')
1602+
1603+
option = Data_IF_OSOption(
1604+
type=schema.type,
1605+
length=schema.length,
1606+
os=schema.os,
1607+
)
1608+
return option
1609+
1610+
def _read_option_if_fcslen(self, schema: 'Schema_IF_FCSLenOption', *,
1611+
options: 'Option') -> 'Data_IF_FCSLenOption':
1612+
"""Read PCAP-NG ``if_fcslen`` option.
1613+
1614+
Args:
1615+
schema: Parsed option schema.
1616+
options: Parsed PCAP-NG options.
1617+
1618+
Returns:
1619+
Constructed option data.
1620+
1621+
"""
1622+
if self._opt[schema.type] > 0:
1623+
raise ProtocolError(f'PCAP-NG: [if_fcslen] option must be only one, '
1624+
f'but {self._opt[schema.type] + 1} found.')
1625+
1626+
option = Data_IF_FCSLenOption(
1627+
type=schema.type,
1628+
length=schema.length,
1629+
fcs_length=schema.fcslen,
1630+
)
1631+
return option
1632+
1633+
def _read_option_if_tsoffset(self, schema: 'Schema_IF_TSOffsetOption', *,
1634+
options: 'Option') -> 'Data_IF_TSOffsetOption':
1635+
"""Read PCAP-NG ``if_tsoffset`` option.
1636+
1637+
Args:
1638+
schema: Parsed option schema.
1639+
options: Parsed PCAP-NG options.
1640+
1641+
Returns:
1642+
Constructed option data.
1643+
1644+
"""
1645+
if self._opt[schema.type] > 0:
1646+
raise ProtocolError(f'PCAP-NG: [if_tsoffset] option must be only one, '
1647+
f'but {self._opt[schema.type] + 1} found.')
1648+
1649+
option = Data_IF_TSOffsetOption(
1650+
type=schema.type,
1651+
length=schema.length,
1652+
offset=schema.tsoffset,
1653+
)
1654+
return option
1655+
1656+
def _read_option_if_hardware(self, schema: 'Schema_IF_HardwareOption', *,
1657+
options: 'Option') -> 'Data_IF_HardwareOption':
1658+
"""Read PCAP-NG ``if_hardware`` option.
1659+
1660+
Args:
1661+
schema: Parsed option schema.
1662+
options: Parsed PCAP-NG options.
1663+
1664+
Returns:
1665+
Constructed option data.
1666+
1667+
"""
1668+
if self._opt[schema.type] > 0:
1669+
raise ProtocolError(f'PCAP-NG: [if_hardware] option must be only one, '
1670+
f'but {self._opt[schema.type] + 1} found.')
1671+
1672+
option = Data_IF_HardwareOption(
1673+
type=schema.type,
1674+
length=schema.length,
1675+
hardware=schema.hardware,
1676+
)
1677+
return option
1678+
1679+
def _read_option_if_txspeed(self, schema: 'Schema_IF_TxSpeedOption', *,
1680+
options: 'Option') -> 'Data_IF_TxSpeedOption':
1681+
"""Read PCAP-NG ``if_txspeed`` option.
1682+
1683+
Args:
1684+
schema: Parsed option schema.
1685+
options: Parsed PCAP-NG options.
1686+
1687+
Returns:
1688+
Constructed option data.
1689+
1690+
"""
1691+
if self._opt[schema.type] > 0:
1692+
raise ProtocolError(f'PCAP-NG: [if_txspeed] option must be only one, '
1693+
f'but {self._opt[schema.type] + 1} found.')
1694+
1695+
option = Data_IF_TxSpeedOption(
1696+
type=schema.type,
1697+
length=schema.length,
1698+
speed=schema.tx_speed,
1699+
)
1700+
return option
1701+
1702+
def _read_option_if_rxspeed(self, schema: 'Schema_IF_RxSpeedOption', *,
1703+
options: 'Option') -> 'Data_IF_RxSpeedOption':
1704+
"""Read PCAP-NG ``if_rxspeed`` option.
1705+
1706+
Args:
1707+
schema: Parsed option schema.
1708+
options: Parsed PCAP-NG options.
1709+
1710+
Returns:
1711+
Constructed option data.
1712+
1713+
"""
1714+
if self._opt[schema.type] > 0:
1715+
raise ProtocolError(f'PCAP-NG: [if_rxspeed] option must be only one, '
1716+
f'but {self._opt[schema.type] + 1} found.')
1717+
1718+
option = Data_IF_RxSpeedOption(
1719+
type=schema.type,
1720+
length=schema.length,
1721+
speed=schema.rx_speed,
1722+
)
1723+
return option
1724+
15851725
def _make_block_unknown(self, block: 'Optional[Data_UnknownBlock]' = None, *,
15861726
data: 'bytes' = b'',
15871727
**kwargs: 'Any') -> 'Schema_UnknownBlock':
@@ -2108,7 +2248,7 @@ def _make_option_if_tzone(self, type: 'Enum_OptionType', option: 'Optional[Data_
21082248

21092249
return Schema_IF_TZoneOption(
21102250
type=type,
2111-
length=1,
2251+
length=4,
21122252
tzone=tzone_val,
21132253
)
21142254

@@ -2149,3 +2289,143 @@ def _make_option_if_filter(self, type: 'Enum_OptionType', option: 'Optional[Data
21492289
code=filter_val,
21502290
filter=expr_val,
21512291
)
2292+
2293+
def _make_option_if_os(self, type: 'Enum_OptionType', option: 'Optional[Data_IF_OSOption]' = None, *,
2294+
os: 'str' = platform.platform(),
2295+
**kwargs: 'Any') -> 'Schema_IF_OSOption':
2296+
"""Make PCAP-NG ``if_os`` option.
2297+
2298+
Args:
2299+
type: Option type.
2300+
option: Option data model.
2301+
os: Operating system name.
2302+
**kwargs: Arbitrary keyword arguments.
2303+
2304+
Returns:
2305+
Constructed option schema.
2306+
2307+
"""
2308+
if self._opt[type] > 0:
2309+
raise ProtocolError(f'PCAP-NG: [if_os] option must be only one, '
2310+
f'but {self._opt[type] + 1} found.')
2311+
2312+
if option is not None:
2313+
os = option.os
2314+
2315+
return Schema_IF_OSOption(
2316+
type=type,
2317+
length=len(os),
2318+
os=os,
2319+
)
2320+
2321+
def _make_option_if_fcslen(self, type: 'Enum_OptionType', option: 'Optional[Data_IF_FCSLenOption]' = None, *,
2322+
fcs_length: 'int' = 4,
2323+
**kwargs: 'Any') -> 'Schema_IF_FCSLenOption':
2324+
"""Make PCAP-NG ``if_fcslen`` option.
2325+
2326+
Args:
2327+
type: Option type.
2328+
option: Option data model.
2329+
fcs_length: FCS length, in bytes.
2330+
**kwargs: Arbitrary keyword arguments.
2331+
2332+
Returns:
2333+
Constructed option schema.
2334+
2335+
"""
2336+
if self._opt[type] > 0:
2337+
raise ProtocolError(f'PCAP-NG: [if_fcslen] option must be only one, '
2338+
f'but {self._opt[type] + 1} found.')
2339+
2340+
if option is not None:
2341+
fcs_length = option.fcs_length
2342+
2343+
return Schema_IF_FCSLenOption(
2344+
type=type,
2345+
length=1,
2346+
fcslen=fcs_length,
2347+
)
2348+
2349+
def _make_option_if_hardware(self, type: 'Enum_OptionType', option: 'Optional[Data_IF_HardwareOption]' = None, *,
2350+
hardware: 'str' = platform.processor(),
2351+
**kwargs: 'Any') -> 'Schema_IF_HardwareOption':
2352+
"""Make PCAP-NG ``if_hardware`` option.
2353+
2354+
Args:
2355+
type: Option type.
2356+
option: Option data model.
2357+
os: Operating system name.
2358+
**kwargs: Arbitrary keyword arguments.
2359+
2360+
Returns:
2361+
Constructed option schema.
2362+
2363+
"""
2364+
if self._opt[type] > 0:
2365+
raise ProtocolError(f'PCAP-NG: [if_hardware] option must be only one, '
2366+
f'but {self._opt[type] + 1} found.')
2367+
2368+
if option is not None:
2369+
hardware = option.hardware
2370+
2371+
return Schema_IF_HardwareOption(
2372+
type=type,
2373+
length=len(hardware),
2374+
hardware=hardware,
2375+
)
2376+
2377+
def _make_option_if_txspeed(self, type: 'Enum_OptionType', option: 'Optional[Data_IF_TxSpeedOption]' = None, *,
2378+
speed: 'int' = 100000000,
2379+
**kwargs: 'Any') -> 'Schema_IF_TxSpeedOption':
2380+
"""Make PCAP-NG ``if_txspeed`` option.
2381+
2382+
Args:
2383+
type: Option type.
2384+
option: Option data model.
2385+
speed: Interface transmit speed, in bits per second.
2386+
**kwargs: Arbitrary keyword arguments.
2387+
2388+
Returns:
2389+
Constructed option schema.
2390+
2391+
"""
2392+
if self._opt[type] > 0:
2393+
raise ProtocolError(f'PCAP-NG: [if_txspeed] option must be only one, '
2394+
f'but {self._opt[type] + 1} found.')
2395+
2396+
if option is not None:
2397+
speed = option.speed
2398+
2399+
return Schema_IF_TxSpeedOption(
2400+
type=type,
2401+
length=8,
2402+
tx_speed=speed,
2403+
)
2404+
2405+
def _make_option_if_rxspeed(self, type: 'Enum_OptionType', option: 'Optional[Data_IF_RxSpeedOption]' = None, *,
2406+
speed: 'int' = 100000000,
2407+
**kwargs: 'Any') -> 'Schema_IF_RxSpeedOption':
2408+
"""Make PCAP-NG ``if_rxspeed`` option.
2409+
2410+
Args:
2411+
type: Option type.
2412+
option: Option data model.
2413+
speed: Interface receive speed, in bits per second.
2414+
**kwargs: Arbitrary keyword arguments.
2415+
2416+
Returns:
2417+
Constructed option schema.
2418+
2419+
"""
2420+
if self._opt[type] > 0:
2421+
raise ProtocolError(f'PCAP-NG: [if_txspeed] option must be only one, '
2422+
f'but {self._opt[type] + 1} found.')
2423+
2424+
if option is not None:
2425+
speed = option.speed
2426+
2427+
return Schema_IF_RxSpeedOption(
2428+
type=type,
2429+
length=8,
2430+
rx_speed=speed,
2431+
)

0 commit comments

Comments
 (0)