Annotation of embedaddon/ipsec-tools/src/racoon/misc.c, revision 1.1.1.1

1.1       misho       1: /*     $NetBSD: misc.c,v 1.6 2008/07/15 00:47:09 mgrooms Exp $ */
                      2: 
                      3: /*     $KAME: misc.c,v 1.23 2001/08/16 14:37:29 itojun Exp $   */
                      4: 
                      5: /*
                      6:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      7:  * All rights reserved.
                      8:  * 
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
                     17:  * 3. Neither the name of the project nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  * 
                     21:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  */
                     33: 
                     34: #include "config.h"
                     35: 
                     36: #include <sys/types.h>
                     37: #include <sys/param.h>
                     38: #include <sys/stat.h>
                     39: #include <sys/time.h>
                     40: 
                     41: #include <stdlib.h>
                     42: #include <stdio.h>
                     43: #include <string.h>
                     44: #include <errno.h>
                     45: #include <syslog.h>
                     46: #include <ctype.h>
                     47: #include <fcntl.h>
                     48: 
                     49: #include "var.h"
                     50: #include "misc.h"
                     51: #include "debug.h"
                     52: 
                     53: #if 0
                     54: static int bindump __P((void *, size_t));
                     55: 
                     56: static int
                     57: bindump(buf0, len)
                     58:         void *buf0;
                     59:         size_t len;
                     60: {
                     61:        unsigned char *buf = (unsigned char *)buf0;
                     62:        size_t i;
                     63: 
                     64:        for (i = 0; i < len; i++) {
                     65:                if ((buf[i] & 0x80) || !isprint(buf[i]))
                     66:                        printf("\\x%x", buf[i]);
                     67:                else
                     68:                        printf("%c", buf[i]);
                     69:        }
                     70:        printf("\n");
                     71: 
                     72:        return 0;
                     73: }
                     74: #endif
                     75: 
                     76: int
                     77: racoon_hexdump(buf0, len)
                     78:        void *buf0;
                     79:        size_t len;
                     80: {
                     81:        caddr_t buf = (caddr_t)buf0;
                     82:        size_t i;
                     83: 
                     84:        for (i = 0; i < len; i++) {
                     85:                if (i != 0 && i % 32 == 0)
                     86:                        printf("\n");
                     87:                if (i % 4 == 0)
                     88:                        printf(" ");
                     89:                printf("%02x", (unsigned char)buf[i]);
                     90:        }
                     91:        printf("\n");
                     92: 
                     93:        return 0;
                     94: }
                     95: 
                     96: char *
                     97: bit2str(n, bl)
                     98:        int n, bl;
                     99: {
                    100: #define MAXBITLEN 128
                    101:        static char b[MAXBITLEN + 1];
                    102:        int i;
                    103: 
                    104:        if (bl > MAXBITLEN)
                    105:                return "Failed to convert.";    /* NG */
                    106:        memset(b, '0', bl);
                    107:        b[bl] = '\0';
                    108: 
                    109:        for (i = 0; i < bl; i++) {
                    110:                if (n & (1 << i))
                    111:                        b[bl - 1 - i] = '1';
                    112:        }
                    113: 
                    114:        return b;
                    115: }
                    116: 
                    117: const char *
                    118: debug_location(file, line, func)
                    119:        const char *file;
                    120:        int line;
                    121:        const char *func;
                    122: {
                    123:        static char buf[1024];
                    124:        const char *p;
                    125: 
                    126:        /* truncate pathname */
                    127:        p = strrchr(file, '/');
                    128:        if (p)
                    129:                p++;
                    130:        else
                    131:                p = file;
                    132: 
                    133:        if (func)
                    134:                snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
                    135:        else
                    136:                snprintf(buf, sizeof(buf), "%s:%d", p, line);
                    137: 
                    138:        return buf;
                    139: }
                    140: 
                    141: /*
                    142:  * get file size.
                    143:  * -1: error occured.
                    144:  */
                    145: int
                    146: getfsize(path)
                    147:        char *path;
                    148: {
                    149:         struct stat st;
                    150: 
                    151:         if (stat(path, &st) != 0)
                    152:                 return -1;
                    153:         else
                    154:                 return st.st_size;
                    155: }
                    156: 
                    157: /*
                    158:  * set the close-on-exec flag for file descriptor fd.
                    159:  */
                    160: void
                    161: close_on_exec(fd)
                    162:        int fd;
                    163: {
                    164:        fcntl(fd, F_SETFD, FD_CLOEXEC);
                    165: }
                    166: 
                    167: /*
                    168:  * calculate the difference between two times.
                    169:  * t1: start
                    170:  * t2: end
                    171:  */
                    172: double
                    173: timedelta(t1, t2)
                    174:        struct timeval *t1, *t2;
                    175: {
                    176:        if (t2->tv_usec >= t1->tv_usec)
                    177:                return t2->tv_sec - t1->tv_sec +
                    178:                        (double)(t2->tv_usec - t1->tv_usec) / 1000000;
                    179: 
                    180:        return t2->tv_sec - t1->tv_sec - 1 +
                    181:                (double)(1000000 + t2->tv_usec - t1->tv_usec) / 1000000;
                    182: }

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