zone V1.0

This commit is contained in:
akool 1999-06-18 12:41:57 +00:00
parent 3f89db0f48
commit cab9c9994f
14 changed files with 8922 additions and 6286 deletions

View File

@ -1,4 +1,4 @@
/* $Id: zone.c,v 1.3 1999/06/15 20:05:25 akool Exp $
/* $Id: zone.c,v 1.4 1999/06/18 12:41:57 akool Exp $
*
* Zonenberechnung
*
@ -19,6 +19,9 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Log: zone.c,v $
* Revision 1.4 1999/06/18 12:41:57 akool
* zone V1.0
*
* Revision 1.3 1999/06/15 20:05:25 akool
* isdnlog Version 3.33
* - big step in using the new zone files
@ -38,7 +41,11 @@
* deinitialize
*
* int getZone(int provider, char *from, char *to)
* returns zone for provider, -1 on not found, -2 on error
* returns zone for provider, UNKNOWN on not found, -2 on error
*
* int getAreacode(int provider, char *num, char **text)
* returns len of areacode in num and in text a malloced string
* UNKNOWN on not found
*
*/
@ -64,11 +71,14 @@ extern const char *basename (const char *name);
#include "isdnlog.h"
#include "tools.h"
#endif
#include <gdbm.h>
#include "zone.h"
/* this config (from config.in) could go in global policy */
#include "zone/config.h"
#include "zone/common.h"
struct sth {
GDBM_FILE fh;
_DB fh;
char *path;
int provider;
int used;
@ -76,20 +86,14 @@ struct sth {
int *table;
char pack_key, pack_table;
int table_size;
int oz;
int numlen;
} ;
typedef unsigned char UC; /* len 1 */
typedef unsigned short US; /* len 2 */
typedef unsigned long UL; /* len 4 */
typedef enum {false,true} bool;
#ifndef min
#define min(a,b) (a) < (b) ? (a) : (b)
#endif
static struct sth *sthp;
static int count;
static char version[] = "0.92";
static char version[] = "1.00";
#define LINK 127
#define INFO_LEN 80
#define LENGTH 120
@ -129,7 +133,7 @@ void exitZone(int provider)
sthp[i].path = 0;
free(sthp[i].table);
sthp[i].table = 0;
gdbm_close(sthp[i].fh);
CLOSE(sthp[i].fh);
sthp[i].fh = 0;
if (i == count-1) /* last released ? */
count--;
@ -203,11 +207,15 @@ int initZone(int provider, char *path, char **msg)
sthp[ocount].used=0;
sthp[ocount].fh=0;
sthp[ocount].real = -1;
sthp[ocount].oz=1;
sthp[ocount].numlen=0;
/* now search for same path */
found = false;
for (i=0; i<count-1; i++) {
if (sthp[i].path && strcmp(sthp[i].path, path) == 0) {
sthp[ocount].fh = sthp[i].fh;
sthp[ocount].oz = sthp[i].oz;
sthp[ocount].numlen = sthp[i].numlen;
sthp[ocount].pack_key = sthp[i].pack_key;
sthp[ocount].pack_table = sthp[i].pack_table;
sthp[ocount].table = sthp[i].table;
@ -231,17 +239,17 @@ int initZone(int provider, char *path, char **msg)
"Zone V%s: Error: Out of mem 2", version);
return -1;
}
if((sthp[ocount].fh = gdbm_open(path, 0, GDBM_READER, 0, 0)) == 0) {
if((sthp[ocount].fh = OPEN(path, READ)) == 0) {
if (msg)
snprintf (message, LENGTH,
"Zone V%s: Error: gdbm_open '%s': '%s'",
version, path, gdbm_strerror(gdbm_errno));
version, path, GET_ERR);
return -1;
}
/* read info */
key.dptr = vinfo;
key.dsize = 7;
value = gdbm_fetch(sthp[ocount].fh, key);
value = FETCH(sthp[ocount].fh, key);
if (value.dptr == 0) {
if (msg)
snprintf (message, LENGTH,
@ -256,6 +264,14 @@ int initZone(int provider, char *path, char **msg)
for (p++,n=0,q=dversion; n<6 && *p != ' '; n++)
*q++ = *p++;
*q = '\0';
if (*dversion != *version) {
if (msg)
snprintf (message, LENGTH,
"Zone V%s: Error: Provider %d File '%s': incompatible Dataversion %s",
version, provider, path, dversion);
exitZone(provider);
return -1;
}
break;
case 'K' :
sthp[ocount].pack_key = *(++p);
@ -271,6 +287,14 @@ int initZone(int provider, char *path, char **msg)
p++;
tsize = strtol(p, &p, 10);
break;
case 'O' :
p++;
sthp[ocount].oz = strtol(p, &p, 10);
break;
case 'L' :
p++;
sthp[ocount].numlen = strtol(p, &p, 10);
break;
}
} /* for */
free (value.dptr);
@ -280,12 +304,13 @@ int initZone(int provider, char *path, char **msg)
strchr("SL", sthp[ocount].pack_key) == 0 ||
sthp[ocount].pack_table == '\x0' ||
strchr("CSL", sthp[ocount].pack_table) == 0 ||
sthp[ocount].numlen == 0 ||
csize == 0 ||
tsize == 0) {
if (msg)
snprintf (message, LENGTH,
"Zone V%s: Error: Provider %d File '%s' seems to be corrupted",
version, provider, path);
"Zone V%s: Error: Provider %d File '%s' seems to be corrupted:\n%s",
version, provider, path, value.dptr);
exitZone(provider);
return -1;
}
@ -303,7 +328,7 @@ int initZone(int provider, char *path, char **msg)
}
key.dptr = table;
key.dsize = 7;
value = gdbm_fetch(sthp[ocount].fh, key);
value = FETCH(sthp[ocount].fh, key);
if (value.dptr == 0) {
if (msg)
snprintf (message, LENGTH,
@ -321,10 +346,10 @@ int initZone(int provider, char *path, char **msg)
if (msg) {
snprintf (message, LENGTH,
"Zone V%s: Provider %d File '%s' opened fine - "
"V%s K%d C%d N%d T%d",
"V%s K%d C%d N%d T%d O%d L%d",
version, provider, path,
dversion, sthp[ocount].pack_key, sthp[ocount].pack_table,
csize, tsize);
csize, tsize, sthp[ocount].oz, sthp[ocount].numlen);
}
}
else {
@ -342,8 +367,11 @@ static int _getZ(struct sth *sthp, char *from, char *sto) {
static char newfrom[LENGTH];
bool found = false;
char *temp;
int res;
if (strcmp(from, sto) > 0) {
if ((res=strcmp(from, sto)) == 0)
return sthp->oz;
else if (res > 0) {
temp=from;
from=sto;
sto=temp;
@ -360,14 +388,16 @@ static int _getZ(struct sth *sthp, char *from, char *sto) {
key.dptr = (char *) &lifrom;
key.dsize = sizeof(UL);
}
value = gdbm_fetch(fh, key);
value = FETCH(fh, key);
if (value.dptr) {
char *p = value.dptr;
char to[10];
US count;
int ito;
unsigned char z=0;
/* here is since 1.00 a zero-terminated strring */
while (*p++);
/* skipped */
count = *((US*)p)++;
while (count--) {
bool ind = true;
@ -401,7 +431,7 @@ static int _getZ(struct sth *sthp, char *from, char *sto) {
} /* if dptr */
newfrom[strlen(newfrom)-1] = '\0';
}
return -1;
return UNKNOWN;
}
int getZone(int provider, char *from, char *to)
@ -418,8 +448,57 @@ int getZone(int provider, char *from, char *to)
return UNKNOWN;
}
static int _getAreacode(struct sth *sthp, char *from, char **text) {
_DB fh = sthp->fh;
datum key, value;
static char newfrom[LENGTH];
bool found = false;
int len;
strncpy(newfrom, from, sthp->numlen);
newfrom[sthp->numlen] = '\0';
while ((len=strlen(newfrom))) {
UL lifrom = (UL) atol(newfrom); /* keys could be long */
US ifrom = (US) lifrom;
if (sthp->pack_key == 2) {
key.dptr = (char *) &ifrom;
key.dsize = sizeof(US);
}
else {
key.dptr = (char *) &lifrom;
key.dsize = sizeof(UL);
}
value = FETCH(fh, key);
if (value.dptr) {
if (!*value.dptr) {/* found shortcut */
if (*dbv == 'G') /* GDBM has a malloced string in dptr */
free(value.dptr);
return UNKNOWN;
}
if (*dbv == 'G') /* GDBM has a malloced string in dptr */
*text = value.dptr;
else
*text = strdup(value.dptr);
return len;
} /* if dptr */
newfrom[strlen(newfrom)-1] = '\0';
}
return UNKNOWN;
}
#ifdef ZONETEST
int getAreacode(int provider, char *from, char **text)
{
int i;
char *path=NULL;
for (i=0; i<count; i++)
if (sthp[i].provider == provider) {
path = sthp[i].path ? sthp[i].path : sthp[sthp[i].real].path;
if (sthp[i].fh == 0)
return UNKNOWN;
return _getAreacode(&sthp[i], from, text);
}
}
#ifdef STANDALONE
static int checkZone(char *zf, char* df,int num1,int num2, bool verbose)
{
@ -431,11 +510,12 @@ static int checkZone(char *zf, char* df,int num1,int num2, bool verbose)
fprintf(stderr,"%s\n", msg);
exit(1);
}
if(verbose)
printf("%s\n", msg);
if (num1 && num2) {
snprintf(from, 9, "%d",num1);
snprintf(to, 9, "%d",num2);
ret = getZone(1, from, to);
if (verbose)
printf("%s %s = %d\n", from, to, ret);
}
else {
@ -482,12 +562,33 @@ static int checkZone(char *zf, char* df,int num1,int num2, bool verbose)
}
fclose(fp);
if (verbose)
printf("'%s' verified ok.\n", df);
printf("'%s' verified %s.\n", df, ret==0? "Ok": "NoNo");
}
exitZone(1);
return ret;
}
static int checkArea(char *df, char *from, int verbose) {
char *msg, *text;
int ret=0;
if (initZone(1, df, &msg)) {
fprintf(stderr,"%s\n", msg);
exit(1);
}
if(verbose)
printf("%s\n", msg);
ret = getAreacode(1, from, &text);
if(ret != UNKNOWN) {
printf("%s:%d '%s'\n", from, ret, text);
free(text);
}
else
printf("%s - UNKNOWN\n", from);
exitZone(1);
return ret;
}
int main (int argc, char *argv[])
{
int verbose=false;
@ -495,6 +596,7 @@ int main (int argc, char *argv[])
char *zf=0;
int c;
int num1=0, num2=0;
char snum1[LENGTH];
while ( (c=getopt(argc, argv, "vVd:z:")) != EOF) {
switch (c) {
case 'v' : verbose = true; break;
@ -506,6 +608,7 @@ int main (int argc, char *argv[])
while (optind < argc) {
if (!num1 && isdigit(*argv[optind])) {
num1 = atoi(argv[optind]);
strncpy(snum1, argv[optind], LENGTH);
optind++;
continue;
}
@ -518,7 +621,10 @@ int main (int argc, char *argv[])
}
if (df && (zf || (num1 && num2)))
return checkZone(zf, df, num1, num2, verbose);
if (df && num1)
return checkArea(df, snum1, verbose);
fprintf(stderr, "Usage:\n%s -d DBfile -v -V { -z Zonefile | num1 num2 }\n", basename(argv[0]));
fprintf(stderr, "\t-d DBfile -v -V num1\n");
return 0;
}
#endif

View File

@ -0,0 +1,81 @@
#
# Makefile for mkzonedb (borrowed from iMON)
#
# (C) 1999 l.toetsch <lt@toetsch.at>
#
SHELL = /bin/sh
CFLAGS = -Wall -O2
INCLUDES =
LDFLAGS = @DBMLIB@
DBEXT = @DBEXT@
RDBEXT = @RDBEXT@
PROGRAM = mkzonedb
MODULES = $(PROGRAM).o
MANPAGE = $(PROGRAM).8
HEADERS = config.h common.h
MANDIR = @CONFIG_MANDIR@
SBINDIR = @CONFIG_SBINDIR@
MAN8DIR = $(MANDIR)/man8
INSTALL = @INSTALL@
INSTALL_PROGRAM = $(INSTALL) -o 0 -g 0 -m 0750
INSTALL_MAN = $(INSTALL) -o 0 -g 0 -m 0644
prefix = @prefix@
exec_prefix = @exec_prefix@
CC = @CC@
.SUFFIXES:
.SUFFIXES: .c .o
%.o: %.c Makefile
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
all: $(PROGRAM) zone
config:
@./configure
config.h: config.h.in
@./configure
Makefile: Makefile.in config.status
./config.status
config.status: configure
./config.status --recheck
$(MODULES): $(HEADERS)
$(PROGRAM): $(MODULES)
$(CC) $(CFLAGS) $? $(LDFLAGS) -o $@
zone: ../zone.c $(HEADERS) Makefile
$(CC) -DSTANDALONE -g ../zone.c $(LDFLAGS) -o $@
install-man: $(PROGRAM).man
mkdir -p $(DESTDIR)$(MAN8DIR)
$(INSTALL_MAN) $< $(DESTDIR)$(MAN8DIR)/$(MANPAGE)
install: $(PROGRAM) install-man
$(INSTALL_PROGRAM) $(PROGRAM) $(DESTDIR)$(SBINDIR)/$(PROGRAM)
install-strip: $(PROGRAM)
$(INSTALL_PROGRAM) -s $(PROGRAM) $(DESTDIR)$(SBINDIR)/$(PROGRAM)
uninstall:
rm -f $(DESTDIR)$(SBINDIR)/$(PROGRAM) $(DESTDIR)$(MAN8DIR)/$(MANPAGE)
clean:
rm -f *.o *~ $(PROGRAM) *.db *.gdbm zone
distclean: clean
rm -f config.h config.status config.cache config.log *.man Makefile
test:
./$(PROGRAM) -d uta$(DBEXT) -r at/uta/zred.uta -c at/code -v
./zone -d uta$(RDBEXT) -z at/uta/zoneall.uta -v
zonefiles:
./$(PROGRAM) -d ../../zone-at-1012$(DBEXT) -r at/1012/zred-at-1012 -c at/code -v -l4
./$(PROGRAM) -d ../../zone-at-uta$(DBEXT) -r at/uta/zred.uta -c at/code -v
./$(PROGRAM) -d ../../zone-at-pta$(DBEXT) -r at/pta/zred.pta -c at/code -v
./$(PROGRAM) -d ../../zone-de-dtag$(DBEXT) -r de/01033/zred.dtag -c de/code -v

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,65 @@
/* mkzonedb.c, zone.c needs this */
#define READ 0
#define WRITE 1
#if HAVE_LIBGDBM
#include <gdbm.h>
static char dbv[]="GDBM"; /* don't change */
#define OPEN(name,wr) gdbm_open(name,0,wr?GDBM_NEWDB:GDBM_READER,0644,0)
#define CLOSE(db) gdbm_close(db)
#define GET_ERR gdbm_strerror(gdbm_errno)
#define STORE(db, key, value) gdbm_store(db, key, value, GDBM_INSERT)
#define FETCH(db, key) gdbm_fetch(db, key)
#define _DB GDBM_FILE
#elif HAVE_LIBDBM
/* this is not tested, I don't have libdbm installed */
#include <ndbm.h> /* Fixme: which include? */
#include <fcntl.h>
static char dbv[]="DBM";
#define OPEN(name,wr) dbm_open(name,wr?O_RDWR|O_CREAT:O_RONLY,0644)
#define CLOSE(db) dbm_close(db)
#define GET_ERR "unknown"
#define STORE(db, key, value) dbm_store(db, key, value, DBM_INSERT)
#define FETCH(db, key) dbm_fetch(db, key)
#define _DB DBM *
#elif HAVE_LIBDB
#if 0
/* there are too mny libdb out there */
#define DB_DBM_HSEARCH
#include <db.h>
#else
#include <ndbm.h>
#endif
#include <fcntl.h>
static char dbv[]="DB";
#define OPEN(name,wr) dbm_open(name,wr?O_RDWR|O_CREAT:O_RONLY,0644)
#define CLOSE(db) dbm_close(db)
#define GET_ERR "unknown"
#define STORE(db, key, value) dbm_store(db, key, value, DBM_INSERT)
#define FETCH(db, key) dbm_fetch(db, key)
#define _DB DB *
#else
Sorry, no database found in configure
#endif
/* if the following doesn't - how can we find datatypes with len 1,2,4 ? */
#if SIZEOF_CHAR != 1
Something is wrong sizeof(char) != 1
#else
typedef unsigned char UC;
#endif
#if SIZEOF_SHORT != 2
Something is wrong sizeof(short) != 2
#else
typedef unsigned short US; /* len 2 */
#endif
#if SIZEOF_LONG != 4
#if SIZEOF_INT != 4
Something is wrong sizeof(long/int) != 4
#else
typedef unsigned int UL; /* len 4 */
#endif
#else
typedef unsigned long UL; /* len 4 */
#endif
typedef enum {false,true} bool;

View File

@ -52,9 +52,12 @@ else {
# verify codefile against orig or reduced zone file
sub get_codes {
open(IN, $codef) || die("Can't read $codef");
my @codes = <IN>;
my (@codes, $num, $ort, $line);
while ($line = <IN>) {
($num, $ort) =split(/\t/, $line);
push(@codes, $num);
}
close(IN);
chomp(@codes);
if($mem && $reducedf) {
&read_compact;
}
@ -121,6 +124,9 @@ sub read_compact {
next if ($key eq "_tAbLe\x0" || $key eq "vErSiO\x0");
my($temp) = unpack($pack_key, $key);
next if( $temp !~ /\d/);
my($ind);
my($ort) = substr($value,0, $ind=index($value, "\x0"));
$value = substr($value, $ind+1);
my($count) = unpack('S', $value);
$value=substr($value, 2); # past count
while ($count--) {

View File

@ -0,0 +1,9 @@
/* configure sets these don't change */
#undef HAVE_LIBGDBM
#undef HAVE_LIBDBM
#undef HAVE_LIBDB
#undef SIZEOF_CHAR
#undef SIZEOF_SHORT
#undef SIZEOF_LONG

1729
isdnlog/tools/zone/configure vendored Executable file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,44 @@
dnl Process this file with autoconf to produce a configure script.
AC_INIT(mkzonedb.c)
AC_CONFIG_HEADER(config.h)
AC_PREFIX_DEFAULT(/usr)
CONFIG_MANDIR=`eval echo ${CONFIG_MANDIR:-"/usr/man"}`
CONFIG_SBINDIR=`eval echo ${CONFIG_SBINDIR:-"/sbin"}`
dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
dnl Checks for libraries.
dnl Berkeley DB appends RDBEXT
AC_CHECK_LIB(gdbm, gdbm_open, DBMLIB=-lgdbm;DBEXT=".gdbm";RDBEXT=".gdbm"; AC_DEFINE(HAVE_LIBGDBM),
AC_CHECK_LIB(dbm, dbm_open, DBMLIB=-ldbm;RDBEXT=".db"; AC_DEFINE(HAVE_LIBDBM),
AC_CHECK_LIB(db, dbm_open, DBMLIB=-ldb;RDBEXT=".db"; AC_DEFINE(HAVE_LIBDB),
AC_MSG_ERROR("No db found"))))
dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h)
dnl Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T
dnl Only 1,2,4 will work by now
AC_CHECK_SIZEOF(char,1)
AC_CHECK_SIZEOF(short,2)
AC_CHECK_SIZEOF(long,4)
AC_CHECK_SIZEOF(int,4)
dnl Checks for library functions.
AC_CHECK_FUNCS(strdup strtoul)
AC_SUBST(DBMLIB)
AC_SUBST(DBEXT)
AC_SUBST(RDBEXT)
AC_SUBST(CC)
AC_SUBST(INSTALL)
AC_SUBST(CONFIG_SBINDIR)
AC_SUBST(CONFIG_MANDIR)
AC_OUTPUT(Makefile)

120
isdnlog/tools/zone/ddb Executable file
View File

@ -0,0 +1,120 @@
#!/usr/bin/perl
# dump database
#
# Copyright 1999 by Leopold Toetsch <lt@toetsch.at>
#
# 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, 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.
use strict;
use GDBM_File;
use Getopt::Std;
my $LINK = 127;
my($L) = 'S'; # pack short
$| =1;
my($zonef, $reducedf, $from, $to, $auto,$verbose, $mem, %all, %link);
my($halt, $codef, %zones, $reverse, $dbm, @nums);
my $reduce_a=1; #dont't turn on, it doesn't work
&getargs;
&read_compact;
sub min {
return $_[0] < $_[1] ? $_[0] : $_[1];
}
sub read_compact {
my ($from,$to,$z,$n);
$verbose=3;
print "Reading $reducedf...\n" if($verbose);
my(%db,$key,$value);
tie(%db, 'GDBM_File',$reducedf, GDBM_READER, 644);
my ($vers) = $db{"vErSiO\x0"};
print "$vers\n" if($verbose);
my (@vinfo) = split(/ /, $vers);
my ($v, $pack_key, $pack_table, $n, $t, $i, $ind);
foreach (@vinfo) {
$v = $1 if(/V(.*)/);
$pack_key = $1 if(/K(\w)/);
$pack_table = $1 if(/C(\w)/);
$n = $1 if(/N(\d+)/);
$t = $1 if(/T(\d+)/);
}
my($bnum) = $db{"_tAbLe\x0"};
my %len = ('C' => 1, 'S' => 2, 'L' => 4);
print "pack_table $pack_table pack_ke $pack_key\n" if($verbose>=2);
$i=min($n, 256);
@nums = unpack("$pack_table$i", $bnum);
print "Table $i:\n@nums\n" if($verbose>=3);
$n=0;
print "Entries\n";
while ( ($key, $value) = each(%db) ) {
next if ($key eq "_tAbLe\x0" || $key eq "vErSiO\x0");
my($temp) = unpack($pack_key, $key);
next if( $temp !~ /\d/);
my($ind);
my($ort) = substr($value,0, $ind=index($value, "\x0"));
print "$temp $ort\n";
$value = substr($value, $ind+1);
my($count) = unpack('S', $value);
$value=substr($value, 2); # past count
while ($count--) {
my($b, $z, $v);
$z = unpack("C", $value);
$value=substr($value, 1);
$L = 'C';
$ind = 1;
if ($z >= 128) {
$z -= 128;
$L = $pack_key;
$ind=0;
}
$b = unpack($L, $value);
$value=substr($value, $len{$L});
print STDERR "$n\r" if ($verbose==1 && $n%1000==0);
$n++;
$b = $nums[$b] if($ind);
print "$temp $b $z\n" if($verbose==3);
$all{"$temp $b"} = $z;
$link{$temp} = $b if($z eq $LINK);
}
} # while each
print STDERR "$n\n" if ($verbose);
untie(%db);
print STDERR "$n\n" if ($verbose);
exit;
}
sub getargs {
my(%opt);
$from = $to = '';
$auto=0;
$dbm=0;
$halt=1;
$mem=1; # for mem = 0, data must be resorted for 'look'
$verbose=1;
push(@ARGV,'-V'); # last options seems not to work, so I push 1 more
getopt('d:v:', \%opt);
$reducedf=$opt{'d'};
$verbose=$opt{'v'} if($opt{'v'} ne '');
$verbose=3;
&usage unless($reducedf);
}
sub usage {
print "ddb -d database\n";
exit 1;
}

File diff suppressed because it is too large Load Diff

5219
isdnlog/tools/zone/de/code Normal file

File diff suppressed because it is too large Load Diff

2
isdnlog/tools/zone/install-sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
exit 0

View File

@ -0,0 +1,469 @@
/*
* Make zone datafile
*
* Copyright 1999 by Leopold Toetsch <lt@toetsch.at>
*
* SYNOPSIS
* mkzonedb -r Zonefile -d database [ -f ] [ -v ] [ - V ]
*
* 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
static char progversion[] = "1.00";
/* first char must match dataversion */
#define STANDALONE
#define _MKZONEDB_C_
#include <limits.h>
#ifdef STANDALONE
#include <stdlib.h>
#define __USE_GNU
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#ifndef __USE_MISC
extern const char *basename (const char *name);
#endif
#else
#include "isdnlog.h"
#include "tools.h"
#endif
#include "config.h"
#include "common.h"
void usage(char *argv[]) {
fprintf(stderr, "%s: -r RedZonefile -c Code -d Database [ -v ] [ -V ] [ -o Localzone ] [ -l Len ]\n",
basename(argv[0]));
exit(EXIT_FAILURE);
}
static int (*zones)[3];
static int *numbers;
static bool verbose=false;
static int table[256];
static int tablelen, keylen, keydigs, maxnum;
static int n, nn;
typedef struct {
int num;
char *code;
} code_t;
static code_t *codes = 0;
static int nc;
static int ortszone=1;
static int numlen;
static void read_codefile(char *cf) {
FILE *fp;
int l, llen = 0;
#if 0
char *line, *p;
#else
char line[BUFSIZ], *p;
#endif
nc = 0;
if (verbose)
printf("Reading %s\n", cf);
if ((fp=fopen(cf, "r")) == 0) {
fprintf(stderr, "Coudn't read '%s'\n", cf);
exit(EXIT_FAILURE);
}
maxnum = 0;
while (!feof(fp)) {
if (verbose && (nc % 1000) == 0) {
printf("%d\r", nc);
fflush(stdout);
}
#if 0
l = getline(&line, &llen, fp);
#else
fgets(line, BUFSIZ, fp);
#endif
if (!isdigit(*line))
continue;
line[strlen(line)-1] = '\0';
if ((codes=realloc(codes, (nc+1)*sizeof(code_t))) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
codes[nc].num = strtoul(line, &p, 10);
if (codes[nc].num > maxnum)
maxnum = codes[nc].num;
if (*p != '\t') {
fprintf(stderr, "'%s' seems to be an invalide codfile\n", cf);
exit(EXIT_FAILURE);
}
if ( (codes[nc].code = strdup(++p)) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
nc++;
}
if (verbose) {
printf("%d Codes read\n", nc);
fflush(stdout);
}
fclose(fp);
/* we append a dumm for not defined nums */
if ((codes=realloc(codes, (nc+1)*sizeof(code_t))) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
codes[nc].num=-1;
codes[nc].code=strdup("");
free(cf);
#if 0
free(line);
#endif
}
static void read_rzfile(char *rf) {
int i;
char *line, *p, *op;
size_t llen, l;
FILE *fp;
int from,to,z;
if ((fp=fopen(rf, "r")) == 0) {
fprintf(stderr, "Coudn't read '%s'\n", rf);
exit(EXIT_FAILURE);
}
if ((line = malloc(40)) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
llen=40;
n=0;
zones = 0;
if ((numbers = calloc(maxnum+1, sizeof(int))) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
i=keylen=keydigs=0;
if (verbose)
printf("Reading %s\n", rf);
while (!feof(fp)) {
if (verbose && (n % 1000) == 0) {
printf("%d\r", n);
fflush(stdout);
}
#if 0
l = getline(&line, &llen, fp);
#else
fgets(line, BUFSIZ, fp);
l = strlen(line);
#endif
if (!l || l == -1 || !*line)
break;
if (l>40)
fprintf(stderr, "Possible junk in line %d", n);
p = line;
from = strtoul(p, &p, 10);
if (p-line > keydigs)
keydigs=p-line;
p++;
op = p;
to = strtoul(p, &p, 10);
if (p-op > keydigs)
keydigs=p-op;
p++;
z = strtoul(p, &p, 10);
if (z > 127) {
fprintf(stderr, "Something is wrong with this file (line %d)\n", n);
exit(EXIT_FAILURE);
}
if ((zones = realloc(zones, (n+1)*3*sizeof(int))) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
zones[n][0]=from;
zones[n][1]=to;
zones[n][2]=z;
if (from > keylen)
keylen=from;
if (to > keylen)
keylen=to;
numbers[to]++;
n++;
}
free(line);
free(rf);
}
static void make_table() {
int i, j, k;
tablelen = 0;
if (verbose)
printf("%d\nSorting\n", n);
nn = maxnum;
for (j=0; j<256; j++) {
int max = 0;
k = -1;
for (i=0; i<=maxnum; i++) {
if (numbers[i] > max) {
k = i;
max = numbers[i];
}
}
if (k == -1) {
nn = j;
break;
}
numbers[k]=0;
table[j] = k;
if (k > tablelen)
tablelen = k;
}
free(numbers);
if (nn > 256)
nn = 256;
}
static int comp_func(const void *p1, const void *p2) {
return ((code_t*)p1)->num - ((code_t*)p2)->num;
}
/* insert the code of from into value */
static void insert_code(datum *value, int ifrom) {
int l;
code_t *cp, key;
key.num = ifrom;
cp = bsearch(&key, codes, nc, sizeof(code_t), comp_func);
if (!cp)
cp = &codes[nc];
l = strlen(cp->code) + 1;
if ((value->dptr = realloc(value->dptr, value->dsize+l)) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
memmove(value->dptr+l, value->dptr, value->dsize);
value->dsize += l;
memcpy(value->dptr, cp->code, l);
*cp->code = '\0'; /* invalidate */
return;
}
static void write_remaining_codes(_DB db) {
int i,l;
UL kul;
US kus;
datum key, value;
char *val;
for (i=0 ; i< nc; i++)
if (codes[i].num > 0 && *codes[i].code) {
if (keylen == 4) {
kul = (UL)codes[i].num;
key.dptr = (char*)&kul;
}
else {
kus = (US)codes[i].num;
key.dptr = (char*)&kus;
}
key.dsize = keylen;
val = malloc(l=(strlen(codes[i].code)+3));
strcpy(val, codes[i].code);
val[l-1] = val[l-2] = '\0'; /* count=0 */
value.dptr = val;
value.dsize = l;
if(STORE(db, key, value)) {
fprintf(stderr, "Error storing key '%d' - %s\n",codes[i].num,GET_ERR);
/* exit(EXIT_FAILURE); */
}
free(val);
}
}
static void write_db(char * df) {
_DB db;
datum key, value;
UL ul, kul;
US us, kus;
int ofrom;
int vlen;
US count;
char *val, *p;
int i, j;
char version[80];
qsort(codes, nc, sizeof(code_t), comp_func);
if (verbose)
printf("Writing\n");
if((db=OPEN(df,WRITE)) == 0) {
fprintf(stderr, "Can't create '%s' - %s\n", df, GET_ERR);
exit(EXIT_FAILURE);
}
/* tablelen .. len of table entries */
/* keylen = keysize */
keylen = keylen > 0xffff ? 4 : 2;
tablelen = tablelen > 0xffff ? 4 : tablelen > 0xff ? 2 : 1;
/* write version & table */
key.dptr = "vErSiO";
key.dsize = 7;
sprintf(version,"V1.00 K%c C%c N%d T%d O%d L%d",
keylen==2?'S':'L',tablelen==1?'C':tablelen==2?'S':'L',
nn,n, ortszone, numlen?numlen:keydigs);
value.dptr = version;
value.dsize = strlen(version)+1;
STORE(db, key, value);
if ((p = val = calloc(nn, tablelen)) == 0) {
fprintf(stderr, "Out of mem\n");
exit(EXIT_FAILURE);
}
key.dptr = "_tAbLe";
key.dsize = 7;
for (i=0; i<nn; i++)
if(tablelen==1)
*p++ = (UC)table[i];
else if(tablelen == 2)
*((US*)p)++ = (US)table[i];
else
*((UL*)p)++ = (UL)table[i];
value.dptr = val;
value.dsize = nn*tablelen;
STORE(db, key, value);
free(val);
/* and write data */
val = malloc(2); /* size */
vlen = 2;
ofrom = -1;
count = 0;
for (i=0; i<n; i++) {
bool found = false;
UC uc;
if (verbose && (i % 1000) == 0) {
printf("%d\r", i);
fflush(stdout);
}
if (ofrom != -1 && ofrom != zones[i][0]) {
*((US*)val) = count;
value.dptr = val;
value.dsize = vlen;
insert_code(&value, ofrom);
if(STORE(db, key, value)) {
fprintf(stderr, "Error storing key '%d' - %s\n",ofrom,GET_ERR);
exit(EXIT_FAILURE);
}
free(val);
}
if (ofrom != zones[i][0]) {
count = 0;
val = malloc(2); /* size */
vlen = 2;
/* set up key */
ofrom = zones[i][0];
if (keylen == 4) {
kul = (UL)ofrom;
key.dptr = (char*)&kul;
}
else {
kus = (US)ofrom;
key.dptr = (char*)&kus;
}
key.dsize = keylen;
}
count++;
for (j=0; j<nn; j++)
if(table[j] == zones[i][1]) {
found = true;
val = realloc(val, vlen+2);
uc = (UC)zones[i][2];
val[vlen++] = uc;
uc = (UC)j;
val[vlen++] = uc;
break;
}
if (!found) {
val = realloc(val, vlen+1+keylen);
zones[i][2] |= 128;
uc = (UC)zones[i][2];
val[vlen++] = uc;
if(keylen == 2) {
us = (US)zones[i][1];
*((US*)(&val[vlen])) = us;
}
else {
ul = (UL)zones[i][1];
*((UL*)(&val[vlen])) = ul;
}
vlen+=keylen;
}
}
if(verbose)
printf("%d\n", i);
/* write last */
*((US*)val) = count;
value.dptr = val;
value.dsize = vlen;
insert_code(&value, ofrom);
STORE(db, key, value);
free(val);
write_remaining_codes(db);
CLOSE(db);
free(zones);
free(df);
for (i = 0; i <= nc; i++)
free(codes[i].code);
free(codes);
}
int main (int argc, char *argv[])
{
char *df=0;
char *rf=0;
char *cf=0;
int c;
if (argc < 2)
usage(argv);
while ( (c=getopt(argc, argv, "vVr:d:c:o:l:")) != EOF) {
switch (c) {
case 'v' : verbose = true; break;
case 'V' : printf("%s: V%s Db=%s\n",
basename(argv[0]), progversion, dbv); exit(1);
case 'd' : df = strdup(optarg); break;
case 'r' : rf = strdup(optarg); break;
case 'c' : cf = strdup(optarg); break;
case 'o' : ortszone = atoi(optarg); break;
case 'l' : numlen = atoi(optarg); break;
}
}
read_codefile(cf);
read_rzfile(rf);
/* make table of 256 maxused to's */
make_table();
write_db(df);
/* Uff this got longer as I thought,
C is a real low level language -
now it's clear, why I prefer Perl
*/
return(EXIT_SUCCESS);
}

View File

@ -15,7 +15,7 @@
<LI>zone.c, <A HREF="#zone">zone</A> - i4l-zonefuncs / Standalone zonefile verify</LI>
</UL>
<H2><A NAME="redzone">redzone</A></H2>
<P><CITE>redzone</CITE> reduces a <A HREF="#zonef">Zonefile</A> by inserting wildcards. The reduction for AT is 99,6%,
<P><CITE>redzone</CITE> reduces a <A HREF="#zonef">Zonefile</A> by inserting wildcards. The reduction for AT is 99,5%,
for DE 99,3%. </P>
<H3>Synopsis</H3>
<PRE>redzone -z<CITE>Zonefile</CITE> -r<CITE>Reducedfile</CITE> [ -v<CITE>verboselevel</CITE> ] [ -n ] [ -llen ] [ -k ] [ -o ]</PRE>
@ -56,20 +56,23 @@ compzone { -z<CITE>Zonefile</CITE> | -r<CITE>Reducedfile</CITE> } -c<CITE>Codefi
<PRE>./compzone -zzoneall -rzoneall.red -a -m</PRE>
<P>Verifys the reduced zonefile against the full file and stops on first error.</P>
<H2>mkzonedb</H2>
<P>Makes a zonedatabase (gdbm - GNU Database) from a reduced file.</P>
<H2><A NAME="mkzonedb">mkzonedb</A></H2>
<P>Makes a zonedatabase (gdbm - GNU Database or db - Berkeley DB) from a reduced file.</P>
<H3>Synopsis</H3>
<PRE>./mkzonedb -d<CITE>zone-CC-PP.gdbm</CITE> -r<CITE>ReducedFile</CITE> [ -f ] [ -v<CITE>Verboselevel</CITE> ]</PRE>
<PRE>./mkzonedb -d <CITE>zone-CC-PP.gdbm</CITE> -r <CITE>ReducedFile</CITE> -c Codefile [ -v<CITE>Verboselevel</CITE> ] [ -o Localzone ] [ -l Len ]</PRE>
<H3>Options</H3>
<PRE>-r<CITE>ReducedFile</CITE> ... reduced zone file from <STRONG>redzone</STRONG>
-d<CITE>zone-CC-PP.gdbm</CITE> ...CC = Iso-Countrycode, PP = Providernumber<BR>-f force a new file, don't update an existing
-vVerboselevel ... default = 1</PRE>
<PRE>-r <CITE>ReducedFile</CITE> ... reduced zone file from <STRONG>redzone</STRONG>
-d <CITE>zone-CC-PP.gdbm</CITE> ...CC = Iso-Countrycode, PP = Providername/number<BR>-c <CITE>Codefile</CITE><BR>-v Verboselevel ... default = 1<BR>-o Localzone ... default = 1<BR>-l Len ... default = maxlen of data</PRE>
<P>For examples look at the <TT>Makefile</TT> in target <TT>zonefiles</TT>.</P>
<H2><A NAME="zone">zone</A></H2>
<H3>Standalone-Synopsis</H3>
<PRE>zone -d <CITE>Datafile</CITE> [ -v ] [ -V ] { -z <CITE>zonefile</CITE> | num1 num2 }</PRE>
<P>or</P>
<PRE>zone -d <CITE>Datafile</CITE> [ -v ] [ -V ] num1 </PRE>
<H3>Options</H3>
<PRE>-v ... verbose
-V ... print version and exit</PRE>
@ -78,7 +81,8 @@ compzone { -z<CITE>Zonefile</CITE> | -r<CITE>Reducedfile</CITE> } -c<CITE>Codefi
<PRE>cc -DSTANDALONE -o zone zone.c -lgdbm</PRE>
<H3>I4L-Interface:</H3>
<PRE>int initZone(int provider, char *datapath, char **msg)<BR> returns -1 on error, 0 if ok<BR>void exitZone(int provider)<BR>int getZone(int provider, char *from, char *to)<BR> returns -2 on error, -1 on not found, zone if ok</PRE>
<PRE>int initZone(int provider, char *datapath, char **msg)<BR> returns -1 on error, 0 if ok<BR>void exitZone(int provider)<BR>int getZone(int provider, char *from, char *to)<BR> returns -2 on error, UNKNOWN on not found, zone if ok<BR>int getAreacode(int provider, char *num, char **text)<BR> returns UNKNOWN if not found, or len of areacode<BR> text is a malloced string containing the name of the area
the caller should free this text.</PRE>
<H2>Glossary</H2>
<H4><A NAME="zonef">Zonefile</A></H4>