All pastes #717057 Raw Edit

Unnamed

public text v1 · immutable
#717057 ·published 2007-09-27 11:09 UTC
rendered paste body
я╗┐using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Net;

[Serializable]
[SqlUserDefinedType(Format.Native, IsByteOrdered=true)]
public struct IPAddress : INullable
{
    internal long value;

    [SqlMethod(OnNullCall = false)]
    public static IPAddress Parse(SqlString s)
    {
        System.Net.IPAddress ipa = System.Net.IPAddress.Parse(s.Value);

        IPAddress ipaNew = new IPAddress();
        ipaNew.value = ipa.Address;
        return ipaNew;
    }

    public bool IsNull
    {
        get { return value < 0; }
    }

    public static IPAddress Null
    {
        get
        {
            return new IPAddress(-1);
        }
    }

    public override string ToString()
    {
        if(value==-1) return "NULL";
        else return new System.Net.IPAddress(value).ToString();
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #region address bytes
    public SqlByte b1
    {
        get { return new SqlByte((byte)(value & 0xFF)); }
    }

    public SqlByte b2
    {
        get { return new SqlByte((byte)((value & 0xFF00) >> 8)); }
    }

    public SqlByte b3
    {
        get { return new SqlByte((byte)((value & 0xFF0000) >> 16)); }
    }

    public SqlByte b4
    {
        get { return new SqlByte((byte)((value & 0xFF000000) >> 24)); }
    }
    #endregion

    private IPAddress(long val)
    {
        this.value = val;
    }

    public SqlString SubnetType()
    {
        byte firstOctet = (byte)(value & 0xFF);
        if (firstOctet > 0 && firstOctet < 127) return new SqlString("A");
        else if (firstOctet >= 128 & firstOctet < 192) return new SqlString("B");
        else if (firstOctet >= 192 && firstOctet < 224) return new SqlString("C");
        else if (firstOctet >= 224 && firstOctet < 240) return new SqlString("D");
        else if (firstOctet >= 240) return new SqlString("E");
        else return SqlString.Null;
    }

    public IPAddress ApplyMask(IPAddress mask)
    {
        return new IPAddress(value & mask.value);
    }
}