Jazea / Onvif.Core

A lightweight ONVIF Discovery and Client library.
MIT License
71 stars 26 forks source link

Onvif.Core.Discovery.WSDiscovery.Discover throws exception when the scope is not present in <wsd:ProbeMatch>. #21

Closed jlovald closed 1 month ago

jlovald commented 2 months ago

The Synology NASes on our network responds to the discovery probe with something along the lines of:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pub="http://schemas.microsoft.com/windows/pub/2005/07" xmlns:un0="http://schemas.microsoft.com/windows/pnpx/2005/10" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsd="http://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:wsdp="http://schemas.xmlsoap.org/ws/2006/02/devprof" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
    <soap:Header>
        <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
        <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches</wsa:Action>
        <wsa:MessageID>urn:uuid:24b80b0d-cf21-4ef2-9a06-9ae5ee91fc52</wsa:MessageID>
        <wsa:RelatesTo>urn:uuid:a6c652df-7cfb-480a-940a-f9e3eb38240a</wsa:RelatesTo>
        <wsd:AppSequence InstanceId="1715932323" MessageNumber="1578465" SequenceId="urn:uuid:218020b4-3b3d-4a94-aeb4-2a6474a70dee"/>
    </soap:Header>
    <soap:Body>
        <wsd:ProbeMatches>
            <wsd:ProbeMatch>
                <wsa:EndpointReference>
                    <wsa:Address>urn:uuid:dfd56e79-4bbe-4eae-b026-b5f472cc138c</wsa:Address>
                </wsa:EndpointReference>
                <wsd:Types>wsdp:Device pub:Computer</wsd:Types>
                <wsd:XAddrs>http://NAS-18:5357/dfd56e79-4bbe-4eae-b026-b5f472cc138c/dfd56e79-4bbe-4eae-b026-b5f472cc138c</wsd:XAddrs>
                <wsd:MetadataVersion>2</wsd:MetadataVersion>
            </wsd:ProbeMatch>
        </wsd:ProbeMatches>
    </soap:Body>
</soap:Envelope>

Which means that the Scopes in ProbeMatch defaults to null. Leading to

private string ParseModelFromScopes(string scopes)
{
  return Regex.Match(scopes, "(?<=hardware/).*?(?= )").Value;
}
private string ParseNameFromScopes(string scopes)
{
  return Regex.Match(scopes, "(?<=name/).*?(?= )").Value;
}

throwing System.ArgumentNullException due to Regex.Match throwing that exception when the input and or pattern parameters are null .

The exception stops the discovery of all devices on the network. Ideally the discovery process should be a bit more robust, and maybe filter out non-ONVIF devices. The latter might be out of scope.