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

The process cannot access the file because it is being used by another process. #1

Open
sauceCruet opened this issue Jul 4, 2023 · 1 comment

Comments

@sauceCruet
Copy link

String dInfo = "tun1";
TunTapDevice Device = new TunTapDevice(dInfo);
Device.ConfigTun(IPAddress.Parse("10.2.2.2"), IPAddress.Parse("10.2.2.1"), IPAddress.Parse("255.255.255.0"));
Device.SetConnectionState(ConnectionStatus.Connected);
Device.CreateDeviceIOStream(1500);
FileStream DeviceIOStream = Device.TunTapDeviceIOStream;
while (true)
{
var bytes = new byte[0xFFFF];
var length = DeviceIOStream.Read(bytes);
int version = bytes[0] >> 4;
int IHL = bytes[0] & 0x0F;
if (version == 4 && IHL == 5) // IPv4 Packet with no options field
{
IPv4Packet received = new IPv4Packet(bytes);
Console.WriteLine($"{received.Protocol} {received.SourceIP}-{received.DestIP}");
}
}
DeviceIOStream.Close();
Console.WriteLine();

Unhandled exception. System.IO.IOException: The process cannot access the file because it is being used by another process.
at System.IO.Strategies.FileStreamHelpers.CheckFileCall(Int64 , String , Boolean )
at System.IO.RandomAccess.ReadAtOffset(SafeFileHandle , Span1 , Int64 ) at System.IO.Strategies.OSFileStreamStrategy.Read(Span1 )
at System.IO.Strategies.BufferedFileStreamStrategy.ReadSpan(Span1 , ArraySegment1 )
at System.IO.Strategies.BufferedFileStreamStrategy.Read(Span1 ) at System.IO.FileStream.Read(Span1 )
at ConsoleApp28.Program.Main(String[] args)
at ConsoleApp28.Program.

(String[] args)
Aborted (core dumped)

@HBSnail
Copy link
Owner

HBSnail commented Jul 6, 2023

Hello sauceCruet,

Emmm. It seems like the TUN device assigned to this program was occupied by other process. I'm a little confused about what platform are you using. The code to make it work varies from OS to OS.

On Windows, you are suppose to use Device GUID to initialize instead of it's name like tun1.
On Linux, please make sure tun1 device has been created and open properly. Currently, function ConfigTun() and SetConnectionState() can only be used on Windows, so you need to do it on Linux manually.

Could you please provide me more info about your platform like OS and it's version.

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

No branches or pull requests

2 participants