Git Product home page Git Product logo

universaltuntapdriver's Introduction

UniversalTunTapDriver


A driver for TUN/TAP devices to support basic operations on both linux and windows platform.

UniversalTunTapDriver is a lightweight library which allows you to control TUN/TAP devices on your windows or linux machine easily.


UniversalTunTapDriver release list

UniversalTunTapDriver V1.2

released in Feb 10, 2023

  • Add bad handle detection
  • Fix bug: asynchronous I/O can not been used on linux
  • Simplify device initialization
  • More detailed readme doc

UniversalTunTapDriver V1.1

released in Aug 21, 2021

  • Add TunIOManager to organize the I/O operation of a TUN device
  • Fix some bugs
  • Support asynchronous I/O on linux platform

UniversalTunTapDriver V1.0

released in Jan 19, 2021

  • Update form TunTapDriver-windows* project
  • Add support for linux platform
  • Fix some bugs

TunTapDriver-windows V1.0

released in Sep 30, 2020

  • Upload document and test project
  • Fix some bugs

TunTapDriver-windows V0.9.9

released in Aug 22, 2020

  • Upload code
  • Open source under MPL2.0 License

*This project is TunTapDriver-windows before Jan 19, 2021. TunTapDriver-windows only support to control TUN/TAP device under windows platform. All the features in the previous are included in UniversalTunTapDriver project.


TODO LIST

  • Fix some bugs when running on linux platform
  • Finish the UNFINISHED code
  • Develop and test the control abality of TAP devices on windows and linux platform.
  • Open source under MPL2.0 License
  • Support to control TUN devices on linux platform
  • Support to control TUN devices on windows platform

How to use

1.Clone the repository

git clone https://github.com/HBSnail/UniversalTunTapDriver.git

2.Compile the source code

This process should be done by yourself.

3.Add reference to your own project

This process should be done by yourself.

4.Use the APIs we offers in your application

4.0 Get all of the TUN/TAP devices installed on your computer
 // The following code only works on windows
 // On linux please use "ip" and "ifconfig" in the terminal for createing and enumerating all network interfaces
 // ip tuntap add dev tun0 mode tun
 // On windows you MUST install 'TAP-Windows Adapter V9' before execute the code
 List<TunTapDeviceInfo> DeviceList  = GetTapGuidList("tap0901");
 
 //TunTapDeviceInfo is a kind of structure defined as follow
 /*
     public struct TunTapDeviceInfo
        {
            public string Name;
            public string Guid;
            public TunTapDeviceInfo(string n, string g)
            {
                Name = n;
                Guid = g;
            }
        }
 */
4.1 Create&initial the deivce
 // On linux please set the dInfo to tun name. like "tun0" "tap0" ect.
 // On windows please set the dInfo to tun guid. like "{xxxxx-xxxxx-xxxx}".
    String dInfo; 
    TunTapDevice Device = New TunTapDevice(dInfo);
 // After this you will successfully create and initial a TUN/TAP device except the name or guid does not exist or the divice was occupied by other process. 
4.2 Config&set device states
 // The following code only works on windows
 // On linux please use "ifconfig" or "ip" in the terminal.
 // ifconfig tun0 x.x.x.x netmask x.x.x.x up
 // 

 Device.ConfigTun(IPAddress.Parse("LOC_IP"), IPAddress.Parse("REM_IP"), IPAddress.Parse("NET_MASK"));
 Device.SetConnectionState(ConnectionStatus.Connected);
4.3 Create&get device I/O stream
 Device.CreateDeviceIOStream(1500);
 FileStream DeviceIOStream = Device.TunTapDeviceIOStream;
4.4 READ&WRITE the I/O stream to read&write TUN/TAP devices.
This process should be done by yourself.

Reference project

Thanks!


Auther: HBSnail
Feb 10, 2023

universaltuntapdriver's People

Contributors

hbsnail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

universaltuntapdriver's Issues

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

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.