rust-pcap / pcap

Rust language pcap library
Apache License 2.0
610 stars 138 forks source link

Replace lookupdev with findalldevs #160

Closed Wojtek242 closed 2 years ago

Wojtek242 commented 3 years ago

Turns out there's already an example of using findalldevs in pcap. It was in the function below list.

However, by removing lookupdev I realised that the case of no default device was not handled at all. I've currently handled this by returning a Results<Option<Device>, Error> as opposed to a Result<Device, Error>.

A possible alternative would be to translate the null option to an error, but that would be semantically different from what libpcap does which does not consider the null case to be an error.

Currently a draft PR, because I want to have a think if there's a slightly prettier solution.

Closes #158

lambdaiv commented 2 years ago

I think the Result<Options<Device>, Error> return type makes more sense to me, especially since it matches what libpcap does. I'm not sure about a prettier way to do it, but the end result type works for me.

Wojtek242 commented 2 years ago

In this PR I remove wstr_to_string as the need for it came from a bug in the now obsolete pcap_lookupdev:

In WinPcap and Npcap, this function may return a UTF-16 string rather than an ASCII or UTF-8 string. From https://npcap.com/guide/wpcap/pcap_lookupdev.html

This does not seem to apply to pcap_findalldevs on Windows: https://npcap.com/guide/wpcap/pcap_findalldevs.html

Therefore, this also closes #165.