FreeDSx / SNMP

A Pure PHP SNMP Library.
MIT License
58 stars 15 forks source link

SnmpClient not working when sending v3 traps #22

Open DenizOezcan opened 3 weeks ago

DenizOezcan commented 3 weeks ago

When using the SnmpClient to send v3 traps I get following exception: FreeDSx\Snmp\Exception\ProtocolException : Expected a SNMPv3 response, but got: v0

The problem is, that \FreeDSx\Snmp\Protocol\ClientProtocolHandler::sendRequestGetResponse returns null for a SNMPv3 trap, as no response is expected. This causes the exception mentioned above being thrown due to the following logic in \FreeDSx\Snmp\Protocol\ClientProtocolHandler::sendV3Message from line 206 and following :

            $response = $this->sendRequestGetResponse($message);

            if ($response instanceof MessageResponseV3) {
                $response = $securityModule->handleIncomingMessage($response, $options);
            }
            if (!$response instanceof MessageResponseV3) {
                throw new ProtocolException(sprintf(
                    'Expected a SNMPv3 response, but got: v%d',
                    $response instanceof MessageResponseInterface ? $response->getVersion() : 0
                ));
            }

This could be fixed with a null check, e.g.:

            $response = $this->sendRequestGetResponse($message);

            if(null === $response) {
                return null;
            }

            if ($response instanceof MessageResponseV3) {
                $response = $securityModule->handleIncomingMessage($response, $options);
            }
            if (!$response instanceof MessageResponseV3) {
                throw new ProtocolException(sprintf(
                    'Expected a SNMPv3 response, but got: v%d',
                    $response instanceof MessageResponseInterface ? $response->getVersion() : 0
                ));
            }