Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:43:56+00:00 2026-06-08T05:43:56+00:00

I want to check if an IP address is in a certain range, matching

  • 0

I want to check if an IP address is in a certain range, matching by "*" only. For example, “202.121.189.8” is in “202.121.189.*“.

The scenario is that I have a list of banned IPs, some of them contains "*", so I wrote a function, it works fine so far:

static bool IsInRange(string ip, List<string> ipList)
{
    if (ipList.Contains(ip))
    {
        return true;
    }

    var ipSets = ip.Split('.');
    foreach (var item in ipList)
    {
        var itemSets = item.Split('.');
        for (int i = 0; i < 4; i++)
        {
            if (itemSets[i] == "*")
            {
                bool isMatch = true;
                for (int j = 0; j < i; j++)
                {
                    if (ipSets[i - j - 1] != itemSets[i - j - 1])
                    {
                        isMatch = false;
                    }
                }
                if (isMatch)
                {
                    return true;
                }
            }
        }
    }
    return false;
}

Test code:

string ip = "202.121.189.8";
List<string> ipList = new List<string>() { "202.121.168.25", "202.121.189.*" };

Console.WriteLine(IsInRange(ip, ipList));

But I think what i wrote is very stupid, and I want to optimize it, does anyone have an idea how to simplify this function? not to use so many “for….if…”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:43:57+00:00Added an answer on June 8, 2026 at 5:43 am

    A good idea would be to represent the banned subnets in a form of a pair: mask + base address. So your check will look like that:

    banned = (ip & mask == baseaddress & mask);
    

    For 11.22.33.* the base address will be 11*0x1000000 + 22*0x10000 + 33*0x100, mask will be 0xffffff00.

    For single address 55.44.33.22 the address will be 55*0x1000000 + 44*0x10000 * 33*0x100 + 22, mask will be 0xffffffff.

    You’ll need to convert the address to a 32-bit int as a separate procedure.

    After that all, your code will look like that:

    int numip = ip2int(ip);
    bool isIpBanned = banList.Any(item =>
                numip & item.mask == item.baseaddress & item.mask);
    

    By the way, this way you’ll be able to represent even bans on smaller subsets.

    int ip2int(string ip) // error checking omitted
    {
        var parts = ip.Split('.');
        int result = 0;
        foreach (var p in parts)
            result = result * 0x100 + int.Parse(p);
    }
    
    
    class BanItem { public int baseaddres; public int mask; }
    
    BanItem ip2banItem(string ip)
    {
        BanItem bi = new BanItem() { baseaddres = 0, mask = 0 };
        var parts = ip.Split('.');
        foreach (var p in parts)
        {
            bi.baseaddress *= 0x100;
            bi.mask *= 0x100;
            if (p != "*")
            {
                bi.mask += 0xff;
                bi.baseaddress += int.Parse(p);
            }
        }
        return bi;
    }
    
    banList = banIps.Select(ip2banItem).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check if a string is a hostname or an ip-address in
I want to check a string to allow only a-z A-Z 0-9 . /
I am trying to look up cells in a certain column that have a
I want to check availability of web service. this is my code that i
here, i want to check only if the column Active is Yes, then get
I am currently writing an app that within a certain activity, we want the
I want to check number of times a device ip address come to my
So we have a site that we want to redirect all users to login
for some days i have been struggeling with making only certain ips accessing my
I want to check if my object address is 0x0. How to do it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.