My objective is to list all the ip cameras in a given network by C# code.
I am able to list all the ip addresses on my network using the GetIpNetTable(C# code) here.
Can sharppcap be of any help in this regard?
Note I am absolutely new to networking, so please bear with me.
Or is there any other way by which given an ip address, I could first verify if its an ip cam and then get its details. Note that ip-camera could be of any make.
IP Cameras uses onvif standard. According to that you can list all ip cameras on your network by sending a xml soap message to broadcasting ip address on port 3702 using UDP protocol.
So if you are on single level network then your broadcast address will be 192.168.1.255. Please google about broadcasting address as I am not a network person and cannot explain it better.
So here is what you need to do.
I am pasting code for your reference.
private static async Task<List<string>> GetSoapResponsesFromCamerasAsync()
{
var result = new List<string>();
using ( var client = new UdpClient() )
{
var ipEndpoint = new IPEndPoint( IPAddress.Parse( "192.168.1.255" ), 3702 );
client.EnableBroadcast = true;
try
{
var soapMessage = GetBytes( CreateSoapRequest() );
var timeout = DateTime.Now.AddSeconds( TimeoutInSeconds );
await client.SendAsync( soapMessage, soapMessage.Length, ipEndpoint );
while ( timeout > DateTime.Now )
{
if ( client.Available > 0 )
{
var receiveResult = await client.ReceiveAsync();
var text = GetText( receiveResult.Buffer );
result.Add( text );
}
else
{
await Task.Delay( 10 );
}
}
}
catch ( Exception exception )
{
Console.WriteLine( exception.Message );
}
}
return result;
}
private static string CreateSoapRequest()
{
Guid messageId = Guid.NewGuid();
const string soap = @"
<?xml version=""1.0"" encoding=""UTF-8""?>
<e:Envelope xmlns:e=""http://www.w3.org/2003/05/soap-envelope""
xmlns:w=""http://schemas.xmlsoap.org/ws/2004/08/addressing""
xmlns:d=""http://schemas.xmlsoap.org/ws/2005/04/discovery""
xmlns:dn=""http://www.onvif.org/ver10/device/wsdl"">
<e:Header>
<w:MessageID>uuid:{0}</w:MessageID>
<w:To e:mustUnderstand=""true"">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To>
<w:Action a:mustUnderstand=""true"">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action>
</e:Header>
<e:Body>
<d:Probe>
<d:Types>dn:Device</d:Types>
</d:Probe>
</e:Body>
</e:Envelope>
";
var result = string.Format( soap, messageId );
return result;
}
private static byte[] GetBytes( string text )
{
return Encoding.ASCII.GetBytes( text );
}
private static string GetText( byte[] bytes )
{
return Encoding.ASCII.GetString( bytes, 0, bytes.Length );
}
private string GetAddress( string soapMessage )
{
var xmlNamespaceManager = new XmlNamespaceManager( new NameTable() );
xmlNamespaceManager.AddNamespace( "g", "http://schemas.xmlsoap.org/ws/2005/04/discovery" );
var element = XElement.Parse( soapMessage ).XPathSelectElement( "//g:XAddrs[1]", xmlNamespaceManager );
return element?.Value ?? string.Empty;
}