Click here to Skip to main content
12,998,863 members (70,192 online)
Click here to Skip to main content
Add your own
alternative version

Stats

36.3K views
950 downloads
21 bookmarked
Posted 25 Oct 2007

DNSBL Lookup Class

, 19 Nov 2007
Rate this:
Please Sign up or sign in to vote.
A class for looking up IP addresses against DNS black list server (Anti Spam List Servers)

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 AddressBest practice index
access.redhawk.org22
accredit.habeas.com14
bl.deadbeef.com20
bl.spamcannibal.org36
bl.spamcop.net2
blackholes.uceb.org37
blacklist.spambag.org30
cbl.abuseat.org3
cbl.ni.bg38
cblless.anti-spam.org.cn35
combined.njabl.org11
combined.rbl.msrbl.net31
dnsbl.ahbl.org18
dnsbl.burnt-tech.com19
dnsbl.delink.net21
dnsbl.njabl.org10
dnsbl.sorbs.net4
dnsbl.tqmcube.com27
dnsbl-1.uceprotect.net32
dnsbl-2.uceprotect.net33
dnsbl-3.uceprotect.net34
dul.dnsbl.sorbs.net6
http.dnsbl.sorbs.net7
ko.tqmcube.com25
list.dsbl.org15
misc.dnsbl.sorbs.net9
multihop.dsbl.org16
no-more-funn.moensted.dk23
prc.tqmcube.com26
psbl.surriel.com29
rbl.spamlab.com13
sbl-xbl.spamhaus.org1
smtp.dnsbl.sorbs.net8
socks.dnsbl.sorbs.net5
spam.tqmcube.com24
ubl.unsubscore.com28
unconfirmed.dsbl.org17
zen.spamhaus.org12

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<bytearray.length />;
            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<bytearray.length />;
            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

  • 25th October, 2007 - Version 1 of the class posted
  • 26th October, 2007 - List of servers added
  • 20th November, 2007 - Correction to the nested class exIPAddress

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
Questionyou helped me alot to create this web site Pin
faragas130-Nov-11 13:15
memberfaragas130-Nov-11 13:15 
NewsSpamhaus updates... Pin
motific17-Oct-10 9:23
membermotific17-Oct-10 9:23 
GeneralRe: Spamhaus updates... Pin
Paw Jershauge18-Oct-10 5:16
memberPaw Jershauge18-Oct-10 5:16 
GeneralFor the ones that just want a simple more limited checker Pin
joaeri29-Aug-10 10:57
memberjoaeri29-Aug-10 10:57 
General[Message Deleted] Pin
Danny Rodriguez27-Jan-08 9:11
memberDanny Rodriguez27-Jan-08 9:11 
GeneralPlease see the history... Pin
PawJershauge19-Nov-07 22:33
memberPawJershauge19-Nov-07 22:33 
QuestionI like this...but Pin
MathiasW12-Nov-07 12:34
memberMathiasW12-Nov-07 12:34 
AnswerRe: I like this...but Pin
PawJershauge12-Nov-07 20:48
memberPawJershauge12-Nov-07 20:48 
GeneralUseful Pin
duwke31-Oct-07 4:14
memberduwke31-Oct-07 4:14 
GeneralRe: Useful Pin
PawJershauge2-Nov-07 9:00
memberPawJershauge2-Nov-07 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Paw Jershauge
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid