This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/lib/falists.c

229 lines
4.7 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
* File ..................: falists.c
* Purpose ...............: SEEN-BY and PATH lists
* Last modification date : 25-Mar-2001
*
*****************************************************************************
* Copyright (C) 1997-2001
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS 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.
*
* MBSE BBS 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 MBSE BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************************/
#include "libs.h"
#include "structs.h"
#include "clcomm.h"
#include "common.h"
void tidy_falist(fa_list **fap)
{
fa_list *tmp,*old;
for (tmp = *fap; tmp; tmp = old) {
old = tmp->next;
tidy_faddr(tmp->addr);
free(tmp);
}
*fap = NULL;
}
int in_list(faddr *addr, fa_list **fap, int fourd)
{
fa_list *tmp;
Syslog('M', "in_list: Seeking seen-by match for %s", ascinode(addr,0x06));
if (addr->point) {
Syslog('M', "in_list: No seen-by check for point address");
return 0;
}
for (tmp = *fap; tmp; tmp = tmp->next)
if ((tmp->addr->net == addr->net) &&
((!fourd) || (fourd && (tmp->addr->zone == addr->zone))) &&
((!fourd) || (fourd && (tmp->addr->point == addr->point))) &&
(tmp->addr->node == addr->node)) {
Syslog('M', "in_list: Match found");
return 1;
}
Syslog('M', "in_list: Match not found");
return 0;
}
void fill_list(fa_list **fap, char *str, fa_list **omit, int force)
{
fa_list *tmp;
faddr *ta;
static unsigned int oldnet;
char *buf, *p, *q, *r;
int allowskip = 1;
if ((str == NULL) || (*str == '\0'))
return;
buf = xstrcpy(str);
r = buf + strlen(buf);
for (p = strtok(buf," \t\n"), q = p + strlen(p) + 1;
p;
p = (q < r) ? strtok(q, " \t\n"):NULL, q = p ? p + strlen(p) + 1:r)
if ((ta = parsefnode(p))) {
if (ta->net == 0)
ta->net = oldnet;
else
oldnet = ta->net;
if (allowskip && omit && *omit && (metric(ta,(*omit)->addr) == 0)) {
Syslog('m', "fill_list: omit %s", ascfnode(ta,0x1f));
tmp = *omit;
*omit = (*omit)->next;
tmp->next = NULL;
tidy_falist(&tmp);
} else {
allowskip = 0;
tmp = (fa_list *)malloc(sizeof(fa_list));
tmp->next = *fap;
tmp->addr = ta;
tmp->force = force;
*fap = tmp;
}
}
free(buf);
return;
}
void fill_path(fa_list **fap, char *str)
{
fa_list **tmp;
faddr *ta;
static unsigned int oldnet;
char *buf, *p, *q, *r;
if ((str == NULL) || (*str == '\0'))
return;
buf = xstrcpy(str);
for (tmp = fap; *tmp; tmp = &((*tmp)->next)); /*nothing*/
r = buf + strlen(buf);
for (p = strtok(buf, " \t\n"), q = p + strlen(p) + 1;
p;
p = (q < r) ? strtok(q, " \t\n") : NULL, q = p ? p + strlen(p) + 1 : r)
if ((ta = parsefnode(p))) {
if (ta->net == 0)
ta->net=oldnet;
else
oldnet=ta->net;
*tmp = (fa_list *)malloc(sizeof(fa_list));
(*tmp)->next = NULL;
(*tmp)->addr = ta;
tmp = &((*tmp)->next);
}
free(buf);
return;
}
int compaddr(fa_list **,fa_list **);
void uniq_list(fa_list **fap)
{
fa_list *ta, *tan;
if (*fap == NULL)
return;
for (ta = *fap; ta; ta = ta->next) {
while ((tan = ta->next) && (compaddr(&ta, &tan) == 0)) {
ta->next = tan->next;
tidy_faddr(tan->addr);
free(tan);
}
ta->next = tan;
}
}
void sort_list(fa_list **fap)
{
fa_list *ta, **vector;
size_t n = 0, i;
if (*fap == NULL)
return;
for (ta = *fap; ta; ta = ta->next)
n++;
vector = (fa_list **)malloc(n * sizeof(fa_list *));
i = 0;
for (ta = *fap; ta; ta = ta->next) {
vector[i++] = ta;
}
qsort(vector,n,sizeof(faddr*),
(int(*)(const void*,const void*))compaddr);
(*fap) = vector[0];
i = 1;
for (ta = *fap; ta; ta = ta->next) {
while ((i < n) && (compaddr(&ta,&(vector[i])) == 0))
{
tidy_faddr((vector[i])->addr);
free(vector[i]);
i++;
}
if (i < n)
ta->next=vector[i++];
else
ta->next=NULL;
}
free(vector);
return;
}
int compaddr(fa_list **fap1, fa_list **fap2)
{
if ((*fap1)->addr->net != (*fap2)->addr->net)
return ((*fap1)->addr->net - (*fap2)->addr->net);
else
return ((*fap1)->addr->node - (*fap2)->addr->node);
}