├── UniversalTunTapDriver ├── UniversalTunTapDriver.csproj ├── WinAPI.cs ├── LinuxAPI.cs ├── TunTapHelper_windows.cs ├── TunTapHelper_linux.cs ├── TunIOManager.cs ├── TunTapHelper.cs └── TunTapDevice.cs ├── UniversalTunTapDriver.sln ├── README.md ├── .gitignore └── LICENSE /UniversalTunTapDriver/UniversalTunTapDriver.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | HBSnail 6 | FlyingSocketDevelopingGroup 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /UniversalTunTapDriver.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31613.86 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniversalTunTapDriver", "UniversalTunTapDriver\UniversalTunTapDriver.csproj", "{DFD56ACB-72BD-4157-BC83-84BA2B08807F}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {DFD56ACB-72BD-4157-BC83-84BA2B08807F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {DFD56ACB-72BD-4157-BC83-84BA2B08807F}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {DFD56ACB-72BD-4157-BC83-84BA2B08807F}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {DFD56ACB-72BD-4157-BC83-84BA2B08807F}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {915B124E-9AFC-4BCB-8684-3BF61A528CEA} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UniversalTunTapDriver 2 | ------ 3 | ## A driver for TUN/TAP devices to support basic operations on both linux and windows platform. 4 | 5 | UniversalTunTapDriver is a lightweight library which allows you to control TUN/TAP devices on your **windows** or **linux** machine easily. 6 | 7 | 8 | 9 | ---------- 10 | ### **UniversalTunTapDriver release list** 11 | #### ***UniversalTunTapDriver V1.2*** 12 | released in *Feb 10, 2023* 13 | > * Add bad handle detection 14 | > * Fix bug: asynchronous I/O can not been used on linux 15 | > * Simplify device initialization 16 | > * More detailed readme doc 17 | 18 | #### ***UniversalTunTapDriver V1.1*** 19 | released in *Aug 21, 2021* 20 | > * Add TunIOManager to organize the I/O operation of a TUN device 21 | > * Fix some bugs 22 | > * Support asynchronous I/O on linux platform 23 | 24 | #### ***UniversalTunTapDriver V1.0*** 25 | released in *Jan 19, 2021* 26 | > * Update form **TunTapDriver-windows*** project 27 | > * Add support for **linux** platform 28 | > * Fix some bugs 29 | 30 | #### ***TunTapDriver-windows V1.0*** 31 | released in *Sep 30, 2020* 32 | > * Upload document and test project 33 | > * Fix some bugs 34 | 35 | #### ***TunTapDriver-windows V0.9.9*** 36 | released in *Aug 22, 2020* 37 | > * Upload code 38 | > * Open source under **MPL2.0 License** 39 | 40 | *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. 41 | 42 | ---------- 43 | 44 | 45 | ### **TODO LIST** 46 | 47 | - [x] Fix some bugs when running on linux platform 48 | - [x] Finish the **UNFINISHED** code 49 | - [x] Develop and test the control abality of TAP devices on windows and linux platform. 50 | - [x] Open source under MPL2.0 License 51 | - [x] Support to control TUN devices on linux platform 52 | - [x] Support to control TUN devices on windows platform 53 | 54 | 55 | ---------- 56 | 57 | 58 | ### **How to use** 59 | 60 | #### **1.Clone the repository** 61 | 62 | ```bash 63 | git clone https://github.com/HBSnail/UniversalTunTapDriver.git 64 | ``` 65 | 66 | #### **2.Compile the source code** 67 | 68 | ``` 69 | This process should be done by yourself. 70 | ``` 71 | 72 | #### **3.Add reference to your own project** 73 | 74 | ``` 75 | This process should be done by yourself. 76 | ``` 77 | 78 | #### **4.Use the APIs we offers in your application** 79 | 80 | ##### **4.0 Get all of the TUN/TAP devices installed on your computer** 81 | ```csharp 82 | // The following code only works on windows 83 | // On linux please use "ip" and "ifconfig" in the terminal for createing and enumerating all network interfaces 84 | // ip tuntap add dev tun0 mode tun 85 | // On windows you MUST install 'TAP-Windows Adapter V9' before execute the code 86 | List DeviceList = GetTapGuidList("tap0901"); 87 | 88 | //TunTapDeviceInfo is a kind of structure defined as follow 89 | /* 90 | public struct TunTapDeviceInfo 91 | { 92 | public string Name; 93 | public string Guid; 94 | public TunTapDeviceInfo(string n, string g) 95 | { 96 | Name = n; 97 | Guid = g; 98 | } 99 | } 100 | */ 101 | ``` 102 | ##### **4.1 Create&initial the deivce** 103 | ```csharp 104 | // On linux please set the dInfo to tun name. like "tun0" "tap0" ect. 105 | // On windows please set the dInfo to tun guid. like "{xxxxx-xxxxx-xxxx}". 106 | String dInfo; 107 | TunTapDevice Device = New TunTapDevice(dInfo); 108 | // 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. 109 | ``` 110 | ##### **4.2 Config&set device states** 111 | ```csharp 112 | // The following code only works on windows 113 | // On linux please use "ifconfig" or "ip" in the terminal. 114 | // ifconfig tun0 x.x.x.x netmask x.x.x.x up 115 | // 116 | 117 | Device.ConfigTun(IPAddress.Parse("LOC_IP"), IPAddress.Parse("REM_IP"), IPAddress.Parse("NET_MASK")); 118 | Device.SetConnectionState(ConnectionStatus.Connected); 119 | 120 | ``` 121 | ##### **4.3 Create&get device I/O stream** 122 | ```csharp 123 | Device.CreateDeviceIOStream(1500); 124 | FileStream DeviceIOStream = Device.TunTapDeviceIOStream; 125 | ``` 126 | 127 | ##### **4.4 READ&WRITE the I/O stream to read&write TUN/TAP devices.** 128 | ``` 129 | This process should be done by yourself. 130 | ``` 131 | 132 | ---------- 133 | 134 | ### **Reference project** 135 | Thanks! 136 | 137 | - [openvpn][2] 138 | - [tap-windows6][3] 139 | - [toyvpn][4] 140 | - [tun-go][5] 141 | 142 | 143 | ---------- 144 | 145 | 146 | Auther: [HBSnail][1] 147 | Feb 10, 2023 148 | 149 | [1]: https://github.com/HBSnail 150 | [2]: https://github.com/OpenVPN/openvpn 151 | [3]: https://github.com/OpenVPN/tap-windows6 152 | [4]: https://android.googlesource.com/platform/development/+/master/samples/ToyVpn 153 | [5]: https://github.com/Alienero/tun-go 154 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/WinAPI.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | using System; 378 | using System.IO; 379 | using System.Runtime.InteropServices; 380 | 381 | namespace UniversalTunTapDriver 382 | { 383 | public static class WinAPI 384 | { 385 | 386 | public const uint FILE_ATTRIBUTE_SYSTEM = 0x4; 387 | public const uint FILE_FLAG_OVERLAPPED = 0x40000000; 388 | public const uint METHOD_BUFFERED = 0; 389 | public const uint FILE_ANY_ACCESS = 0; 390 | public const uint FILE_DEVICE_UNKNOWN = 0x22; 391 | 392 | [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 393 | public static extern IntPtr CreateFile(string FileName, [MarshalAs(UnmanagedType.U4)] FileAccess DesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare ShareMode, uint SecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode CreationDisposition, uint FlagsAndAttributes, IntPtr TemplateFile); 394 | 395 | 396 | [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 397 | public static extern bool DeviceIoControl(IntPtr Device, uint IoControlCode, IntPtr InBuffer, uint InBufferSize, IntPtr OutBuffer, uint OutBufferSize, ref uint BytesReturned, IntPtr Overlapped); 398 | } 399 | } 400 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/LinuxAPI.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | 378 | using System; 379 | using System.Runtime.InteropServices; 380 | 381 | namespace UniversalTunTapDriver 382 | { 383 | public static class LinuxAPI 384 | { 385 | 386 | public const int O_ACCMODE = 0x00000003; 387 | public const int O_RDONLY = 0x00000000; 388 | public const int O_WRONLY = 0x00000001; 389 | public const int O_RDWR = 0x00000002; 390 | public const int O_CREAT = 0x00000040; 391 | public const int O_EXCL = 0x00000080; 392 | public const int O_NOCTTY = 0x00000100; 393 | public const int O_TRUNC = 0x00000200; 394 | public const int O_APPEND = 0x00000400; 395 | public const int O_NONBLOCK = 0x00000800; 396 | public const int O_NDELAY = 0x00000800; 397 | public const int O_SYNC = 0x00101000; 398 | public const int O_ASYNC = 0x00002000; 399 | 400 | [DllImport("libc.so.6", EntryPoint = "open",SetLastError = true)] 401 | public static extern int Open(string fileName, int mode); 402 | 403 | [DllImport("libc.so.6", EntryPoint = "ioctl", SetLastError = true)] 404 | public static extern int Ioctl(int fd, UInt32 request, byte[] dat); 405 | 406 | [DllImport("libc.so.6", EntryPoint = "read", SetLastError = true)] 407 | internal static extern int Read(int handle, byte[] data, int length); 408 | 409 | [DllImport("libc.so.6", EntryPoint = "write", SetLastError = true)] 410 | internal static extern int Write(int handle, byte[] data, int length); 411 | 412 | } 413 | } 414 | 415 | 416 | 417 | 418 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/TunTapHelper_windows.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | 378 | using Microsoft.Win32; 379 | using System; 380 | using System.Collections.Generic; 381 | using System.IO; 382 | using static UniversalTunTapDriver.TunTapHelper; 383 | 384 | namespace UniversalTunTapDriver 385 | { 386 | 387 | public static class TunTapHelper_windows 388 | { 389 | public const int TAP_WIN_IOCTL_GET_MAC = 1; 390 | public const int TAP_WIN_IOCTL_GET_VERSION = 2; 391 | public const int TAP_WIN_IOCTL_GET_MTU = 3; 392 | public const int TAP_WIN_IOCTL_GET_INFO = 4; 393 | public const int TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT = 5; 394 | public const int TAP_WIN_IOCTL_SET_MEDIA_STATUS = 6; 395 | public const int TAP_WIN_IOCTL_CONFIG_DHCP_MASQ = 7; 396 | public const int TAP_WIN_IOCTL_GET_LOG_LINE = 8; 397 | public const int TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT = 9; 398 | public const int TAP_WIN_IOCTL_CONFIG_TUN = 10; 399 | public const string UsermodeDeviceSpace = @"\\.\\Global\\"; 400 | 401 | 402 | public static IntPtr GetDevicePrt_windows(string DeviceIdentification) 403 | { 404 | return WinAPI.CreateFile(UsermodeDeviceSpace + DeviceIdentification + ".tap", FileAccess.ReadWrite, FileShare.ReadWrite, 0, FileMode.Open, WinAPI.FILE_ATTRIBUTE_SYSTEM | WinAPI.FILE_FLAG_OVERLAPPED, IntPtr.Zero); 405 | } 406 | 407 | public static uint CTL_CODE(uint iDeviceType, uint iFunction, uint iMethod, uint iAccess) 408 | { 409 | return ((iDeviceType << 16) | (iAccess << 14) | (iFunction << 2) | iMethod); 410 | } 411 | } 412 | } 413 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/TunTapHelper_linux.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | using System; 378 | 379 | 380 | namespace UniversalTunTapDriver 381 | { 382 | public static class TunTapHelper_linux 383 | { 384 | const UInt32 TUNSETIFF = 1074025674; 385 | 386 | public static int GetDevicePtr_linux(string Name) 387 | { 388 | int TUNinterface = LinuxAPI.Open("/dev/net/tun", LinuxAPI.O_NONBLOCK | LinuxAPI.O_RDWR | LinuxAPI.O_ASYNC); 389 | if (TUNinterface >= 0) 390 | { 391 | byte[] ifreqFREG0 = System.Text.Encoding.ASCII.GetBytes(Name); 392 | Array.Resize(ref ifreqFREG0, 16); 393 | byte[] ifreqFREG1 = { 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 394 | //IFF_TUN | IFF_NO_PI == 4097 == 1001 == 0x10,0x01 tun0 395 | byte[] ifreq = BytesPlusBytes(ifreqFREG0, ifreqFREG1); 396 | int stat = LinuxAPI.Ioctl(TUNinterface, TUNSETIFF, ifreq); 397 | if (stat >= 0) 398 | { 399 | return TUNinterface; 400 | } 401 | } 402 | return 0; 403 | } 404 | 405 | private static byte[] BytesPlusBytes(byte[] A, byte[] B) 406 | { 407 | byte[] ret = new byte[A.Length + B.Length - 1 + 1]; 408 | int k = 0; 409 | for (var i = 0; i <= A.Length - 1; i++) 410 | ret[i] = A[i]; 411 | k = A.Length; 412 | for (var i = k; i <= ret.Length - 1; i++) 413 | ret[i] = B[i - k]; 414 | return ret; 415 | } 416 | } 417 | } 418 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/TunIOManager.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | using System; 378 | using System.Collections.Generic; 379 | using System.IO; 380 | using System.Net; 381 | using System.Reflection; 382 | using System.Runtime.InteropServices; 383 | using System.Threading; 384 | using System.Threading.Tasks; 385 | using static UniversalTunTapDriver.TunTapHelper; 386 | 387 | namespace UniversalTunTapDriver 388 | { 389 | public class TunIOManager 390 | { 391 | private TunTapDevice TunDevice; 392 | public List NotifyList; 393 | 394 | bool IsStart = false; 395 | public byte[] TunBuffer = new byte[1500]; 396 | 397 | public FileStream DeviceIOStream = null; 398 | public int DeviceMTU { get; set; } 399 | public string DeviceIdentification { get; set; } 400 | public byte[] DeviceMAC { get; set; } 401 | public string DeviceVersion { get; set; } 402 | 403 | public TunIOManager(string DeviceIdentification) 404 | { 405 | this.DeviceIdentification = DeviceIdentification; 406 | TunDevice = new TunTapDevice(DeviceIdentification); 407 | 408 | DeviceMAC = TunDevice.GetMAC(); 409 | DeviceMTU = TunDevice.GetMTU(); 410 | DeviceVersion = TunDevice.GetVersion(); 411 | NotifyList = new List(); 412 | } 413 | 414 | public void Start(IPAddress local,IPAddress rem,IPAddress mask) 415 | { 416 | IsStart = true; 417 | TunDevice.ConfigTun(local,rem,mask); 418 | TunDevice.SetConnectionState(ConnectionStatus.Connected); 419 | TunDevice.CreateDeviceIOStream(TunBuffer.Length); 420 | DeviceIOStream = TunDevice.TunTapDeviceIOStream; 421 | MonitorTunStream(); 422 | } 423 | 424 | public void Send(byte[] dat,int offset,int length) 425 | { 426 | DeviceIOStream.Write(dat,offset,length); 427 | DeviceIOStream.Flush(); 428 | } 429 | 430 | private async void MonitorTunStream() 431 | { 432 | Retry: 433 | try 434 | { 435 | while (IsStart) 436 | { 437 | int leng = await DeviceIOStream.ReadAsync(TunBuffer, 0, TunBuffer.Length); 438 | if (leng > 0) 439 | { 440 | byte[] data = new byte[leng]; 441 | Array.Copy(TunBuffer, data, leng); 442 | for (int i = 0; i < NotifyList.Count; i++) 443 | { 444 | try 445 | { 446 | Type T = NotifyList[i].GetType(); 447 | T.InvokeMember("OnTunReceive", BindingFlags.Default | BindingFlags.InvokeMethod, null, NotifyList[i], new object[] { (object)data }); 448 | } 449 | catch (Exception) { } 450 | } 451 | 452 | } 453 | 454 | } 455 | } 456 | catch (Exception) 457 | { 458 | Thread.Sleep(100); 459 | goto Retry; 460 | } 461 | } 462 | 463 | public void Close() 464 | { 465 | NotifyList.Clear(); 466 | IsStart = false; 467 | TunDevice.Close(); 468 | DeviceMTU = -1; 469 | DeviceIdentification = null; 470 | DeviceVersion = null; 471 | Array.Clear(DeviceMAC, 0, DeviceMAC.Length); 472 | DeviceIOStream = null; 473 | } 474 | } 475 | } 476 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/TunTapHelper.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | 378 | using Microsoft.Win32; 379 | using System; 380 | using System.Runtime.InteropServices; 381 | using static UniversalTunTapDriver.TunTapHelper_windows; 382 | using static UniversalTunTapDriver.TunTapHelper_linux; 383 | using System.Collections.Generic; 384 | 385 | namespace UniversalTunTapDriver 386 | { 387 | public static class TunTapHelper 388 | { 389 | public struct TunTapDeviceInfo 390 | { 391 | public string Name; 392 | public string Guid; 393 | public TunTapDeviceInfo(string name, string guid) 394 | { 395 | Name = name; 396 | Guid = guid; 397 | } 398 | } 399 | 400 | public enum ConnectionStatus 401 | { 402 | Connected = 1, 403 | Disconnected = 0 404 | } 405 | 406 | public static string GetAdapterNameByGuid(string iGuid) 407 | { 408 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 409 | { 410 | return Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" + @"\" + iGuid + @"\Connection", false).GetValue("Name").ToString(); 411 | } 412 | else 413 | { 414 | return iGuid; 415 | } 416 | } 417 | 418 | 419 | public static int GetDevicePrt(string DeviceIdentification) 420 | { 421 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 422 | { 423 | return (int)GetDevicePrt_windows(DeviceIdentification); 424 | 425 | } 426 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 427 | { 428 | return GetDevicePtr_linux(DeviceIdentification); 429 | } 430 | else 431 | { 432 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 433 | } 434 | } 435 | 436 | public static List GetTapGuidList(string iComponentId) 437 | { 438 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 439 | { 440 | RegistryKey AdaptersInRegistry = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}", false); 441 | string[] KeyNames = AdaptersInRegistry.GetSubKeyNames(); 442 | List TapGuidList = new List(); 443 | for (var i = 0; i <= KeyNames.Length - 1; i++) 444 | { 445 | try 446 | { 447 | if (KeyNames[i].Trim().ToLower() == "properties" || KeyNames[i].Trim().ToLower() == "configuration") 448 | continue; 449 | RegistryKey SingleAdapterInRegistry = AdaptersInRegistry.OpenSubKey(KeyNames[i]); 450 | string CID = SingleAdapterInRegistry.GetValue("ComponentId").ToString(); 451 | if (CID != null && CID.ToLower().Trim() == iComponentId) 452 | { 453 | string iGuid = SingleAdapterInRegistry.GetValue("NetCfgInstanceId").ToString(); 454 | TapGuidList.Add(new TunTapDeviceInfo(GetAdapterNameByGuid(iGuid), iGuid)); 455 | } 456 | } 457 | catch (Exception) 458 | { 459 | } 460 | } 461 | return TapGuidList; 462 | 463 | } 464 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 465 | { 466 | return null; 467 | } 468 | else 469 | { 470 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 471 | } 472 | 473 | } 474 | 475 | 476 | } 477 | 478 | } 479 | -------------------------------------------------------------------------------- /UniversalTunTapDriver/TunTapDevice.cs: -------------------------------------------------------------------------------- 1 | /* 2 | Mozilla Public License Version 2.0 3 | ================================== 4 | 5 | 1. Definitions 6 | -------------- 7 | 8 | 1.1. "Contributor" 9 | means each individual or legal entity that creates, contributes to 10 | the creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | means the combination of the Contributions of others (if any) used 14 | by a Contributor and that particular Contributor's Contribution. 15 | 16 | 1.3. "Contribution" 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | means Source Code Form to which the initial Contributor has attached 21 | the notice in Exhibit A, the Executable Form of such Source Code 22 | Form, and Modifications of such Source Code Form, in each case 23 | including portions thereof. 24 | 25 | 1.5. "Incompatible With Secondary Licenses" 26 | means 27 | 28 | (a) that the initial Contributor has attached the notice described 29 | in Exhibit B to the Covered Software; or 30 | 31 | (b) that the Covered Software was made available under the terms of 32 | version 1.1 or earlier of the License, but not also under the 33 | terms of a Secondary License. 34 | 35 | 1.6. "Executable Form" 36 | means any form of the work other than Source Code Form. 37 | 38 | 1.7. "Larger Work" 39 | means a work that combines Covered Software with other material, in 40 | a separate file or files, that is not Covered Software. 41 | 42 | 1.8. "License" 43 | means this document. 44 | 45 | 1.9. "Licensable" 46 | means having the right to grant, to the maximum extent possible, 47 | whether at the time of the initial grant or subsequently, any and 48 | all of the rights conveyed by this License. 49 | 50 | 1.10. "Modifications" 51 | means any of the following: 52 | 53 | (a) any file in Source Code Form that results from an addition to, 54 | deletion from, or modification of the contents of Covered 55 | Software; or 56 | 57 | (b) any new file in Source Code Form that contains any Covered 58 | Software. 59 | 60 | 1.11. "Patent Claims" of a Contributor 61 | means any patent claim(s), including without limitation, method, 62 | process, and apparatus claims, in any patent Licensable by such 63 | Contributor that would be infringed, but for the grant of the 64 | License, by the making, using, selling, offering for sale, having 65 | made, import, or transfer of either its Contributions or its 66 | Contributor Version. 67 | 68 | 1.12. "Secondary License" 69 | means either the GNU General Public License, Version 2.0, the GNU 70 | Lesser General Public License, Version 2.1, the GNU Affero General 71 | Public License, Version 3.0, or any later versions of those 72 | licenses. 73 | 74 | 1.13. "Source Code Form" 75 | means the form of the work preferred for making modifications. 76 | 77 | 1.14. "You" (or "Your") 78 | means an individual or a legal entity exercising rights under this 79 | License. For legal entities, "You" includes any entity that 80 | controls, is controlled by, or is under common control with You. For 81 | purposes of this definition, "control" means (a) the power, direct 82 | or indirect, to cause the direction or management of such entity, 83 | whether by contract or otherwise, or (b) ownership of more than 84 | fifty percent (50%) of the outstanding shares or beneficial 85 | ownership of such entity. 86 | 87 | 2. License Grants and Conditions 88 | -------------------------------- 89 | 90 | 2.1. Grants 91 | 92 | Each Contributor hereby grants You a world-wide, royalty-free, 93 | non-exclusive license: 94 | 95 | (a) under intellectual property rights (other than patent or trademark) 96 | Licensable by such Contributor to use, reproduce, make available, 97 | modify, display, perform, distribute, and otherwise exploit its 98 | Contributions, either on an unmodified basis, with Modifications, or 99 | as part of a Larger Work; and 100 | 101 | (b) under Patent Claims of such Contributor to make, use, sell, offer 102 | for sale, have made, import, and otherwise transfer either its 103 | Contributions or its Contributor Version. 104 | 105 | 2.2. Effective Date 106 | 107 | The licenses granted in Section 2.1 with respect to any Contribution 108 | become effective for each Contribution on the date the Contributor first 109 | distributes such Contribution. 110 | 111 | 2.3. Limitations on Grant Scope 112 | 113 | The licenses granted in this Section 2 are the only rights granted under 114 | this License. No additional rights or licenses will be implied from the 115 | distribution or licensing of Covered Software under this License. 116 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 117 | Contributor: 118 | 119 | (a) for any code that a Contributor has removed from Covered Software; 120 | or 121 | 122 | (b) for infringements caused by: (i) Your and any other third party's 123 | modifications of Covered Software, or (ii) the combination of its 124 | Contributions with other software (except as part of its Contributor 125 | Version); or 126 | 127 | (c) under Patent Claims infringed by Covered Software in the absence of 128 | its Contributions. 129 | 130 | This License does not grant any rights in the trademarks, service marks, 131 | or logos of any Contributor (except as may be necessary to comply with 132 | the notice requirements in Section 3.4). 133 | 134 | 2.4. Subsequent Licenses 135 | 136 | No Contributor makes additional grants as a result of Your choice to 137 | distribute the Covered Software under a subsequent version of this 138 | License (see Section 10.2) or under the terms of a Secondary License (if 139 | permitted under the terms of Section 3.3). 140 | 141 | 2.5. Representation 142 | 143 | Each Contributor represents that the Contributor believes its 144 | Contributions are its original creation(s) or it has sufficient rights 145 | to grant the rights to its Contributions conveyed by this License. 146 | 147 | 2.6. Fair Use 148 | 149 | This License is not intended to limit any rights You have under 150 | applicable copyright doctrines of fair use, fair dealing, or other 151 | equivalents. 152 | 153 | 2.7. Conditions 154 | 155 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 156 | in Section 2.1. 157 | 158 | 3. Responsibilities 159 | ------------------- 160 | 161 | 3.1. Distribution of Source Form 162 | 163 | All distribution of Covered Software in Source Code Form, including any 164 | Modifications that You create or to which You contribute, must be under 165 | the terms of this License. You must inform recipients that the Source 166 | Code Form of the Covered Software is governed by the terms of this 167 | License, and how they can obtain a copy of this License. You may not 168 | attempt to alter or restrict the recipients' rights in the Source Code 169 | Form. 170 | 171 | 3.2. Distribution of Executable Form 172 | 173 | If You distribute Covered Software in Executable Form then: 174 | 175 | (a) such Covered Software must also be made available in Source Code 176 | Form, as described in Section 3.1, and You must inform recipients of 177 | the Executable Form how they can obtain a copy of such Source Code 178 | Form by reasonable means in a timely manner, at a charge no more 179 | than the cost of distribution to the recipient; and 180 | 181 | (b) You may distribute such Executable Form under the terms of this 182 | License, or sublicense it under different terms, provided that the 183 | license for the Executable Form does not attempt to limit or alter 184 | the recipients' rights in the Source Code Form under this License. 185 | 186 | 3.3. Distribution of a Larger Work 187 | 188 | You may create and distribute a Larger Work under terms of Your choice, 189 | provided that You also comply with the requirements of this License for 190 | the Covered Software. If the Larger Work is a combination of Covered 191 | Software with a work governed by one or more Secondary Licenses, and the 192 | Covered Software is not Incompatible With Secondary Licenses, this 193 | License permits You to additionally distribute such Covered Software 194 | under the terms of such Secondary License(s), so that the recipient of 195 | the Larger Work may, at their option, further distribute the Covered 196 | Software under the terms of either this License or such Secondary 197 | License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices 202 | (including copyright notices, patent notices, disclaimers of warranty, 203 | or limitations of liability) contained within the Source Code Form of 204 | the Covered Software, except that You may alter any license notices to 205 | the extent required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on 212 | behalf of any Contributor. You must make it absolutely clear that any 213 | such warranty, support, indemnity, or liability obligation is offered by 214 | You alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | --------------------------------------------------- 222 | 223 | If it is impossible for You to comply with any of the terms of this 224 | License with respect to some or all of the Covered Software due to 225 | statute, judicial order, or regulation then You must: (a) comply with 226 | the terms of this License to the maximum extent possible; and (b) 227 | describe the limitations and the code they affect. Such description must 228 | be placed in a text file included with all distributions of the Covered 229 | Software under this License. Except to the extent prohibited by statute 230 | or regulation, such description must be sufficiently detailed for a 231 | recipient of ordinary skill to be able to understand it. 232 | 233 | 5. Termination 234 | -------------- 235 | 236 | 5.1. The rights granted under this License will terminate automatically 237 | if You fail to comply with any of its terms. However, if You become 238 | compliant, then the rights granted under this License from a particular 239 | Contributor are reinstated (a) provisionally, unless and until such 240 | Contributor explicitly and finally terminates Your grants, and (b) on an 241 | ongoing basis, if such Contributor fails to notify You of the 242 | non-compliance by some reasonable means prior to 60 days after You have 243 | come back into compliance. Moreover, Your grants from a particular 244 | Contributor are reinstated on an ongoing basis if such Contributor 245 | notifies You of the non-compliance by some reasonable means, this is the 246 | first time You have received notice of non-compliance with this License 247 | from such Contributor, and You become compliant prior to 30 days after 248 | Your receipt of the notice. 249 | 250 | 5.2. If You initiate litigation against any entity by asserting a patent 251 | infringement claim (excluding declaratory judgment actions, 252 | counter-claims, and cross-claims) alleging that a Contributor Version 253 | directly or indirectly infringes any patent, then the rights granted to 254 | You by any and all Contributors for the Covered Software under Section 255 | 2.1 of this License shall terminate. 256 | 257 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 258 | end user license agreements (excluding distributors and resellers) which 259 | have been validly granted by You or Your distributors under this License 260 | prior to termination shall survive termination. 261 | 262 | ************************************************************************ 263 | * * 264 | * 6. Disclaimer of Warranty * 265 | * ------------------------- * 266 | * * 267 | * Covered Software is provided under this License on an "as is" * 268 | * basis, without warranty of any kind, either expressed, implied, or * 269 | * statutory, including, without limitation, warranties that the * 270 | * Covered Software is free of defects, merchantable, fit for a * 271 | * particular purpose or non-infringing. The entire risk as to the * 272 | * quality and performance of the Covered Software is with You. * 273 | * Should any Covered Software prove defective in any respect, You * 274 | * (not any Contributor) assume the cost of any necessary servicing, * 275 | * repair, or correction. This disclaimer of warranty constitutes an * 276 | * essential part of this License. No use of any Covered Software is * 277 | * authorized under this License except under this disclaimer. * 278 | * * 279 | ************************************************************************ 280 | 281 | ************************************************************************ 282 | * * 283 | * 7. Limitation of Liability * 284 | * -------------------------- * 285 | * * 286 | * Under no circumstances and under no legal theory, whether tort * 287 | * (including negligence), contract, or otherwise, shall any * 288 | * Contributor, or anyone who distributes Covered Software as * 289 | * permitted above, be liable to You for any direct, indirect, * 290 | * special, incidental, or consequential damages of any character * 291 | * including, without limitation, damages for lost profits, loss of * 292 | * goodwill, work stoppage, computer failure or malfunction, or any * 293 | * and all other commercial damages or losses, even if such party * 294 | * shall have been informed of the possibility of such damages. This * 295 | * limitation of liability shall not apply to liability for death or * 296 | * personal injury resulting from such party's negligence to the * 297 | * extent applicable law prohibits such limitation. Some * 298 | * jurisdictions do not allow the exclusion or limitation of * 299 | * incidental or consequential damages, so this exclusion and * 300 | * limitation may not apply to You. * 301 | * * 302 | ************************************************************************ 303 | 304 | 8. Litigation 305 | ------------- 306 | 307 | Any litigation relating to this License may be brought only in the 308 | courts of a jurisdiction where the defendant maintains its principal 309 | place of business and such litigation shall be governed by laws of that 310 | jurisdiction, without reference to its conflict-of-law provisions. 311 | Nothing in this Section shall prevent a party's ability to bring 312 | cross-claims or counter-claims. 313 | 314 | 9. Miscellaneous 315 | ---------------- 316 | 317 | This License represents the complete agreement concerning the subject 318 | matter hereof. If any provision of this License is held to be 319 | unenforceable, such provision shall be reformed only to the extent 320 | necessary to make it enforceable. Any law or regulation which provides 321 | that the language of a contract shall be construed against the drafter 322 | shall not be used to construe this License against a Contributor. 323 | 324 | 10. Versions of the License 325 | --------------------------- 326 | 327 | 10.1. New Versions 328 | 329 | Mozilla Foundation is the license steward. Except as provided in Section 330 | 10.3, no one other than the license steward has the right to modify or 331 | publish new versions of this License. Each version will be given a 332 | distinguishing version number. 333 | 334 | 10.2. Effect of New Versions 335 | 336 | You may distribute the Covered Software under the terms of the version 337 | of the License under which You originally received the Covered Software, 338 | or under the terms of any subsequent version published by the license 339 | steward. 340 | 341 | 10.3. Modified Versions 342 | 343 | If you create software not governed by this License, and you want to 344 | create a new license for such software, you may create and use a 345 | modified version of this License if you rename the license and remove 346 | any references to the name of the license steward (except to note that 347 | such modified license differs from this License). 348 | 349 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 350 | Licenses 351 | 352 | If You choose to distribute Source Code Form that is Incompatible With 353 | Secondary Licenses under the terms of this version of the License, the 354 | notice described in Exhibit B of this License must be attached. 355 | 356 | Exhibit A - Source Code Form License Notice 357 | ------------------------------------------- 358 | 359 | This Source Code Form is subject to the terms of the Mozilla Public 360 | License, v. 2.0. If a copy of the MPL was not distributed with this 361 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 362 | 363 | If it is not possible or desirable to put the notice in a particular 364 | file, then You may include the notice in a location (such as a LICENSE 365 | file in a relevant directory) where a recipient would be likely to look 366 | for such a notice. 367 | 368 | You may add additional accurate notices of copyright ownership. 369 | 370 | Exhibit B - "Incompatible With Secondary Licenses" Notice 371 | --------------------------------------------------------- 372 | 373 | This Source Code Form is "Incompatible With Secondary Licenses", as 374 | defined by the Mozilla Public License, v. 2.0. 375 | 376 | */ 377 | 378 | using System; 379 | using System.IO; 380 | using System.Net; 381 | using System.Runtime.InteropServices; 382 | using Microsoft.Win32.SafeHandles; 383 | using static UniversalTunTapDriver.TunTapHelper; 384 | using static UniversalTunTapDriver.TunTapHelper_windows; 385 | using static UniversalTunTapDriver.WinAPI; 386 | 387 | namespace UniversalTunTapDriver 388 | { 389 | public class TunTapDevice 390 | { 391 | private int DeviceHandle = -1; 392 | public string DeviceIdentification; 393 | public FileStream TunTapDeviceIOStream; 394 | 395 | public TunTapDevice(string DeviceIdentification) 396 | { 397 | this.DeviceIdentification = DeviceIdentification; 398 | this.DeviceHandle = GetDevicePrt(this.DeviceIdentification); 399 | } 400 | 401 | public bool SetConnectionState(ConnectionStatus iState) 402 | { 403 | if (DeviceHandle == -1 || DeviceHandle == 0) 404 | { 405 | throw new Exception("DeviceHandle=" + DeviceHandle.ToString()); 406 | } 407 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 408 | { 409 | uint Length = 0; 410 | IntPtr cconfig = Marshal.AllocHGlobal(4); 411 | Marshal.WriteInt32(cconfig, (int)iState); 412 | return DeviceIoControl((IntPtr)DeviceHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_SET_MEDIA_STATUS, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 4, cconfig, 4, ref Length, IntPtr.Zero); 413 | } 414 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 415 | { 416 | //On linux platform this operation must be done manually 417 | //Always return true 418 | return true; 419 | } 420 | else 421 | { 422 | //return false; 423 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 424 | } 425 | } 426 | 427 | public bool CreateDeviceIOStream(int bufferSize) 428 | { 429 | if (TunTapDeviceIOStream != null) 430 | { 431 | return true; 432 | } 433 | SafeFileHandle Handle = new SafeFileHandle((IntPtr)DeviceHandle, true); 434 | if (Handle.IsInvalid) 435 | { 436 | return false; 437 | } 438 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 439 | { 440 | TunTapDeviceIOStream = new FileStream(Handle, FileAccess.ReadWrite, bufferSize, true); 441 | } 442 | else 443 | { 444 | // since we have opeded the device in async mode, we must not set the stream as async in linux system 445 | TunTapDeviceIOStream = new FileStream(Handle, FileAccess.ReadWrite, bufferSize, false); 446 | } 447 | return true; 448 | } 449 | 450 | public void Dispose() 451 | { 452 | Close(); 453 | if (TunTapDeviceIOStream != null) 454 | { 455 | TunTapDeviceIOStream.Flush(); 456 | TunTapDeviceIOStream.Close(); 457 | TunTapDeviceIOStream.Dispose(); 458 | TunTapDeviceIOStream = null; 459 | } 460 | DeviceHandle = -1; 461 | DeviceIdentification = null; 462 | } 463 | 464 | public void Close() 465 | { 466 | SetConnectionState(ConnectionStatus.Disconnected); 467 | } 468 | 469 | public bool ConfigTun(IPAddress localIPAddress, IPAddress remIPAddress, IPAddress mask) 470 | { 471 | if(DeviceHandle== -1 || DeviceHandle == 0) 472 | { 473 | throw new Exception("DeviceHandle=" + DeviceHandle.ToString()); 474 | } 475 | 476 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 477 | { 478 | uint Length = 0; 479 | IntPtr cconfig = Marshal.AllocHGlobal(12); 480 | byte[] IP = BitConverter.GetBytes(System.Convert.ToUInt32(localIPAddress.Address)); 481 | for (var i = 0; i < IP.Length ; i++) 482 | Marshal.WriteByte(cconfig, i, IP[i]); 483 | IP = BitConverter.GetBytes(System.Convert.ToUInt32(remIPAddress.Address)); 484 | for (var i = 0; i < IP.Length; i++) 485 | Marshal.WriteByte(cconfig, i + 4, IP[i]); 486 | IP = BitConverter.GetBytes(System.Convert.ToUInt32(mask.Address)); 487 | for (var i = 0; i < IP.Length ; i++) 488 | Marshal.WriteByte(cconfig, i + 8, IP[i]); 489 | return DeviceIoControl((IntPtr)DeviceHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_CONFIG_TUN, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 12, cconfig, 12, ref Length, IntPtr.Zero); 490 | } 491 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 492 | { 493 | //On linux platform this operation must be done manually 494 | //Always return true 495 | return true; 496 | } 497 | else 498 | { 499 | //return false; 500 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 501 | } 502 | } 503 | public bool ConfigPoint2Point(IPAddress LocalIPAddress, IPAddress RemoteIPAddress) 504 | { 505 | if (DeviceHandle == -1 || DeviceHandle == 0) 506 | { 507 | throw new Exception("DeviceHandle=" + DeviceHandle.ToString()); 508 | } 509 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 510 | { 511 | uint Length = 0; 512 | IntPtr cconfig = Marshal.AllocHGlobal(8); 513 | byte[] IP = BitConverter.GetBytes(System.Convert.ToUInt32(LocalIPAddress.Address)); 514 | for (var i = 0; i < IP.Length; i++) 515 | Marshal.WriteByte(cconfig, i, IP[i]); 516 | IP = BitConverter.GetBytes(System.Convert.ToUInt32(RemoteIPAddress.Address)); 517 | for (var i = 0; i < IP.Length; i++) 518 | Marshal.WriteByte(cconfig, i + 4, IP[i]); 519 | return DeviceIoControl((IntPtr)DeviceHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 8, cconfig, 8, ref Length, IntPtr.Zero); 520 | } 521 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 522 | { 523 | //On linux platform this operation must be done manually 524 | //Always return true 525 | return true; 526 | } 527 | else 528 | { 529 | //return false; 530 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 531 | } 532 | } 533 | 534 | public byte[] GetMAC() 535 | { 536 | if (DeviceHandle == -1 || DeviceHandle == 0) 537 | { 538 | throw new Exception("DeviceHandle=" + DeviceHandle.ToString()); 539 | } 540 | 541 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 542 | { 543 | uint Length = 0; 544 | IntPtr cconfig = Marshal.AllocHGlobal(6); 545 | if (DeviceIoControl((IntPtr)DeviceHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_GET_MAC, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 6, cconfig, 6, ref Length, IntPtr.Zero)) 546 | { 547 | byte[] ret = new byte[Length - 1 + 1]; 548 | for (int i = 0; i <= Length - 1; i++) 549 | ret[i] = Marshal.ReadByte(cconfig, i); 550 | return ret; 551 | } 552 | return null; 553 | } 554 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 555 | { 556 | //NotSupport ON LINUX 557 | 558 | return new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 559 | } 560 | else 561 | { 562 | //return false; 563 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 564 | } 565 | } 566 | 567 | public string GetVersion() 568 | { 569 | if (DeviceHandle == -1 || DeviceHandle == 0) 570 | { 571 | throw new Exception("DeviceHandle=" + DeviceHandle.ToString()); 572 | } 573 | 574 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 575 | { 576 | uint Length = 0; 577 | IntPtr cconfig = Marshal.AllocHGlobal(12); 578 | if (DeviceIoControl((IntPtr)DeviceHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_GET_VERSION, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 12, cconfig, 12, ref Length, IntPtr.Zero)) 579 | { 580 | string ret = Marshal.ReadInt32(cconfig, 0) + "." + Marshal.ReadInt32(cconfig, 4); 581 | if (Marshal.ReadInt32(cconfig, 8) == 1) 582 | ret += " (DEBUG)"; 583 | return ret; 584 | } 585 | return "UNDEFINED"; 586 | } 587 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 588 | { 589 | //NotSupport ON LINUX 590 | return "UNDEFINED"; 591 | } 592 | else 593 | { 594 | //return false; 595 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 596 | } 597 | } 598 | 599 | public int GetMTU() 600 | { 601 | if (DeviceHandle == -1 || DeviceHandle == 0) 602 | { 603 | throw new Exception("DeviceHandle=" + DeviceHandle.ToString()); 604 | } 605 | 606 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 607 | { 608 | uint Length = 0; 609 | IntPtr cconfig = Marshal.AllocHGlobal(4); 610 | if (DeviceIoControl((IntPtr)DeviceHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_GET_MTU, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 4, cconfig, 4, ref Length, IntPtr.Zero)) 611 | return Marshal.ReadInt32(cconfig, 0); 612 | return -1; 613 | } 614 | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 615 | { 616 | //NotSupport ON LINUX 617 | return -1; 618 | } 619 | else 620 | { 621 | //return false; 622 | throw new PlatformNotSupportedException("This program can only run on windows OR linux platform."); 623 | } 624 | } 625 | 626 | } 627 | 628 | 629 | } 630 | --------------------------------------------------------------------------------