Click here to Skip to main content
15,893,486 members
Articles / Web Development / HTML

My own Mailinator in 5 minutes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
19 Nov 2012CPOL6 min read 31.7K   405   13  
A simple Mailinator clone developed in five minutes with the NetFluid framework
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace LumiSoft.Net
{
    /// <summary>
    /// Holds IP bind info.
    /// </summary>
    public class IPBindInfo
    {
        private readonly string m_HostName = "";
        private readonly BindInfoProtocol m_Protocol = BindInfoProtocol.TCP;
        private readonly SslMode m_SslMode = SslMode.None;
        private readonly X509Certificate2 m_pCertificate;
        private readonly IPEndPoint m_pEndPoint;

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="protocol">Bind protocol.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        public IPBindInfo(string hostName, BindInfoProtocol protocol, IPAddress ip, int port)
        {
            if (ip == null)
            {
                throw new ArgumentNullException("ip");
            }

            m_HostName = hostName;
            m_Protocol = protocol;
            m_pEndPoint = new IPEndPoint(ip, port);
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <param name="sslMode">Specifies SSL mode.</param>
        /// <param name="sslCertificate">Certificate to use for SSL connections.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        public IPBindInfo(string hostName, IPAddress ip, int port, SslMode sslMode, X509Certificate2 sslCertificate)
            : this(hostName, BindInfoProtocol.TCP, ip, port, sslMode, sslCertificate)
        {
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="protocol">Bind protocol.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <param name="sslMode">Specifies SSL mode.</param>
        /// <param name="sslCertificate">Certificate to use for SSL connections.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public IPBindInfo(string hostName, BindInfoProtocol protocol, IPAddress ip, int port, SslMode sslMode,
                          X509Certificate2 sslCertificate)
        {
            if (ip == null)
            {
                throw new ArgumentNullException("ip");
            }

            m_HostName = hostName;
            m_Protocol = protocol;
            m_pEndPoint = new IPEndPoint(ip, port);
            m_SslMode = sslMode;
            m_pCertificate = sslCertificate;
            if ((sslMode == SslMode.SSL || sslMode == SslMode.TLS) && sslCertificate == null)
            {
                throw new ArgumentException("SSL requested, but argument 'sslCertificate' is not provided.");
            }
        }

        #region override method Equals

        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>Returns true if two objects are equal.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is IPBindInfo))
            {
                return false;
            }

            var bInfo = (IPBindInfo) obj;
            if ((bInfo.HostName != m_HostName) || (bInfo.Protocol != m_Protocol) || (!bInfo.EndPoint.Equals(m_pEndPoint)) || (bInfo.SslMode != m_SslMode) || !Equals(bInfo.Certificate, m_pCertificate))
            {
                return false;
            }
            return true;
        }

        #endregion

        #region override method GetHashCode

        /// <summary>
        /// Returns the hash code.
        /// </summary>
        /// <returns>Returns the hash code.</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #endregion

        #region Properties Implementation

        /// <summary>
        /// Gets host name.
        /// </summary>
        public string HostName
        {
            get { return m_HostName; }
        }

        /// <summary>
        /// Gets protocol.
        /// </summary>
        public BindInfoProtocol Protocol
        {
            get { return m_Protocol; }
        }

        /// <summary>
        /// Gets IP end point.
        /// </summary>
        public IPEndPoint EndPoint
        {
            get { return m_pEndPoint; }
        }

        /// <summary>
        /// Gets IP address.
        /// </summary>
        public IPAddress IP
        {
            get { return m_pEndPoint.Address; }
        }

        /// <summary>
        /// Gets port.
        /// </summary>
        public int Port
        {
            get { return m_pEndPoint.Port; }
        }

        /// <summary>
        /// Gets SSL mode.
        /// </summary>
        public SslMode SslMode
        {
            get { return m_SslMode; }
        }

        /// <summary>
        /// Gets SSL certificate.
        /// </summary>
        [Obsolete("Use property Certificate instead.")]
        public X509Certificate2 SSL_Certificate
        {
            get { return m_pCertificate; }
        }

        /// <summary>
        /// Gets SSL certificate.
        /// </summary>
        public X509Certificate2 Certificate
        {
            get { return m_pCertificate; }
        }


        /// <summary>
        /// Gets or sets user data. This is used internally don't use it !!!.
        /// </summary>
        public object Tag { get; set; }

        #endregion
    }
}

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 Code Project Open License (CPOL)


Written By
Chief Technology Officer Genny Mobility
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions