home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / hdsetup / ipmask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-10  |  2.2 KB  |  88 lines

  1. /* ipmask.c
  2.  *
  3.  * Given argv[1] as a decimal netmask and argv[2] as a decimal IP address,
  4.  * print the resulting broadcast and network addresses to stdout.  This is
  5.  * potentially useful in scripts which need the broadcast address and the
  6.  * network address but want to ask the user as few questions as possible.
  7.  *
  8.  * Copyright 1994 by David Niemi.  Written in about 30 minutes on 13 Aug.
  9.  * The author places no restrictions on the use of this program, provided
  10.  * that this copyright is preserved in any derived source code.
  11.  *
  12.  * Typical compilation command for Linux:
  13.  *    cc ipmask.c -Wall -O -m486 -N -o ipmask -s
  14.  */
  15.  
  16. #define MYNAME "ipmask"
  17.  
  18. #include <stdio.h>
  19.  
  20. void Usage(void) {
  21.     fprintf (stderr,
  22.         "USAGE: %s <decimal netmask> <decimal IP address>\n",
  23.         MYNAME);
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28. unsigned long netmask, ipaddr, netaddr, broadcast;
  29. int in[4], j;
  30. unsigned char bc[4],na[4];
  31.  
  32.     if (3 != argc) {
  33.         Usage();
  34.         exit(1);
  35.     }
  36.  
  37.     /* Check netmask */
  38.     if (4 != sscanf(argv[1],"%d.%d.%d.%d", &in[0],&in[1],&in[2],&in[3])) {
  39.         fprintf (stderr,"Invalid netmask \"%s\".\n", argv[1]);
  40.         Usage();
  41.         exit(1);
  42.     }
  43.     for (j=0; j<4; ++j) {
  44.         if (in[j]<0 || in[j]>255) {
  45.             fprintf (stderr,
  46.                 "Invalid octet %d in netmask \"%s\".\n",
  47.                 j+1, argv[1]);
  48.             Usage();
  49.             exit(1);
  50.         }
  51.     }
  52.     netmask = in[3] + 256 * (in[2] + 256 * (in[1] + 256 * in[0]));
  53.  
  54.     /* Check IP address */
  55.     if (4 != sscanf(argv[2],"%d.%d.%d.%d", &in[0],&in[1],&in[2],&in[3])) {
  56.         fprintf (stderr,"Invalid IP address \"%s\".\n", argv[2]);
  57.         Usage();
  58.         exit(1);
  59.     }
  60.     for (j=0; j<4; ++j) {
  61.         if (in[j]<0 || in[j]>255) {
  62.             fprintf (stderr,
  63.                 "Invalid octet %d in IP address \"%s\".\n",
  64.                 j+1, argv[1]);
  65.             Usage();
  66.             exit(1);
  67.         }
  68.     }
  69.     ipaddr = in[3] + 256 * (in[2] + 256 * (in[1] + 256 * in[0]));
  70.  
  71.     broadcast = ipaddr | (~ netmask);
  72.     bc[0] = broadcast / 256 / 256 / 256;
  73.     bc[1] = (broadcast / 256 / 256) % 256;
  74.     bc[2] = (broadcast / 256) % 256;
  75.     bc[3] = broadcast % 256;
  76.  
  77.     netaddr = ipaddr & netmask;
  78.     na[0] = netaddr / 256 / 256 / 256;
  79.     na[1] = (netaddr / 256 / 256) % 256;
  80.     na[2] = (netaddr / 256) % 256;
  81.     na[3] = netaddr % 256;
  82.  
  83.     printf ("%d.%d.%d.%d %d.%d.%d.%d\n",
  84.         bc[0], bc[1], bc[2], bc[3], na[0], na[1], na[2], na[3]);
  85.  
  86.     exit(0);
  87. }
  88.