Annotation of embedaddon/libpdel/structs/type/structs_type_ip4.c, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include <sys/types.h>
                     42: #include <sys/socket.h>
                     43: #include <netinet/in.h>
                     44: #include <arpa/inet.h>
                     45: #include <net/ethernet.h>
                     46: 
                     47: #include <stdio.h>
                     48: #include <stdlib.h>
                     49: #include <stdarg.h>
                     50: #include <string.h>
                     51: #include <errno.h>
                     52: #include <ctype.h>
                     53: 
                     54: #include "structs/structs.h"
                     55: #include "structs/type/array.h"
                     56: #include "structs/type/ip4.h"
                     57: #include "util/typed_mem.h"
                     58: 
                     59: /*********************************************************************
                     60:                        IPv4 ADDRESS TYPE
                     61: *********************************************************************/
                     62: 
                     63: static structs_ascify_t                structs_ip4_ascify;
                     64: static structs_binify_t                structs_ip4_binify;
                     65: 
                     66: static char *
                     67: structs_ip4_ascify(const struct structs_type *type,
                     68:        const char *mtype, const void *data)
                     69: {
                     70:        const u_char *const bytes
                     71:            = (const u_char *)&((const struct in_addr *)data)->s_addr;
                     72:        char buf[16];
                     73: 
                     74:        /* Don't use inet_ntoa(), it's not thread safe */
                     75:        snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
                     76:            bytes[0], bytes[1], bytes[2], bytes[3]);
                     77:        return (STRDUP(mtype, buf));
                     78: }
                     79: 
                     80: static int
                     81: structs_ip4_binify(const struct structs_type *type,
                     82:        const char *ascii, void *data, char *ebuf, size_t emax)
                     83: {
                     84:        char buf[32];
                     85:        char *s;
                     86:        char *t;
                     87:        int val;
                     88:        int i;
                     89: 
                     90:        /* Trim whitespace */
                     91:        while (isspace(*ascii))
                     92:                ascii++;
                     93:        strlcpy(buf, ascii, sizeof(buf));
                     94:        for (i = strlen(buf); i > 0 && isspace(buf[i - 1]); i--);
                     95:        buf[i] = '\0';
                     96: 
                     97:        /* Parse each byte */
                     98:        for (s = buf, i = 0; i < 4; s = t, i++) {
                     99:                for (t = s; isdigit(*t); t++);
                    100:                if (*t != ((i < 3) ? '.' : '\0'))
                    101:                        goto bogus;
                    102:                if (t - s < 1 || t - s > 3)
                    103:                        goto bogus;
                    104:                *t++ = '\0';
                    105:                if ((val = atoi(s)) > 255)
                    106:                        goto bogus;
                    107:                ((u_char *)data)[i] = (u_char)val;
                    108:        }
                    109: 
                    110:        /* OK */
                    111:        return (0);
                    112: 
                    113: bogus:
                    114:        strlcpy(ebuf, "invalid IP address", emax);
                    115:        errno = EINVAL;
                    116:        return (-1);
                    117: }
                    118: 
                    119: const struct structs_type structs_type_ip4 = {
                    120:        sizeof(struct in_addr),
                    121:        "ip4",
                    122:        STRUCTS_TYPE_PRIMITIVE,
                    123:        structs_region_init,
                    124:        structs_region_copy,
                    125:        structs_region_equal,
                    126:        structs_ip4_ascify,
                    127:        structs_ip4_binify,
                    128:        structs_region_encode,
                    129:        structs_region_decode,
                    130:        structs_nothing_free,
                    131: };
                    132: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>