mac版本 sadptool_海康 设备 发现(SADPTool原理)
SADPTool原理:
向239.255.255.250:37020發(fā)送基于ONVIF協(xié)議的 udp 組播,設(shè)備會監(jiān)聽?239.255.255.250:37020,收到指令,會向 發(fā)送組播的機器,返回設(shè)備信息,也會向239.255.255.250:37020 發(fā)送 設(shè)備信息的 組播。
發(fā)送 報文(Uuid是Guid,每次都生成一個新的,用同一個,我沒有試過,可不可以)
12D17626-0DE1-48DB-97E3-40B106467932inquiry
返回報文
12D17626-0DE1-48DB-97E3-40B106467932
inquiry
139991
CS-C6TC-32WFR
CS-C6TC-32WFR0120170327CCCH738231995
8000
80
54-c4-15-9a-38-72
172.20.20.100
255.255.255.0
172.20.20.254
false
0
1
V5.2.3build 180804
V1.0 build 180731
2018-10-18 21:30:10
N/A
true
false
通過 wireshark 抓包發(fā)現(xiàn),發(fā)送?ONVIF協(xié)議后,還會 廣播 一個 包
但是不知道,是做何作用,我用SharpPcap 模擬發(fā)送了一個EarthNet II 的包,這個包 是固定不變的。
EarthNet II 的包,C# 原生 Socket 貌似不支持,raw_Socket 寫起來太繁瑣了,SharpPcap 另一個用處是可以基于網(wǎng)卡抓包。
搜索設(shè)備代碼:
public void SearchDecives()
{
//EtherNetII 協(xié)議
byte[] SearchIpCameraByte = new byte[]{
0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,//目的地Mac
0x00 ,0x15 ,0x5d ,0xe6 ,0xaa ,0x0b ,//源地址Mac
0x80 ,0x33 ,//協(xié)議類型(0x8033螢石自定義類型)
0x21 ,0x02,//發(fā)送數(shù)據(jù)
0x01 ,0x42 ,0x00 ,0x00 ,0x24 ,0x03 ,0x06 ,0x04 ,0x03 ,0x00 ,0x13 ,0x82 ,0x00 ,0x15 ,0x5d ,0xe6,
0xaa ,0x0b ,0xa9 ,0xfe ,0x50 ,0x50 ,0xff ,0xff ,0xff ,0xff ,0xff ,0xff ,0x00 ,0x00 ,0x00 ,0x00,
0x00 ,0x00 ,0x00 ,0x00 ,0xfe ,0x80 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x64 ,0x7e ,0x5d ,0xb0,
0x9e ,0x93 ,0xcd ,0x07 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
};
var devices = CaptureDeviceList.Instance;
Console.Write("---------------------------");
// differentiate based upon types
foreach (ICaptureDevice dev in devices)
{
if (dev is AirPcapDevice)
{
// process as an AirPcapDevice
}
else if (dev is WinPcapDevice)
{
dev.Open();
//dev.Capture();
//dev.StartCapture();
dev.SendPacket(SearchIpCameraByte);
Thread.Sleep(1000);
//dev.StopCapture();
//dev.Close();
// process as an WinPcapDevice
}
else if (dev is LibPcapLiveDevice)
{
// process as an LibPcapLiveDevice
}
}
return;
}
/// /// 查找設(shè)備
///
///
///
private void button1_Click(object sender, EventArgs e)
{
SendInQuiry();
SearchDecives();
SendMSearch();
}
/// /// 搜索upnp 設(shè)備
/// 向ssdp服務(wù)地址239.255.255.250:1900組播發(fā)送數(shù)據(jù)
///
public void SendMSearch()
{
//向ssdp服務(wù)地址239.255.255.250:1900組播發(fā)送數(shù)據(jù)
IPEndPoint ipend = new IPEndPoint(IPAddress.Any, 0);
UdpClient client = new UdpClient(ipend);
client.EnableBroadcast = false;
client.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"));
IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
byte[] buf = Encoding.Default.GetBytes("M-SEARCH * HTTP/1.1\r\nHOST:239.255.255.250:1900\r\nMAN:\"ssdp:discover\"\r\nST:upnp:rootdevice\r\nMX:3\r\n\r\n");
client.Send(buf, buf.Length, multicast);
//異步接收數(shù)據(jù)
//client.BeginReceive(new AsyncCallback(ReceiveBack), client);
//設(shè)置網(wǎng)絡(luò)超時時間,一段時間未接收到數(shù)據(jù)時自動退出
//client.Client.ReceiveTimeout = 150;
//單播接收數(shù)據(jù)
//while (true)
//{
// byte[] value = client.Receive(ref ipend);
// string msg = Encoding.Default.GetString(value);
// //OLog.Info(msg);
// Console.WriteLine(msg + "-" + ipend.Address.ToString());
//}
}
/// /// 發(fā)送設(shè)備探究
/// 向ssdp服務(wù)地址239.255.255.250:37020組播發(fā)送數(shù)據(jù)
///
public void SendInQuiry()
{
var uuid = Guid.NewGuid().ToString().ToUpper();
//A207AD1A-962B-480B-A32C-4B0D98CBA0D4inquiry
var InQuiry = new byte[]{
0x3c ,0x3f ,0x78 ,0x6d ,0x6c ,0x20,
0x76 ,0x65 ,0x72 ,0x73 ,0x69 ,0x6f ,0x6e ,0x3d ,0x22 ,0x31 ,0x2e ,0x30 ,0x22 ,0x20 ,0x65 ,0x6e,
0x63 ,0x6f ,0x64 ,0x69 ,0x6e ,0x67 ,0x3d ,0x22 ,0x75 ,0x74 ,0x66 ,0x2d ,0x38 ,0x22 ,0x3f ,0x3e,
0x3c ,0x50 ,0x72 ,0x6f ,0x62 ,0x65 ,0x3e ,0x3c ,0x55 ,0x75 ,0x69 ,0x64 ,0x3e
}.
Concat(System.Text.Encoding.UTF8.GetBytes(uuid)).//uuid
Concat(new byte[]{
0x3c ,0x2f ,0x55 ,0x75 ,0x69 ,0x64 ,0x3e ,0x3c ,0x54 ,0x79 ,0x70 ,0x65 ,0x73 ,0x3e ,0x69,
0x6e ,0x71 ,0x75 ,0x69 ,0x72 ,0x79 ,0x3c ,0x2f ,0x54 ,0x79 ,0x70 ,0x65 ,0x73 ,0x3e ,0x3c ,0x2f,
0x50 ,0x72 ,0x6f ,0x62 ,0x65 ,0x3e
}).ToArray();
IPEndPoint ipend = new IPEndPoint(IPAddress.Any, 0);
UdpClient client = new UdpClient(ipend);
client.EnableBroadcast = false;
client.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"));
IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 37020);
client.Send(InQuiry, InQuiry.Length, multicast);
}
監(jiān)聽組播代碼:
private void button2_Click(object sender, EventArgs e)
{
//接收組播數(shù)據(jù)
Thread t = new Thread(new ThreadStart(RecvThread));
t.IsBackground = true;
t.Start();
//Socket recv_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//recv_socket.Bind(new IPEndPoint(IPAddress.Any, 1900));
//recv_socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveMessage, recv_socket);
}
/// /// 獲取 ssdp 組播
/// ssdp地址固定:239.255.255.250:1900
/// 海康設(shè)備組播到 37020 端口
///
void RecvThread()
{
//綁定組播端口
UdpClient client = new UdpClient(37020);
client.EnableBroadcast = false;
client.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"));
IPEndPoint mult = null;
while (true)
{
byte[] buf = client.Receive(ref mult);
string msg = Encoding.Default.GetString(buf);
Console.WriteLine(msg);
MessageBox.Show(msg);
//Console.WriteLine(mult.Address.ToString());
}
}
還可以用 ARP 映射原理 獲取 局域網(wǎng) 所有機器的MAC地址,根據(jù)MAC 前6位判斷是 哪家生產(chǎn)的設(shè)備,同一企業(yè)生產(chǎn)的 網(wǎng)絡(luò)設(shè)備mac地址 前6位一般是固定的。
海康設(shè)備Mac地址 都是54-c4-15 開頭的
我這里用的是 cmd arp -a,取arp映射的,也可以 sharppcap抓包,或者Windows api 發(fā)送arp 協(xié)議 包,取返回值
private static string GetARPResult()
{
Process p = null;
string output = string.Empty;
try
{
p = Process.Start(new ProcessStartInfo("arp", "-a")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
});
output = p.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("IPInfo: Error Retrieving 'arp -a' Results", ex);
}
finally
{
if (p != null)
{
p.Close();
}
}
return output;
}
總結(jié)
以上是生活随笔為你收集整理的mac版本 sadptool_海康 设备 发现(SADPTool原理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 相机标定(一) —— 深入理解齐次坐标及
- 下一篇: 什么是“中台”