forked from jech/shncpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
192 lines (165 loc) · 4.34 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright (c) 2015 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdarg.h>
#include <sys/mman.h>
#include "shncpd.h"
#include "util.h"
int
read_random_bytes(void *buf, int len)
{
int fd, rc;
fd = open("/dev/urandom", O_RDONLY);
if(fd < 0) {
errno = ENOSYS;
return -1;
}
rc = read(fd, buf, len);
if(rc < len)
rc = -1;
close(fd);
return rc;
}
int
gettime(struct timespec *ts)
{
return clock_gettime(CLOCK_MONOTONIC, ts);
}
int
ts_compare(const struct timespec *s1, const struct timespec *s2)
{
if(s1->tv_sec < s2->tv_sec)
return -1;
else if(s1->tv_sec > s2->tv_sec)
return 1;
else if(s1->tv_nsec < s2->tv_nsec)
return -1;
else if(s1->tv_nsec > s2->tv_nsec)
return 1;
else
return 0;
}
/* {0, 0} represents infinity */
void
ts_min(struct timespec *d, const struct timespec *s)
{
if(s->tv_sec == 0)
return;
if(d->tv_sec == 0 || ts_compare(d, s) > 0) {
*d = *s;
}
}
void
ts_minus(struct timespec *d,
const struct timespec *s1, const struct timespec *s2)
{
if(s1->tv_nsec >= s2->tv_nsec) {
d->tv_nsec = s1->tv_nsec - s2->tv_nsec;
d->tv_sec = s1->tv_sec - s2->tv_sec;
} else {
d->tv_nsec = s1->tv_nsec + 1000000000 - s2->tv_nsec;
d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
}
}
int
ts_minus_msec(const struct timespec *s1, const struct timespec *s2)
{
return (s1->tv_sec - s2->tv_sec) * 1000 +
(s1->tv_nsec - s2->tv_nsec) / 1000000;
}
static void
ts_add_nsec(struct timespec *d, const struct timespec *s, long long nsecs)
{
*d = *s;
while(nsecs + d->tv_nsec > 1000000000) {
d->tv_sec += 1;
nsecs -= 1000000000LL;
}
while(nsecs + d->tv_nsec < 0) {
d->tv_sec -= 1;
nsecs += 1000000000LL;
}
d->tv_nsec += nsecs;
}
static const long long million = 1000000LL;
void
ts_add_msec(struct timespec *d, const struct timespec *s, int msecs)
{
ts_add_nsec(d, s, msecs * million);
}
void
ts_add_random(struct timespec *d, const struct timespec *s, int msecs)
{
ts_add_nsec(d, s, (random() % msecs) * million + random() % million);
}
void
ts_zero(struct timespec *d)
{
d->tv_sec = 0;
d->tv_nsec = 0;
}
const char *
format_32(const unsigned char *data)
{
static char buf[4][16];
static int i = 0;
i = (i + 1) % 4;
snprintf(buf[i], 16, "%02x:%02x:%02x:%02x",
data[0], data[1], data[2], data[3]);
return buf[i];
}
const char *
format_64(const unsigned char *data)
{
static char buf[4][28];
static int i = 0;
i = (i + 1) % 4;
snprintf(buf[i], 28, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7]);
return buf[i];
}
void
do_debugf(int level, const char *format, ...)
{
va_list args;
va_start(args, format);
if(debug_level >= level) {
vfprintf(stderr, format, args);
fflush(stderr);
}
va_end(args);
}
void *
allocate_buffer(int size)
{
void *p;
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if(p == MAP_FAILED)
return NULL;
return p;
}