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 7750037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:11:12+00:00 2026-06-01T11:11:12+00:00

so i am trying to return a best possible matching interface from routing entries.

  • 0

so i am trying to return a best possible matching interface from routing entries. However, it is not exactly working the way i want it to. I got 5 out 6 values returned the way should be but I am pretty sure I have a million entries in a routing table my algorithm would not work.
I am using Binary Search to solve this problem. But, for example, the interface that i want to return has a ipaddress which is smaller than the ipaddress i am passing as an argument, then the binary search algorithm does not work. the structure looks like this:

struct routeEntry_t
{
    uint32_t ipAddr;
    uint32_t netMask;
    int interface;
};
routeEntry_t routing_table[100000];

let’s say the routing table looks like this:

{ 0x00000000, 0x00000000, 1 }, // [0]

{ 0x0A000000, 0xFF000000, 2 }, // [1]

{ 0x0A010000, 0xFFFF0000, 10 }, // [2]

{ 0x0D010100, 0xFFFFFF00, 4 }, // [3]

{ 0x0B100000, 0xFFFF0000, 3 }, // [4]

{ 0x0A010101, 0xFFFFFFFF, 5 }, // [5]

Example input/output:

  1. Regular search

Input: 0x0D010101 Output: 4 (entry [3])

Input: 0x0B100505 Output: 3 (entry [4])

  1. To find an arbitrary address, it should go to the default interface.

Input: 0x0F0F0F0F Output: 1 (entry [0])

  1. To find an address that matches multiple entries, take the best-match.

Input: 0x0A010200 Output: 10 (entry [2])

Input: 0x0A050001 Output: 2 (entry [1])

Input: 0x0A010101 Output: 5 (entry [5])

But my output looks like 2, 3, 1, 10, 1, 5. I don’t understand where I am messing things up. Could you please explain where I am doing wrong? any help would be great. Thanks in advance. However this is what my algorithm looks like (assuming the entries are sorted):

int interface(uint32_t ipAddr)

{
int start = 0;

int end = SIZE-1;

int mid = 0;

vector<int> matched_entries;

vector<int>::iterator it;

matched_entries.reserve(SIZE);

it = matched_entries.begin();


if (start > end)
    return -1;

while (start <= end)
{
    mid = start + ((end-start)/2);

    if (routing_table[mid].ipAddr == ipAddr)
        return routing_table[mid].interface;
    else if (routing_table[mid].ipAddr > ipAddr)
    {
        uint32_t result = routing_table[mid].netMask & ipAddr;
        if (result == routing_table[mid].ipAddr)
        {
            matched_entries.push_back(mid);
        }
        end = mid-1;
    }
    else
    {
        uint32_t result = routing_table[mid].netMask & ipAddr;
        if (result == routing_table[mid].ipAddr)
        {
            matched_entries.insert(it,mid);
        }
        start = mid+1;
    }
}


int matched_ip = matched_entries.back();
if (routing_table[matched_ip].netMask & ipAddr)
    return routing_table[matched_ip].interface;
else
    return routing_table[0].interface;  

}

  • 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-01T11:11:13+00:00Added an answer on June 1, 2026 at 11:11 am

    The “right” interface is the entry with the most specific netmask whose IP address is on the same subnet as your input.

    Let’s look at what netmasks are, and how they work, in more detail.

    Notation

    Although netmasks are usually written in dotted-decimal or hex notation, the binary representation of an IPv4 netmask is always 32 bits; that is, it’s exactly the same length as an IP address. The netmask always starts with zero or more 1 bits and is padded with 0 bits to complete its 32-bit length. When a netmask is applied to an IP address, they’re “lined up” bit by bit. The bits in the IP address that correspond to the 1 bits in the netmask determine the network number of the IP address; those corresponding to the 0 bits in the netmask determine the device number.

    Purpose

    Netmasks are used to divide an address space into smaller subnets. Devices on the same subnet can communicate with each other directly using the TCP/IP protocol stack. Devices on different subnets must use one or more routers to forward data between them. Because they isolate subnets from each other, netmasks are a natural way to create logical groupings of devices. For example, each location or department within a company may have its own subnet, or each type of device (printers, PCs, etc.) may have its own subnet.

    Example netmasks:

    255.255.255.128 → FF FF FF 10 → 1111 1111 1111 1111 1111 1111 1000 0000
    This netmask specifies that the first 25 bits of an IP address determine the network number; the final 7 bits determine the device number. This means there can be 225 different subnets, each with 27 = 128 devices.*

    255.255.255.0     → FF FF FF 00 → 1111 1111 1111 1111 1111 1111 0000 0000
    This netmask specifies an address space with 224 subnets, each with 28 = 256 individual addresses. This is a very common configuration—so common that it’s known simply as a “Class C” network.

    255.255.192.0     → FF FF FC 00 → 1111 1111 1111 1111 1111 1100 0000 0000
    This netmask specifies 222 subnets, each with 210 = 1024 addresses. It might be used inside a large corporation, where each department has several hundred devices that should be logically grouped together.

    An invalid netmask (note the internal zeroes):
    255.128.255.0     → FF 80 FF 00 → 1111 1111 1000 0000 1111 1111 0000 0000

    Calculations

    Here are a few examples that show how a netmask determines the network number and the device number of an IP address.

      IP Address: 192.168.0.1 → C0 A8 00 01
      Netmask: 255.255.255.0 → FF FF FF 00
    This device is on the subnet 192.168.0.0. It can communicate directly with other devices whose IP addresses are of the form 192.168.0.x

      IP Address: 192.168.0.1     → C0 A8 00 01
      IP Address: 192.168.0.130 → C0 A8 00 82
      Netmask: 255.255.255.128 → FF FF FF 80
    These two devices are on different subnets and cannot communicate with each other without a router.

      IP Address: 10.10.195.27 → 0A 0A C3 1B
      Netmask: 255.255.0.0 → FF FF 00 00
    This is an address on a “Class B” network that can communicate with the 216 addresses on the 10.10.0.0 network.

    You can see that the more 1 bits at the beginning of a netmask, the more specific it is. That is, more 1 bits create a “smaller” subnet that consists of fewer devices.

    Putting it all together

    A routing table, like yours, contains triplets of netmasks, IP addresses, and interfaces. (It may also contain a “cost’ metric, which indicates which of several interfaces is the “cheapest” to use, if they are both capable of routing data to a particular destination. For example, one may use an expensive dedicated line.)

    In order to route a packet, the router finds the interface with the most specific match for the packet’s destination. An entry with an address addr and a netmask mask matches a destination IP address dest if (addr & netmask) == (dest & netmask), where & indicates a bitwise AND operation. In English, we want the smallest subnet that is common to both addresses.

    Why? Suppose you and a colleague are in a hotel that’s part of a huge chain with both a corporate wired network and a wireless network. You’ve also connected to your company’s VPN. Your routing table might look something like this:

     Destination    Netmask    Interface   Notes
     -----------    --------   ---------   -----
     Company email  FFFFFF00   VPN         Route ALL company traffic thru VPN
     Wired network  FFFF0000   Wired       Traffic to other hotel addresses worldwide
     Default        00000000   Wireless    All other traffic
    

    The most specific rule will route your company email safely through the VPN, even if the address happens to match the wired network also. All traffic to other addresses within the hotel chain will be routed through the wired network. And everything else will be sent through the wireless network.

    * Actually, in every subnet, 2 of the addresses—the highest and the lowest—are reserved. The all-ones address is the broadcast address: this address sends data to every device on the subnet. And the all-zeroes address is used by a device to refer to itself when it doesn’t yet have it’s own IP address. I’ve ignored those for simplicity.


    So the algorithm would be something like this:

    initialize:
      Sort routing table by netmask from most-specific to least specific.
      Within each netmask, sort by IP address.
    
    search:
      foreach netmask {
        Search IP addresses for (input & netmask) == (IP address & netmask)
        Return corresponding interface if found
      }
      Return default interface
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to return the value that a $ajax call returns, from a function
I'm trying to return a reference to an object, not pass it by value.
I'm trying to return a JSON response from a Django view call via an
i'm trying to return a custome object from a web service object returned only
I'm trying to return a random number from a method but apparently the implicit
Is there any way to return distinct values with blank/null data from a table
I am trying to adhere to best multi-layer design practices, and don't want my
I'm trying to find the best way to redirect a user to a page
What is the best way to unit test a method that doesn't return anything?
I am trying to figure out the best, or most efficient way of doing

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.