Click here to Skip to main content
15,883,809 members
Articles / Desktop Programming / WPF

Calcium: A modular application toolset leveraging PRISM – Part 1

Rate me:
Please Sign up or sign in to vote.
4.93/5 (70 votes)
1 Jun 2009BSD17 min read 250.5K   208  
Calcium provides much of what one needs to rapidly build a multifaceted and sophisticated modular application. Includes a host of modules and services, and an infrastructure that is ready to use in your next application.
/*
<File>
	<Copyright>Copyright © 2007, Daniel Vaughan. All rights reserved.</Copyright>
	<License see="prj:///Documentation/License.txt"/>
	<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
	<CreationDate>2007/11/19 20:00</CreationDate>
	<LastSubmissionDate>$Date: $</LastSubmissionDate>
	<Version>$Revision: $</Version>
</File>
*/

using System;
using System.Configuration;
using System.Net;

namespace DanielVaughan.Logging.Filters
{
	/// <summary>
	/// Restricts logging based on an IP address range.
	/// </summary>
	class IPAddressRangeFilter : FilterBase
	{
		uint begin;
		uint end;

		public IPAddressRangeFilter()
		{
			Init += IPAddressRangeFilter_Init;
		}

		void IPAddressRangeFilter_Init(object sender, FilterInitEventArgs e)
		{
			ArgumentValidator.AssertNotNull(e, "e");
			ArgumentValidator.AssertNotNull(e.ConfigurationElement, "e.ConfigurationElement");

			/* Reset state. */
			begin = end = 0;

			var beginAttribute = e.ConfigurationElement.Attributes["Begin"];
			if (beginAttribute == null)
			{
				throw new ClientLoggingException("Begin ip address attribute does not exists."); /* TODO: Make localizable resource. */
			}

			try
			{
				begin = ToUInt(IPAddress.Parse(beginAttribute.Value));
			}
			catch (Exception ex)
			{
				throw new ClientLoggingException("Begin ip address is not a valid IP address.", ex); /* TODO: Make localizable resource. */
			}

			var endAttribute = e.ConfigurationElement.Attributes["End"];
			if (endAttribute == null)
			{
				throw new ClientLoggingException("End ip address attribute does not exists."); /* TODO: Make localizable resource. */
			}

			try
			{
				end = ToUInt(IPAddress.Parse(endAttribute.Value));
			}
			catch (Exception ex)
			{
				throw new ClientLoggingException("End ip address is not a valid IP address.", ex); /* TODO: Make localizable resource. */
			}
            
			if (Action == FilterAction.Default)
			{
				Action = FilterAction.Allow;
			}
			if (Action != FilterAction.Allow && Action != FilterAction.Deny)
			{
				throw new ConfigurationErrorsException(InvalidActionMessage);
			}
		}

		public override bool IsValid(LogEntryOrigin origin, IClientInfo info)
		{
			string addressValue = info.IPAddress;

			bool withinRange = string.IsNullOrEmpty(addressValue) || IsWithinRange(begin, addressValue, end);
			switch (Action)
			{
				case FilterAction.Allow:
					return withinRange;
				case FilterAction.Deny:
					return !withinRange;
			}
			
			throw new ConfigurationErrorsException(InvalidActionMessage);
		}

		/// <summary>
		/// Determines whether the specified addressValue IP address 
		/// falls within the range specified by beginAddress and endAddress.
		/// </summary>
		/// <param name="beginAddress">The begin address.</param>
		/// <param name="addressValue">The address value to test.</param>
		/// <param name="endAddress">The end address.</param>
		/// <returns>
		/// 	<c>true</c> if the addressValue is within the specified range; otherwise, <c>false</c>.
		/// </returns>
		static bool IsWithinRange(uint beginAddress, string addressValue, uint endAddress)
		{
			IPAddress address;
			if (!IPAddress.TryParse(addressValue, out address))
			{
				return false;
			}
			uint ip = ToUInt(address);
			return ip >= beginAddress && ip <= endAddress;
		}

		/// <summary>
		/// Converts and <see cref="IPAddress"/> to an unsigned int.
		/// </summary>
		/// <param name="ipAddress">The ip address to convert.</param>
		/// <returns>A <code>uint</code> representing 
		/// the specified ipAddress.</returns>
		static uint ToUInt(IPAddress ipAddress)
		{
			byte[] bytes = ipAddress.GetAddressBytes();
			
			uint result = (uint)bytes[0] << 24;
			result += (uint)bytes[1] << 16;
			result += (uint)bytes[2] << 8;
			result += bytes[3];

			return result;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions