Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/scapy/data.py: 67%

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

387 statements  

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> 

5 

6""" 

7Global variables and functions for handling external data sets. 

8 

9This module lazily loads the following files or their platform equivalents: 

10 

11- /etc/protocols: IP protocol names (IP_PROTOS). 

12- /etc/services: TCP, UDP, and SCTP service names (TCP_SERVICES, 

13 UDP_SERVICES, and SCTP_SERVICES). 

14- /etc/ethertypes: Ethernet protocol names (ETHER_TYPES). 

15- Wireshark's share/wireshark/manuf: MAC manufacturers (MANUFDB). 

16 

17EtherTypes and manufacturers fall back to copies bundled with Scapy. 

18Bluetooth company identifiers (BLUETOOTH_CORE_COMPANY_IDENTIFIERS) are also 

19loaded lazily from bundled data. EtherTypes, services, manufacturers, and 

20Bluetooth identifiers use an on-disk pickle cache when available. 

21""" 

22 

23import calendar 

24import hashlib 

25import os 

26import pickle 

27import warnings 

28 

29from functools import lru_cache, wraps 

30 

31from scapy.consts import FREEBSD, NETBSD, OPENBSD, WINDOWS 

32from scapy.dadict import DADict, fixname 

33from scapy.error import log_loading 

34 

35# Typing imports 

36from typing import ( 

37 Any, 

38 Callable, 

39 Dict, 

40 Iterator, 

41 List, 

42 Optional, 

43 Tuple, 

44 Union, 

45 cast, 

46) 

47from scapy.compat import DecoratorCallable 

48 

49 

50############ 

51# Consts # 

52############ 

53 

54ETHER_ANY = b"\x00" * 6 

55ETHER_BROADCAST = b"\xff" * 6 

56 

57# From bits/socket.h 

58SOL_PACKET = 263 

59# From asm/socket.h 

60SO_ATTACH_FILTER = 26 

61SO_TIMESTAMPNS = 35 # SO_TIMESTAMPNS_OLD: not 2038 safe 

62 

63ETH_P_ALL = 3 

64ETH_P_IP = 0x800 

65ETH_P_ARP = 0x806 

66ETH_P_IPV6 = 0x86dd 

67ETH_P_MACSEC = 0x88e5 

68 

69# From net/if_arp.h 

70ARPHDR_ETHER = 1 

71ARPHDR_METRICOM = 23 

72ARPHDR_PPP = 512 

73ARPHDR_LOOPBACK = 772 

74ARPHDR_TUN = 65534 

75 

76# From pcap/dlt.h 

77DLT_NULL = 0 

78DLT_EN10MB = 1 

79DLT_EN3MB = 2 

80DLT_AX25 = 3 

81DLT_PRONET = 4 

82DLT_CHAOS = 5 

83DLT_IEEE802 = 6 

84DLT_ARCNET = 7 

85DLT_SLIP = 8 

86DLT_PPP = 9 

87DLT_FDDI = 10 

88if OPENBSD: 

89 DLT_RAW = 14 

90else: 

91 DLT_RAW = 12 

92DLT_RAW_ALT = 101 # At least in Argus 

93if FREEBSD or NETBSD: 

94 DLT_SLIP_BSDOS = 13 

95 DLT_PPP_BSDOS = 14 

96else: 

97 DLT_SLIP_BSDOS = 15 

98 DLT_PPP_BSDOS = 16 

99if FREEBSD: 

100 DLT_PFSYNC = 121 

101else: 

102 DLT_PFSYNC = 18 

103 DLT_HHDLC = 121 

104DLT_ATM_CLIP = 19 

105DLT_PPP_SERIAL = 50 

106DLT_PPP_ETHER = 51 

107DLT_SYMANTEC_FIREWALL = 99 

108DLT_C_HDLC = 104 

109DLT_IEEE802_11 = 105 

110DLT_FRELAY = 107 

111if OPENBSD: 

112 DLT_LOOP = 12 

113 DLT_ENC = 13 

114else: 

115 DLT_LOOP = 108 

116 DLT_ENC = 109 

117DLT_LINUX_SLL = 113 

118DLT_LTALK = 114 

119DLT_PFLOG = 117 

120DLT_PRISM_HEADER = 119 

121DLT_AIRONET_HEADER = 120 

122DLT_IP_OVER_FC = 122 

123DLT_IEEE802_11_RADIO = 127 

124DLT_ARCNET_LINUX = 129 

125DLT_LINUX_IRDA = 144 

126DLT_IEEE802_11_RADIO_AVS = 163 

127DLT_LINUX_LAPD = 177 

128DLT_BLUETOOTH_HCI_H4 = 187 

129DLT_USB_LINUX = 189 

130DLT_PPI = 192 

131DLT_IEEE802_15_4_WITHFCS = 195 

132DLT_BLUETOOTH_HCI_H4_WITH_PHDR = 201 

133DLT_AX25_KISS = 202 

134DLT_PPP_WITH_DIR = 204 

135DLT_FC_2 = 224 

136DLT_CAN_SOCKETCAN = 227 

137if OPENBSD: 

138 DLT_IPV4 = DLT_RAW 

139 DLT_IPV6 = DLT_RAW 

140else: 

141 DLT_IPV4 = 228 

142 DLT_IPV6 = 229 

143DLT_IEEE802_15_4_NOFCS = 230 

144DLT_USBPCAP = 249 

145DLT_NETLINK = 253 

146DLT_USB_DARWIN = 266 

147DLT_BLUETOOTH_LE_LL = 251 

148DLT_BLUETOOTH_LINUX_MONITOR = 254 

149DLT_BLUETOOTH_LE_LL_WITH_PHDR = 256 

150DLT_VSOCK = 271 

151DLT_NORDIC_BLE = 272 

152DLT_ETHERNET_MPACKET = 274 

153DLT_LINUX_SLL2 = 276 

154 

155# From net/ipv6.h on Linux (+ Additions) 

156IPV6_ADDR_UNICAST = 0x01 

157IPV6_ADDR_MULTICAST = 0x02 

158IPV6_ADDR_CAST_MASK = 0x0F 

159IPV6_ADDR_LOOPBACK = 0x10 

160IPV6_ADDR_GLOBAL = 0x00 

161IPV6_ADDR_LINKLOCAL = 0x20 

162IPV6_ADDR_SITELOCAL = 0x40 # deprecated since Sept. 2004 by RFC 3879 

163IPV6_ADDR_SCOPE_MASK = 0xF0 

164# IPV6_ADDR_COMPATv4 = 0x80 # deprecated; i.e. ::/96 

165# IPV6_ADDR_MAPPED = 0x1000 # i.e.; ::ffff:0.0.0.0/96 

166IPV6_ADDR_6TO4 = 0x0100 # Added to have more specific info (should be 0x0101 ?) # noqa: E501 

167IPV6_ADDR_UNSPECIFIED = 0x10000 

168 

169# from if_arp.h 

170ARPHRD_ETHER = 1 

171ARPHRD_EETHER = 2 

172ARPHRD_AX25 = 3 

173ARPHRD_PRONET = 4 

174ARPHRD_CHAOS = 5 

175ARPHRD_IEEE802 = 6 

176ARPHRD_ARCNET = 7 

177ARPHRD_DLCI = 15 

178ARPHRD_ATM = 19 

179ARPHRD_METRICOM = 23 

180ARPHRD_SLIP = 256 

181ARPHRD_CSLIP = 257 

182ARPHRD_SLIP6 = 258 

183ARPHRD_CSLIP6 = 259 

184ARPHRD_ADAPT = 264 

185ARPHRD_CAN = 280 

186ARPHRD_PPP = 512 

187ARPHRD_CISCO = 513 

188ARPHRD_RAWHDLC = 518 

189ARPHRD_TUNNEL = 768 

190ARPHRD_FRAD = 770 

191ARPHRD_LOOPBACK = 772 

192ARPHRD_LOCALTLK = 773 

193ARPHRD_FDDI = 774 

194ARPHRD_SIT = 776 

195ARPHRD_FCPP = 784 

196ARPHRD_FCAL = 785 

197ARPHRD_FCPL = 786 

198ARPHRD_FCFABRIC = 787 

199ARPHRD_IRDA = 783 

200ARPHRD_IEEE802_TR = 800 

201ARPHRD_IEEE80211 = 801 

202ARPHRD_IEEE80211_PRISM = 802 

203ARPHRD_IEEE80211_RADIOTAP = 803 

204ARPHRD_IEEE802154 = 804 

205ARPHRD_NETLINK = 824 

206ARPHRD_VSOCKMON = 826 # from pcap/pcap-linux.c 

207ARPHRD_LAPD = 8445 # from pcap/pcap-linux.c 

208ARPHRD_NONE = 0xFFFE 

209 

210ARPHRD_TO_DLT = { # netlink -> datalink 

211 ARPHRD_ETHER: DLT_EN10MB, 

212 ARPHRD_METRICOM: DLT_EN10MB, 

213 ARPHRD_LOOPBACK: DLT_EN10MB, 

214 ARPHRD_EETHER: DLT_EN3MB, 

215 ARPHRD_AX25: DLT_AX25_KISS, 

216 ARPHRD_PRONET: DLT_PRONET, 

217 ARPHRD_CHAOS: DLT_CHAOS, 

218 ARPHRD_CAN: DLT_LINUX_SLL, 

219 ARPHRD_IEEE802_TR: DLT_IEEE802, 

220 ARPHRD_IEEE802: DLT_EN10MB, 

221 ARPHRD_ARCNET: DLT_ARCNET_LINUX, 

222 ARPHRD_FDDI: DLT_FDDI, 

223 ARPHRD_ATM: -1, 

224 ARPHRD_IEEE80211: DLT_IEEE802_11, 

225 ARPHRD_IEEE80211_PRISM: DLT_PRISM_HEADER, 

226 ARPHRD_IEEE80211_RADIOTAP: DLT_IEEE802_11_RADIO, 

227 ARPHRD_PPP: DLT_RAW, 

228 ARPHRD_CISCO: DLT_C_HDLC, 

229 ARPHRD_SIT: DLT_RAW, 

230 ARPHRD_CSLIP: DLT_RAW, 

231 ARPHRD_SLIP6: DLT_RAW, 

232 ARPHRD_CSLIP6: DLT_RAW, 

233 ARPHRD_ADAPT: DLT_RAW, 

234 ARPHRD_SLIP: DLT_RAW, 

235 ARPHRD_RAWHDLC: DLT_RAW, 

236 ARPHRD_DLCI: DLT_RAW, 

237 ARPHRD_FRAD: DLT_FRELAY, 

238 ARPHRD_LOCALTLK: DLT_LTALK, 

239 18: DLT_IP_OVER_FC, 

240 ARPHRD_FCPP: DLT_FC_2, 

241 ARPHRD_FCAL: DLT_FC_2, 

242 ARPHRD_FCPL: DLT_FC_2, 

243 ARPHRD_FCFABRIC: DLT_FC_2, 

244 ARPHRD_IRDA: DLT_LINUX_IRDA, 

245 ARPHRD_LAPD: DLT_LINUX_LAPD, 

246 ARPHRD_NONE: DLT_RAW, 

247 ARPHRD_IEEE802154: DLT_IEEE802_15_4_NOFCS, 

248 ARPHRD_NETLINK: DLT_NETLINK, 

249 ARPHRD_VSOCKMON: DLT_VSOCK, 

250} 

251 

252# Constants for PPI header types. 

253PPI_DOT11COMMON = 2 

254PPI_DOT11NMAC = 3 

255PPI_DOT11NMACPHY = 4 

256PPI_SPECTRUM_MAP = 5 

257PPI_PROCESS_INFO = 6 

258PPI_CAPTURE_INFO = 7 

259PPI_AGGREGATION = 8 

260PPI_DOT3 = 9 

261PPI_GPS = 30002 

262PPI_VECTOR = 30003 

263PPI_SENSOR = 30004 

264PPI_ANTENNA = 30005 

265PPI_BTLE = 30006 

266 

267# Human-readable type names for PPI header types. 

268PPI_TYPES = { 

269 PPI_DOT11COMMON: 'dot11-common', 

270 PPI_DOT11NMAC: 'dot11-nmac', 

271 PPI_DOT11NMACPHY: 'dot11-nmacphy', 

272 PPI_SPECTRUM_MAP: 'spectrum-map', 

273 PPI_PROCESS_INFO: 'process-info', 

274 PPI_CAPTURE_INFO: 'capture-info', 

275 PPI_AGGREGATION: 'aggregation', 

276 PPI_DOT3: 'dot3', 

277 PPI_GPS: 'gps', 

278 PPI_VECTOR: 'vector', 

279 PPI_SENSOR: 'sensor', 

280 PPI_ANTENNA: 'antenna', 

281 PPI_BTLE: 'btle', 

282} 

283 

284 

285# On windows, epoch is 01/02/1970 at 00:00 

286EPOCH = calendar.timegm((1970, 1, 2, 0, 0, 0, 3, 1, 0)) - 86400 

287 

288MTU = 0xffff # a.k.a give me all you have 

289 

290 

291# In fact, IANA enterprise-numbers file available at 

292# http://www.iana.org/assignments/enterprise-numbers 

293# is simply huge (more than 2Mo and 600Ko in bz2). I'll 

294# add only most common vendors, and encountered values. 

295# -- arno 

296IANA_ENTERPRISE_NUMBERS = { 

297 9: "ciscoSystems", 

298 35: "Nortel Networks", 

299 43: "3Com", 

300 311: "Microsoft", 

301 2636: "Juniper Networks, Inc.", 

302 4526: "Netgear", 

303 5771: "Cisco Systems, Inc.", 

304 5842: "Cisco Systems", 

305 11129: "Google, Inc", 

306 16885: "Nortel Networks", 

307} 

308 

309 

310def scapy_data_cache(name): 

311 # type: (str) -> Callable[[DecoratorCallable], DecoratorCallable] 

312 """ 

313 This decorator caches the loading of 'data' dictionaries, in order to reduce 

314 loading times. 

315 """ 

316 from scapy.main import SCAPY_CACHE_FOLDER 

317 if SCAPY_CACHE_FOLDER is None: 

318 # Cannot cache. 

319 return lambda x: x 

320 cachepath = SCAPY_CACHE_FOLDER / (name + ".pickle") 

321 

322 def _cached_loader(func, name=name): 

323 # type: (DecoratorCallable, str) -> DecoratorCallable 

324 """Wrap a data loader with the named disk cache.""" 

325 @wraps(func) 

326 def load(filename=None): 

327 # type: (Optional[str]) -> Any 

328 """Load data from the cache or the original loader.""" 

329 cache_id = hashlib.sha256((filename or "").encode()).hexdigest() 

330 if cachepath.exists(): 

331 try: 

332 with cachepath.open("rb") as fd: 

333 data = _CacheUnpickler(fd).load() 

334 if data["id"] == cache_id: 

335 return data["content"] 

336 except Exception as ex: 

337 log_loading.info( 

338 "Couldn't load cache from %s: %s" % ( 

339 str(cachepath), 

340 str(ex), 

341 ) 

342 ) 

343 cachepath.unlink(missing_ok=True) 

344 # Cache does not exist or is invalid. 

345 content = func(filename) 

346 data = { 

347 "content": content, 

348 "id": cache_id, 

349 } 

350 try: 

351 cachepath.parent.mkdir(parents=True, exist_ok=True) 

352 with cachepath.open("wb") as fd: 

353 pickle.dump(data, fd) 

354 return content 

355 except Exception as ex: 

356 log_loading.info( 

357 "Couldn't write cache into %s: %s" % ( 

358 str(cachepath), 

359 str(ex) 

360 ) 

361 ) 

362 return content 

363 return load # type: ignore 

364 return _cached_loader 

365 

366 

367def load_protocols(filename=None, _fallback=None, _integer_base=10, 

368 _cls=DADict[int, str]): 

369 # type: (Optional[str], Optional[Callable[[], Iterator[str]]], int, type) -> DADict[int, str] # noqa: E501 

370 """Parse /etc/protocols and return values as a dictionary.""" 

371 if filename is None: 

372 if WINDOWS: 

373 filename = os.path.join( 

374 os.environ["SystemRoot"], "system32", "drivers", "etc", "protocol" 

375 ) 

376 else: 

377 filename = "/etc/protocols" 

378 dct = _cls(_name=filename) # type: DADict[int, str] 

379 

380 def _process_data(fdesc): 

381 # type: (Iterator[str]) -> None 

382 for line in fdesc: 

383 try: 

384 shrp = line.find("#") 

385 if shrp >= 0: 

386 line = line[:shrp] 

387 line = line.strip() 

388 if not line: 

389 continue 

390 lt = tuple(line.split()) 

391 if len(lt) < 2 or not lt[0]: 

392 continue 

393 dct[int(lt[1], _integer_base)] = fixname(lt[0]) 

394 except Exception as e: 

395 log_loading.info( 

396 "Couldn't parse file [%s]: line [%r] (%s)", 

397 filename, 

398 line, 

399 e, 

400 ) 

401 try: 

402 if not filename: 

403 raise IOError 

404 with open(filename, "r", errors="backslashreplace") as fdesc: 

405 _process_data(fdesc) 

406 except IOError: 

407 if _fallback: 

408 _process_data(_fallback()) 

409 else: 

410 log_loading.info("Can't open %s file", filename) 

411 return dct 

412 

413 

414class EtherDA(DADict[int, str]): 

415 # Backward compatibility: accept 

416 # ETHER_TYPES["MY_GREAT_TYPE"] = 12 

417 def __setitem__(self, attr, val): 

418 # type: (int, str) -> None 

419 if isinstance(attr, str): 

420 attr, val = val, attr 

421 warnings.warn( 

422 "ETHER_TYPES now uses the integer value as key !", 

423 DeprecationWarning 

424 ) 

425 super(EtherDA, self).__setitem__(attr, val) 

426 

427 def __getitem__(self, attr): 

428 # type: (int) -> Any 

429 if isinstance(attr, str): 

430 warnings.warn( 

431 "Please use 'ETHER_TYPES.%s'" % attr, 

432 DeprecationWarning 

433 ) 

434 return super(EtherDA, self).__getattr__(attr) 

435 return super(EtherDA, self).__getitem__(attr) 

436 

437 

438@scapy_data_cache("ethertypes") 

439def load_ethertypes(filename=None): 

440 # type: (Optional[str]) -> EtherDA 

441 """Load EtherTypes from a file or the copy bundled with Scapy.""" 

442 def _fallback() -> Iterator[str]: 

443 # Fallback. Lazy loaded as the file is big. 

444 from scapy.libs.ethertypes import DATA 

445 return iter(DATA.split("\n")) 

446 prot = load_protocols(filename or "scapy/ethertypes", 

447 _fallback=_fallback, 

448 _integer_base=16, 

449 _cls=EtherDA) 

450 return cast(EtherDA, prot) 

451 

452 

453@scapy_data_cache("services") 

454def load_services(filename=None): 

455 # type: (Optional[str]) -> Tuple[DADict[int, str], DADict[int, str], DADict[int, str]] # noqa: E501 

456 """Parse /etc/services and return TCP, UDP, and SCTP dictionaries.""" 

457 if filename is None: 

458 if WINDOWS: 

459 filename = os.path.join( 

460 os.environ["SystemRoot"], "system32", "drivers", "etc", "services" 

461 ) 

462 else: 

463 filename = "/etc/services" 

464 tdct = DADict(_name="%s-tcp" % filename) # type: DADict[int, str] 

465 udct = DADict(_name="%s-udp" % filename) # type: DADict[int, str] 

466 sdct = DADict(_name="%s-sctp" % filename) # type: DADict[int, str] 

467 dcts = { 

468 b"tcp": tdct, 

469 b"udp": udct, 

470 b"sctp": sdct, 

471 } 

472 try: 

473 with open(filename, "rb") as fdesc: 

474 for line in fdesc: 

475 try: 

476 shrp = line.find(b"#") 

477 if shrp >= 0: 

478 line = line[:shrp] 

479 line = line.strip() 

480 if not line: 

481 continue 

482 lt = tuple(line.split()) 

483 if len(lt) < 2 or not lt[0]: 

484 continue 

485 if b"/" not in lt[1]: 

486 continue 

487 port, proto = lt[1].split(b"/", 1) 

488 try: 

489 dtct = dcts[proto] 

490 except KeyError: 

491 continue 

492 name = fixname(lt[0]) 

493 if b"-" in port: 

494 sport, eport = port.split(b"-") 

495 for i in range(int(sport), int(eport) + 1): 

496 dtct[i] = name 

497 else: 

498 dtct[int(port)] = name 

499 except Exception as e: 

500 log_loading.warning( 

501 "Couldn't parse file [%s]: line [%r] (%s)", 

502 filename, 

503 line, 

504 e, 

505 ) 

506 except IOError: 

507 log_loading.info("Can't open /etc/services file") 

508 return tdct, udct, sdct 

509 

510 

511class ManufDA(DADict[str, Tuple[str, str]]): 

512 def ident(self, v): 

513 # type: (Any) -> str 

514 return fixname(v[0] if isinstance(v, tuple) else v) 

515 

516 def _get_manuf_couple(self, mac): 

517 # type: (str) -> Tuple[str, str] 

518 oui = ":".join(mac.split(":")[:3]).upper() 

519 return self.d.get(oui, (mac, mac)) 

520 

521 def _get_manuf(self, mac): 

522 # type: (str) -> str 

523 return self._get_manuf_couple(mac)[1] 

524 

525 def _get_short_manuf(self, mac): 

526 # type: (str) -> str 

527 return self._get_manuf_couple(mac)[0] 

528 

529 def _resolve_MAC(self, mac): 

530 # type: (str) -> str 

531 oui = ":".join(mac.split(":")[:3]).upper() 

532 if oui in self: 

533 return ":".join([self[oui][0]] + mac.split(":")[3:]) 

534 return mac 

535 

536 def lookup(self, mac): 

537 # type: (str) -> Tuple[str, str] 

538 """Find OUI name matching to a MAC""" 

539 return self._get_manuf_couple(mac) 

540 

541 def reverse_lookup(self, name, case_sensitive=False): 

542 # type: (str, bool) -> Dict[str, str] 

543 """ 

544 Find all MACs registered to a OUI 

545 

546 :param name: the OUI name 

547 :param case_sensitive: default to False 

548 :returns: a dict of mac:tuples (Name, Extended Name) 

549 """ 

550 if case_sensitive: 

551 filtr = lambda x, l: any(x in z for z in l) # type: Callable[[str, Tuple[str, str]], bool] # noqa: E501 

552 else: 

553 name = name.lower() 

554 filtr = lambda x, l: any(x in z.lower() for z in l) 

555 return {k: v for k, v in self.d.items() if filtr(name, v)} # type: ignore 

556 

557 def __dir__(self): 

558 # type: () -> List[str] 

559 return [ 

560 "_get_manuf", 

561 "_get_short_manuf", 

562 "_resolve_MAC", 

563 "loopkup", 

564 "reverse_lookup", 

565 ] + super(ManufDA, self).__dir__() 

566 

567 

568class _CacheUnpickler(pickle.Unpickler): 

569 """Unpickle cache data without loading classes outside its data types.""" 

570 

571 _ALLOWED = { 

572 ("scapy.dadict", "DADict"): DADict, 

573 ("scapy.data", "EtherDA"): EtherDA, 

574 ("scapy.data", "ManufDA"): ManufDA, 

575 } 

576 

577 def find_class(self, module, name): 

578 # type: (str, str) -> Any 

579 try: 

580 return self._ALLOWED[(module, name)] 

581 except KeyError as exc: 

582 raise pickle.UnpicklingError( 

583 "Forbidden class in cache: %s.%s" % (module, name) 

584 ) from exc 

585 

586 

587@scapy_data_cache("manufdb") 

588def load_manuf(filename=None): 

589 # type: (Optional[str]) -> ManufDA 

590 """Load Wireshark manufacturer names and return values as a dictionary.""" 

591 if filename is None and not WINDOWS: 

592 filename = select_path( 

593 ['/usr', '/usr/local', '/opt', '/opt/wireshark', 

594 '/Applications/Wireshark.app/Contents/Resources'], 

595 "share/wireshark/manuf" 

596 ) 

597 manufdb = ManufDA(_name=filename or "scapy/manufdb") 

598 

599 def _process_data(fdesc): 

600 # type: (Iterator[str]) -> None 

601 for line in fdesc: 

602 try: 

603 line = line.strip() 

604 if not line or line.startswith("#"): 

605 continue 

606 parts = line.split(None, 2) 

607 oui, shrt = parts[:2] 

608 lng = parts[2].lstrip("#").strip() if len(parts) > 2 else "" 

609 lng = lng or shrt 

610 manufdb[oui] = shrt, lng 

611 except Exception: 

612 log_loading.warning("Couldn't parse one line from [%s] [%r]", 

613 filename, line, exc_info=True) 

614 

615 try: 

616 if not filename: 

617 raise IOError 

618 with open(filename, "r", errors="backslashreplace") as fdesc: 

619 _process_data(fdesc) 

620 except IOError: 

621 # Fallback. Lazy loaded as the file is big. 

622 from scapy.libs.manuf import DATA 

623 _process_data(iter(DATA.split("\n"))) 

624 return manufdb 

625 

626 

627@scapy_data_cache("bluetoothids") 

628def load_bluetoothids(filename=None): 

629 # type: (Optional[str]) -> DADict[int, str] 

630 """Load Bluetooth company IDs and return values as a dictionary.""" 

631 from scapy.libs.bluetoothids import DATA 

632 identifiers = DADict(_name="scapy/bluetoothids") # type: DADict[int, str] 

633 identifiers.update(cast(Dict[int, str], DATA)) 

634 return identifiers 

635 

636 

637def select_path(directories, filename): 

638 # type: (List[str], str) -> Optional[str] 

639 """Find filename among several directories""" 

640 for directory in directories: 

641 path = os.path.join(directory, filename) 

642 if os.path.exists(path): 

643 return path 

644 return None 

645 

646 

647@lru_cache(maxsize=1) 

648def _load_services(): 

649 # type: () -> Tuple[DADict[int, str], DADict[int, str], DADict[int, str]] 

650 """Share one services load across the TCP, UDP, and SCTP dictionaries.""" 

651 return load_services() 

652 

653 

654IP_PROTOS: DADict[int, str] = DADict( 

655 lazy_load=load_protocols, 

656 _name="IP_PROTOS_UNLOADED", 

657) 

658TCP_SERVICES: DADict[int, str] = DADict( 

659 lazy_load=lambda: _load_services()[0], 

660 _name="TCP_SERVICES_UNLOADED", 

661) 

662UDP_SERVICES: DADict[int, str] = DADict( 

663 lazy_load=lambda: _load_services()[1], 

664 _name="UDP_SERVICES_UNLOADED", 

665) 

666SCTP_SERVICES: DADict[int, str] = DADict( 

667 lazy_load=lambda: _load_services()[2], 

668 _name="SCTP_SERVICES_UNLOADED", 

669) 

670ETHER_TYPES = EtherDA( 

671 lazy_load=lambda: load_ethertypes(None if WINDOWS else "/etc/ethertypes"), 

672 _name="ETHER_TYPES_UNLOADED", 

673) 

674MANUFDB = ManufDA( 

675 lazy_load=load_manuf, 

676 _name="MANUFDB_UNLOADED", 

677) 

678BLUETOOTH_CORE_COMPANY_IDENTIFIERS: DADict[int, str] = DADict( 

679 lazy_load=load_bluetoothids, 

680 _name="BLUETOOTH_CORE_COMPANY_IDENTIFIERS_UNLOADED", 

681) 

682 

683 

684##################### 

685# knowledge bases # 

686##################### 

687KBBaseType = Optional[Union[str, List[Tuple[str, Dict[str, Dict[str, str]]]]]] 

688 

689 

690class KnowledgeBase(object): 

691 def __init__(self, filename): 

692 # type: (Optional[Any]) -> None 

693 self.filename = filename 

694 self.base = None # type: KBBaseType 

695 

696 def lazy_init(self): 

697 # type: () -> None 

698 self.base = "" 

699 

700 def reload(self, filename=None): 

701 # type: (Optional[Any]) -> None 

702 if filename is not None: 

703 self.filename = filename 

704 oldbase = self.base 

705 self.base = None 

706 self.lazy_init() 

707 if self.base is None: 

708 self.base = oldbase 

709 

710 def get_base(self): 

711 # type: () -> Union[str, List[Tuple[str, Dict[str,Dict[str,str]]]]] 

712 if self.base is None: 

713 self.lazy_init() 

714 return cast(Union[str, List[Tuple[str, Dict[str, Dict[str, str]]]]], self.base)