ethernet adapter name fully configurable on linux and windows. Fixes issue #4

This commit is contained in:
uhi22 2023-05-26 09:16:52 +02:00
parent 9e9eb30151
commit 5e63e82ee7
2 changed files with 36 additions and 9 deletions

View file

@ -37,10 +37,13 @@ import pcap
import pyPlcIpv6 import pyPlcIpv6
import udplog import udplog
import time import time
import os
from helpers import * # prettyMac etc from helpers import * # prettyMac etc
from pyPlcModes import * from pyPlcModes import *
from mytestsuite import * from mytestsuite import *
from random import random from random import random
from configmodule import getConfigValue, getConfigValueBool
import sys
MAC_BROADCAST = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ] MAC_BROADCAST = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ]
@ -1020,14 +1023,36 @@ class pyPlcHomeplug():
def findEthernetAdaptor(self): def findEthernetAdaptor(self):
self.strInterfaceName="eth1" # default, if the real is not found if (os.name == 'nt'):
# On Windows
# print("Interfaces:\n" + '\n'.join(pcap.findalldevs()))
# For windows, we use a dirty solution here: The pcap uses numbered interfaces like eth0, eth1 etc.,
# but the mapping between these numbers and the physical devices is not stable. To find out the
# correct interface, we search for its name (e.g. '\Device\NPF_{E4B8176C-8516-4D48-88BC-85225ABCF259}' in
# the list of all interfaces.
strWindowsInterfaceName = getConfigValue("eth_windows_interface_name")
print("The configured windows interface name is " + strWindowsInterfaceName)
self.strInterfaceName = "" # default for "not found"
for i in range(0, 10):
strInterfaceName = pcap.ex_name("eth"+str(i))
if (strInterfaceName == strWindowsInterfaceName):
#print("This is the wanted Ethernet adaptor.")
self.strInterfaceName="eth"+str(i)
print("This interface is in pcap " + self.strInterfaceName)
if (self.strInterfaceName == ""):
print("ERROR: No matching interface was found. Make sure that you configured an existing eth_windows_interface_name in pyPlc.ini.")
print("The following interfaces are available:")
# print("Interfaces:\n" + '\n'.join(pcap.findalldevs())) # print("Interfaces:\n" + '\n'.join(pcap.findalldevs()))
for i in range(0, 10): for i in range(0, 10):
strInterfaceName = pcap.ex_name("eth"+str(i)) strInterfaceName = pcap.ex_name("eth"+str(i))
if (strInterfaceName == '\\Device\\NPF_{E4B8176C-8516-4D48-88BC-85225ABCF259}'): print("eth"+ str(i) + " is " + strInterfaceName)
#print("This is the wanted Ethernet adaptor.")
self.strInterfaceName="eth"+str(i) sys.exit()
#print("eth"+ str(i) + " is " + strInterfaceName) else:
# On Linux (e.g. Raspberry)
# Take the interface name from the ini file. For Linux, this is all we need.
self.strInterfaceName=getConfigValue("eth_interface")
print("Linux interface is " + self.strInterfaceName)
def enterPevMode(self): def enterPevMode(self):
self.iAmEvse = 0 # not emulating a charging station self.iAmEvse = 0 # not emulating a charging station

View file

@ -15,6 +15,7 @@ import time # for time.sleep()
import errno import errno
import os import os
import subprocess import subprocess
from configmodule import getConfigValue, getConfigValueBool
class pyPlcTcpClientSocket(): class pyPlcTcpClientSocket():
def __init__(self, callbackAddToTrace): def __init__(self, callbackAddToTrace):
@ -52,7 +53,8 @@ class pyPlcTcpClientSocket():
#print(host[0:5].lower()) #print(host[0:5].lower())
if (host[0:5].lower()=="fe80:"): if (host[0:5].lower()=="fe80:"):
#print("This is a link local address. We need to add %eth0 at the end.") #print("This is a link local address. We need to add %eth0 at the end.")
host = host + "%eth1" ethInterface = getConfigValue("eth_interface") # e.g. "eth0"
host = host + "%" + ethInterface
socket_addr = socket.getaddrinfo(host,port,socket.AF_INET6,socket.SOCK_DGRAM,socket.SOL_UDP)[0][4] socket_addr = socket.getaddrinfo(host,port,socket.AF_INET6,socket.SOCK_DGRAM,socket.SOL_UDP)[0][4]
#print(socket_addr) #print(socket_addr)