Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace netifaces by scapy #1489

Merged
merged 16 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/run_tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
netifaces==0.11.0
scapy==2.5.0
5 changes: 3 additions & 2 deletions ci/run_tests/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import subprocess
import argparse
import netifaces as ni
from scapy.all import get_if_addr

PCAP_FILE_PATH = os.path.join("Tests", "Pcap++Test", "PcapExamples", "example.pcap")

Expand Down Expand Up @@ -32,7 +32,8 @@ def main():
)
args = parser.parse_args()

ip_address = ni.ifaddresses(args.interface)[ni.AF_INET][0]["addr"]
ip_address = get_if_addr(args.interface)

print("IP address is: %s" % ip_address)

try:
Expand Down
16 changes: 13 additions & 3 deletions ci/run_tests/run_tests_windows.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import os
import argparse
import subprocess
import netifaces as ni
import scapy.arch.windows

TCPREPLAY_PATH = "tcpreplay-4.4.1-win"
PCAP_FILE_PATH = os.path.abspath(
os.path.join("Tests", "Pcap++Test", "PcapExamples", "example.pcap")
)


def get_ip_by_guid(guid):
interfaces = scapy.arch.windows.get_windows_if_list()
for iface in interfaces:
if iface["guid"] == guid:
# Return the second IP address if it exists, otherwise return None
return iface["ips"][1] if len(iface["ips"]) > 1 else None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we always return the second address?
If we want to return the first IPv4 address, maybe we can do this instead:

def validate_ipv4_address(address):
    try:
        IPv4Address(address)
        return True
    except ValueError:
        return False


def get_ip_by_guid(guid):
    interfaces = scapy.arch.windows.get_windows_if_list()
    for iface in interfaces:
        if iface["guid"] == guid:
            # Return the first IPv4 address if exists, otherwise return None
            return next(filter(validate_ipv4_address, iface["ips"]), None)

    # Return None if no matching interface is found
    return None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in the returnediface["ips"] list, the first element will be IPv6 address, and then the second element is the ipv4 address. One example is 'ips': ['fe80::ff8e:5110:73f1:e14d', '169.254.226.73']}.

I agree using check function do make more sense then just assign a fixed random number. I will change.

Copy link
Collaborator

@tigercosmos tigercosmos Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhengfeihe It will be better to leave some comments in the code as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tigercosmos added

# Return None if no matching interface is found
return None


def find_interface():
completed_process = subprocess.run(
["tcpreplay.exe", "--listnics"],
Expand All @@ -26,8 +36,8 @@ def find_interface():
if len(columns) > 1 and columns[1].startswith("\\Device\\NPF_"):
interface = columns[1]
try:
ni_interface = interface.lstrip("\\Device\\NPF_")
ip_address = ni.ifaddresses(ni_interface)[ni.AF_INET][0]["addr"]
nic_guid = interface.lstrip("\\Device\\NPF_")
ip_address = get_ip_by_guid(nic_guid)
if ip_address.startswith("169.254"):
continue
completed_process = subprocess.run(
Expand Down
Loading