LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 30
Check if IP belongs to subnet


Hi,

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?

Best,
Chris
 
Old 06-11-2007, 10:39 AM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

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
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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 168.192.10.145 which is decimal and cannot be AND-ed.

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

jlinkels
 
  


Reply


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


All times are GMT -5. The time now is 01:49 AM.

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