public static SnmpDatagram GetSnmpDatagram(IpPacket ipPacket) { var udpDatagram = ipPacket.ToUdpDatagram(); if (udpDatagram == default(UdpDatagram)) { return(default(SnmpDatagram)); } try { SnmpDatagram snmpDatagram; var result = udpDatagram.TryParseSnmpDatagram(out snmpDatagram); if (result) { return(snmpDatagram); } } catch { // Ignored. } return(default(SnmpDatagram)); }
protected override bool TryParse(IpPacket packet, out IEnvelope envelope) { envelope = null; if (!packet.PacketHeader.DestinationIpAddress.Equals(this.ListenEndPoint.Address)) { return(false); } var upacket = packet.ToUdpDatagram(false); var isValid = upacket.UdpDatagramHeader.DestinationPort == this.ListenEndPoint.Port; if (isValid) { Syslog syslog; try { syslog = SyslogParser.Parse( upacket.UdpData, upacket.ReceivedTime, upacket.PacketHeader.SourceIpAddress.ToString()); } catch (Exception) { return(false); } envelope = new SyslogEnvelope { OccurrenceTime = packet.ReceivedTime, ReceivedTime = packet.ReceivedTime, Payload = upacket.UdpData.Array, PayloadInstance = syslog, }; } return(isValid); }
protected override bool TryParse(IpPacket packet, out IEnvelope envelope) { envelope = null; if (!packet.PacketHeader.DestinationIpAddress.Equals(this.ListenEndPoint.Address)) { return(false); } var upacket = packet.ToUdpDatagram(false); var isValid = upacket.UdpDatagramHeader.DestinationPort == this.ListenEndPoint.Port; if (isValid) { SnmpDatagram datagram; if (upacket.TryParseSnmpDatagram(out datagram)) { isValid = datagram.Header.Version == SnmpVersion.V1 || datagram.Header.Version == SnmpVersion.V2C; if (isValid) { envelope = new SnmpTrapEnvelope { OccurrenceTime = packet.ReceivedTime, ReceivedTime = packet.ReceivedTime, Payload = upacket.UdpData.Array, PayloadInstance = new SnmpDatagram( packet.ReceivedTime, packet.PacketHeader.SourceIpAddress.ToString(), datagram.Header, datagram.VarBinds), }; } } } return(isValid); }