OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [sys/] [linux/] [net/] [inet_net_pton.c] - Diff between revs 207 and 345

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

Rev 207 Rev 345
/*
/*
 * Copyright (c) 1996 by Internet Software Consortium.
 * Copyright (c) 1996 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_SCCS) && !defined(lint)
#if defined(LIBC_SCCS) && !defined(lint)
static const char orig_rcsid[] = "From Id: inet_net_pton.c,v 1.8 1996/11/21 10:28:12 vixie Exp $";
static const char orig_rcsid[] = "From Id: inet_net_pton.c,v 1.8 1996/11/21 10:28:12 vixie Exp $";
#endif
#endif
#include <sys/cdefs.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/types.h>
 
 
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
 
 
#include <assert.h>
#include <assert.h>
#include <ctype.h>
#include <ctype.h>
#include <errno.h>
#include <errno.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
#ifdef SPRINTF_CHAR
#ifdef SPRINTF_CHAR
# define SPRINTF(x) strlen(sprintf/**/x)
# define SPRINTF(x) strlen(sprintf/**/x)
#else
#else
# define SPRINTF(x) ((size_t)sprintf x)
# define SPRINTF(x) ((size_t)sprintf x)
#endif
#endif
 
 
static int      inet_net_pton_ipv4(const char *src, u_char *dst, size_t size);
static int      inet_net_pton_ipv4(const char *src, u_char *dst, size_t size);
 
 
/*
/*
 * static int
 * static int
 * inet_net_pton(af, src, dst, size)
 * inet_net_pton(af, src, dst, size)
 *      convert network number from presentation to network format.
 *      convert network number from presentation to network format.
 *      accepts hex octets, hex strings, decimal octets, and /CIDR.
 *      accepts hex octets, hex strings, decimal octets, and /CIDR.
 *      "size" is in bytes and describes "dst".
 *      "size" is in bytes and describes "dst".
 * return:
 * return:
 *      number of bits, either imputed classfully or specified with /CIDR,
 *      number of bits, either imputed classfully or specified with /CIDR,
 *      or -1 if some failure occurred (check errno).  ENOENT means it was
 *      or -1 if some failure occurred (check errno).  ENOENT means it was
 *      not a valid network specification.
 *      not a valid network specification.
 * author:
 * author:
 *      Paul Vixie (ISC), June 1996
 *      Paul Vixie (ISC), June 1996
 */
 */
int
int
inet_net_pton(af, src, dst, size)
inet_net_pton(af, src, dst, size)
        int af;
        int af;
        const char *src;
        const char *src;
        void *dst;
        void *dst;
        size_t size;
        size_t size;
{
{
        switch (af) {
        switch (af) {
        case AF_INET:
        case AF_INET:
                return (inet_net_pton_ipv4(src, dst, size));
                return (inet_net_pton_ipv4(src, dst, size));
        default:
        default:
                errno = EAFNOSUPPORT;
                errno = EAFNOSUPPORT;
                return (-1);
                return (-1);
        }
        }
}
}
 
 
/*
/*
 * static int
 * static int
 * inet_net_pton_ipv4(src, dst, size)
 * inet_net_pton_ipv4(src, dst, size)
 *      convert IPv4 network number from presentation to network format.
 *      convert IPv4 network number from presentation to network format.
 *      accepts hex octets, hex strings, decimal octets, and /CIDR.
 *      accepts hex octets, hex strings, decimal octets, and /CIDR.
 *      "size" is in bytes and describes "dst".
 *      "size" is in bytes and describes "dst".
 * return:
 * return:
 *      number of bits, either imputed classfully or specified with /CIDR,
 *      number of bits, either imputed classfully or specified with /CIDR,
 *      or -1 if some failure occurred (check errno).  ENOENT means it was
 *      or -1 if some failure occurred (check errno).  ENOENT means it was
 *      not an IPv4 network specification.
 *      not an IPv4 network specification.
 * note:
 * note:
 *      network byte order assumed.  this means 192.5.5.240/28 has
 *      network byte order assumed.  this means 192.5.5.240/28 has
 *      0x11110000 in its fourth octet.
 *      0x11110000 in its fourth octet.
 * author:
 * author:
 *      Paul Vixie (ISC), June 1996
 *      Paul Vixie (ISC), June 1996
 */
 */
static int
static int
inet_net_pton_ipv4(src, dst, size)
inet_net_pton_ipv4(src, dst, size)
        const char *src;
        const char *src;
        u_char *dst;
        u_char *dst;
        size_t size;
        size_t size;
{
{
        static const char
        static const char
                xdigits[] = "0123456789abcdef",
                xdigits[] = "0123456789abcdef",
                digits[] = "0123456789";
                digits[] = "0123456789";
        int n, ch, tmp, dirty, bits;
        int n, ch, tmp, dirty, bits;
        const u_char *odst = dst;
        const u_char *odst = dst;
 
 
        ch = *src++;
        ch = *src++;
        if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
        if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
            && isascii(src[1]) && isxdigit(src[1])) {
            && isascii(src[1]) && isxdigit(src[1])) {
                /* Hexadecimal: Eat nybble string. */
                /* Hexadecimal: Eat nybble string. */
                if (size <= 0)
                if (size <= 0)
                        goto emsgsize;
                        goto emsgsize;
                *dst = 0, dirty = 0;
                *dst = 0, dirty = 0;
                src++;  /* skip x or X. */
                src++;  /* skip x or X. */
                while ((ch = *src++) != '\0' &&
                while ((ch = *src++) != '\0' &&
                       isascii(ch) && isxdigit(ch)) {
                       isascii(ch) && isxdigit(ch)) {
                        if (isupper(ch))
                        if (isupper(ch))
                                ch = tolower(ch);
                                ch = tolower(ch);
                        n = strchr(xdigits, ch) - xdigits;
                        n = strchr(xdigits, ch) - xdigits;
                        assert(n >= 0 && n <= 15);
                        assert(n >= 0 && n <= 15);
                        *dst |= n;
                        *dst |= n;
                        if (!dirty++)
                        if (!dirty++)
                                *dst <<= 4;
                                *dst <<= 4;
                        else if (size-- > 0)
                        else if (size-- > 0)
                                *++dst = 0, dirty = 0;
                                *++dst = 0, dirty = 0;
                        else
                        else
                                goto emsgsize;
                                goto emsgsize;
                }
                }
                if (dirty)
                if (dirty)
                        size--;
                        size--;
        } else if (isascii(ch) && isdigit(ch)) {
        } else if (isascii(ch) && isdigit(ch)) {
                /* Decimal: eat dotted digit string. */
                /* Decimal: eat dotted digit string. */
                for (;;) {
                for (;;) {
                        tmp = 0;
                        tmp = 0;
                        do {
                        do {
                                n = strchr(digits, ch) - digits;
                                n = strchr(digits, ch) - digits;
                                assert(n >= 0 && n <= 9);
                                assert(n >= 0 && n <= 9);
                                tmp *= 10;
                                tmp *= 10;
                                tmp += n;
                                tmp += n;
                                if (tmp > 255)
                                if (tmp > 255)
                                        goto enoent;
                                        goto enoent;
                        } while ((ch = *src++) != '\0' &&
                        } while ((ch = *src++) != '\0' &&
                                 isascii(ch) && isdigit(ch));
                                 isascii(ch) && isdigit(ch));
                        if (size-- <= 0)
                        if (size-- <= 0)
                                goto emsgsize;
                                goto emsgsize;
                        *dst++ = (u_char) tmp;
                        *dst++ = (u_char) tmp;
                        if (ch == '\0' || ch == '/')
                        if (ch == '\0' || ch == '/')
                                break;
                                break;
                        if (ch != '.')
                        if (ch != '.')
                                goto enoent;
                                goto enoent;
                        ch = *src++;
                        ch = *src++;
                        if (!isascii(ch) || !isdigit(ch))
                        if (!isascii(ch) || !isdigit(ch))
                                goto enoent;
                                goto enoent;
                }
                }
        } else
        } else
                goto enoent;
                goto enoent;
 
 
        bits = -1;
        bits = -1;
        if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) {
        if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) {
                /* CIDR width specifier.  Nothing can follow it. */
                /* CIDR width specifier.  Nothing can follow it. */
                ch = *src++;    /* Skip over the /. */
                ch = *src++;    /* Skip over the /. */
                bits = 0;
                bits = 0;
                do {
                do {
                        n = strchr(digits, ch) - digits;
                        n = strchr(digits, ch) - digits;
                        assert(n >= 0 && n <= 9);
                        assert(n >= 0 && n <= 9);
                        bits *= 10;
                        bits *= 10;
                        bits += n;
                        bits += n;
                } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
                } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
                if (ch != '\0')
                if (ch != '\0')
                        goto enoent;
                        goto enoent;
                if (bits > 32)
                if (bits > 32)
                        goto emsgsize;
                        goto emsgsize;
        }
        }
 
 
        /* Firey death and destruction unless we prefetched EOS. */
        /* Firey death and destruction unless we prefetched EOS. */
        if (ch != '\0')
        if (ch != '\0')
                goto enoent;
                goto enoent;
 
 
        /* If nothing was written to the destination, we found no address. */
        /* If nothing was written to the destination, we found no address. */
        if (dst == odst)
        if (dst == odst)
                goto enoent;
                goto enoent;
        /* If no CIDR spec was given, infer width from net class. */
        /* If no CIDR spec was given, infer width from net class. */
        if (bits == -1) {
        if (bits == -1) {
                if (*odst >= 240)       /* Class E */
                if (*odst >= 240)       /* Class E */
                        bits = 32;
                        bits = 32;
                else if (*odst >= 224)  /* Class D */
                else if (*odst >= 224)  /* Class D */
                        bits = 4;
                        bits = 4;
                else if (*odst >= 192)  /* Class C */
                else if (*odst >= 192)  /* Class C */
                        bits = 24;
                        bits = 24;
                else if (*odst >= 128)  /* Class B */
                else if (*odst >= 128)  /* Class B */
                        bits = 16;
                        bits = 16;
                else                    /* Class A */
                else                    /* Class A */
                        bits = 8;
                        bits = 8;
                /* If imputed mask is narrower than specified octets, widen. */
                /* If imputed mask is narrower than specified octets, widen. */
                if (bits >= 8 && bits < ((dst - odst) * 8))
                if (bits >= 8 && bits < ((dst - odst) * 8))
                        bits = (dst - odst) * 8;
                        bits = (dst - odst) * 8;
        }
        }
        /* Extend network to cover the actual mask. */
        /* Extend network to cover the actual mask. */
        while (bits > ((dst - odst) * 8)) {
        while (bits > ((dst - odst) * 8)) {
                if (size-- <= 0)
                if (size-- <= 0)
                        goto emsgsize;
                        goto emsgsize;
                *dst++ = '\0';
                *dst++ = '\0';
        }
        }
        return (bits);
        return (bits);
 
 
 enoent:
 enoent:
        errno = ENOENT;
        errno = ENOENT;
        return (-1);
        return (-1);
 
 
 emsgsize:
 emsgsize:
        errno = EMSGSIZE;
        errno = EMSGSIZE;
        return (-1);
        return (-1);
}
}
 
 
/*
/*
 * Weak aliases for applications that use certain private entry points,
 * Weak aliases for applications that use certain private entry points,
 * and fail to include <arpa/inet.h>.
 * and fail to include <arpa/inet.h>.
 */
 */
#undef inet_net_pton
#undef inet_net_pton
__weak_reference(__inet_net_pton, inet_net_pton);
__weak_reference(__inet_net_pton, inet_net_pton);
 
 

powered by: WebSVN 2.1.0

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