コード例 #1
0
ファイル: Sniffer.cs プロジェクト: sebazza/snmplogger
        private static void HandlePacket(object sender, CaptureEventArgs ev)
        {
            try
            {
                Packet packet = ParseEvent(ev);

                if (packet != null)
                {
                    IpPacket ip_packet = ExtractIpPacket(packet);
                    if (ip_packet != null)
                    {
                        IPAddress source_address = ip_packet.SourceAddress;
                        UdpPacket udp_packet     = ExtractUdpPacket(ip_packet);

                        if (udp_packet != null)
                        {
                            try
                            {
                                var message = new SnmpMessage(udp_packet.PayloadData);

                                lock ("log_message")
                                {
                                    if (message.IsValidSnmpMessage())
                                    {
                                        Log.LogMessage(message, source_address);
                                    }
                                    else
                                    {
                                        Log.LogInvalidMessage(message, source_address);
                                    }
                                }
                            }
                            catch (BerException ex)
                            {
                                Debug.WriteLine(ex.ToString());
                                Log.LogInvalidMessage("Error: " + ex.ToString() + Environment.NewLine + "  Message raw data: " + Utils.GetBytes(udp_packet.PayloadData),
                                                      source_address);
                            }
                        }

                        Console.WriteLine("{0} Message received from {1}", DateTime.Now.ToString("s"), source_address);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Log.LogMessage("Expection: " + ex.ToString());
            }
        }
コード例 #2
0
        public static void LogInvalidMessage(SnmpMessage message, IPAddress ip_address)
        {
            string filename = Log.ErrorLogFilename(ip_address);

            Log.LogInvalidMessage(message.MessageData + Environment.NewLine + message.Dump(), filename);
        }
コード例 #3
0
        public static void LogMessage(SnmpMessage message, IPAddress ip_address)
        {
            string filename = Log.LogFilename(ip_address);

            Log.LogMessage(message.LogMessage(), filename);
        }