LinuxQuestions.org
Visit Jeremy's Blog.
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 01-22-2009, 08:09 AM   #1
quantt
Member
 
Registered: Aug 2008
Posts: 62

Rep: Reputation: 15
C cidr argv


hi all!
Me need parse argv and took value about subnets from cli. What's I've already...
Code:
unsigned int ip[4];
unsigned int mask;
int degree;
int hosts;
int count;
sscanf(argv[i], "%d.%d.%d.%d/%d", &ip[0], &ip[1], &ip[2], &ip[3], &mask);
degree = 32 - mask;
hosts=pow(2,degree);
for(hosts>count;count++){


printf("host\n");
// where host it's either address from input subnet.
// How to get first and last address from existed subnet?
}
I can do calculi a hosts value from given string in above code, but I'm confused about which's find out first and last address from given subnet. For example, if I've got subnet such as 192.168.20.16/28 me need took address 192.168.20.16 and 192.168.20.31, but I can get 192.168.20.22/28 from argv and it's will be compared with 192.168.20.16/28 because 192.168.20.22 to belongs that subnet.

Last edited by quantt; 01-22-2009 at 09:52 AM.
 
Old 01-22-2009, 08:55 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
If you want help, try to ask your question in understandable English.

I looked at a few of your posts trying to guess whether they are legitimate attempts to communicate in English by someone who doesn't speak English. I can't tell, but they look more like attempts to be annoying and difficult by someone who does speak English.
 
Old 01-22-2009, 09:37 AM   #3
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
How to get first and last address from existed subnet?
 
Old 01-22-2009, 11:26 AM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Your english really is difficult to follow, but unlike the previous poster I don't think you are doing it on purpose.

If I understand you correctly, you are trying to figure out how to use the netmask.

IPv4 addresses are 32 bits long, and the netmask provides a mask that lets you define how many of those bits are part of the same local network. Basically, if you AND the address with the network mask, any address that gives the same result is part of the same subnet.

Thus, when your netmask is 28 bits, you construct a 32 bit integer whose bit values consist of 28 1 bits followed by 4 0 bits. In IPv4 notation, this is 255.255.255.240.

So, for any group of addresses,

if ((address1 & netmask) == (address2 & netmask));

then those addresses are in the same subnet. Thus, from your example, 192.168.20.22, 192.168.20.31, and 192.168.20.16 are all in the same subnet.

Last edited by jiml8; 01-22-2009 at 11:27 AM.
 
Old 01-23-2009, 05:58 AM   #5
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Thanks for your reply, but if I've quantity hosts and that chars...
&ip[0]= 192
&ip[1]= 168
&ip[2]= 20
&ip[3]= 22
In what way I'll get a first adders from that subnet? It I've got &ip[3]= 16 it will be most easy, me necessary anticipated that. How to do it?
 
Old 01-23-2009, 08:09 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
1) Convert the four 8-bit numbers to one 32-bit number:
unsigned long address = (((((ip[0]<<8)+ip[1])<<8)+ip[2])<<8)+ip[3];

2) Compute the first address
unsigned long first_address = address &~(hosts-1);

3) I think you wanted to loop through all the addresses:
for (address=first_address; address<first_address+hosts; ++address)

4) convert each back from one 32-bit number to four 8-bit numbers:
for (int i=0; i<4; ++i) id[i] = (address >> (24-i*8)) & 0xFF;

Edit: I fixed a bug in the above line after quantt observed that it didn't work.

Last edited by johnsfine; 01-26-2009 at 07:53 AM.
 
Old 01-23-2009, 10:57 AM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by quantt View Post
Thanks for your reply, but if I've quantity hosts and that chars...
&ip[0]= 192
&ip[1]= 168
&ip[2]= 20
&ip[3]= 22
In what way I'll get a first adders from that subnet? It I've got &ip[3]= 16 it will be most easy, me necessary anticipated that. How to do it?
The first address in the subnet is merely any address in the subnet ANDed with the the netmask address since that returns the address with the unmasked bits set to zero (in this case, 192.168.20.16). The last address in the subnet is merely the first address + (2^N)-1 where N is the number of address bits that are NOT masked off, since this returns the address with the unmasked bits all set to 1 (in your case, N is 4 so the last address is 192.168.20.31).
 
Old 01-23-2009, 10:23 PM   #8
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
What's there? I marked it orange light.
first_address = address &~(hosts-1);

Last edited by quantt; 01-24-2009 at 12:47 AM.
 
Old 01-24-2009, 08:48 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
In C the expression
Code:
a &~ (b)
means the bitwise AND of all bits that are set in "a" and not set in "(b)".

In your code "hosts" is the length of a contiguous range of IP addresses and is known to be a power of two. So (hosts-1) is a number in which the bits that are set are exactly the bits that can vary in that subnet. So "address&~(hosts-1)" takes an address within the subnet and preserves all the bits that cannot vary while clearing all the bis that can vary in the subnet, which gives you the lowest address in the subnet.
 
Old 01-24-2009, 09:09 AM   #10
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
If I doing whatever like this
Quote:
for (address=first_address; address<first_address+hosts; ++address){
printf("%u\n\n", address);
for (;i<4; i++) id[i] = (address >> (i*8)) & 0xFF;
printf("%u\n", id[i]);
}
I've got it crazy of result
Quote:
3232240640

0
3232240641

0
3232240642

0
3232240643

0
But I expect something like this
Quote:
3232240640
192.168.20.0
3232240641
192.168.20.1
3232240642
192.168.20.2
3232240643
192.168.20.3

Last edited by quantt; 01-26-2009 at 06:15 AM.
 
Old 01-26-2009, 06:15 AM   #11
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Could you help me?
 
Old 01-26-2009, 08:03 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by quantt View Post
If I doing whatever like this
for (;i<4; i++) id[i] = (address >> (i*8)) & 0xFF;
I had a bug in what I first posted (I corrected it above); I was setting the elements if id[] in reverse sequence. But you seem to have added an additional bug, not initializing i.

Try this:
Code:
for (address=first_address; address<first_address+hosts; ++address){
   printf("%u ", address);
   for (i=0; i<4; i++) {
      id[i] = (address >> (24-i*8)) & 0xFF;
      printf("%u%c", id[i], (i==3) ? '\n' : '.' );
} }
 
  


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
bash CIDR calculator genderbender Programming 18 10-29-2014 11:13 AM
LXer: Tutorial: Networking 101: Understanding Subnets and CIDR LXer Syndicated Linux News 0 07-31-2008 03:40 AM
Understanding CIDR notation for iptables Madone_SL_5.5 Linux - Networking 5 12-07-2007 11:26 AM
argc argv linuxanswer Programming 8 10-25-2003 08:54 PM
Help with argv pilot1 Programming 7 08-23-2003 03:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration