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

Replace netifaces by scapy #1489

merged 16 commits into from
Aug 14, 2024

Conversation

zhengfeihe
Copy link
Contributor

@zhengfeihe zhengfeihe commented Jul 7, 2024

Solved issue #1384

Choose psutil as the new plan since it has a better maintenance record and more users compared to another option, ifaddr, used by some other projects.]

Additionally, psutil supports all the platforms we use.

Move to use scapy as the new solution, well support all the platform

@zhengfeihe zhengfeihe requested a review from seladb as a code owner July 7, 2024 09:38
@zhengfeihe zhengfeihe changed the base branch from master to dev July 7, 2024 09:39
@zhengfeihe
Copy link
Contributor Author

@tigercosmos Can you take a look at this?

@zhengfeihe zhengfeihe changed the title Replace netinterface by psutil Replace netifaces by psutil Jul 7, 2024
@zhengfeihe zhengfeihe force-pushed the fix/remove-netifaces branch from eafb916 to 4f4147f Compare July 7, 2024 09:45
@tigercosmos
Copy link
Collaborator

@zhengfeihe overall it looks fine. could you resolve the CI errors?

@@ -1 +1 @@
netifaces==0.11.0
psutil==5.9.8
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not use the latest 6.0.0

@tigercosmos
Copy link
Collaborator

ummm, psutil doesn't have a prebuilt package?

Building wheels for collected packages: psutil
  Building wheel for psutil (pyproject.toml): started
  Building wheel for psutil (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error
  
  × Building wheel for psutil (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [54 lines of output]
      /tmp/tmp7nuea6gq.c:1:10: fatal error: linux/ethtool.h: No such file or directory
          1 | #include <linux/ethtool.h>
            |          ^~~~~~~~~~~~~~~~~
      compilation terminated.
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-310
      creating build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_compat.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pswindows.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pslinux.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pssunos.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_common.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psposix.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psaix.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psosx.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/__init__.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psbsd.py -> build/lib.linux-x86_64-cpython-310/psutil
      creating build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_memleaks.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_contracts.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_process.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_system.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_osx.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_connections.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_windows.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/__main__.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_sunos.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_misc.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_aix.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_process_all.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/runner.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_bsd.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_unicode.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/__init__.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_posix.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_linux.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_testutils.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      running build_ext
      building 'psutil._psutil_linux' extension
      creating build/temp.linux-x86_64-cpython-310
      creating build/temp.linux-x86_64-cpython-310/psutil
      creating build/temp.linux-x86_64-cpython-310/psutil/arch
      creating build/temp.linux-x86_64-cpython-310/psutil/arch/linux
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=[60](https://github.com/seladb/PcapPlusPlus/actions/runs/9826595600/job/27128244120?pr=1489#step:8:61)0 -DPy_LIMITED_API=0x03060000 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -DPSUTIL_LINUX=1 -I/usr/include/python3.10 -c psutil/_psutil_common.c -o build/temp.linux-x86_64-cpython-310/psutil/_psutil_common.o
      psutil/_psutil_common.c:9:10: fatal error: Python.h: No such file or directory
          9 | #include <Python.h>
            |          ^~~~~~~~~~
      compilation terminated.
      psutil could not be installed from sources. Perhaps Python header files are not installed. Try running:
        sudo apk add gcc python3-dev musl-dev linux-headers
      error: command '/usr/bin/gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for psutil
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (psutil)
Failed to build psutil

@tigercosmos
Copy link
Collaborator

@zhengfeihe Seems that we need to build Alpine ourselves?
https://github.com/giampaolo/psutil/blob/master/INSTALL.rst

@tigercosmos
Copy link
Collaborator

also, for Windows:

Installing collected packages: psutil
Successfully installed psutil-6.0.0
Cannot find an interface to run tests on!

@zhengfeihe
Copy link
Contributor Author

ummm, psutil doesn't have a prebuilt package?

Building wheels for collected packages: psutil
  Building wheel for psutil (pyproject.toml): started
  Building wheel for psutil (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error
  
  × Building wheel for psutil (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [54 lines of output]
      /tmp/tmp7nuea6gq.c:1:10: fatal error: linux/ethtool.h: No such file or directory
          1 | #include <linux/ethtool.h>
            |          ^~~~~~~~~~~~~~~~~
      compilation terminated.
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-310
      creating build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_compat.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pswindows.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pslinux.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pssunos.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_common.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psposix.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psaix.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psosx.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/__init__.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psbsd.py -> build/lib.linux-x86_64-cpython-310/psutil
      creating build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_memleaks.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_contracts.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_process.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_system.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_osx.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_connections.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_windows.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/__main__.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_sunos.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_misc.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_aix.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_process_all.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/runner.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_bsd.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_unicode.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/__init__.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_posix.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_linux.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_testutils.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      running build_ext
      building 'psutil._psutil_linux' extension
      creating build/temp.linux-x86_64-cpython-310
      creating build/temp.linux-x86_64-cpython-310/psutil
      creating build/temp.linux-x86_64-cpython-310/psutil/arch
      creating build/temp.linux-x86_64-cpython-310/psutil/arch/linux
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=[60](https://github.com/seladb/PcapPlusPlus/actions/runs/9826595600/job/27128244120?pr=1489#step:8:61)0 -DPy_LIMITED_API=0x03060000 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -DPSUTIL_LINUX=1 -I/usr/include/python3.10 -c psutil/_psutil_common.c -o build/temp.linux-x86_64-cpython-310/psutil/_psutil_common.o
      psutil/_psutil_common.c:9:10: fatal error: Python.h: No such file or directory
          9 | #include <Python.h>
            |          ^~~~~~~~~~
      compilation terminated.
      psutil could not be installed from sources. Perhaps Python header files are not installed. Try running:
        sudo apk add gcc python3-dev musl-dev linux-headers
      error: command '/usr/bin/gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for psutil
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (psutil)
Failed to build psutil

Seems we need to install python-dev ? giampaolo/psutil#1143

@zhengfeihe zhengfeihe force-pushed the fix/remove-netifaces branch from e754a22 to 2241be8 Compare July 7, 2024 12:05
@tigercosmos
Copy link
Collaborator

@zhengfeihe I used your code to run in #1486, but still got the error. seems not the issue of "python-dev".
I feel like netifaces and psutil both have trouble for alpine. Maybe we should use the subprocess to run ifconfig of ip address directly? cc @seladb @Dimi1010 @clementperon @egecetin

WARNING: The directory '/github/home/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Requirement already satisfied: pip in ./venv/lib/python3.10/site-packages (23.0.1)

Notice:  A new release of pip is available: 23.0.1 -> 24.1.1
Notice:  To update, run: pip install --upgrade pip
WARNING: The directory '/github/home/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Collecting psutil==6.0.0
  Downloading psutil-6.0.0.tar.gz (508 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 508.1/508.1 kB 10.8 MB/s eta 0:00:00
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Building wheels for collected packages: psutil
  Building wheel for psutil (pyproject.toml): started
  Building wheel for psutil (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error
  
  × Building wheel for psutil (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [55 lines of output]
      /tmp/tmpgwbawz3k.c:1:10: fatal error: linux/ethtool.h: No such file or directory
          1 | #include <linux/ethtool.h>
            |          ^~~~~~~~~~~~~~~~~
      compilation terminated.
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-310
      creating build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_compat.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pswindows.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pslinux.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_pssunos.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_common.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psposix.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psaix.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psosx.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/__init__.py -> build/lib.linux-x86_64-cpython-310/psutil
      copying psutil/_psbsd.py -> build/lib.linux-x86_64-cpython-310/psutil
      creating build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_memleaks.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_contracts.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_process.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_system.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_osx.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_connections.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_windows.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/__main__.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_sunos.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_misc.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_aix.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_process_all.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/runner.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_bsd.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_unicode.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/__init__.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_posix.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_linux.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      copying psutil/tests/test_testutils.py -> build/lib.linux-x86_64-cpython-310/psutil/tests
      running build_ext
      building 'psutil._psutil_linux' extension
      creating build/temp.linux-x86_64-cpython-310
      creating build/temp.linux-x86_64-cpython-310/psutil
      creating build/temp.linux-x86_64-cpython-310/psutil/arch
      creating build/temp.linux-x86_64-cpython-310/psutil/arch/linux
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=600 -DPy_LIMITED_API=0x03060000 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -DPSUTIL_LINUX=1 -I/__w/PcapPlusPlus/PcapPlusPlus/venv/include -I/usr/include/python3.10 -c psutil/_psutil_common.c -o build/temp.linux-x86_64-cpython-310/psutil/_psutil_common.o
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -Os -fomit-frame-pointer -g -O2 -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=600 -DPy_LIMITED_API=0x03060000 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -DPSUTIL_LINUX=1 -I/__w/PcapPlusPlus/PcapPlusPlus/venv/include -I/usr/include/python3.10 -c psutil/_psutil_linux.c -o build/temp.linux-x86_64-cpython-310/psutil/_psutil_linux.o
      psutil/_psutil_linux.c:[13](https://github.com/seladb/PcapPlusPlus/actions/runs/9827062735/job/27129314926?pr=1486#step:9:14):10: fatal error: linux/ethtool.h: No such file or directory
         13 | #include <linux/ethtool.h>  // DUPLEX_*
            |          ^~~~~~~~~~~~~~~~~
      compilation terminated.
      psutil could not be installed from sources. Perhaps Python header files are not installed. Try running:
        sudo apk add gcc python3-dev musl-dev linux-headers
      error: command '/usr/bin/gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for psutil
ERROR: Could not build wheels for psutil, which is required to install pyproject.toml-based projects
Failed to build psutil

Notice:  A new release of pip is available: [23](https://github.com/seladb/PcapPlusPlus/actions/runs/9827062735/job/27129314926?pr=1486#step:9:24).0.1 -> 24.1.1
Notice:  To update, run: pip install --upgrade pip

@tigercosmos
Copy link
Collaborator

woo! I make it works. https://github.com/seladb/PcapPlusPlus/actions/runs/9827423291/job/27130086746

@zhengfeihe
Copy link
Contributor Author

still try to debug windows though..

@tigercosmos
Copy link
Collaborator

@zhengfeihe I see. for the linux part, we can refer #4783 and pick the necessary code. Your PR will be merged before #4783.

@tigercosmos
Copy link
Collaborator

btw, you can disable other tests for faster CI result, like what I did: 5f80c36

@zhengfeihe zhengfeihe force-pushed the fix/remove-netifaces branch from f2ceda3 to e6474c0 Compare July 14, 2024 12:36
@@ -1 +1 @@
netifaces==0.11.0
scapy==2.5.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would avoid using scapy, since we should not use a big library for just a small function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me try to find other alternative methods.

@@ -65,7 +77,7 @@ def main():
tcpreplay_interface, ip_address = find_interface()
if not tcpreplay_interface or not ip_address:
print("Cannot find an interface to run tests on!")
exit(1)
exit(1)
Copy link
Owner

Choose a reason for hiding this comment

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

I think this was probably done by accident?

Comment on lines 16 to 18
if len(iface["ips"]) > 0:
# iface["ips"] will put ipv6 address first
return iface["ips"][1]
Copy link
Owner

Choose a reason for hiding this comment

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

  1. Since we're taking the second element from iface["ips"], don't we want to check if len(iface["ips"]) > 1?
  2. A nicer way to do it can be:
    return iface["ips"][1] if len(iface["ips"]) > 1 else 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.

Sure. I will change it.

@seladb
Copy link
Owner

seladb commented Jul 17, 2024

@zhengfeihe @tigercosmos it seems we decided to replace netifaces with scapy instead of psutils. We should probably change the PR title

@clementperon
Copy link
Collaborator

Sorry, I missed this what was the remaining issue with psutil ?

For Alpine we could either install the python3-dev or py3-psutil package if needed

But for windows do you have a log of the issue ?

@zhengfeihe
Copy link
Contributor Author

zhengfeihe commented Jul 22, 2024

Sorry, I missed this what was the remaining issue with psutil ?

For Alpine we could either install the python3-dev or py3-psutil package if needed

But for windows do you have a log of the issue ?

Yes. Windows seems to be a little complicated. We get the guid of NIC other than the name.
I think scapy can handle the most cases pretty well , but @tigercosmos favors simple solutions, since it's a big library. I will try to find a better solution.

@tigercosmos
Copy link
Collaborator

Sorry, I missed this what was the remaining issue with psutil ?
For Alpine we could either install the python3-dev or py3-psutil package if needed
But for windows do you have a log of the issue ?

Yes. Windows seems to be a little complicated. We get the guid of NIC other than the name. I think scapy can handle the most cases pretty well , but @tigercosmos favors simple solutions, since it's a big library. I will try to find a better solution.

@zhengfeihe The scapy can be a temporarily solution if you cannot find another library. I would be suprised if there is no network tool for cross-platform support. If that is the case, maybe we should write one for ourself.

@tigercosmos
Copy link
Collaborator

@zhengfeihe Shall we finish the PR by this weekend?

@zhengfeihe zhengfeihe changed the title Replace netifaces by psutil Replace netifaces by scapy Aug 5, 2024
@zhengfeihe
Copy link
Contributor Author

@seladb I think it's ready. The two failed tests seems not caused by my code's change, do you have ideas on fixing it?

Copy link
Collaborator

@tigercosmos tigercosmos left a comment

Choose a reason for hiding this comment

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

LGTM

@tigercosmos tigercosmos added this to the Augest 2024 Release milestone Aug 7, 2024
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

@Dimi1010 Dimi1010 linked an issue Aug 9, 2024 that may be closed by this pull request
@zhengfeihe
Copy link
Contributor Author

@seladb can you take a look at this again?

interfaces = scapy.arch.windows.get_windows_if_list()
for iface in interfaces:
ips = iface.get("ips", [])
if not isinstance(ips, list):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this check is redundant, as you use iface.get("ips", []) already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

print(f"Unexpected format for 'ips' in interface: {iface}")
return None
# Use filter inside next to find the first valid IPv4 address
# The first element in the returned iface["ips"] list is set to be a ipv6 address;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: ";" in the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

Copy link

codecov bot commented Aug 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.84%. Comparing base (8344607) to head (b06c3fa).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1489      +/-   ##
==========================================
- Coverage   82.84%   82.84%   -0.01%     
==========================================
  Files         273      273              
  Lines       46090    46090              
  Branches     9268     9253      -15     
==========================================
- Hits        38185    38184       -1     
+ Misses       7084     7082       -2     
- Partials      821      824       +3     
Flag Coverage Δ
fedora39 74.50% <ø> (+<0.01%) ⬆️
macos-12 80.84% <ø> (+<0.01%) ⬆️
macos-13 80.26% <ø> (ø)
macos-14 80.19% <ø> (ø)
mingw32 71.32% <ø> (-0.03%) ⬇️
mingw64 71.29% <ø> (-0.05%) ⬇️
npcap 84.66% <ø> (-0.11%) ⬇️
rhel94 74.26% <ø> (-0.01%) ⬇️
ubuntu2004 57.82% <ø> (ø)
ubuntu2004-zstd 57.95% <ø> (-0.03%) ⬇️
ubuntu2204 74.22% <ø> (+<0.01%) ⬆️
ubuntu2204-icpx 58.17% <ø> (+<0.01%) ⬆️
unittest 82.84% <ø> (-0.01%) ⬇️
windows-2019 84.77% <ø> (-0.02%) ⬇️
windows-2022 84.78% <ø> (-0.02%) ⬇️
winpcap 84.75% <ø> (ø)
xdp 49.30% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@seladb seladb merged commit 0cc772e into seladb:dev Aug 14, 2024
39 checks passed
@seladb
Copy link
Owner

seladb commented Aug 14, 2024

Thank you @zhengfeihe for working on this fix, much appreciated!! 🙏 🙏

@tigercosmos
Copy link
Collaborator

@zhengfeihe nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Find alternative package for netifaces
5 participants