I want to check an IP address against a given network/netmask, but I am
not sure how to do this.
For example:
ip=192.168.1.5 nw/nm=192.168.1.0/24 = match
ip=192.168.1.5 nw/nm=192.168.2.0/24 = no match
ip=192.168.1.5 nw/nm=192.168.0.0/16 = match
ip=10.120.12.5 nw/nm=192.168.2.0/24 = no match
ip=10.120.12.5 nw/nm=10.120.12.5/32 = match
This is what I have so far, but I am now stuck. I think I am missing
one small piece of the puzzle. Where you see inet_addr(), I also tried
inet_aton() (and using s_addr), but this did not work either. On the
final step, I am not sure if I need to AND or need to OR, but neither
seem to work as I expected.
Any help would be greatly appreciated.
Thanks,
-John
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void
Usage
(char *pgm) {
fprintf( stderr, "\n" );
fprintf( stderr, "Usage : %s [ ip ] [ network/netmask ]\n",
pgm );
fprintf( stderr, "Exmaple: %s 192.168.1.9 192.168.1.0/24\n",
pgm );
fprintf( stderr, "\n" );
}
int
main
(int argc, char *argv[]) {
char *s_ip, *s_nwnm, *s_nw, *s_nm;
char *ptr;
unsigned int i, nm;
unsigned int mask;
in_addr_t ip2, nw2;
if ( 3 != argc ) {
Usage(argv[0]);
return 1;
}
s_ip = argv[1];
s_nwnm = argv[2];
ptr = index(s_nwnm,(i nt) '/');
if ( NULL != ptr ) {
s_nm = ptr+1;
i = ptr - argv[2];
s_nw = s_nwnm;
s_nw[i] = '\0';
}
printf("\n");
printf("ip: _%s_ nw: _%s_ nm: _%s_\n\n", s_ip, s_nw, s_nm);
ip2 = inet_addr(s_ip) ;
nw2 = inet_addr(s_nw) ;
printf("ip2: %u\n", ip2);
printf("nw2: %u\n", nw2);
nm = 32 - atoi(s_nm);
mask = -1;
mask <<= nm;
printf("nm: %u\n\n", mask);
printf(" or: %u :: %u == %u\n", (ip2|mask), (nw2|mask),
(ip2|mask)==(nw 2|mask) );
printf("and: %u :: %u == %u\n", (ip2&mask), (nw2&mask),
(ip2&mask)==(nw 2&mask) );
printf("\n");
return 0;
} 1 5687
In article <11************ **********@g10g 2000cwb.googleg roups.com>,
<jo**@fcs.uga.e du> wrote: I want to check an IP address against a given network/netmask, but I am not sure how to do this.
For example:
ip=192.168.1 .5 nw/nm=192.168.1.0/24 = match ip=192.168.1 .5 nw/nm=192.168.2.0/24 = no match
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
Those last three headers are not part of standard C, so your
program is non-portable, and any part of your program that uses
those functions is considered Off Topic in this newsgroup.
Try comp.unix.progr ammer .
int main (int argc, char *argv[]) {
unsigned int mask;
nm = 32 - atoi(s_nm); mask = -1; mask <<= nm; printf("nm: %u\n\n", mask);
If your unsigned int is only 16 bits, then that calculation of
the mask would not work. You should use unsigned long for these
calculations.
printf(" or: %u :: %u == %u\n", (ip2|mask), (nw2|mask), (ip2|mask)==(nw 2|mask) );
That would tell you whether they have the same broadcast address.
printf("and: %u :: %u == %u\n", (ip2&mask), (nw2&mask), (ip2&mask)==(nw 2&mask) );
And that would tell you whether they have the same base address.
In the case of testing to see whether an IP address fits a given
network and range, testing either one of these would work. In
the more general case where the IP address itself is accompanied
by a netmask, the tests get a little more complicated, particularily
if you are attempting to determine whether one range fully encloses
the other or if there is some overlap or no overlap.
When you switch to long, be sure to convert your %u formats to %lu .
printf("\n"); return 0; }
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ruslan Spivak |
last post by:
Hello.
I need to do the following:
i have ip address(a lot of different ip addresses) and i need to
determine if it belongs to some network. (for example ip -
193.108.240.17, network 193.108.240.0/22).
How can i solve this?
Thanks in advance.
Your help is very appreciated.
|
by: Andrew McLean |
last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if
there is any code available to help. I've Googled without success.
Basically, I have two databases containing lists of postal addresses and
need to look for matching addresses in the two databases. More
precisely, for each address in database A I want to find a single
matching address in database B.
I'm 90% of the way there, in the sense that I have a simplistic...
|
by: Mario |
last post by:
Help.........
I have a CDROM that has address information from the USPS.
If you have any source code for this I would appreciate it.
What I want to do is pull the address and zip code from the CDRom and store
in my database.
Any suggestions.
|
by: Sandman |
last post by:
So, I have this list of valid IP scopes, in the form below. How do I
match if $_SERVER is covered in any of these scopes?
193.11.120.0/21
193.11.128.0/24
193.11.129.0/24
193.11.130.0/24
193.11.131.0/24
|
by: MrNobody |
last post by:
I need to find out the IP address of my local network on the WAN. There is
alot of information on getting my local machine IP address but I can't find
anything for getting the IP address my network is on.
I was thinking I could make a HTTP Request to a server which displays your
IP address as they see it and scrape it out of the response but I was hoping
for a method which is less dependent on some server out on the web...
| |
by: jkhan |
last post by:
Hi!
I have problem which I shown in title and below as well. I have a Form name as Cancelation. The clientThread2() method is called on Form Load if I Close the Form and want to reload then the above error is generated as I tried to close, shutdown etc for closing the socket or thread but useless. Any one have idea about it.
The above error is generated when it reached to this line ==> sender = new IPEndPoint(IPAddress.Any, 0); as I...
|
by: pbd22 |
last post by:
Hi. I am getting the below error:
Event Type: Error
Event Source: PeskyService
Event Category: None
Event ID: 0
Date: 1/30/2008
Time: 5:49:10 PM
User: N/A
|
by: BA |
last post by:
Hi Everyone,
I have an application that sits behind a server farm, the application needs
to pass its NLB IP address in the message that it sends to another service.
From C# code, how can I determine the IP address of the network load
balanced machine that the message is generated from?
So, in essence, I have server1, server2 and server3 sitting behind the NLB
IP address 100.1.2.100, then I have server4 and server5 sitting behind NLB...
|
by: dschu012 |
last post by:
I am trying to make a regex to match simple addresses like
123 Main Street
Simpleville, VA 12345
I have little experience with regex's and through reading and lots of messing around I have come up with.
/(\d{1,5}).*\b(A|C|D|F|G|HI|I|K|LA|M|N|O|P|RI|S|T|UT|V|W)\b/gm
|
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...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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();...
|
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...
| |
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...
| |