Skip to content

Commit

Permalink
Undo erroneous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phasewalker18 authored Mar 12, 2018
1 parent 805113a commit 6ffd670
Showing 1 changed file with 33 additions and 55 deletions.
88 changes: 33 additions & 55 deletions control24common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
import optparse
import os
import time
import sys

import netifaces

if sys.platform.startswith('win'):
import _winreg as wr #pylint: disable=E0401

'''
This file is part of ReaControl24. Control Surface Middleware.
Copyright (C) 2018 PhaseWalker
Expand Down Expand Up @@ -121,60 +117,42 @@ def opts_common(desc):
oprs.set_defaults(debug=False, logdir=logdir)
return oprs

def get_ip_address(ifname):
"""Use netifaces to retrieve ip address, but handle if it doesn't exist"""
try:
addr_l = netifaces.ifaddresses(ifname)[netifaces.AF_INET]
return [{k: v.encode('ascii', 'ignore') for k, v in addr.iteritems()} for addr in addr_l]
except KeyError:
return None

def list_networks_win(networks):
"""Windows shim for list_networks. Also go to the registry to
get a friendly name"""
reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
reg_key = wr.OpenKey(
reg,
r'SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}'
)
for key, val in networks.iteritems():
val['pcapname'] = '\\Device\\NPF_{}'.format(key)
net_regkey = r'{}\Connection'.format(key)
try:
net_key = wr.OpenKey(reg_key, net_regkey)
net_name = wr.QueryValueEx(net_key, 'Name')[0]
except WindowsError: #pylint: disable=E0602
pass
if net_name:
val['name'] = net_name
wr.CloseKey(reg_key)
return networks

def list_networks():
"""Gather networks info via netifaces library"""
default_not_found = True
names = [a.encode('ascii', 'ignore') for a in netifaces.interfaces()]
results = {}
for interface in names:
inner = {'pcapname': interface}
#ip
ips = get_ip_address(interface)
if ips:
inner['ip'] = ips
if default_not_found and any([ip.has_key('addr') and not ip.has_key('peer') for ip in ips]):
default_not_found = False
inner['default'] = True
results[interface] = inner
if sys.platform.startswith('win'):
return list_networks_win(results)
return results

def format_ip(ipaddr, port):
"""from ip and port provide a string with ip:port"""
return '{}:{}'.format(ipaddr, port)


def ipv4(interface_name=None):
""" Get an IPv4 address plausible default. """
# to not take into account loopback addresses (no interest here)
ifaces = netifaces.interfaces()
default_interface = DEFAULTS.get('interface')
default_address = DEFAULTS.get('ip')
found = False
if not interface_name is None and not interface_name in ifaces:
raise KeyError('%s is not a valid interface name' % interface_name)
for interface in ifaces:
if interface_name is None or unicode(interface_name) == interface:
config = netifaces.ifaddresses(interface)
# AF_INET is not always present
if netifaces.AF_INET in config.keys():
for link in config[netifaces.AF_INET]:
# loopback holds a 'peer' instead of a 'broadcast' address
if 'addr' in link.keys() and 'peer' not in link.keys():
default_interface = interface
default_address = link['addr']
found = True
break

if not interface_name is None and not found:
raise LookupError(
'%s interface has no ipv4 addresses' % interface_name)

return default_interface, default_address

def hexl(inp):
"""Convert to hex string using binascii but
then pretty it up by spacing the groups"""
shex = binascii.hexlify(inp)
return ' '.join([shex[i:i+2] for i in range(0, len(shex), 2)])

def format_ip(ipaddr, port):
"""from ip and port provide a string with ip:port"""
return '{}:{}'.format(ipaddr, port)

0 comments on commit 6ffd670

Please sign in to comment.