gpt4 book ai didi

c# - 如何通过没有 IP 地址的网卡发送 WOL 包(或任何东西)?

转载 作者:太空狗 更新时间:2023-10-29 20:06:42 25 4
gpt4 key购买 nike

我试图在所有接口(interface)上发送一个 WOL 包以唤醒网关(这是 DHCP 服务器,所以机器还没有 IP)。

而且我似乎只能将套接字绑定(bind)到 IP 和端口对...

所以问题是:如何创建绑定(bind)到没有 IP 的 NIC 的套接字(或其他东西)?(任何语言都可以。首选c#)

@ctacke:我知道 WOL 是通过 MAC 地址完成的...我的问题是 Windows 只在 NIC 上发送 UDP 广播,Windows 认为这是主 NIC(甚至不是默认路由的 NIC我的 Vista 机器)。而且我似乎找不到将套接字绑定(bind)到没有 IP 地址的接口(interface)的方法。 (就像 DHCP 客户端那样做)

@Arnout:为什么不呢?客户端知道网关的 MAC 地址。我只想像 DHCP 客户端最初那样发送 WOL 数据包...(DHCP 发现数据包声称来自 0.0.0.0)我不介意是否必须逐字节构建整个数据包...

最佳答案

看来我找到了解决办法。可以使用 winpcap 将数据包注入(inject)任何接口(interface)。.net 有很好的包装器:http://www.tamirgal.com/home/dev.aspx?Item=SharpPcap

(我更喜欢不需要安装额外库的解决方案...)

更新:这是我在所有接口(interface)上发送 WOL 数据包的结果:

//You need SharpPcap for this to work

private void WakeFunction(string MAC_ADDRESS)
{
/* Retrieve the device list */
Tamir.IPLib.PcapDeviceList devices = Tamir.IPLib.SharpPcap.GetAllDevices();

/*If no device exists, print error */
if (devices.Count < 1)
{
Console.WriteLine("No device found on this machine");
return;
}

foreach (NetworkDevice device in devices)
{
//Open the device
device.PcapOpen();

//A magic packet is a broadcast frame containing anywhere within its payload: 6 bytes of ones
//(resulting in hexadecimal FF FF FF FF FF FF), followed by sixteen repetitions

byte[] bytes = new byte[120];
int counter = 0;
for (int y = 0; y < 6; y++)
bytes[counter++] = 0xFF;
//now repeat MAC 16 times
for (int y = 0; y < 16; y++)
{
int i = 0;
for (int z = 0; z < 6; z++)
{
bytes[counter++] =
byte.Parse(MAC_ADDRESS.Substring(i, 2),
NumberStyles.HexNumber);
i += 2;
}
}

byte[] etherheader = new byte[54];//If you say so...
var myPacket = new Tamir.IPLib.Packets.UDPPacket(EthernetFields_Fields.ETH_HEADER_LEN, etherheader);

//Ethernet
myPacket.DestinationHwAddress = "FFFFFFFFFFFFF";//it's buggy if you don't have lots of "F"s... (I don't really understand it...)
try { myPacket.SourceHwAddress = device.MacAddress; }
catch { myPacket.SourceHwAddress = "0ABCDEF"; }//whatever
myPacket.EthernetProtocol = EthernetProtocols_Fields.IP;

//IP
myPacket.DestinationAddress = "255.255.255.255";
try { myPacket.SourceAddress = device.IpAddress; }
catch { myPacket.SourceAddress = "0.0.0.0"; }
myPacket.IPProtocol = IPProtocols_Fields.UDP;
myPacket.TimeToLive = 50;
myPacket.Id = 100;
myPacket.Version = 4;
myPacket.IPTotalLength = bytes.Length - EthernetFields_Fields.ETH_HEADER_LEN; //Set the correct IP length
myPacket.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;

//UDP
myPacket.SourcePort = 9;
myPacket.DestinationPort = 9;
myPacket.UDPLength = UDPFields_Fields.UDP_HEADER_LEN;


myPacket.UDPData = bytes;
myPacket.ComputeIPChecksum();
myPacket.ComputeUDPChecksum();

try
{
//Send the packet out the network device
device.PcapSendPacket(myPacket);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

device.PcapClose();
}
}

关于c# - 如何通过没有 IP 地址的网卡发送 WOL 包(或任何东西)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/445411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com