1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/* pathalias -- by steve bellovin, as told to peter honeyman */
#ifndef lint
static char *sccsid = "@(#)printit.c 9.4 89/02/07";
static char *rcsid = "@(#)smail/pd/pathalias:RELEASE-3_2_0_102:printit.c,v 1.5 1997/02/09 21:51:08 woods Exp";
#endif
#include "def.h"
/*
* print the routes by traversing the shortest path tree in preorder.
* use lots of char bufs -- profiling indicates this costs about 5 kbytes
*/
/* exports */
extern void printit();
/* imports */
extern int Cflag, Vflag, Dflag, Fflag;
extern node *Home;
extern char *Netchars;
extern void die();
#ifndef SMAIL_3
extern int strlen();
#endif
/* privates */
static link *Ancestor; /* for -f option */
STATIC void preorder(), setpath(), printhost(), printdomain();
STATIC char *hostpath();
STATIC int printable();
/* in practice, even the longest paths are < 100 bytes */
#define PATHSIZE 512
void
printit()
{ link *l;
char pbuf[PATHSIZE];
/* print home */
if (Cflag)
printf("%ld\t", (long) Home->n_cost);
printf("%s\t%%s\n", Home->n_name);
strcpy(pbuf, "%s");
for (l = Home->n_link; l; l = l->l_next) {
if (l->l_flag & LTREE) {
l->l_flag &= ~LTREE;
Ancestor = l;
preorder(l, pbuf);
strcpy(pbuf, "%s");
}
}
fflush(stdout);
fflush(stderr);
}
/*
* preorder traversal of shortest path tree.
*/
STATIC void
preorder(l, ppath)
register link *l;
char *ppath;
{ register node *n;
node *ncp; /* circular copy list */
Cost cost;
char npath[PATHSIZE];
short p_dir; /* DIR bits of parent (for nets) */
char p_op; /* net op of parent (for nets) */
setpath(l, ppath, npath);
n = l->l_to;
if (printable(n)) {
if (Fflag)
cost = Ancestor->l_to->n_cost;
else
cost = n->n_cost;
if (ISADOMAIN(n))
printdomain(n, npath, cost);
else if (!(n->n_flag & NNET)) {
printhost(n, npath, cost);
}
n->n_flag |= PRINTED;
for (ncp = n->n_copy; ncp != n; ncp = ncp->n_copy)
ncp->n_flag |= PRINTED;
}
/* prepare routing bits for domain members */
p_dir = l->l_flag & LDIR;
p_op = l->l_netop;
/* recursion */
for (l = n->n_link; l; l = l->l_next) {
if (!(l->l_flag & LTREE))
continue;
/* network member inherits the routing syntax of its gateway */
if (ISANET(n)) {
l->l_flag = (l->l_flag & ~LDIR) | p_dir;
l->l_netop = p_op;
}
l->l_flag &= ~LTREE;
preorder(l, npath);
}
}
STATIC int
printable(n)
register node *n;
{ node *ncp;
link *l;
if (n->n_flag & PRINTED)
return 0;
/* is there a cheaper copy? */
for (ncp = n->n_copy; n != ncp; ncp = ncp->n_copy) {
if (!(ncp->n_flag & MAPPED))
continue; /* unmapped copy */
if (n->n_cost > ncp->n_cost)
return 0; /* cheaper copy */
if (n->n_cost == ncp->n_cost && !(ncp->n_flag & NTERMINAL))
return 0; /* synthetic copy */
}
/* will a domain route suffice? */
if (Dflag && !ISANET(n) && ISADOMAIN(n->n_parent)) {
/*
* are there any interesting links? a link
* is interesting if it doesn't point back
* to the parent, and is not an alias.
*/
/* check n */
for (l = n->n_link; l; l = l->l_next) {
if (l->l_to == n->n_parent)
continue;
if (!(l->l_flag & LALIAS))
return 1;
}
/* check copies of n */
for (ncp = n->n_copy; ncp != n; ncp = ncp->n_copy) {
for (l = ncp->n_link; l; l = l->l_next) {
if (l->l_to == n->n_parent)
continue;
if (!(l->l_flag & LALIAS))
return 1;
}
}
/* domain route suffices */
return 0;
}
return 1;
}
STATIC void
setpath(l, ppath, npath)
link *l;
register char *ppath, *npath;
{ register node *next, *parent;
char netchar;
next = l->l_to;
parent = next->n_parent;
netchar = NETCHAR(l);
/* for magic @->% conversion */
if (parent->n_flag & ATSIGN)
next->n_flag |= ATSIGN;
/*
* i've noticed that distant gateways to domains frequently get
* ...!gateway!user@dom.ain wrong. ...!gateway!user%dom.ain
* seems to work, so if next is a domain and the parent is
* not the local host, force a magic %->@ conversion. in this
* context, "parent" is the nearest ancestor that is not a net.
*/
while (ISANET(parent))
parent = parent->n_parent;
if (ISADOMAIN(next) && parent != Home)
next->n_flag |= ATSIGN;
/*
* special handling for nets (including domains) and aliases.
* part of the trick is to treat aliases to domains as 0 cost
* links. (the author believes they should be declared as such
* in the input, but the world disagrees).
*/
if (ISANET(next) || ((l->l_flag & LALIAS) && !ISADOMAIN(parent))) {
strcpy(npath, ppath);
return;
}
if (netchar == '@')
if (next->n_flag & ATSIGN)
netchar = '%'; /* shazam? shaman? */
else
next->n_flag |= ATSIGN;
/* remainder should be a sprintf -- foo on '%' as an operator */
for ( ; (*npath = *ppath) != 0; ppath++) {
if (*ppath == '%') {
switch(ppath[1]) {
case 's':
ppath++;
npath = hostpath(npath, l, netchar);
break;
case '%':
*++npath = *++ppath;
npath++;
break;
default:
die("unknown escape in setpath");
break;
}
} else
npath++;
}
}
STATIC char *
hostpath(path, l, netchar)
register char *path;
register link *l;
char netchar;
{ register node *prev;
prev = l->l_to->n_parent;
if (NETDIR(l) == LLEFT) {
/* host!%s */
strcpy(path, l->l_to->n_name);
path += strlen(path);
while (ISADOMAIN(prev)) {
strcpy(path, prev->n_name);
path += strlen(path);
prev = prev->n_parent;
}
*path++ = netchar;
if (netchar == '%')
*path++ = '%';
*path++ = '%';
*path++ = 's';
} else {
/* %s@host */
*path++ = '%';
*path++ = 's';
*path++ = netchar;
if (netchar == '%')
*path++ = '%';
strcpy(path, l->l_to->n_name);
path += strlen(path);
while (ISADOMAIN(prev)) {
strcpy(path, prev->n_name);
path += strlen(path);
prev = prev->n_parent;
}
}
return path;
}
STATIC void
printhost(n, path, cost)
register node *n;
char *path;
Cost cost;
{
if (n->n_flag & PRINTED)
die("printhost called twice");
n->n_flag |= PRINTED;
/* skip private hosts */
if ((n->n_flag & ISPRIVATE) == 0) {
if (Cflag)
printf("%ld\t", (long) cost);
fputs(n->n_name, stdout);
putchar('\t');
puts(path);
}
}
STATIC void
printdomain(n, path, cost)
register node *n;
char *path;
Cost cost;
{ node *p;
if (n->n_flag & PRINTED)
die("printdomain called twice");
n->n_flag |= PRINTED;
/*
* print a route for dom.ain if it is a top-level domain, unless
* it is private.
*
* print a route for sub.dom.ain only if all its ancestor dom.ains
* are private and sub.dom.ain is not private.
*/
if (!ISADOMAIN(n->n_parent)) {
/* top-level domain */
if (n->n_flag & ISPRIVATE) {
Vprintf(stderr, "ignoring private top-level domain %s\n", n->n_name);
return;
}
} else {
/* subdomain */
for (p = n->n_parent; ISADOMAIN(p); p = p->n_parent)
if (!(p->n_flag & ISPRIVATE))
return;
if (n->n_flag & ISPRIVATE)
return;
}
/* print it (at last!) */
if (Cflag)
printf("%ld\t", (long) cost);
do {
fputs(n->n_name, stdout);
n = n->n_parent;
} while (ISADOMAIN(n));
putchar('\t');
puts(path);
}
|