[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: netcfg



  Regarding ip calculations.

The main formula for ip calculations is:
  nna = nip & nsm;  /* numeric network addr.   = numeric ip bitwise and                    numeric subnetmask */
  nbc = nip | ~nsm; /*  -"-    broadcast addr. =      -"-     -"-   or  bitwise inverse of   -"-     -"-      */

You can try my ipcalc (source below).

$ ~/sw/prg/c/ipcalc
Usage:
        /home/karl/sw/prg/c/ipcalc [opt] ip.nr/netmask
        /home/karl/sw/prg/c/ipcalc [opt] ip.nr subnetmask
Synopsis:
        without [opt], will print out the network address, ip.nr, broadcast address, subnet mask and cidr
        opt = -i, used with ifconfig
        opt = -r, used with route
Examples:
        $ /home/karl/sw/prg/c/ipcalc 192.168.3.34/24
        192.168.3.0 192.168.3.255
        $ /home/karl/sw/prg/c/ipcalc 192.168.3.34 255.255.255.0
        192.168.3.0 192.168.3.255
        # ifconfig eth0 `/home/karl/sw/prg/c/ipcalc -i 192.263.3.35/27`
        # route add `/home/karl/sw/prg/c/ipcalc -i 192.263.3.35/27`
$ ~/sw/prg/c/ipcalc 192.168.93.22 255.255.255.224
192.168.93.0 192.168.93.22 192.168.93.31 255.255.255.224 27
$ ~/sw/prg/c/ipcalc 192.168.93.22/27
192.168.93.0 192.168.93.22 192.168.93.31 255.255.255.224 27

# ~karl/sw/prg/c/ipcalc -i 192.168.92.24/24
192.168.92.24 netmask 255.255.255.0 broadcast 192.168.92.255
# ~karl/sw/prg/c/ipcalc -r 192.168.92.24/25
-net 192.168.92.0 netmask 255.255.255.128
# ifconfig eth0 `~karl/sw/prg/c/ipcalc -i 192.168.92.24/25`
# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:C0:F0:57:BE:82  
          inet addr:192.168.92.24  Bcast:192.168.92.127  Mask:255.255.255.128
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          Interrupt:9 Base address:0xa700 

#

Regards,
/Karl

-----------------------------------------------------------------------
Karl Hammar                    Aspö Data           karl@kalle.csb.ki.se
Lilla Aspö 2340             +46  173 140 57                    Networks
S-742 94 Östhammar         +46  70 511 97 84                  Computers
Sweden                                                       Consulting
-----------------------------------------------------------------------


From: Joey Hess <joeyh@debian.org>
Subject: Re: netcfg
Date: Thu, 7 Dec 2000 21:45:57 -0800

> David Whedon wrote:
> > I'm starting to look at network configuration.  I'm using dbootstrap/netconfig.c
> > as a guide, debconf'ing the messages to start.
> 
> I hope you're not actually copying the code <twitch>, just the flow.
> 
> I have a little debconf C client library that makes it fairly painless
> to telk to debconf from C, you can use things like this:
> 
> debconf_command("TITLE", "This is my little peice of debian-installer", NULL);
> debconf_command("INPUT", "medium", "foo/bar", NULL);
> debconf_command("GO", NULL);
> debconf_command("GET", "foo/bar", NULL);
> if (strcmp(debconf_ret, "true") == 0) {
> 	/* yes, do foo/bar */
> }
> 
> debconf.{c,h} are currently scattered accross the d-i tree; I intend to
> move them into a library that we can link to (shared/static I dunno) soon,
> as the current situation is ridiculous.
> 
> > I figure this is a good place
> > to start.  Other than autodetection and giving the user the option of answering
> > fewer questions, is there anything new people are looking for in the network
> > setup?  Anything from the current installer that was a problem in the past?
> 
> It's always seemed pretty well done to me. I like how it calculates the
> third value from the first two. Anyone who understands networking or has
> the appropriate numbers on paper can use it pretty well.
> 
> We might want to toss in a dotted-quad data type into cdebconf (and
> perhaps debconf itself too) to facilitate entering IPs, I don't really
> know.
> 
> > In any case, I'm sort of claiming the network setup,
> 
> Great. It definitly needs to be done soon.
> 
> Are you going to do just the manual setup, leave dhcp for later? They
> should be seperate packages unless they wind up sharing tons of code and
> being rather small.
> 
> -- 
> see shy jo
> 
> 
> -- 
> To UNSUBSCRIBE, email to debian-boot-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

/** Copyright: Karl Hammar, Aspö Data
 ** Copyright terms: GPL
 **/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
/*typedef unsigned long long u64;*/


int Usage(char *cmd);
int parse1( u32 *nip, u32 *nsm, int *cidr, char *str);
int parse2( u32 *nip, u32 *nsm, int *cidr, char *ip, char *sm);
int mksm( u32 *sm, long int cidr);
char * dot2num( u32 *num, char *dot);
void num2dot( u32 num, char *dot);


u32 tab[] = {
  0x00000000,			/* 0 */
  0x80000000,			/* 1 */
  0xC0000000,			/* 2 */
  0xE0000000,			/* 3 */
  0xF0000000,			/* 4 */
  0xF8000000,			/* 5 */
  0xFC000000,			/* 6 */
  0xFE000000,			/* 7 */
  0xFF000000,			/* 8 */
  0xFF800000,			/* 9 */
  0xFFC00000,			/* 10 */
  0xFFE00000,			/* 11 */
  0xFFF00000,			/* 12 */
  0xFFF80000,			/* 13 */
  0xFFFC0000,			/* 14 */
  0xFFFE0000,			/* 15 */
  0xFFFF0000,			/* 16 */
  0xFFFF8000,			/* 17 */
  0xFFFFC000,			/* 18 */
  0xFFFFE000,			/* 19 */
  0xFFFFF000,			/* 20 */
  0xFFFFF800,			/* 21 */
  0xFFFFFC00,			/* 22 */
  0xFFFFFE00,			/* 23 */
  0xFFFFFF00,			/* 24 */
  0xFFFFFF80,			/* 25 */
  0xFFFFFFC0,			/* 26 */
  0xFFFFFFE0,			/* 27 */
  0xFFFFFFF0,			/* 28 */
  0xFFFFFFF8,			/* 29 */
  0xFFFFFFFC,			/* 30 */
  0xFFFFFFFE,			/* 31 */
  0xFFFFFFFF			/* 32 */
};

int Usage(char *cmd) {
  printf("Usage:\n");
  printf("	%s [opt] ip.nr/netmask\n", cmd);
  printf("	%s [opt] ip.nr subnetmask\n", cmd);
  printf("Synopsis:\n");
  printf("	without [opt], will print out the network address, ip.nr, broadcast address, subnet mask and cidr\n");
  printf("	opt = -i, used with ifconfig\n");
  printf("	opt = -r, used with route\n");
  printf("Examples:\n");
  printf("	$ %s 192.168.3.34/24\n", cmd);
  printf("	192.168.3.0 192.168.3.255\n");
  printf("	$ %s 192.168.3.34 255.255.255.0\n", cmd);
  printf("	192.168.3.0 192.168.3.255\n");
  printf("	# ifconfig eth0 `%s -i 192.263.3.35/27`\n", cmd);
  printf("	# route add `%s -i 192.263.3.35/27`\n", cmd);
  return -1;
}

int main(int argc, char *argv[]) {
  char *cmd = argv[0];
  int opt = '\0';
  int status;
  u32 nip, nsm, nna, nbc;
  int cidr;
  char ip[16];
  char sm[16];
  char na[16];
  char bc[16];

  argc--; argv++;		/* shift out cmd */
  if (argc == 0) return Usage(cmd);

  if (argv[0][0] == '-') {
    opt = argv[0][1];
    if (!strchr("ir", opt)) return Usage(cmd);
    if (argv[0][2] != '\0') return Usage(cmd);
    argc--; argv++; /* shift out option */
  }

  if (argc == 1) {
    status = parse1( &nip, &nsm, &cidr, argv[0]);
  } else if (argc == 2) {
    status = parse2( &nip, &nsm, &cidr, argv[0], argv[1]);
  } else return Usage(cmd);

  if (status) return Usage(cmd);
  nna = nip & nsm;
  nbc = nip | ~nsm;

  num2dot( nip, ip);
  num2dot( nsm, sm);
  num2dot( nna, na);
  num2dot( nbc, bc);

  switch (opt) {
  case 'i':
    printf("%s netmask %s broadcast %s\n", ip, sm, bc);
    break;
  case 'r':
    printf("-net %s netmask %s\n", na, sm);
    break;
  case 'h':
    printf("%x %x %x %x\n", nip, nsm, nna, nbc);
    break;
  default:
    printf("%s %s %s %s %d\n", na, ip, bc, sm, cidr);
  }
  return 0;
}

int parse1( u32 *nip, u32 *nsm, int *cidr, char *str) {
  char *p;
  char *e;
  long int val;

  p = dot2num( nip, str);
  if (!p) return -1;
  if (*p != '/') return -1;
  if (*p == '\0') return -1;
  p++;

  val = strtoul( p, &e, 10);
  if (*e != '\0') return -1;

  if (mksm( nsm, val)) return -1;
  *cidr = val;
  return 0;
}

int parse2( u32 *nip, u32 *nsm, int *cidr, char *ip, char *sm) {
  char *p;
  int ix;

  p = dot2num( nip, ip);
  if (!p || *p != '\0') return -1;

  p = dot2num( nsm, sm);
  if (!p || *p != '\0') return -1;

  for (ix = 0; ix < 33; ix++) {
    if (tab[ix] == *nsm) {
      *cidr = ix;
      return 0;
    }
  }

  return -1;
}

int mksm( u32 *sm, long int cidr) {
  if (0 <= cidr && cidr <= 32) {
    *sm = tab[cidr];
    return 0;
  } else {
    return -1;
  }
}

char * dot2num( u32 *num, char *dot) {
  char *p = dot - 1;
  char *e;
  int ix;
  unsigned long val;

  if (!dot) return NULL;
  *num = 0;
  for (ix = 0; ix < 4; ix++) {
    *num <<= 8;
    p++;
    val = strtoul( p, &e, 10);
    if (e == p) val = 0;
    else if (val > 255) return NULL;
    *num += val;
    /*printf("%#8x, %#2x\n", *num, val);*/
    if (ix < 3 && *e != '.') return NULL;
    p = e;
  }

  return p;
}

void num2dot( u32 num, char *dot) {
  /* dot MUST point to an char arr[16] or longer area */
  int byte[4];
  int ix;

  if (!dot) return;
  for (ix = 3; ix >=0; ix--) {
    byte[ix] = num & 0xff;
    num >>= 8;
  }
  sprintf( dot, "%d.%d.%d.%d", byte[0], byte[1], byte[2], byte[3] );
}



Reply to: