[英]Placing a TAP-Windows device in TAP mode
我正在使用TAP-Windows(來自OpenVPN項目)創建一個界面,如此處和此處所述 。 我基於第一篇文章中提供的示例代碼:
const string UsermodeDeviceSpace = "\\\\.\\Global\\";
string devGuid = GetDeviceGuid();
IntPtr ptr= CreateFile(UsermodeDeviceSpace+devGuid+".tap",
FileAccess.ReadWrite,
FileShare.ReadWrite,
0,
FileMode.Open,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
IntPtr.Zero);
int len;
IntPtr pstatus = Marshal.AllocHGlobal(4);
Marshal.WriteInt32(pstatus, 1);
// Set media status to connected
DeviceIoControl (ptr,
TAP_CONTROL_CODE (6, METHOD_BUFFERED), pstatus, 4,
pstatus,
4,
out len,
IntPtr.Zero);
IntPtr ptun = Marshal.AllocHGlobal(12);
Marshal.WriteInt32(ptun, 0, 0x0100030a); // 10.0.0.1, Interface address
Marshal.WriteInt32(ptun, 4, 0x0000030a); // 10.0.0.0, Network
Marshal.WriteInt32(ptun, 8, unchecked((int)0x00ffffff)); // 255.255.255.0, Netmask
// Config TUN
DeviceIoControl (ptr,
TAP_CONTROL_CODE (10, METHOD_BUFFERED)
ptun,
12,
ptun,
12,
out len,
IntPtr.Zero);
tap = new FileStream(ptr, FileAccess.ReadWrite, true, 10000, true);
// Setup callbacks, etc. here.
while (true)
{
// Read from the device
}
設備似乎處於TUN模式 - 當我獲得數據包的有效負載時,不包括以太網頭。 強制接口在TAP模式下運行的正確方法是什么?
我在VirtualBox中的32位Windows 7虛擬機上運行代碼。
我剛才正在使用這個例子。 我知道你打電話的時候:
IntPtr ptun = Marshal.AllocHGlobal(12);
Marshal.WriteInt32(ptun, 0, 0x0100030a); // 10.0.0.1, Interface address
Marshal.WriteInt32(ptun, 4, 0x0000030a); // 10.0.0.0, Network
Marshal.WriteInt32(ptun, 8, unchecked((int)0x00ffffff)); // 255.255.255.0, Netmask
// Config TUN
DeviceIoControl (ptr,
TAP_CONTROL_CODE (10, METHOD_BUFFERED)
ptun,
12,
ptun,
12,
out len,
IntPtr.Zero);
當界面處於TUN模式時。 我做的只是忽略這一點(我沒有調用DeviceIoControl),然后是文件流接收的所有數據包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.