1## This file is part of Scapy 2## See http://www.secdev.org/projects/scapy for more informations 3## Copyright (C) Philippe Biondi <phil@secdev.org> 4## This program is published under a GPLv2 license 5 6""" 7Operating system specific functionality. 8""" 9 10from __future__ import absolute_import 11import socket 12 13from scapy.consts import LINUX, OPENBSD, FREEBSD, NETBSD, DARWIN, \ 14 SOLARIS, WINDOWS, BSD, IS_64BITS, LOOPBACK_NAME, plt, MATPLOTLIB_INLINED, \ 15 MATPLOTLIB_DEFAULT_PLOT_KARGS, PYX 16from scapy.error import * 17import scapy.config 18from scapy.pton_ntop import inet_pton 19from scapy.data import * 20 21def str2mac(s): 22 return ("%02x:"*6)[:-1] % tuple(orb(x) for x in s) 23 24if not WINDOWS: 25 if not scapy.config.conf.use_pcap and not scapy.config.conf.use_dnet: 26 from scapy.arch.bpf.core import get_if_raw_addr 27 28def get_if_addr(iff): 29 return socket.inet_ntoa(get_if_raw_addr(iff)) 30 31def get_if_hwaddr(iff): 32 addrfamily, mac = get_if_raw_hwaddr(iff) 33 if addrfamily in [ARPHDR_ETHER,ARPHDR_LOOPBACK]: 34 return str2mac(mac) 35 else: 36 raise Scapy_Exception("Unsupported address family (%i) for interface [%s]" % (addrfamily,iff)) 37 38 39# Next step is to import following architecture specific functions: 40# def get_if_raw_hwaddr(iff) 41# def get_if_raw_addr(iff): 42# def get_if_list(): 43# def get_working_if(): 44# def attach_filter(s, filter, iface): 45# def set_promisc(s,iff,val=1): 46# def read_routes(): 47# def read_routes6(): 48# def get_if(iff,cmd): 49# def get_if_index(iff): 50 51if LINUX: 52 from scapy.arch.linux import * 53 if scapy.config.conf.use_pcap or scapy.config.conf.use_dnet: 54 from scapy.arch.pcapdnet import * 55elif BSD: 56 from scapy.arch.unix import read_routes, read_routes6, in6_getifaddr 57 58 if scapy.config.conf.use_pcap or scapy.config.conf.use_dnet: 59 from scapy.arch.pcapdnet import * 60 else: 61 from scapy.arch.bpf.supersocket import L2bpfListenSocket, L2bpfSocket, L3bpfSocket 62 from scapy.arch.bpf.core import * 63 scapy.config.conf.use_bpf = True 64 scapy.config.conf.L2listen = L2bpfListenSocket 65 scapy.config.conf.L2socket = L2bpfSocket 66 scapy.config.conf.L3socket = L3bpfSocket 67elif SOLARIS: 68 from scapy.arch.solaris import * 69elif WINDOWS: 70 from scapy.arch.windows import * 71 72if scapy.config.conf.iface is None: 73 scapy.config.conf.iface = scapy.consts.LOOPBACK_INTERFACE 74 75 76def get_if_addr6(iff): 77 """ 78 Returns the main global unicast address associated with provided 79 interface, in human readable form. If no global address is found, 80 None is returned. 81 """ 82 for x in in6_getifaddr(): 83 if x[2] == iff and x[1] == IPV6_ADDR_GLOBAL: 84 return x[0] 85 86 return None 87 88def get_if_raw_addr6(iff): 89 """ 90 Returns the main global unicast address associated with provided 91 interface, in network format. If no global address is found, None 92 is returned. 93 """ 94 ip6= get_if_addr6(iff) 95 if ip6 is not None: 96 return inet_pton(socket.AF_INET6, ip6) 97 98 return None 99