OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [sys/] [linux/] [net/] [ns_samedomain.c] - Diff between revs 148 and 158

Only display areas with differences | Details | Blame | View Log

Rev 148 Rev 158
/*
/*
 * Copyright (c) 1995,1999 by Internet Software Consortium.
 * Copyright (c) 1995,1999 by Internet Software Consortium.
 *
 *
 * Permission to use, copy, modify, and distribute this software for any
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * copyright notice and this permission notice appear in all copies.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 * SOFTWARE.
 */
 */
 
 
#if !defined(_LIBC) && !defined(lint)
#if !defined(_LIBC) && !defined(lint)
static const char rcsid[] = "$BINDId: ns_samedomain.c,v 8.9 1999/10/15 21:06:51 vixie Exp $";
static const char rcsid[] = "$BINDId: ns_samedomain.c,v 8.9 1999/10/15 21:06:51 vixie Exp $";
#endif
#endif
 
 
#include <sys/types.h>
#include <sys/types.h>
#include <arpa/nameser.h>
#include <arpa/nameser.h>
#include <errno.h>
#include <errno.h>
#include <string.h>
#include <string.h>
 
 
#include "libc-symbols.h"
#include "libc-symbols.h"
 
 
/*
/*
 * int
 * int
 * ns_samedomain(a, b)
 * ns_samedomain(a, b)
 *      Check whether a name belongs to a domain.
 *      Check whether a name belongs to a domain.
 * Inputs:
 * Inputs:
 *      a - the domain whose ancestory is being verified
 *      a - the domain whose ancestory is being verified
 *      b - the potential ancestor we're checking against
 *      b - the potential ancestor we're checking against
 * Return:
 * Return:
 *      boolean - is a at or below b?
 *      boolean - is a at or below b?
 * Notes:
 * Notes:
 *      Trailing dots are first removed from name and domain.
 *      Trailing dots are first removed from name and domain.
 *      Always compare complete subdomains, not only whether the
 *      Always compare complete subdomains, not only whether the
 *      domain name is the trailing string of the given name.
 *      domain name is the trailing string of the given name.
 *
 *
 *      "host.foobar.top" lies in "foobar.top" and in "top" and in ""
 *      "host.foobar.top" lies in "foobar.top" and in "top" and in ""
 *      but NOT in "bar.top"
 *      but NOT in "bar.top"
 */
 */
 
 
int
int
ns_samedomain(const char *a, const char *b) {
ns_samedomain(const char *a, const char *b) {
        size_t la, lb;
        size_t la, lb;
        int diff, i, escaped;
        int diff, i, escaped;
        const char *cp;
        const char *cp;
 
 
        la = strlen(a);
        la = strlen(a);
        lb = strlen(b);
        lb = strlen(b);
 
 
        /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
        /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
        if (la != 0 && a[la - 1] == '.') {
        if (la != 0 && a[la - 1] == '.') {
                escaped = 0;
                escaped = 0;
                /* Note this loop doesn't get executed if la==1. */
                /* Note this loop doesn't get executed if la==1. */
                for (i = la - 2; i >= 0; i--)
                for (i = la - 2; i >= 0; i--)
                        if (a[i] == '\\') {
                        if (a[i] == '\\') {
                                if (escaped)
                                if (escaped)
                                        escaped = 0;
                                        escaped = 0;
                                else
                                else
                                        escaped = 1;
                                        escaped = 1;
                        } else
                        } else
                                break;
                                break;
                if (!escaped)
                if (!escaped)
                        la--;
                        la--;
        }
        }
 
 
        /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
        /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
        if (lb != 0 && b[lb - 1] == '.') {
        if (lb != 0 && b[lb - 1] == '.') {
                escaped = 0;
                escaped = 0;
                /* note this loop doesn't get executed if lb==1 */
                /* note this loop doesn't get executed if lb==1 */
                for (i = lb - 2; i >= 0; i--)
                for (i = lb - 2; i >= 0; i--)
                        if (b[i] == '\\') {
                        if (b[i] == '\\') {
                                if (escaped)
                                if (escaped)
                                        escaped = 0;
                                        escaped = 0;
                                else
                                else
                                        escaped = 1;
                                        escaped = 1;
                        } else
                        } else
                                break;
                                break;
                if (!escaped)
                if (!escaped)
                        lb--;
                        lb--;
        }
        }
 
 
        /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
        /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
        if (lb == 0)
        if (lb == 0)
                return (1);
                return (1);
 
 
        /* 'b' longer than 'a' means 'a' can't be in 'b'. */
        /* 'b' longer than 'a' means 'a' can't be in 'b'. */
        if (lb > la)
        if (lb > la)
                return (0);
                return (0);
 
 
        /* 'a' and 'b' being equal at this point indicates sameness. */
        /* 'a' and 'b' being equal at this point indicates sameness. */
        if (lb == la)
        if (lb == la)
                return (strncasecmp(a, b, lb) == 0);
                return (strncasecmp(a, b, lb) == 0);
 
 
        /* Ok, we know la > lb. */
        /* Ok, we know la > lb. */
 
 
        diff = la - lb;
        diff = la - lb;
 
 
        /*
        /*
         * If 'a' is only 1 character longer than 'b', then it can't be
         * If 'a' is only 1 character longer than 'b', then it can't be
         * a subdomain of 'b' (because of the need for the '.' label
         * a subdomain of 'b' (because of the need for the '.' label
         * separator).
         * separator).
         */
         */
        if (diff < 2)
        if (diff < 2)
                return (0);
                return (0);
 
 
        /*
        /*
         * If the character before the last 'lb' characters of 'b'
         * If the character before the last 'lb' characters of 'b'
         * isn't '.', then it can't be a match (this lets us avoid
         * isn't '.', then it can't be a match (this lets us avoid
         * having "foobar.com" match "bar.com").
         * having "foobar.com" match "bar.com").
         */
         */
        if (a[diff - 1] != '.')
        if (a[diff - 1] != '.')
                return (0);
                return (0);
 
 
        /*
        /*
         * We're not sure about that '.', however.  It could be escaped
         * We're not sure about that '.', however.  It could be escaped
         * and thus not a really a label separator.
         * and thus not a really a label separator.
         */
         */
        escaped = 0;
        escaped = 0;
        for (i = diff - 2; i >= 0; i--)
        for (i = diff - 2; i >= 0; i--)
                if (a[i] == '\\')
                if (a[i] == '\\')
                        if (escaped)
                        if (escaped)
                                escaped = 0;
                                escaped = 0;
                        else
                        else
                                escaped = 1;
                                escaped = 1;
                else
                else
                        break;
                        break;
        if (escaped)
        if (escaped)
                return (0);
                return (0);
 
 
        /* Now compare aligned trailing substring. */
        /* Now compare aligned trailing substring. */
        cp = a + diff;
        cp = a + diff;
        return (strncasecmp(cp, b, lb) == 0);
        return (strncasecmp(cp, b, lb) == 0);
}
}
 
 
/*
/*
 * int
 * int
 * ns_subdomain(a, b)
 * ns_subdomain(a, b)
 *      is "a" a subdomain of "b"?
 *      is "a" a subdomain of "b"?
 */
 */
int
int
ns_subdomain(const char *a, const char *b) {
ns_subdomain(const char *a, const char *b) {
        return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
        return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
}
}
 
 
/*
/*
 * int
 * int
 * ns_makecanon(src, dst, dstsize)
 * ns_makecanon(src, dst, dstsize)
 *      make a canonical copy of domain name "src"
 *      make a canonical copy of domain name "src"
 * notes:
 * notes:
 *      foo -> foo.
 *      foo -> foo.
 *      foo. -> foo.
 *      foo. -> foo.
 *      foo.. -> foo.
 *      foo.. -> foo.
 *      foo\. -> foo\..
 *      foo\. -> foo\..
 *      foo\\. -> foo\\.
 *      foo\\. -> foo\\.
 */
 */
 
 
int
int
ns_makecanon(const char *src, char *dst, size_t dstsize) {
ns_makecanon(const char *src, char *dst, size_t dstsize) {
        size_t n = strlen(src);
        size_t n = strlen(src);
 
 
        if (n + sizeof "." > dstsize) {
        if (n + sizeof "." > dstsize) {
                __set_errno (EMSGSIZE);
                __set_errno (EMSGSIZE);
                return (-1);
                return (-1);
        }
        }
        strcpy(dst, src);
        strcpy(dst, src);
        while (n > 0 && dst[n - 1] == '.')               /* Ends in "." */
        while (n > 0 && dst[n - 1] == '.')               /* Ends in "." */
                if (n > 1 && dst[n - 2] == '\\' &&      /* Ends in "\." */
                if (n > 1 && dst[n - 2] == '\\' &&      /* Ends in "\." */
                    (n < 2 || dst[n - 3] != '\\'))      /* But not "\\." */
                    (n < 2 || dst[n - 3] != '\\'))      /* But not "\\." */
                        break;
                        break;
                else
                else
                        dst[--n] = '\0';
                        dst[--n] = '\0';
        dst[n++] = '.';
        dst[n++] = '.';
        dst[n] = '\0';
        dst[n] = '\0';
        return (0);
        return (0);
}
}
 
 
/*
/*
 * int
 * int
 * ns_samename(a, b)
 * ns_samename(a, b)
 *      determine whether domain name "a" is the same as domain name "b"
 *      determine whether domain name "a" is the same as domain name "b"
 * return:
 * return:
 *      -1 on error
 *      -1 on error
 *      0 if names differ
 *      0 if names differ
 *      1 if names are the same
 *      1 if names are the same
 */
 */
 
 
int
int
ns_samename(const char *a, const char *b) {
ns_samename(const char *a, const char *b) {
        char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
        char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
 
 
        if (ns_makecanon(a, ta, sizeof ta) < 0 ||
        if (ns_makecanon(a, ta, sizeof ta) < 0 ||
            ns_makecanon(b, tb, sizeof tb) < 0)
            ns_makecanon(b, tb, sizeof tb) < 0)
                return (-1);
                return (-1);
        if (strcasecmp(ta, tb) == 0)
        if (strcasecmp(ta, tb) == 0)
                return (1);
                return (1);
        else
        else
                return (0);
                return (0);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.