Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/scapy/arch/__init__.py: 38%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SPDX-License-Identifier: GPL-2.0-only
2# This file is part of Scapy
3# See https://scapy.net/ for more information
4# Copyright (C) Philippe Biondi <phil@secdev.org>
6"""
7Operating system specific functionality.
8"""
10import socket
11import sys
13from scapy.config import conf, _set_conf_sockets
14from scapy.consts import LINUX, SOLARIS, WINDOWS, BSD
15from scapy.data import (
16 IPV6_ADDR_GLOBAL,
17 IPV6_ADDR_LOOPBACK,
18)
19from scapy.error import log_loading
20from scapy.interfaces import (
21 _GlobInterfaceType,
22 network_name,
23 resolve_iface,
24)
25from scapy.pton_ntop import inet_pton, inet_ntop
27from scapy.libs.extcap import load_extcap
29# Typing imports
30from typing import (
31 List,
32 Optional,
33 Tuple,
34 Union,
35 TYPE_CHECKING,
36)
38if TYPE_CHECKING:
39 from scapy.interfaces import NetworkInterface
41# Note: the typing of this file is heavily ignored because MyPy doesn't allow
42# to import the same function from different files.
44# This list only includes imports that are common across all platforms.
45__all__ = [ # noqa: F405
46 "get_if_addr",
47 "get_if_addr6",
48 "get_if_hwaddr",
49 "get_if_list",
50 "get_if_raw_addr",
51 "get_if_raw_addr6",
52 "get_working_if",
53 "in6_getifaddr",
54 "read_nameservers",
55 "read_routes",
56 "read_routes6",
57 "load_extcap",
58 "SIOCGIFHWADDR",
59]
61# BACKWARD COMPATIBILITY
62from scapy.interfaces import (
63 get_if_list,
64 get_working_if,
65)
68# We build the utils functions BEFORE importing the underlying handlers
69# because they might be themselves imported within the arch/ folder.
71def str2mac(s):
72 # Duplicated from scapy/utils.py for import reasons
73 # type: (bytes) -> str
74 return ("%02x:" * 6)[:-1] % tuple(s)
77def get_if_addr(iff):
78 # type: (_GlobInterfaceType) -> str
79 """
80 Returns the IPv4 of an interface or "0.0.0.0" if not available
81 """
82 return inet_ntop(socket.AF_INET, get_if_raw_addr(iff)) # noqa: F405
85def get_if_hwaddr(iff):
86 # type: (_GlobInterfaceType) -> str
87 """
88 Returns the MAC (hardware) address of an interface
89 """
90 return resolve_iface(iff).mac or "00:00:00:00:00:00"
93def get_if_addr6(niff):
94 # type: (_GlobInterfaceType) -> Optional[str]
95 """
96 Returns the main global unicast address associated with provided
97 interface, in human readable form. If no global address is found,
98 None is returned.
99 """
100 iff = network_name(niff)
101 scope = IPV6_ADDR_GLOBAL
102 if iff == conf.loopback_name:
103 scope = IPV6_ADDR_LOOPBACK
104 return next((x[0] for x in in6_getifaddr()
105 if x[2] == iff and x[1] == scope), None)
108def get_if_raw_addr6(iff):
109 # type: (_GlobInterfaceType) -> Optional[bytes]
110 """
111 Returns the main global unicast address associated with provided
112 interface, in network format. If no global address is found, None
113 is returned.
114 """
115 ip6 = get_if_addr6(iff)
116 if ip6 is not None:
117 return inet_pton(socket.AF_INET6, ip6)
119 return None
122# Next step is to import following architecture specific functions:
123# def attach_filter(s, filter, iface)
124# def get_if_raw_addr(iff)
125# def in6_getifaddr()
126# def read_nameservers()
127# def read_routes()
128# def read_routes6()
129# def set_promisc(s,iff,val=1)
131if LINUX:
132 from scapy.arch.linux import * # noqa F403
133elif BSD:
134 from scapy.arch.bpf.core import * # noqa F403
135 if not conf.use_pcap:
136 # Native
137 from scapy.arch.bpf.supersocket import * # noqa F403
138 conf.use_bpf = True
139 SIOCGIFHWADDR = 0 # mypy compat
140elif SOLARIS:
141 from scapy.arch.solaris import * # noqa F403
142elif WINDOWS:
143 from scapy.arch.windows import * # noqa F403
144 from scapy.arch.windows.native import * # noqa F403
145 from scapy.arch.windows.sspi import * # noqa F403
146 SIOCGIFHWADDR = 0 # mypy compat
147else:
148 log_loading.critical(
149 "Scapy currently does not support %s! I/O will NOT work!" % sys.platform
150 )
151 SIOCGIFHWADDR = 0 # mypy compat
153 # DUMMYS
154 def get_if_raw_addr(iff: Union['NetworkInterface', str]) -> bytes:
155 return b"\0\0\0\0"
157 def in6_getifaddr() -> List[Tuple[str, int, str]]:
158 return []
160 def read_nameservers() -> List[str]:
161 return []
163 def read_routes() -> List[str]:
164 return []
166 def read_routes6() -> List[str]:
167 return []
169if LINUX or BSD:
170 conf.load_layers.append("tuntap")
172_set_conf_sockets() # Apply config