Click here to Skip to main content
6,305,776 members and growing! (15,052 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

DNSBL Lookup Class

By Paw Jershauge

A class for looking up IP addresses against DNS black list server (Anti Spam List Servers)
C# 2.0.NET 2.0, Win2K, WinXP, Win2003, VistaVS2005, Dev
Posted:25 Oct 2007
Updated:19 Nov 2007
Views:9,077
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 3.03 Rating: 4.33 out of 5

1

2
1 vote, 20.0%
3
2 votes, 40.0%
4
2 votes, 40.0%
5

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

  • 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)

About the Author

Paw Jershauge


Member
An Code Generator, for making SQL table into .Net Classses (2nd place in the Code Generation 2008 Competition)
A Class for getting the Rss feed list of a website
Seagate Date Code Calculator
DNSBL lookup Class
Base N converter (N = 10-62)
Lightweight Directory Access Protocol Uniform resource identifier (LDAPUri)
LoginHours from DirectoryEntry into boolean array
Occupation: Software Developer
Location: Denmark Denmark

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
General[Message Deleted] PinmemberDanny Rodriguez10:11 27 Jan '08  
GeneralPlease see the history... PinmemberPawJershauge23:33 19 Nov '07  
QuestionI like this...but PinmemberMathiasW13:34 12 Nov '07  
AnswerRe: I like this...but PinmemberPawJershauge21:48 12 Nov '07  
GeneralUseful Pinmemberduwke5:14 31 Oct '07  
GeneralRe: Useful PinmemberPawJershauge10:00 2 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Nov 2007
Editor: Deeksha Shenoy
Copyright 2007 by Paw Jershauge
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project