|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T t
Length: 6892 (0x1aec) Types: TextFile Names: »tb_getdomain.c«
└─⟦2d1937cfd⟧ Bits:30007241 EUUGD22: P.P 5.0 └─⟦dc59850a2⟧ »EurOpenD22/pp5.0/pp-5.tar.Z« └─⟦e5a54fb17⟧ └─⟦this⟧ »pp-5.0/Lib/table/tb_getdomain.c«
/* tb_getdomain.c - maps an alias domain into a normalised one and optionally, maps a normalised domain into its next hop out. */ # ifndef lint static char Rcsid[] = "@(#)$Header: /cs/research/pp/hubris/pp-beta/Lib/table/RCS/tb_getdomain.c,v 5.0 90/09/20 16:15:26 pp Exp Locker: pp $"; # endif /* * $Header: /cs/research/pp/hubris/pp-beta/Lib/table/RCS/tb_getdomain.c,v 5.0 90/09/20 16:15:26 pp Exp Locker: pp $ * * $Log: tb_getdomain.c,v $ * Revision 5.0 90/09/20 16:15:26 pp * rcsforce : 5.0 public release * */ #include "util.h" #include "list_rchan.h" #define MASK_PREFBIT 1 char *bits2str(); int str2bits(); static int scorebits(); static int dom_lookup (); /* --------------------- Begin Routines --------------------------------- */ /* --- *** Start Description *** --- tb_getdomain (dom_key, chan_key, normalised, order_pref, ppsubdom) dom_key - Could be either an Alias, NRS Short from, or a Normalised domain name. chan_key - The Next Hop hostname. This is used as a key to the channel table. normalised - Normalised version of (dom_key) order_pref - UK only, USA only, UK pref, USA pref. ppsubdom - is this a sub domain - if so which Example: dom_key = blah.torch.co chan_key = torch.co.uk normalised = blah.torch.co.uk order_pref = CH_UK_PREF Routine: - Reads the domain table - Updates normalised. - Optionally updates chan_key if ptr given. - Returns OK or NOTOK. Note: - This Routine always returns the char* normalised in USA order --- *** End Description *** --- */ /* ------------------------------------------------------------------------ */ int tb_getdomain (dom_key, chan_key, normalised, order_pref, ppsubdom) char *dom_key; char *chan_key; char *normalised; int order_pref; char **ppsubdom; { char tbuf[BUFSIZ], bbuf[BUFSIZ], *usa_ord[LINESIZE], *uk_ord[LINESIZE], *str; char *uksd, *ussd; int i, count, minsub, maxsub, usa_score, uk_score; if (dom_key == NULLCP) return NOTOK; *ppsubdom = NULLCP; PP_DBG (("Lib/table/tb_getdomain (domain='%s', order_pref=%d)", dom_key, order_pref)); if (*dom_key == '.' || dom_key [strlen (dom_key) - 1] == '.') return (NOTOK); /* -- do not destroy input -- */ (void) strcpy (tbuf, dom_key); /* -- get things into known order and count parts of name -- */ if ((order_pref & CH_UK_ONLY) != 0) count = str2bits (tbuf, '.', uk_ord, usa_ord); else count = str2bits (tbuf, '.', usa_ord, uk_ord); /* -- look up the usa order name - exact match -- */ str = bits2str (usa_ord, '.'); if (dom_lookup (str, normalised, &minsub, &maxsub, ppsubdom) == OK && minsub == 0) { if (chan_key) (void) strcpy (chan_key, normalised); return (OK); } /* -- ok, if the other way around is allowed - try it -- */ if ((order_pref & MASK_PREFBIT) != 0) { str = bits2str (uk_ord, '.'); if (dom_lookup (str, normalised, &minsub, &maxsub, ppsubdom) == OK && minsub == 0) { if (chan_key) (void) strcpy (chan_key, normalised); return (OK); } } uksd = ussd = NULLCP; /* -- score usa order - count the number of matching bits -- */ usa_score = scorebits (usa_ord, count, bbuf, &ussd); /* -- also score the reverse order if appropriate -- */ uk_score = 0; if ((order_pref & MASK_PREFBIT) != 0) uk_score = scorebits (uk_ord, count, normalised, &uksd); /* -- no matches - can't normalise this thing -- */ if (uk_score == 0 && usa_score == 0) { if (uksd) free (uksd); if (ussd) free (ussd); return (NOTOK); } /* -- usa order is the best fit -- */ if (usa_score >= uk_score) { if (chan_key) (void) strcpy (chan_key, bbuf); *normalised = '\0'; for (i = 0; i < count - usa_score; i++) { (void) strcat (normalised, usa_ord[i]); (void) strcat (normalised, "."); } (void) strcat (normalised, bbuf); *ppsubdom = ussd; if (uksd) free(uksd); return (OK); } /* -- uk order is the best fit -- */ (void) strcpy (bbuf, normalised); *normalised = '\0'; for (i = 0; i < count - uk_score; i++) { (void) strcat (normalised, uk_ord[i]); (void) strcat (normalised, "."); } if (chan_key) (void) strcpy (chan_key, bbuf); (void) strcat (normalised, bbuf); *ppsubdom = uksd; if (ussd) free(ussd); return (OK); } /* utilities - previously DOM_scorebits etc. */ static int scorebits (ordptr, count, buf, subdom) char *ordptr[]; int count; char *buf; char **subdom; { int i, maxsub, minsub; char *tbuf[LINESIZE], *str, **ptr; PP_DBG (("Lib/table/scorebits()")); for (i = 0, ptr = ordptr; *ptr != NULL; ptr++) tbuf[i++] = *ptr; tbuf[i] = NULLCP; for (i = count; i != 0; i--) { str = bits2str (&tbuf[count - i], '.'); if (dom_lookup (str, buf, &minsub, &maxsub, subdom) == OK && (maxsub == NOTOK || maxsub >= (count - i)) && minsub <= (count - i)) return (i); } return 0; } char *bits2str (ptr, sep) register char **ptr; register char sep; { char silly[2]; static char tbuf[LINESIZE]; PP_DBG (("Lib/table/bits2str ('%s')", *ptr)); silly[0] = sep; silly[1] = '\0'; (void) strcpy (tbuf, *ptr++); while (*ptr != NULLCP) { (void) strcat (tbuf, silly); (void) strcat (tbuf, *ptr++); } return (tbuf); } /* This routine takes a hostname in a string and returns two arrays of pointers into the string. The string is "chopped up" with '\0'. The number of elements in the hostname is returned. */ int str2bits (str, sep, forward, reverse) register char *str; register char sep; register char **forward; register char **reverse; { register int i, j; PP_DBG (("Lib/table/str2bits(%s)", str)); *forward++ = str; for (i = 1; *str != '\0'; str++) if (*str == sep) { *str = '\0'; *forward++ = &str[1]; i++; } *forward-- = NULLCP; if (reverse != NULL) { for (j = 0; j < i; j++) *reverse++ = *forward--; *reverse = NULLCP; } return (i); } static int dom_lookup (name, value, minsub, maxsub, subdom) char *name; char *value; int *minsub; int *maxsub; char **subdom; { char *vec[10]; int vecp; static Table *Domain; char *cp; if (Domain == NULLTBL) if ((Domain = tb_nm2struct ("domain")) == NULLTBL) { PP_LOG (LLOG_FATAL, ("No table 'domain' defined")); return (NOTOK); } if (tb_k2val (Domain, name, value) == NOTOK) return NOTOK; if ((vecp = sstr2arg (value, 10, vec, " \t")) < 1) return NOTOK; *maxsub = 0; *minsub = 0; if (vecp > 1) { if (strcmp (vec[1], "*") == 0) { *maxsub = NOTOK; *minsub = 0; } else { if (cp = index (vec[1], ':')) { *cp ++ = NULL; if (*cp && strcmp (cp, "*") == 0) *maxsub = NOTOK; else *maxsub = atoi (cp); *minsub = atoi(vec[1]); } else { *maxsub = atoi (vec[1]); *minsub = 0; } } } *subdom = NULLCP; if (vecp > 2 && lexequ (vec[2], "local") == 0) { if (vecp > 3) *subdom = strdup (vec[3]); else *subdom = strdup (""); } return OK; }