SUMMARY:Determining the Subnet (program)

From: Mark Fergusson (mferg@ddntl.didata.co.za)
Date: Tue Jan 28 1997 - 07:16:10 CST


Fistly, apologies for taking so long to summarize. Thanks to all who
replied.

My original question:

"Is there a program or function that takes as its arguments an ip
address
> and the mask and returns the subnet ?
>
> Eg. 160.100.100.50 255.255.255.0
>
> will return 160.100.100.0
>
> 197.34.183.34 255.255.255.224
>
> will return 197.34.183.32"

The simplest program I have is from Casper Dik. casper@holland.sun.com

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netdb.h>
 
main(int argc, char **argv)
 
{
    unsigned long addr, mask;
    struct in_addr in;
 
    if (argc != 3)
        exit(1);
 
    addr = inet_addr(argv[1]);
    mask = inet_addr(argv[2]);
 
    in.s_addr = addr & mask;
 
    printf("addr %s w/ mask %s yields %s\n", argv[1], argv[2],
        inet_ntoa(in));
}

For those who don't know, working out the Subnet is a logical bit AND.
Example:

        194.80.80.2 mask 255.255.255.240

        1100 0100 0101 0000 0101 0000 0000 0010
        1111 1111 1111 1111 1111 1111 1111 1000

        1100 0100 0101 0000 0101 0000 0000 0000

        which is 194.80.80.0 as expected.

Thanks also to:
Mark `Hex' Hershberger
dengland@ivac.com
ravik@netcom.com
nick.leroy@norland.com
asola@inteliideas.com
fpardo@tisny.com

=============================================
Mark Fergusson Voice: (+27)-31-560-2440 (Direct)
                                             (+27)-31-838333 (Reception)
Dimension Data Fax: (+27)-31-848525
PO Box 20713 Cellulal: 082-4465-118
Durban North Email: mfergusson@ddntl.didata.co.za
4016
South Africa Web: http://www.didata.co.za
==============================================
  



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:43 CDT