dotpcap / sharppcap

Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
1.31k stars 267 forks source link

Non ASCII chars on path \ file name causing exception #300

Closed odedshimon closed 3 years ago

odedshimon commented 3 years ago

Hi!

I came across this exception when trying to read a file that contains non ASCII chars at his name:
"Unable to open offline adapter: : Invalid argument" The non ASCII chars are shown as question marks (picture attached) even though the file exists (the original file name is: "Non ASCII file name - (אבג).pcap"). image

Thank you for the efforts invested in the development of this package!

Oded.

kayoub5 commented 3 years ago

Solution would be to set libpcap to utf-8 mode using pcap_init

chmorgan commented 3 years ago

@kayoub5 should we be doing that by default? There was another user with a similar issue, I think Chinese locale. I don't see us calling pcap_init() anywhere at the moment.

kayoub5 commented 3 years ago

@kayoub5 should we be doing that by default? There was another user with a similar issue, I think Chinese locale. I don't see us calling pcap_init() anywhere at the moment.

I believe so, this ticket is easier to reproduce compared to #87 Please note that the call to pcap_init may fail old libpcap installations, since the method was added rather recently.

chmorgan commented 3 years ago

We don't call pcap_wsockinit either. This looks like new behavior, never heard that we needed to make such calls in the past. https://man7.org/linux/man-pages/man3/pcap_init.3pcap.html

kayoub5 commented 3 years ago

@odedshimon could you try #303 and see if it fixes the problem for you, string encoding is a bit tricky and depends a lot on the framework, os and system encoding being used

odedshimon commented 3 years ago

Sure. I have tested it: cloned the repo, create an updated nuget and installed it (Windows 10, Net Core 3.1.4).

Seems that the file is still not recognized (appears as "????" although the string is "אבגדה"): image image

I have also tried the example project ("ReadingCaptureFile"), still same exception: image

I have uploaded an example file (just change the extension to "pcap") אבגדה.txt

kayoub5 commented 3 years ago

@odedshimon What path does ConfigureStringEncoding take during initialization?

kayoub5 commented 3 years ago

@odedshimon screenshot indicates you are in master branch, encoding support is still in feature/pcap_encoding branch

odedshimon commented 3 years ago

Sorry, I thought it was merged 😃

  1. Did all the above checks again after checking out this branch - no change.
  2. Pcap.Version - "Npcap version 1.50, based on libpcap version 1.10.1-PRE-GIT"
  3. ConfigureStringEncoding - I'm not sure what to check, feel free to guide me.
  4. Npcap version - 1.50
  5. GetACP(), I used python to call winapi - 1252 (see attached photo)
  6. chcp result - Active code page: 437

image

image

kayoub5 commented 3 years ago

This indicates that the file name may have not been passed correctly to the application from the command line image The code for this line is

Console.WriteLine("opening '{0}'", capFile);

Meaning that the file name encoding may have been screwed up even before it arrived to the CaptureFileReaderDevice


ConfigureStringEncoding - I'm not sure what to check, feel free to guide me.

There are 4 return statements, which one got executed, return Encoding.UTF8; return Encoding.GetEncoding((int)GetACP()); or return Encoding.ASCII;

odedshimon commented 3 years ago

I have verified that the path is passed properly and it seems to work: image Well done.