Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 9ec9366dc9bc8300bce317517cdda14102ee3901 @robertmircea committed Dec 2, 2010
@@ -0,0 +1,18 @@
+obj
+bin
+deploy
+deploy/*
+_ReSharper.*
+*.csproj.user
+*.resharper.user
+*.resharper
+*.suo
+*.cache
+Thumbs.db
+*.log
+log/
+log/*
+*.swp
+*.bak
+~$*
+logs/*
@@ -0,0 +1,190 @@
+namespace Nagios.NSCA.Client
+{
+ // Tamir Khason http://khason.net/
+ //
+ // Released under MS-PL : 6-Apr-09
+
+ using System;
+ using System.Collections;
+ using System.IO;
+ using System.Security.Cryptography;
+ using System.Text;
+
+ /// <summary>Implements a 32-bits cyclic redundancy check (CRC) hash algorithm.</summary>
+ /// <remarks>This class is not intended to be used for security purposes. For security applications use MD5, SHA1, SHA256, SHA384,
+ /// or SHA512 in the System.Security.Cryptography namespace.</remarks>
+ public class CRC32 : HashAlgorithm
+ {
+
+ #region CONSTRUCTORS
+ /// <summary>Creates a CRC32 object using the <see cref="DefaultPolynomial"/>.</summary>
+ public CRC32()
+ : this(DefaultPolynomial)
+ {
+ }
+
+ /// <summary>Creates a CRC32 object using the specified polynomial.</summary>
+ /// <remarks>The polynomical should be supplied in its bit-reflected form. <see cref="DefaultPolynomial"/>.</remarks>
+ [CLSCompliant(false)]
+ public CRC32(uint polynomial)
+ {
+ HashSizeValue = 32;
+ _crc32Table = (uint[])_crc32TablesCache[polynomial];
+ if (_crc32Table == null)
+ {
+ _crc32Table = CRC32._buildCRC32Table(polynomial);
+ _crc32TablesCache.Add(polynomial, _crc32Table);
+ }
+ Initialize();
+ }
+
+ // static constructor
+ static CRC32()
+ {
+ _crc32TablesCache = Hashtable.Synchronized(new Hashtable());
+ _defaultCRC = new CRC32();
+ }
+ #endregion
+
+ #region PROPERTIES
+ /// <summary>Gets the default polynomial (used in WinZip, Ethernet, etc.)</summary>
+ /// <remarks>The default polynomial is a bit-reflected version of the standard polynomial 0x04C11DB7 used by WinZip, Ethernet, etc.</remarks>
+ [CLSCompliant(false)]
+ public static readonly uint DefaultPolynomial = 0xEDB88320; // Bitwise reflection of 0x04C11DB7;
+ #endregion
+
+ #region METHODS
+ /// <summary>Initializes an implementation of HashAlgorithm.</summary>
+ public override void Initialize()
+ {
+ _crc = _allOnes;
+ }
+
+ /// <summary>Routes data written to the object into the hash algorithm for computing the hash.</summary>
+ protected override void HashCore(byte[] buffer, int offset, int count)
+ {
+ for (int i = offset; i < count; i++)
+ {
+ ulong ptr = (_crc & 0xFF) ^ buffer[i];
+ _crc >>= 8;
+ _crc ^= _crc32Table[ptr];
+ }
+ }
+
+ /// <summary>Finalizes the hash computation after the last data is processed by the cryptographic stream object.</summary>
+ protected override byte[] HashFinal()
+ {
+ byte[] finalHash = new byte[4];
+ ulong finalCRC = _crc ^ _allOnes;
+
+ finalHash[0] = (byte)((finalCRC >> 0) & 0xFF);
+ finalHash[1] = (byte)((finalCRC >> 8) & 0xFF);
+ finalHash[2] = (byte)((finalCRC >> 16) & 0xFF);
+ finalHash[3] = (byte)((finalCRC >> 24) & 0xFF);
+
+ return finalHash;
+ }
+
+ /// <summary>Computes the CRC32 value for the given ASCII string using the <see cref="DefaultPolynomial"/>.</summary>
+ public static int Compute(string asciiString)
+ {
+ _defaultCRC.Initialize();
+ return ToInt32(_defaultCRC.ComputeHash(asciiString));
+ }
+
+ /// <summary>Computes the CRC32 value for the given input stream using the <see cref="DefaultPolynomial"/>.</summary>
+ public static int Compute(Stream inputStream)
+ {
+ _defaultCRC.Initialize();
+ return ToInt32(_defaultCRC.ComputeHash(inputStream));
+ }
+
+ /// <summary>Computes the CRC32 value for the input data using the <see cref="DefaultPolynomial"/>.</summary>
+ public static int Compute(byte[] buffer)
+ {
+ _defaultCRC.Initialize();
+ return ToInt32(_defaultCRC.ComputeHash(buffer));
+ }
+
+ /// <summary>Computes the hash value for the input data using the <see cref="DefaultPolynomial"/>.</summary>
+ public static int Compute(byte[] buffer, int offset, int count)
+ {
+ _defaultCRC.Initialize();
+ return ToInt32(_defaultCRC.ComputeHash(buffer, offset, count));
+ }
+
+ /// <summary>Computes the hash value for the given ASCII string.</summary>
+ /// <remarks>The computation preserves the internal state between the calls, so it can be used for computation of a stream data.</remarks>
+ public byte[] ComputeHash(string asciiString)
+ {
+ byte[] rawBytes = ASCIIEncoding.ASCII.GetBytes(asciiString);
+ return ComputeHash(rawBytes);
+ }
+
+ /// <summary>Computes the hash value for the given input stream.</summary>
+ /// <remarks>The computation preserves the internal state between the calls, so it can be used for computation of a stream data.</remarks>
+ new public byte[] ComputeHash(Stream inputStream)
+ {
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ while ((bytesRead = inputStream.Read(buffer, 0, 4096)) > 0)
+ {
+ HashCore(buffer, 0, bytesRead);
+ }
+ return HashFinal();
+ }
+
+ /// <summary>Computes the hash value for the input data.</summary>
+ /// <remarks>The computation preserves the internal state between the calls, so it can be used for computation of a stream data.</remarks>
+ new public byte[] ComputeHash(byte[] buffer)
+ {
+ return ComputeHash(buffer, 0, buffer.Length);
+ }
+
+ /// <summary>Computes the hash value for the input data.</summary>
+ /// <remarks>The computation preserves the internal state between the calls, so it can be used for computation of a stream data.</remarks>
+ new public byte[] ComputeHash(byte[] buffer, int offset, int count)
+ {
+ HashCore(buffer, offset, count);
+ return HashFinal();
+ }
+ #endregion
+
+ #region PRIVATE SECTION
+ private static uint _allOnes = 0xffffffff;
+ private static CRC32 _defaultCRC;
+ private static Hashtable _crc32TablesCache;
+ private uint[] _crc32Table;
+ private uint _crc;
+
+ // Builds a crc32 table given a polynomial
+ private static uint[] _buildCRC32Table(uint polynomial)
+ {
+ uint crc;
+ uint[] table = new uint[256];
+
+ // 256 values representing ASCII character codes.
+ for (int i = 0; i < 256; i++)
+ {
+ crc = (uint)i;
+ for (int j = 8; j > 0; j--)
+ {
+ if ((crc & 1) == 1)
+ crc = (crc >> 1) ^ polynomial;
+ else
+ crc >>= 1;
+ }
+ table[i] = crc;
+ }
+
+ return table;
+ }
+
+ private static int ToInt32(byte[] buffer)
+ {
+ return BitConverter.ToInt32(buffer, 0);
+ }
+ #endregion
+
+ }
+}
@@ -0,0 +1,22 @@
+using System;
+
+namespace Nagios.NSCA.Client
+{
+ public class EncryptorFactory
+ {
+ public static NSCAEncryptionBase CreateEncryptor(NSCAEncryptionType encryptionType)
+ {
+ switch (encryptionType)
+ {
+ case NSCAEncryptionType.Xor:
+ return new XorEncryption();
+ case NSCAEncryptionType.TripleDES:
+ return new TripleDESEncryption();
+ case NSCAEncryptionType.None:
+ return new NoEncryption();
+ default:
+ throw new ArgumentOutOfRangeException("encryptionType");
+ }
+ }
+ }
+}
@@ -0,0 +1,12 @@
+using System;
+
+namespace Nagios.NSCA.Client
+{
+ public interface INSCAClientSender
+ {
+ /// <summary>
+ /// Send a passive check described by message payload to a Nagios NSCA daemon
+ /// </summary>
+ bool SendPassiveCheck(Level level, string hostName, string serviceName, string message);
+ }
+}
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Nagios.NSCA.Client
+{
+ public enum Level
+ {
+ OK,
+ Warning,
+ Critical,
+ Unknown
+ }
+}
@@ -0,0 +1,57 @@
+using System;
+using System.Net.Sockets;
+
+namespace Nagios.NSCA.Client
+{
+ public class NSCAClientSender : INSCAClientSender
+ {
+ private readonly NSCASettings settings;
+ private readonly PassiveCheckProtocolWriter protocolWriter;
+
+ public NSCAClientSender()
+ : this((NSCASettings) System.Configuration.ConfigurationManager.GetSection("nscaSettings"))
+ {
+ }
+
+ public NSCAClientSender(NSCASettings settings)
+ {
+ if (settings == null) throw new ArgumentNullException("settings");
+ this.settings = settings;
+
+ protocolWriter = new PassiveCheckProtocolWriter(settings);
+
+ }
+
+ public bool SendPassiveCheck(Level level, string hostName, string serviceName, string message)
+ {
+ if (string.IsNullOrEmpty(hostName)) throw new ArgumentNullException("hostName");
+ if (string.IsNullOrEmpty(serviceName)) throw new ArgumentNullException("serviceName");
+ if (message == null) throw new ArgumentNullException("message");
+
+ try
+ {
+ using (TcpClient tcpClient = new TcpClient())
+ {
+ tcpClient.Connect(settings.NSCAAddress, settings.Port);
+ using (var stream = tcpClient.GetStream())
+ {
+ byte[] initVector = new byte[128];
+ stream.Read(initVector, 0, 128);
+
+ byte[] timestamp = new byte[4];
+ stream.Read(timestamp, 0, 4);
+
+ var bytesToSend = protocolWriter.EncodeToProtocol(level, timestamp, hostName, serviceName, message, initVector);
+ stream.Write(bytesToSend, 0, bytesToSend.Length);
+ }
+ }
+ return true;
+ }
+ catch
+ {
+ //intentionally swallow exceptions, maybe some logging is required.
+ return false;
+ }
+ }
+ }
+}
@@ -0,0 +1,7 @@
+namespace Nagios.NSCA.Client
+{
+ public abstract class NSCAEncryptionBase
+ {
+ public abstract byte[] Encrypt(byte[] s, byte[] initVector, string password);
+ }
+}
@@ -0,0 +1,9 @@
+namespace Nagios.NSCA.Client
+{
+ public enum NSCAEncryptionType
+ {
+ None,
+ Xor,
+ TripleDES
+ }
+}
@@ -0,0 +1,54 @@
+using System;
+using System.Net;
+using System.Configuration;
+
+namespace Nagios.NSCA.Client
+{
+ /// <summary>
+ /// Settings necessary for sending passive checks to Nagios NSCA daemon
+ /// </summary>
+ public sealed class NSCASettings : ConfigurationSection
+ {
+
+ [ConfigurationProperty("nscaAddress", DefaultValue = "127.0.0.1", IsRequired = false)]
+ public string NSCAAddress
+ {
+ get { return (string) this["nscaAddress"]; }
+ set { this["nscaAddress"] = value; }
+ }
+
+
+ /// <summary>
+ /// The port on which NSCA is listening. Defaults to 5667
+ /// </summary>
+ [ConfigurationProperty("port", DefaultValue = 5667, IsRequired = false)]
+ [IntegerValidator(ExcludeRange = false, MaxValue = 65535, MinValue = 1)]
+ public int Port
+ {
+ get { return (int) this["port"]; }
+ set
+ {
+ if (value < 1 || value > 65535)
+ throw new ArgumentOutOfRangeException("port", "Port value must be between 1 and 65535");
+ this["port"] = value;
+ }
+ }
+
+ /// <summary>
+ /// The password configured in the ncsa.cfg file used by NSCA
+ /// </summary>
+ [ConfigurationProperty("password", DefaultValue = "", IsRequired = false)]
+ public string Password
+ {
+ get { return (string) this["password"]; }
+ set { this["password"] = value; }
+ }
+
+ [ConfigurationProperty("encryptionType", DefaultValue = NSCAEncryptionType.Xor, IsRequired = false)]
+ public NSCAEncryptionType EncryptionType
+ {
+ get { return (NSCAEncryptionType) this["encryptionType"]; }
+ set { this["encryptionType"] = value; }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 9ec9366

Please sign in to comment.