×

# C#在linux使用tun网卡

hqy hqy 发表于2026-04-08 16:34:49 浏览5 评论0

抢沙发发表评论

# C#在linux使用tun网卡

你可以可以直接nuget使用 linker.tun

1、P/Invoke 一些api

C#
internal static class LinuxAPI{    [DllImport("libc.so.6", EntryPoint = "ioctl", SetLastError = true)]    internal static extern int Ioctl(SafeHandle device, uint request, byte[] dat);    internal static int Ioctl(string name, SafeHandle device, uint request)    {        byte[] ifreqFREG0 = Encoding.ASCII.GetBytes(name);        Array.Resize(ref ifreqFREG0, 16);        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 };        byte[] ifreq = BytesPlusBytes(ifreqFREG0, ifreqFREG1);        return LinuxAPI.Ioctl(device, request, ifreq);    }    internal static byte[] BytesPlusBytes(byte[] A, byte[] B)    {        byte[] ret = new byte[A.Length + B.Length - 1 + 1];        int k = 0;        for (var i = 0; i <= A.Length - 1; i++)            ret[i] = A[i];        k = A.Length;        for (var i = k; i <= ret.Length - 1; i++)            ret[i] = B[i - k];        return ret;    }}

2、创建tun网卡

C#
//创建ip tuntap add mode tun dev linker//设置IPip addr add 10.18.18.2/24 dev linker//启用ip link set dev linker up//设置MTUip link set dev linker mtu 1400

3、打开网卡

C#
 SafeFileHandle safeFileHandle = File.OpenHandle("/dev/net/tun", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, FileOptions.Asynchronous); if (safeFileHandle.IsInvalid) {     safeFileHandle?.Dispose();     return false; } int ioctl = LinuxAPI.Ioctl("linker", safeFileHandle, 1074025674); if (ioctl != 0) {     safeFileHandle?.Dispose();     return false; }  FileStream fs = new FileStream(safeFileHandle, FileAccess.ReadWrite, 1500);

4、从网卡读取数据

C#
byte[] buffer = new byte[2 * 1024];int length = fs.Read(buffer.AsSpan());

5、将数据写入网卡

C#
 ReadOnlyMemory<byte> buffer;//你的数据包  fs.Write(buffer.Span); fs.Flush();


打赏

本文链接:https://kinber.cn/post/6388.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客