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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-22-2009, 08:09 AM
|
#1
|
Member
Registered: Aug 2008
Posts: 62
Rep:
|
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.
|
|
|
01-22-2009, 08:55 AM
|
#2
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
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.
|
|
|
01-22-2009, 09:37 AM
|
#3
|
Member
Registered: Aug 2008
Posts: 62
Original Poster
Rep:
|
How to get first and last address from existed subnet?
|
|
|
01-22-2009, 11:26 AM
|
#4
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
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.
|
|
|
01-23-2009, 05:58 AM
|
#5
|
Member
Registered: Aug 2008
Posts: 62
Original Poster
Rep:
|
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?
|
|
|
01-23-2009, 08:09 AM
|
#6
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
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.
|
|
|
01-23-2009, 10:57 AM
|
#7
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Quote:
Originally Posted by quantt
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).
|
|
|
01-23-2009, 10:23 PM
|
#8
|
Member
Registered: Aug 2008
Posts: 62
Original Poster
Rep:
|
What's there? I marked it orange light.
first_address = address &~(hosts-1);
Last edited by quantt; 01-24-2009 at 12:47 AM.
|
|
|
01-24-2009, 08:48 AM
|
#9
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
In C the expression
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.
|
|
|
01-24-2009, 09:09 AM
|
#10
|
Member
Registered: Aug 2008
Posts: 62
Original Poster
Rep:
|
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.
|
|
|
01-26-2009, 06:15 AM
|
#11
|
Member
Registered: Aug 2008
Posts: 62
Original Poster
Rep:
|
Could you help me?
|
|
|
01-26-2009, 08:03 AM
|
#12
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by quantt
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' : '.' );
} }
|
|
|
All times are GMT -5. The time now is 06:52 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|