Click here to Skip to main content
Email Password   helpLost your password?

After downloading the source code, please take a look at the History section for updates to the source code.

Screenshot - program.jpg

Screenshot - classdiagram.jpg

Introduction

This is a class for looking up IP addresses against the DNS black list server.

Background

I needed this class for an application that scans SMTP logs.

Using the Code

VerifyIP IP = new VerifyIP("91.90.8.167",new string[]
    { "sbl-xbl.spamhaus.org", "bl.spamcop.net"});
if (IP.IPAddr.Valid)
      if (IP.BlackList.IsListed)
         Debug.Write(IP.BlackList.VerifiedOnServer);

List of Blacklist Servers

Server Address Best practice index
access.redhawk.org 22
accredit.habeas.com 14
bl.deadbeef.com 20
bl.spamcannibal.org 36
bl.spamcop.net 2
blackholes.uceb.org 37
blacklist.spambag.org 30
cbl.abuseat.org 3
cbl.ni.bg 38
cblless.anti-spam.org.cn 35
combined.njabl.org 11
combined.rbl.msrbl.net 31
dnsbl.ahbl.org 18
dnsbl.burnt-tech.com 19
dnsbl.delink.net 21
dnsbl.njabl.org 10
dnsbl.sorbs.net 4
dnsbl.tqmcube.com 27
dnsbl-1.uceprotect.net 32
dnsbl-2.uceprotect.net 33
dnsbl-3.uceprotect.net 34
dul.dnsbl.sorbs.net 6
http.dnsbl.sorbs.net 7
ko.tqmcube.com 25
list.dsbl.org 15
misc.dnsbl.sorbs.net 9
multihop.dsbl.org 16
no-more-funn.moensted.dk 23
prc.tqmcube.com 26
psbl.surriel.com 29
rbl.spamlab.com 13
sbl-xbl.spamhaus.org 1
smtp.dnsbl.sorbs.net 8
socks.dnsbl.sorbs.net 5
spam.tqmcube.com 24
ubl.unsubscore.com 28
unconfirmed.dsbl.org 17
zen.spamhaus.org 12

The VerifyIp Class

public class VerifyIP
{
    #region Nested classes

    public class exIPAddress
    {
        #region Private fields

        private string[] _adresse;
        private bool _valid;

        #endregion

        #region Class Properties

        public bool Valid
        {
            get { return _valid; }
        }

        public string AsString
        {
            get
            {
                if (_valid)
                {
                    string tmpstr = "";
                    for (int ai = 0; ai < _adresse.Length; ai++)
                    {
                        tmpstr += _adresse[ai];
                        if (ai < _adresse.Length - 1)
                            tmpstr += ".";
                    }
                    return tmpstr;
                }
                else
                    return "";
            }
            set
            {
                _adresse = value.Split(new Char[] { '.' });
                if (_adresse.Length == 4)
                {
                    try
                    {
                        _valid = true;
                        byte tmpx = 0;
                        foreach (string addsec in _adresse)
                        if (!byte.TryParse(addsec, out tmpx))
                        {
                            _valid = false;
                            break;
                        }
                    }
                    catch { _valid = false; }
                }
                else
                    _valid = false;
            }
        }

        public string AsRevString
        {
            get
            {
                if (_valid)
                {
                    string tmpstr = "";
                    for (int ai = _adresse.Length - 1; ai > -1; ai--)
                    {
                        tmpstr += _adresse[ai];
                        if (ai > 0)
                            tmpstr += ".";
                    }
                    return tmpstr;
                }
                else
                    return "";
            }
        }

        public string[] AsStringArray
        {
            get { return _adresse; }
            set
            {
                if (value.Length == 4)
                {
                    try
                    {
                        _valid = true;
                        byte tmpx = 0;
                        foreach (string addsec in value)
                            if (!byte.TryParse(addsec, out tmpx))
                            {
                                _valid = false;
                                break;
                            }
                    }
                    catch { _valid = false; }
                }
                else
                    _valid = false;
            }
        }

        public string[] AsRevStringArray
        {
            get
            {
                string[] tmpstrarr = new string[_adresse.Length];
                for (int ai = _adresse.Length - 1; ai > 0; ai--)
                {
                    tmpstrarr[(_adresse.Length - 1) - ai] = _adresse[ai];
                }
                return tmpstrarr;
            }
        }

        public byte[] AsByteArray
        {
            get
            {
                if (_valid)
                    return StringToByte(_adresse);
                else
                    return new byte[0];
            }
            set
            {
                if (value.Length == 4)
                {
                    _adresse = ByteToString(value);
                    _valid = true;
                }
                else
                    _valid = false;
            }
        }

        public byte[] AsRevByteArray
        {
            get
            {
                byte[] tmpcon = StringToByte(_adresse);
                byte[] tmpbytearr = new byte[tmpcon.Length];
                for (int ai = tmpcon.Length - 1; ai > 0; ai--)
                {
                    tmpbytearr[(tmpcon.Length - 1) - ai] = tmpcon[ai];
                }
                return tmpbytearr;
            }
        }

        public long AsLong
        {
            get
            {
                if (_valid)
                    return StringToLong(_adresse, true);
                else
                    return 0;
            }
            set
            {
                try
                {
                    _adresse = LongToString(value, false);
                    _valid = true;
                }
                catch { _valid = false; }
            }
        }

    public long AsRevLong
    {
        get { return StringToLong(_adresse, false); }
    }

#endregion

#region Contructors

    public exIPAddress() { }
    public exIPAddress(string address)
    {
        this.AsString = address;
    }
    public exIPAddress(string[] address)
    {
        this.AsStringArray = address;
    }
    public exIPAddress(byte[] address)
    {
        this.AsByteArray = address;
    }
    public exIPAddress(long address)
    {
        this.AsLong = address;
    }

#endregion

#region Private methods

    private byte[] StringToByte(string[] strArray)
    {
        try
        {
            byte[] tmp = new byte[strArray.Length];
            for (int ia = 0; ia < strArray.Length - 1; ia++)
                tmp[ia] = byte.Parse(strArray[ia]);
            return tmp;
        }
        catch
        {
            return new byte[0];
        }
    }

    private string[] ByteToString(byte[] byteArray)
    {
        try
        {
            string[] tmp = new string;
            for (int ia = 0; ia < byteArray.Length - 1; ia++)
                tmp[ia] = byteArray[ia].ToString();
            return tmp;
        }
        catch
        {
            return new string[0];
        }
    }
    
    private long StringToLong(string[] straddr, bool Revese)
    {
        long num = 0;
        if (straddr.Length == 4)
        {
            try
            {
                if (Revese)
                    num = (int.Parse(straddr[0])) +
                        (int.Parse(straddr[1]) * 256) +
                        (int.Parse(straddr[2]) * 65536) +
                        (int.Parse(straddr[3]) * 16777216);
                else
                    num = (int.Parse(straddr[3])) +
                        (int.Parse(straddr[2]) * 256) +
                        (int.Parse(straddr[1]) * 65536) +
                        (int.Parse(straddr[0]) * 16777216);
            }
            catch { num = 0; }
        }
        else
            num = 0;
        return num;
    }

    private string[] LongToString(long lngval, bool Revese)
    {
        string[] tmpstrarr = new string[4];
        if (lngval > 0)
        {
            try
            {
                int a = (int)(lngval / 16777216) % 256;
                int b = (int)(lngval / 65536) % 256;
                int c = (int)(lngval / 256) % 256;
                int d = (int)(lngval) % 256;
                if (Revese)
                {
                    tmpstrarr[0] = a.ToString();
                    tmpstrarr[1] = b.ToString();
                    tmpstrarr[2] = c.ToString();
                    tmpstrarr[3] = d.ToString();
                }
                else
                {
                    tmpstrarr[3] = a.ToString();
                    tmpstrarr[2] = b.ToString();
                    tmpstrarr[1] = c.ToString();
                    tmpstrarr[0] = d.ToString();
                }
            }
            catch { }
            return tmpstrarr;
        }
        else
            return tmpstrarr;
    }

    #endregion
}

public class BlackListed
{
    #region private fields

    private bool _IsListed;
    private string _verifiedonserver;
    
    #endregion
    
    #region Class properties
    
    public string VerifiedOnServer
    {
        get { return _verifiedonserver; }
    }

    public bool IsListed
    {
        get { return _IsListed; }
    }

    #endregion
    
    #region Contructor

    public BlackListed(bool listed, string server)
    {
        this._IsListed = listed;
        this._verifiedonserver = server;
    }

    #endregion
}

#endregion

#region Private fields

    private exIPAddress _ip;
    private BlackListed _blacklisted = new BlackListed(false, "");
    
#endregion

#region Class Properties

    public exIPAddress IPAddr
    {
        get { return _ip; }
        set { _ip = value; }
    }

    public BlackListed BlackList
    {
        get { return _blacklisted; }
    }

#endregion

#region Constructors

    public VerifyIP(byte[] address, string[] blacklistservers)
    {
        _ip = new exIPAddress(address);
        VerifyOnServers(blacklistservers);
    }
    public VerifyIP(long address, string[] blacklistservers)
    {
        _ip = new exIPAddress(address);
        VerifyOnServers(blacklistservers);
    }
    public VerifyIP(string address, string[] blacklistservers)
    {
        _ip = new exIPAddress(address);
        VerifyOnServers(blacklistservers);
    }
    public VerifyIP(exIPAddress address, string[] blacklistservers)
    {
        _ip = address;
        VerifyOnServers(blacklistservers);
    }

    #endregion

    #region Private methods

    private void VerifyOnServers(string[] _blacklistservers)
    {
        _blacklisted = null;
        if (_blacklistservers != null && _blacklistservers.Length > 0)
        {
            foreach (string BLSrv in _blacklistservers)
            {
                if (VerifyOnServer(BLSrv))
                {
                    _blacklisted = new BlackListed(true, BLSrv);
                    break;
                }
            }
            if (_blacklisted == null)
                _blacklisted = new BlackListed(false, "");
        }
    }

    private bool VerifyOnServer(string BLServer)
    {
        if (_ip.Valid)  //If IP address is valid continue..
        {
            try
            {
                IPHostEntry ipEntry = Dns.GetHostEntry(_ip.AsRevString + "." +
            BLServer);  //Look up the IP address on the BLServer
                ipEntry = null; //Clear the object
                return true; //IP address was found on the BLServer,
                             //it's then listed in the black list
            }
            catch (System.Net.Sockets.SocketException dnserr)
            {
                if (dnserr.ErrorCode == 11001) // IP address not listed
                    return false;
                else // Another error happened, put other error handling here
                    return false;
            }
        }
        else
            return false;   //IP address is not valid
    }

    #endregion
}

Correction of exIPAddress Source Code

Replace the exIPAddress class code in the VerifyIp class with the following code.
The new code replaces some "value out of index" issues in some of the loops.

public class exIPAddress
{
    #region Private fields

    private string[] _adresse;
    private bool _valid;
    
    #endregion
    
    #region Class Properties
    
    public bool Valid
    {
        get { return _valid; }
    }

    public string AsString
    {
        get
        {
            if (_valid)
            {
                string tmpstr = "";
                for (int ai = 0; ai < _adresse.Length; ai++)
                {
                    tmpstr += _adresse[ai];
                    if (ai < _adresse.Length - 1)
                        tmpstr += ".";
                }
                return tmpstr;
            }
            else
                return "";
        }
        set
        {
            _adresse = value.Split(new Char[] { '.' });
            if (_adresse.Length == 4)
            {
                try
                {
                    _valid = true;
                    byte tmpx = 0;
                    foreach (string addsec in _adresse)
                        if (!byte.TryParse(addsec, out tmpx))
                        {
                            _valid = false;
                            break;
                        }
                }
                catch { _valid = false; }
            }
            else
                _valid = false;
        }
    }
    
    public string AsRevString
    {
        get
        {
            if (_valid)
            {
                string tmpstr = "";
                for (int ai = _adresse.Length - 1; ai >= 0; ai--)
                {
                    tmpstr += _adresse[ai];
                    if (ai > 0)
                        tmpstr += ".";
                }
                return tmpstr;
            }
            else
                return "";
        }
    }

    public string[] AsStringArray
    {
        get { return _adresse; }
        set
        {
            if (value.Length == 4)
            {
                try
                {
                    _valid = true;
                    byte tmpx = 0;
                    foreach (string addsec in value)
                        if (!byte.TryParse(addsec, out tmpx))
                        {
                            _valid = false;
                            break;
                        }
                }
                catch { _valid = false; }
            }
            else
                _valid = false;
        }
    }
    
    public string[] AsRevStringArray
    {
        get
        {
            string[] tmpstrarr = new string[_adresse.Length];
            for (int ai = _adresse.Length - 1; ai >= 0; ai--)
            {
                tmpstrarr[(_adresse.Length - 1) - ai] = _adresse[ai];
            }
            return tmpstrarr;
        }
    }

    public byte[] AsByteArray
    {
        get
        {
            if (_valid)
                return StringToByte(_adresse);
            else
                return new byte[0];
        }
        set
        {
            if (value.Length == 4)
            {
                _adresse = ByteToString(value);
                _valid = true;
            }
            else
                _valid = false;
        }
    }
    
    public byte[] AsRevByteArray
    {
        get
        {
            byte[] tmpcon = StringToByte(_adresse);
            byte[] tmpbytearr = new byte[tmpcon.Length];
            for (int ai = tmpcon.Length-1; ai >= 0; ai--)
            {
                tmpbytearr[(tmpcon.Length - 1) - ai] = tmpcon[ai];
            }
            return tmpbytearr;
        }
    }

    public long AsLong
    {
        get
        {
            if (_valid)
                return StringToLong(_adresse, true);
            else
                return 0;
        }
        set
        {
            try
            {
                _adresse = LongToString(value, false);
                _valid = true;
            }
            catch { _valid = false; }
        }
    }
    
    public long AsRevLong
    {
        get { return StringToLong(_adresse, false); }
    }

    #endregion

    #region Contructors

    public exIPAddress() { }
    public exIPAddress(string address)
    {
        this.AsString = address;
    }
    public exIPAddress(string[] address)
    {
        this.AsStringArray = address;
    }
    public exIPAddress(byte[] address)
    {
        this.AsByteArray = address;
    }
    public exIPAddress(long address)
    {
        this.AsLong = address;
    }

    #endregion

    #region Private methods

    private byte[] StringToByte(string[] strArray)
    {
        try
        {
            byte[] tmp = new byte[strArray.Length];
            for (int ia = 0; ia < strArray.Length; ia++)
            tmp[ia] = byte.Parse(strArray[ia]);
            return tmp;
        }
        catch
        {
            return new byte[0];
        }
    }

    private string[] ByteToString(byte[] byteArray)
    {
        try
        {
            string[] tmp = new string;
            for (int ia = 0; ia < byteArray.Length; ia++)
                tmp[ia] = byteArray[ia].ToString();
            return tmp;
        }
        catch
        {
            return new string[0];
        }
    }

    private long StringToLong(string[] straddr, bool Revese)
    {
        long num = 0;
        if (straddr.Length == 4)
        {
            try
            {
                if (Revese)
                    num = (int.Parse(straddr[0])) +
                        (int.Parse(straddr[1]) * 256) +
                        (int.Parse(straddr[2]) * 65536) +
                        (int.Parse(straddr[3]) * 16777216);
                else
                    num = (int.Parse(straddr[3])) +
                        (int.Parse(straddr[2]) * 256) +
                        (int.Parse(straddr[1]) * 65536) +
                        (int.Parse(straddr[0]) * 16777216);
            }
            catch { num = 0; }
        }
        else
            num = 0;
        return num;
    }

    private string[] LongToString(long lngval, bool Revese)
    {
        string[] tmpstrarr = new string[4];
        if (lngval > 0)
        {
            try
            {
                int a = (int)(lngval / 16777216) % 256;
                int b = (int)(lngval / 65536) % 256;
                int c = (int)(lngval / 256) % 256;
                int d = (int)(lngval) % 256;
                if (Revese)
                {
                    tmpstrarr[0] = a.ToString();
                    tmpstrarr[1] = b.ToString();
                    tmpstrarr[2] = c.ToString();
                    tmpstrarr[3] = d.ToString();
                }
                else
                {
                    tmpstrarr[3] = a.ToString();
                    tmpstrarr[2] = b.ToString();
                    tmpstrarr[1] = c.ToString();
                    tmpstrarr[0] = d.ToString();
                }
            }
            catch { }
            return tmpstrarr;
        }
        else
            return tmpstrarr;
    }

    #endregion
}

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
General[Message Deleted]
Danny Rodriguez
10:11 27 Jan '08  
[Message Deleted]
GeneralPlease see the history...
PawJershauge
23:33 19 Nov '07  
For users that had some errors in the exIPAddress class, please look at the history table above...Smile
QuestionI like this...but
MathiasW
13:34 12 Nov '07  
Nice submission, thanks for sharing!

I only have a question: What is the meaning of your listed "best practice index"? You could add a bit more text and explanation to your article, couldn't you? Wink

Regards,

M. Wuehrmann
AnswerRe: I like this...but
PawJershauge
21:48 12 Nov '07  
Yes you are right, i could have added more text, but i didnt have time for it, at the moment, sorry.

The meaning of the "Best practice index" is, that you should take the list of servers, and put them into an order of the index (like ORDER BY index), in this case, you will get the best result, when looking up an IP address.

Best regards


GeneralUseful
duwke
5:14 31 Oct '07  
I have an app that sends opt-in newsletters, so I definitely need to know if I'm ever blacklisted. I've been manually checking spamcop and spamhaus. I had no idea that you could just query the blacklist servers like this. Thanks for putting this info out there! I'm rolling this into my nightly cron job.
GeneralRe: Useful
PawJershauge
10:00 2 Nov '07  
You are more than welcome... Big Grin


Last Updated 20 Nov 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010