IT-Academy Logo
Sign Up Login Help
Home - Programmieren - C++ - Subnetting mit gnuSubnetter



Subnetting mit gnuSubnetter

Dieser Programmcode nimmt einem Systemadministrator die lästige Aufgabe des IP-Adressen berechnens ab.


Autor: Patrick Bucher (paedubucher)
Datum: 18-10-2005, 20:21:50
Referenzen: keine
Schwierigkeit: Fortgeschrittene
Ansichten: 4246x
Rating: Bisher keine Bewertung.

Hinweis:

Für den hier dargestellte Inhalt ist nicht der Betreiber der Plattform, sondern der jeweilige Autor verantwortlich.
Falls Sie Missbrauch vermuten, bitten wir Sie, uns unter missbrauch@it-academy.cc zu kontaktieren.

[Druckansicht] [Als E-Mail senden] [Kommentar verfassen]



/*
 *   gnuSubnetter v0.1 - build subnets easily
 *
 *   Copyright (C) 2005 by Patrick Bucher
 *   paedubucher@bluewin.ch
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 * used libraries
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*
 *  some programm-information
 */
#define PROGRAMM_NAME "gnuSubnetter"
#define PROGRAMM_VERSION "0.1"
#define YEAR "2005"
#define AUTHOR "Patrick Bucher"
#define EMAIL "paedubucher@bluewin.ch"

/*
 *  function-prototypes
 */
_Bool validateClassifiedSubnetMask(unsigned char *subnetMask);
unsigned char calcNumberOfSubnets(unsigned char proposition);
unsigned char *buildSubnetMask(unsigned char numberOfSubnets, unsigned char *oldSubnetMask);
void printIpAddress(unsigned char *address);
unsigned char *buildNetAddress(unsigned char tmpSubnet, unsigned char numberOfSubnets, unsigned char *oldNetAddress,
                               unsigned char *subnetMask);
unsigned char *buildBroadcast(unsigned char tmpSubnet, unsigned char numberOfSubnets, unsigned char *netAddress,
                              unsigned char *subnetMask);
unsigned int getNumberOfHosts(unsigned char *subnetMask, unsigned char numberOfSubnets);

/*
 *  the main-function
 *
 *  params:
 *  argc: number of arguments
 *  argv: list of argument-strings
 *
 *  return value:
 *  0: all OK
 */
int main(int argc, char *argv[])
{
  unsigned char numberOfSubnetsProposition = 129; // the users proposition for number of subnets
  unsigned char numberOfSubnets = 0; // the calculated number of subnets
  unsigned char *subnetMask = 0; // the subnet mask
  unsigned char *netAddress = 0; // the users network-address
  unsigned char *tmpNetAddress = 0, *tmpBroadcast = 0; // used as temporary copies
  unsigned char n = 0; // to be used as counter

  subnetMask = (unsigned char *)malloc(sizeof(unsigned char) * 4);
  netAddress = (unsigned char *)malloc(sizeof(unsigned char) * 4);

  /* print general information */
  printf("\n%s v%s copyright (c) %s by %s (%s)", PROGRAMM_NAME, PROGRAMM_VERSION, YEAR, AUTHOR, EMAIL);
  printf("\nThis programm is a free software, it's published under the terms of the GPL.\n");

  /* get the users data */
  printf("\nEnter your subnet-mask in following form: XXX.XXX.XXX.XXX\n");
  scanf("%i.%i.%i.%i", &subnetMask[0], &subnetMask[1], &subnetMask[2], &subnetMask[3]);
  fflush(stdin);
  printf("\nEnter your network-address in following form: XXX.XXX.XXX.XXX\n");
  scanf("%i.%i.%i.%i", &netAddress[0], &netAddress[1], &netAddress[2], &netAddress[3]);
  fflush(stdin);
  printf("\nHow many subnets do you want to create?\n");
  scanf("%i", &numberOfSubnetsProposition);
  fflush(stdin);

  /* prints the result */
  if(validateClassifiedSubnetMask(subnetMask) && numberOfSubnetsProposition <= 128)
  {
    printf("\n\ninput:\n");

    printf("network-address:\t");
    printIpAddress(netAddress);

    printf("\nsubnet-mask:\t\t");
    printIpAddress(subnetMask);

    numberOfSubnets = calcNumberOfSubnets(numberOfSubnetsProposition);
    printf("\nsubnets to build:\t%i (=%i)", numberOfSubnetsProposition, numberOfSubnets);
    printf("\n\n");

    printf("Output:");

    printf("\nsubnet-mask:\t\t");
    subnetMask = buildSubnetMask(numberOfSubnets, subnetMask);
    printIpAddress(subnetMask);

    printf("\nhosts per subnet:\t");
    printf("%i", getNumberOfHosts(subnetMask, numberOfSubnets));

    printf("\n");

    for(; n < numberOfSubnets; n++)
    {
      printf("\n-network %i", n + 1);

      printf("\n  network-address:\t");
      tmpNetAddress = buildNetAddress(n, numberOfSubnets, netAddress, subnetMask);
      printIpAddress(tmpNetAddress);

      printf("\n  broadcast-address:\t");
      tmpBroadcast = buildBroadcast(n, numberOfSubnets, tmpNetAddress, subnetMask);
      printIpAddress(tmpBroadcast);

      if(numberOfSubnets < 128)
      {
        printf("\n  first host-adress:\t");
        tmpBroadcast = buildBroadcast(n, numberOfSubnets, tmpNetAddress, subnetMask);
        tmpNetAddress[3]++;
        printIpAddress(tmpNetAddress);

        printf("\n  last host-adress:\t");
        tmpBroadcast[3]--;
        printIpAddress(tmpBroadcast);
      }

      printf("\n");
    }
  }
  else
    printf("\n\nInvalid data entered, please retry with other values!");
  printf("\n");

  return 0;
}

/*
 *  validates the classified subnet-mask
 *  valid values: 0.0.0.0, 255.0.0.0, 255.255.0.0, 255.255.255.0 or 255, 255, 255, 255
 *
 *  params:
 *  subnetMask: the subnet-mask to test
 *
 *  return value:
 *  0: invalid subnet-mask
 *  1: valid subnet-mask
 */
_Bool validateClassifiedSubnetMask(unsigned char *subnetMask)
{
  _Bool valid = 1; // the return value
  _Bool zeroFound = 0; // was there already a 0?
  unsigned char n = 0; // to be used as counter

  for(; n < 4; n++)
  {
    if(subnetMask[n] == 255)
    {
      if(zeroFound)
      {
        /* after the first zero, there must be zeros only */
        valid = 0;
        break;
      }
    }
    else if(!subnetMask[n])
      zeroFound = 1;
    else
    {
      /* classified subnet-masks can only have 0 or 255 segment-values */
      valid = 0;
      break;
    }
  }

  return valid;
}

/*
 *  calculates a realisable number of subnets
 *
 *  params:
 *  proposition: the user's proposition
 *
 *  return value:
 *  0: no subnets to build
 *  or: the number of subnets to build (2 <= 2^x <= 128)
 */
unsigned char calcNumberOfSubnets(unsigned char proposition)
{
  unsigned char numberOfSubnets = 0; // the return value
  unsigned char tmpExp = 0; // temporary exponent

  if(proposition >= 2)
  {
    do
    {
      tmpExp++;
      numberOfSubnets = exp2(tmpExp);
    } while(numberOfSubnets <= 128 && numberOfSubnets < proposition);
  }
  return numberOfSubnets;
}

/*
 *  builds the new subnet-mask
 *
 *  params:
 *  numberOfSubnets: the number of subnets (must be calculated by calcNumberOfSubnets()) to be build
 *  oldSubnetMask: the old mask (must be validated by validateClassifiedSubnetMask()) used to build the new one
 *
 *  return value:
 *  a pointer on an array with the built subnet-mask
 */
unsigned char *buildSubnetMask(unsigned char numberOfSubnets, unsigned char *oldSubnetMask)
{
  unsigned char *newSubnetMask = 0; // the return value
  unsigned char n = 0; // to be used as counter
  _Bool done = 0; // is the significant segment calculated already?

  newSubnetMask = (unsigned char *)malloc(sizeof(unsigned char) * 4);
  for(; n < 4; n++)
  {
    if(oldSubnetMask[n] == 255)
      newSubnetMask[n] = oldSubnetMask[n];
    else if(!done && !oldSubnetMask[n])
    {
      newSubnetMask[n] = 256 - 256 / numberOfSubnets;
      done = 1;
    }
    else
      newSubnetMask[n] = 0;
  }
  return newSubnetMask;
}

/*
 *  prints an ip-address in its usual form
 *
 *  params:
 *  address: the ip-address to be printed on screen
 *
 */
void printIpAddress(unsigned char *address)
{
  unsigned char n = 0; // to be used as counter

  for(; n < 4; n++)
  {
    /* alignment */
    if(address[n] < 100)
    {
      printf(" ");
      if(address[n] < 10)
        printf(" ");
    }

    printf("%i", address[n]);

    /* no point at the end */
    if(n != 3)
      printf(".");
  }
}

/*
 *  builds the network-address for the actual subnet
 *
 *  params:
 *  tmpSubnet: the number of the actual subnet (zero-based!)
 *  numberOfSubnets: the number of all subnets
 *  oldNetAddress: the networt-address for the unsplittet network (will be the net-address of the first subnet!)
 *  subnetMask: the calculated subnet-mask (used to find the significant segment)
 *
 *  return value:
 *  a new network-address for the subnet
 */
 unsigned char *buildNetAddress(unsigned char tmpSubnet, unsigned char numberOfSubnets, unsigned char *oldNetAddress,
                                unsigned char *subnetMask)
{
  unsigned char *newNetAddress = 0; // the return value
  unsigned char sign = 0; // the significant segment
  unsigned char n = 0; // to be used as counter

  newNetAddress = (unsigned char *)malloc(sizeof(unsigned char) * 4);

  /* find the significant segment */
  for(; n < 4; n++)
  {
    if(subnetMask[n] != 255 && subnetMask[n])
    {
      sign = n;
      break;
    }
  }

  /* copy the old network-address */
  for(n = 0; n < 4; n++)
    newNetAddress[n] = oldNetAddress[n];

  newNetAddress[sign] = tmpSubnet * 256 / numberOfSubnets; // calculate the significant segment
  return newNetAddress;
}

/*
 *  builds the broadcast-address for the actual subnet
 *
 *  params:
 *  tmpSubnet: the number of the actual subnet (zero-based!)
 *  numberOfSubnets: the number of all subnets
 *  netAddress: the networt-address for the actual subnet
 *  subnetMask: the calculated subnet-mask (used to find the significant segment)
 */
unsigned char *buildBroadcast(unsigned char tmpSubnet, unsigned char numberOfSubnets, unsigned char *netAddress,
                              unsigned char *subnetMask)
{
  unsigned char *broadcast = 0; // the return value
  unsigned char sign = 0; // the significant segment
  unsigned char n = 0; // to be used as counter

  broadcast = (unsigned char *)malloc(sizeof(unsigned char) * 4);

  /* find the significant segment */
  for(; n < 4; n++)
  {
    if(subnetMask[n] != 255 && subnetMask[n])
    {
      sign = n;
      break;
    }
  }

  /* create the broadcast-address */
  for(n = 0; n < 4; n++)
  {
    if(n < sign)
      broadcast[n] = netAddress[n];
    else if(n == sign)
      broadcast[n] = ((tmpSubnet + 1) * 256 / numberOfSubnets) - 1; // calculate the significant segment
    else
      broadcast[n] = 255;
  }

  return broadcast;
}

/*
 * calculates the number of Hosts for one subnet
 *
 * params:
 * subnetMask: the calculated subnet-mask
 * numberOfSubnets: the number of subnets to be build
 *
 * return value:
 * the possible number of host for one subnet
 */
unsigned int getNumberOfHosts(unsigned char *subnetMask, unsigned char numberOfSubnets)
{
  unsigned int numberOfHosts = 0; // the return value
  unsigned char n = 0; // to be used as counter

  numberOfHosts = 256 / numberOfSubnets;
  for(; n < 4; n++)
  {
    if(!subnetMask[n])
      numberOfHosts *= 256;
  }
  numberOfHosts -= 2; // 2: one for network-address, one for broadcast-address

  return numberOfHosts;
}


Lux
Junior-Member
Beitrag vom:
25-10-2005, 18:13:53

Schreibt man Supernetting nicht mit 2 t ;-)

sieht aber gut aus....

-----------------------------------------------------


[back to top]



Userdaten
User nicht eingeloggt

Gesamtranking
Werbung
Datenbankstand
Autoren:04508
Artikel:00815
Glossar:04116
News:13565
Userbeiträge:16552
Queueeinträge:06246
News Umfrage
Ihre Anforderungen an ein Online-Zeiterfassungs-Produkt?
Mobile Nutzung möglich (Ipone, Android)
Externe API Schnittstelle/Plugins dritter
Zeiterfassung meiner Mitarbeiter
Exportieren in CSV/XLS
Siehe Kommentar



[Results] | [Archiv] Votes: 1154
Comments: 0