xref: /netbsd-src/crypto/dist/ipsec-tools/src/racoon/misc.c (revision 79383b8281bb2621b66b0a2bf9794c33bdf339a3)
1 /*	$NetBSD: misc.c,v 1.7 2018/05/19 20:40:40 maxv 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 int
racoon_hexdump(void * buf0,size_t len)54 racoon_hexdump(void *buf0, size_t len)
55 {
56 	caddr_t buf = (caddr_t)buf0;
57 	size_t i;
58 
59 	for (i = 0; i < len; i++) {
60 		if (i != 0 && i % 32 == 0)
61 			printf("\n");
62 		if (i % 4 == 0)
63 			printf(" ");
64 		printf("%02x", (unsigned char)buf[i]);
65 	}
66 	printf("\n");
67 
68 	return 0;
69 }
70 
71 char *
bit2str(int n,int bl)72 bit2str(int n, int bl)
73 {
74 #define MAXBITLEN 128
75 	static char b[MAXBITLEN + 1];
76 	int i;
77 
78 	if (bl > MAXBITLEN)
79 		return "Failed to convert.";	/* NG */
80 	memset(b, '0', bl);
81 	b[bl] = '\0';
82 
83 	for (i = 0; i < bl; i++) {
84 		if (n & (1 << i))
85 			b[bl - 1 - i] = '1';
86 	}
87 
88 	return b;
89 }
90 
91 const char *
debug_location(const char * file,int line,const char * func)92 debug_location(const char *file, int line, const char *func)
93 {
94 	static char buf[1024];
95 	const char *p;
96 
97 	/* truncate pathname */
98 	p = strrchr(file, '/');
99 	if (p)
100 		p++;
101 	else
102 		p = file;
103 
104 	if (func)
105 		snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
106 	else
107 		snprintf(buf, sizeof(buf), "%s:%d", p, line);
108 
109 	return buf;
110 }
111 
112 /*
113  * get file size.
114  * -1: error occured.
115  */
116 int
getfsize(char * path)117 getfsize(char *path)
118 {
119 	struct stat st;
120 
121 	if (stat(path, &st) != 0)
122 		return -1;
123 	else
124 		return st.st_size;
125 }
126 
127 /*
128  * set the close-on-exec flag for file descriptor fd.
129  */
130 void
close_on_exec(int fd)131 close_on_exec(int fd)
132 {
133 	fcntl(fd, F_SETFD, FD_CLOEXEC);
134 }
135 
136 /*
137  * calculate the difference between two times.
138  * t1: start
139  * t2: end
140  */
141 double
timedelta(struct timeval * t1,struct timeval * t2)142 timedelta(struct timeval *t1, struct timeval *t2)
143 {
144 	if (t2->tv_usec >= t1->tv_usec)
145 		return t2->tv_sec - t1->tv_sec +
146 			(double)(t2->tv_usec - t1->tv_usec) / 1000000;
147 
148 	return t2->tv_sec - t1->tv_sec - 1 +
149 		(double)(1000000 + t2->tv_usec - t1->tv_usec) / 1000000;
150 }
151