source: rtems-libbsd/ipsec-tools/src/racoon/misc.c @ b376ae1

55-freebsd-126-freebsd-12
Last change on this file since b376ae1 was b376ae1, checked in by Christian Mauderer <christian.mauderer@…>, on 05/03/18 at 12:15:11

ipsec-tools: Port libipsec, setkey and racoon.

Note that this replaces the libipsec from FreeBSD with the one provided
by ipsec-tools.

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