Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-11-2007, 09:42 AM   #1
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
Check if IP belongs to subnet


I'm looking to some quick and easy way to determine if the given IP address belongs to the subnet given as network and mask in the slash notation. Is it possible to do with some bitmask matching using some of already defined C structures?

Old 06-11-2007, 10:39 AM   #2
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Original Poster
Rep: Reputation: 30
Ok, I think I have hound the answer but please correct me if I'm wrong. Is it as easy as ANDing the IP with the mask, then ANDing the network with the mask and comparing the results? I'm quite positive my thinking is OK but I'd need second opinion to be 100% assured.
Old 06-11-2007, 06:35 PM   #3
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,605

Rep: Reputation: 686Reputation: 686Reputation: 686Reputation: 686Reputation: 686Reputation: 686
Bottom line it comes to that. You AND both values (IP and network) and if the two values are equal you have a match.

IP was designed in an era that elegant solutions were appreciated instead of throwing in more CPU power to solve the problem of a crappy algorithm.

However beware that common notation for IP is which is decimal and cannot be AND-ed.

Also, AFAIK "slash" notation is meaning that the mask is 0xFFFFFF00 so you'd have to create that value from the /24 string.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
types.h belongs to which package sreenath Linux - Software 2 06-22-2006 10:57 PM
how to check rpm belongs to 32bits or 64bits stewchcken Linux - Software 3 11-22-2005 01:00 AM
Where to find to whom an ip belongs to ? juanb Linux - Security 4 10-24-2004 02:56 PM
where belongs _dl_runtime_resolve()? Hano Programming 1 07-19-2004 03:39 PM
If this belongs in general, please let me know. e477 Debian 1 09-16-2003 05:05 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration