You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] reading pcap files on non UTF-8 Windows system fails due to convert path to str fails #383
Ad src/capture/mod.rs:113, new_raw is accepting a Option<&str> as input, this method is not only used by inactive handle, but also used by active handle reading pcap files.
More specific, at src/capture/activated/offline.rs:20, we do path.as_ref().to_str(), but apparently this would failed if the path is not a UTF8 encoded Path.
The purpose of PathBuf & Path is to avoid encoding issue, to_str() is not an elegant way.
I'm not sure I can fix it, pcap lib is design around unix ecosystem, https://www.tcpdump.org/manpages/pcap_open_offline.3pcap.html, the function is expected an char * and that always expected to be utf-8, I can probably manage to try to send a char * that is utf 16 encoded with a final nul bytes but I doubt it will work.
Another way would be to use FILE * but it's end with the same problem, fopen expect an char *, one would need to use _wfopen or use other windows API. I believe it's too much work for one user and at this point use something dedicated for windows.
So in conclusion... set your windows to UTF-8 or rename your file using ascii character.
PS:
The purpose of PathBuf & Path is to avoid encoding issue, to_str() is not an elegant way.
Well, the commit doesn't seem to work on windows, the error is throw out is: failed to open C:\test\测试.pcapng, libpcap error: C: No such file or directory
Windows sometimes don't use UTF-16 as default for Asian language users,for example GBK for Chinese, Shift_JIS for Japanese, EUC-KR for Korean, assuming UTF-16 on windows is not quite appropriate
I'd like to help improve this part, I'd spent some time next week and see what we can cook :)
There is a pcap_init function in wpcap/libpcap implemention, this is what it saids:
/* * Do whatever initialization is needed for libpcap. * * The argument specifies whether we use the local code page or UTF-8 * for strings; on UN*X, we just assume UTF-8 in places where the encoding * would matter, whereas, on Windows, we use the local code page for * PCAP_CHAR_ENC_LOCAL and UTF-8 for PCAP_CHAR_ENC_UTF_8. * * On Windows, we also disable the hack in pcap_create() to deal with * being handed UTF-16 strings, because if the user calls this they're * explicitly declaring that they will either be passing local code * page strings or UTF-8 strings, so we don't need to allow UTF-16LE * strings to be passed. For good measure, on Windows *and* UN*X, * we disable pcap_lookupdev(), to prevent anybody from even * *trying* to pass the result of pcap_lookupdev() - which might be * UTF-16LE on Windows, for ugly compatibility reasons - to pcap_create() * or pcap_open_live() or pcap_open(). * * Returns 0 on success, -1 on error. */intpcap_new_api; /* pcap_lookupdev() always fails */intpcap_utf_8_mode; /* Strings should be in UTF-8. */intpcap_init(unsigned intopts, char*errbuf)
And I do some test about std::env::args_os, it returns a OsString Iterator, and the under lying encoding is UTF-8 not UTF-16 nor GBK nor other encodings(only test on my PC). I guess there is some deep Rust std library magic down there.
So I think there could be two solutions:
Expose pcap_init function to Rust users
This way does not make any assumption about what user would pass to the pcap_open_offline function, it's all user's duty to remember call pcap_init at the beginning.
Call pcap_init on every call
This way guarantees this function is called, user don't needs to know such stuff even exists. However, there might be some rare occasions that this is wrong, and would lead to another confusing error.
Activity
Stargateur commentedon Mar 22, 2025
I'm not sure I can fix it, pcap lib is design around unix ecosystem, https://www.tcpdump.org/manpages/pcap_open_offline.3pcap.html, the function is expected an
char *and that always expected to be utf-8, I can probably manage to try to send achar *that is utf 16 encoded with a final nul bytes but I doubt it will work.Another way would be to use
FILE *but it's end with the same problem,fopenexpect anchar *, one would need to use_wfopenor use other windows API. I believe it's too much work for one user and at this point use something dedicated for windows.So in conclusion... set your windows to UTF-8 or rename your file using ascii character.
PS:
If only this will be this simple...
Stargateur commentedon Mar 22, 2025
Please try #384 with utf-16 namefile
JackLiar commentedon Mar 23, 2025
Well, the commit doesn't seem to work on windows, the error is throw out is:
failed to open C:\test\测试.pcapng, libpcap error: C: No such file or directoryWindows sometimes don't use UTF-16 as default for Asian language users,for example GBK for Chinese, Shift_JIS for Japanese, EUC-KR for Korean, assuming UTF-16 on windows is not quite appropriate
I'd like to help improve this part, I'd spent some time next week and see what we can cook :)
JackLiar commentedon Mar 23, 2025
I found the possible solution.
There is a
pcap_initfunction in wpcap/libpcap implemention, this is what it saids:And I do some test about
std::env::args_os, it returns aOsString Iterator, and the under lying encoding is UTF-8 not UTF-16 nor GBK nor other encodings(only test on my PC). I guess there is some deep Rust std library magic down there.So I think there could be two solutions:
Expose
pcap_initfunction to Rust usersThis way does not make any assumption about what user would pass to the
pcap_open_offlinefunction, it's all user's duty to remember callpcap_initat the beginning.Call
pcap_initon every callThis way guarantees this function is called, user don't needs to know such stuff even exists. However, there might be some rare occasions that this is wrong, and would lead to another confusing error.
Hope this would help :)
cicku commentedon Aug 19, 2026
#401 only adds
pcap_initinto the comment, I can draft an implementation after then.