|
|||||||||||||||
|
Re: linux-ipsec: apps/utils/libs that parse IP/netbits
From: Henry Spencer <henry(at)spsystems.net>
X continue;
X if (n <= ABITS) {
X n = ABITS - n;
X buf[0] = '0' + n/10;
X buf[1] = '0' + n%10;
X buf[2] = '\0';
X s = buf;
X } else
X s = inet_ntoa(mask);
X
X while ((c = *s++) != '\0') {
X if (dst >= stop)
X return -1;
X *dst++ = c;
X }
X if (dst >= stop)
X return -1;
X *dst++ = '\0';
X return 0;
Date: Wed Jul 15 1998 - 18:09:54 EDT
Try the enclosed. I just wrote it, as a first contribution to a set of utility routines for our user interface. No manpage yet -- let me know if you can't figure it out. It provides network address and mask, with the non-mask bits in the network address forced to 0. It does depend on a couple of existing library routines; that dependency can be removed if it proves awkward. If you compile with -DATOSUBNET_MAIN, it becomes a self-contained test program.
Henry Spencer
henry@spsystems.net
(henry@zoo.toronto.edu)
#! /bin/sh
X#includeX X/* X - atosubnet - convert ASCII "addr/mask" to address and mask X * Mask can be either dotted quad or integer bit count. X * Results are in network byte order. X */ Xint /* 0 success, -1 failure */ Xatosubnet(s, addrp, maskp) Xconst char *s; Xstruct in_addr *addrp; Xstruct in_addr *maskp; X{
X char *slash;
X char *mask;
X unsigned long m;
X int n;
X
X slash = strchr(s, '/');
X if (slash == NULL)
X return -1;
X mask = slash + 1;
X *slash = '\0';
X
X if (inet_aton(s, addrp) == 0)
X return -1;
X
X if (strchr(mask, '.') != NULL) { /* mask is dotted quad too */
X if (inet_aton(mask, maskp) == 0)
X return -1;
X addrp->s_addr &= maskp->s_addr;
X return 0;
X }
X
X /* uh-oh, mask must be a bit count */
X if (*(mask + strspn(mask, "1234567890")) != '\0')
X return -1;
X n = atoi(mask);
X if (n > ABITS)
X return -1;
X m = 0;
X m--; /* all 1s */
X m <<= ABITS - n;
X maskp->s_addr = htonl(m);
X addrp->s_addr &= maskp->s_addr;
X
X return 0;
X} X X X X/* X - subnettoa - convert address and mask to ASCII "addr/mask" X * Inputs are in network byte order. Note that they're structs, not X * pointers to structs. X * Output expresses the mask as a bit count if possible, else dotted quad. X * It is an error for there not to be enough room. X */ Xint /* 0 success, -1 failure */ Xsubnettoa(addr, mask, dst, dstlen) Xstruct in_addr addr; Xstruct in_addr mask;
Xchar *dst; /* where to put the ASCII */
Xsize_t dstlen; /* how big dst is */
X{
X unsigned long m;
X int n;
X char *s;
X char c;
X char *stop = dst + dstlen;
X char buf[5];
X
X s = inet_ntoa(addr);
X while ((c = *s++) != '\0') {
X if (dst >= stop)
X return -1;
X *dst++ = c;
X }
X if (dst >= stop)
X return -1;
X *dst++ = '/';
X
X m = 0;
X m--; /* all 1s */
X for (n = 0; n <= ABITS && htonl(m<X} X X X X#ifdef ATOSUBNET_MAIN X X#include <stdio.h> X Xint Xmain(argc, argv) Xint argc; Xchar *argv[]; X{
X struct in_addr a;
X struct in_addr m;
X char buf[100];
X
X if (argc < 2) {
X fprintf(stderr, "Usage: %s addr/mask\n", argv[0]);
X exit(2);
X }
X
X if (atosubnet(argv[1], &a, &m) < 0) {
X fprintf(stderr, "%s: conversion failed\n", argv[0]);
X exit(1);
X }
X if (subnettoa(a, m, buf, sizeof(buf)) < 0) {
X fprintf(stderr, "%s: reverse conversion failed: ", argv[0]);
X fprintf(stderr, "%s/", inet_ntoa(a));
X fprintf(stderr, "%s\n", inet_ntoa(m));
X exit(1);
X }
X printf("%s\n", buf);
X
X exit(0);
X} X X#endif /* ATOSUBNET_MAIN */ ! echo done Received on Wed Jul 15 18:13:39 1998 This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 12:59:24 EDT |
||||||||||||||
|
|||||||||||||||