windows - Windows 网络设备驱动程序:从驱动程序中设置链接 UP/DOWN
问题描述
我正在为 Windows 编写网络驱动程序。我想做类似下面的伪代码:
Init_interface_link_status = disconnected (Equivalent to DOWN in Linux)
Repeat using delayed workitem:
if (condition is true)
interface_link_status = connected (UP)
break
else
interface_link_status = disconnected (DOWN)
所有这些都发生在驱动程序代码中。
我使用 Windows 驱动程序示例作为参考。我发现了一些看起来很有希望的东西:https://github.com/microsoft/Windows-driver-samples/blob/master/network/ndis/netvmini/6x/adapter.c#L353
AdapterGeneral.MediaConnectState = HWGetMediaConnectStatus(Adapter);
我可以将这个 MediaConnectSate 设置为MediaConnectStateDisconnected
这里和驱动程序在断开连接状态下初始化,这是我想要的。但是在初始化驱动程序后,我找不到在其他地方更改此状态的方法。
解决方案
从专有网络驱动程序代码中找到灵感。此函数打开/关闭接口:
VOID NSUChangeAdapterLinkState(
_In_ PMP_ADAPTER Adapter,
_In_ BOOLEAN TurnInterfaceUP)
/*++
Routine Description:
Change Adapter Link's state. This is equivalent to doing ifup/ifdown on Linux.
Arguments:
Adapter - Pointer to our adapter
TurnInterfaceUP - Pass TRUE to turn interface UP, FALSE to turn DOWN
Return Value:
None
--*/
{
NDIS_LINK_STATE LinkState;
NDIS_STATUS_INDICATION StatusIndication;
RtlZeroMemory(&LinkState, sizeof(LinkState));
LinkState.Header.Revision = NDIS_LINK_STATE_REVISION_1;
LinkState.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;
LinkState.Header.Size = NDIS_SIZEOF_LINK_STATE_REVISION_1;
if (TurnInterfaceUP)
{
LinkState.MediaConnectState = MediaConnectStateConnected;
MP_CLEAR_FLAG(Adapter, fMP_DISCONNECTED);
} else
{
LinkState.MediaConnectState = MediaConnectStateDisconnected;
MP_SET_FLAG(Adapter, fMP_DISCONNECTED);
}
LinkState.RcvLinkSpeed = Adapter->ulLinkRecvSpeed;
LinkState.XmitLinkSpeed = Adapter->ulLinkSendSpeed;
LinkState.MediaDuplexState = MediaDuplexStateFull;
RtlZeroMemory(&StatusIndication, sizeof(StatusIndication));
StatusIndication.Header.Type = NDIS_OBJECT_TYPE_STATUS_INDICATION;
StatusIndication.Header.Revision = NDIS_STATUS_INDICATION_REVISION_1;
StatusIndication.Header.Size = NDIS_SIZEOF_STATUS_INDICATION_REVISION_1;
StatusIndication.SourceHandle = Adapter->AdapterHandle;
StatusIndication.StatusCode = NDIS_STATUS_LINK_STATE;
StatusIndication.StatusBuffer = &LinkState;
StatusIndication.StatusBufferSize = sizeof(NDIS_LINK_STATE);
NdisMIndicateStatusEx(Adapter->AdapterHandle, &StatusIndication);
}
推荐阅读
- python - sqlite 多个同时写入
- javascript - 动态添加的端口不能在jointjs中克隆?
- c# - 如何获取LiveCharts系列中鼠标选中点的坐标?
- c - 我如何优化这个(这是一个我的世界着色器)
- python - 如何使用嵌入式 python 解释器(pybind11)构建和执行 cpp 文件
- c# - 从 JSON 文件在 C# 中创建一个类
- c++ - 此代码显示错误,弹出窗口“调试断言失败”
- .net - 程序作为控制台运行但不作为 Windows 服务运行
- r - dplyr / tidy 方法来过滤基于子字符串的向量?
- javascript - 我在状态中有一个 T 和 Z 格式的数据,React 输入接受 HH:MM ss 格式,所以如何转换