473,666 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IP Address and Subnet Comparison in C

I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0. 1";
char * sSubnet = "192.168.0. 0/24";

if ( address_is_in_s ubnet(sAddress, sSubnet) ){
...
...
...
}

-- br***********@y ahoo.com

Sep 18 '06 #1
5 8889

wr***********@g mail.com wrote:
I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0. 1";
char * sSubnet = "192.168.0. 0/24";

if ( address_is_in_s ubnet(sAddress, sSubnet) ){
...
...
...
}

-- br***********@y ahoo.com
This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.% d", &b[0],&b[1,&b[2],&b[3] );

do the same with the netmask.
sscanf( sAddress, "%d.%d.%d.% d/%d", &m[0],&m[1,&m[2],&m[3], &Bits );

Then take the Bits part and build a mask from it:

unsigned long int Tot = 0;
for( i = 1; i <= Bits; i++ ) Tot = (Tot << 1) | 1;
for(; i <= 32; i++ ) Tot = Tot << 1);

Now you can mask off the candidate ip address and compare it. Better
write a few functions for this:

void MaskEm( ), int IpEqual(), et al...
Hope this gets you started.

BTW there are probably dozens of these routines already written, try
googling for "c code to compare subnets" or somesuch.

Sep 18 '06 #2
On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker " <gr**@comcast.n et>
wrote in comp.lang.c:
>
wr***********@g mail.com wrote:
I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0. 1";
char * sSubnet = "192.168.0. 0/24";

if ( address_is_in_s ubnet(sAddress, sSubnet) ){
...
...
...
}

-- br***********@y ahoo.com

This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.% d", &b[0],&b[1,&b[2],&b[3] );
Surely you didn't really mean to pass one of the *scanf() families the
addresses of unsigned chars matched with a "%d" conversion specifier.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 19 '06 #3

Ancient_Hacker wrote:
wr***********@g mail.com wrote:
I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0. 1";
char * sSubnet = "192.168.0. 0/24";

if ( address_is_in_s ubnet(sAddress, sSubnet) ){
...
...
...
}

-- br***********@y ahoo.com

This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.% d", &b[0],&b[1,&b[2],&b[3] );

do the same with the netmask.
sscanf( sAddress, "%d.%d.%d.% d/%d", &m[0],&m[1,&m[2],&m[3], &Bits );

Then take the Bits part and build a mask from it:

unsigned long int Tot = 0;
for( i = 1; i <= Bits; i++ ) Tot = (Tot << 1) | 1;
for(; i <= 32; i++ ) Tot = Tot << 1);

Now you can mask off the candidate ip address and compare it. Better
write a few functions for this:

void MaskEm( ), int IpEqual(), et al...
Hope this gets you started.

BTW there are probably dozens of these routines already written, try
googling for "c code to compare subnets" or somesuch.

Thanks very much for your help. I appreciate it. How would I go about
masking off the candidate ip address and compare? I know this may seem
like a silly question but as I mentioned above I am new to C
programming.

Sep 19 '06 #4

Jack Klein wrote:
On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker " <gr**@comcast.n et>
wrote in comp.lang.c:

wr***********@g mail.com wrote:
I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.
>
char * sAddress = "192.168.0. 1";
char * sSubnet = "192.168.0. 0/24";
>
if ( address_is_in_s ubnet(sAddress, sSubnet) ){
...
...
...
}
>
-- br***********@y ahoo.com
This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.% d", &b[0],&b[1,&b[2],&b[3] );

Surely you didn't really mean to pass one of the *scanf() families the
addresses of unsigned chars matched with a "%d" conversion specifier.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Should it have been %s? How should this have worked?

Sep 19 '06 #5
[I was baffled by the attributions, so I dropped them.]
unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.% d", &b[0],&b[1,&b[2],&b[3] );

Surely you didn't really mean to pass one of the *scanf() families the
addresses of unsigned chars matched with a "%d" conversion specifier.
Should it have been %s? How should this have worked?
%hhu is the proper format specifier for unsigned char data, but
it is only available in C99.
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Sep 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1221
by: arnie | last post by:
Hi ya all. I am creating a small solution to keep statistics about IP Address accessing a certain system at my workplace. The idea is to be able to group the IP address to a certain subnet so we can see which class of users is accessing the system the most. During development of the collection process I've been using Postgres, Postgres has this a special datatype called CIDR. I can insert an IP Address into a table using this field...
3
1860
by: JamesB | last post by:
Hmm, this doesn't seem to work. I am probably tackling it completely the wrong way, but basically I want a function to receive two IP's and a subnet mask and return True if IP1 and 2 are on the same subnet, otherwise false. I have got this so far: Private Function IsSameSubnet(ByVal IPSource As String, ByVal IPDest As String, ByVal IPSubnet As String) As Boolean
7
5314
by: Chris Fulstow | last post by:
Hi, I need to restirict access at the page level to a range of IP addresses. What's the best approach? I thought of building an HTTP module that could compare the requesting IP with a restricted range, specified in web.config. Any thoughts? Thanks, Chris
4
5129
by: Morten Fagermoen | last post by:
Hi! I try to get the subnet location info using the code below found at http://msdn2.microsoft.com/en-us/library/system.directoryservices.activedirectory.activedirectorysubnet.location.aspx. But it only tells me that "instance" is used before it has been assigned a value. Can anyone tell me what to do to get my subnet location info? Public Property SubnetLocation() As String Get Dim instance As ActiveDirectorySubnet
6
2755
by: minnesøtti | last post by:
Hi there, I am posting to a web-based forum which runs on ASP and uses JavaScript. I post via a proxy-server which rewrites JavaScript, does not send cookies back to the forum server, and which changes its IP address every few minutes within the whole class A network. The moderator says he banned my IP address (let's put the moral issues of it aside for the sake of the technical discussion). This means he banned the whole network of...
0
1586
by: Danny Tsai | last post by:
Does anyone know how to get the MAC address of a computer? The computer is in different subnet and connected through a router. When I used the SendARP() function to get the MAC address, it worked when running within the same subnet. The problem is that SendARP() seems to return the MAC address of the router. I tried both arp and nbtstat commands but they only worked when running within the same subnet.
3
13450
by: Smokey Grindel | last post by:
Is there a way to check if an IP address is in a subnet range? say I have the IP 10.10.0.0 and a subnet of 255.255.0.0 and am given the IP 10.10.50.1 and want to verify that it is in that range.. thanks!
2
3097
by: nussu | last post by:
Hi , I need to find a subnet mask number for a given IP Address number. I have two textbox's once enter the ip address in first textbox i need to get subnet mask number for that ip address in second textbox. Please help me by providing links or c#.net code. Thanks in Advance. Regards, Nussu
1
2405
by: =?Utf-8?B?QWdlbmR1bQ==?= | last post by:
I have an issue where I have a remote IP Address, and I need to discover the local network interface IP Address which is viewable to the remote IP Address (for UPnP document purposes). For example, if I have the remote IP Address 10.0.0.4, and I know all of the local IP Addresses: 127.0.0.1 10.0.0.5 10.1.0.20 192.168.0.5 157.54.185.126
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.