-
Notifications
You must be signed in to change notification settings - Fork 16
/
rtpp_util.c
261 lines (233 loc) · 6.64 KB
/
rtpp_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright (c) 2004-2006 Maxim Sobolev <[email protected]>
* Copyright (c) 2006-2007 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <math.h>
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rtpp_util.h"
#include "rtpp_log.h"
double
getdtime(void)
{
struct timeval timev;
if (gettimeofday(&timev, NULL) == -1)
return -1;
return timev.tv_sec + ((double)timev.tv_usec) / 1000000.0;
}
void
dtime2ts(double dtime, uint32_t *ts_sec, uint32_t *ts_usec)
{
*ts_sec = trunc(dtime);
*ts_usec = round(1000000.0 * (dtime - ((double)*ts_sec)));
}
void
seedrandom(void)
{
int fd;
unsigned long junk;
struct timeval tv;
fd = open("/dev/random", O_RDONLY, 0);
if (fd >= 0) {
read(fd, &junk, sizeof(junk));
close(fd);
}
gettimeofday(&tv, NULL);
srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
}
int
drop_privileges(struct cfg *cf)
{
if (cf->stable.run_gname != NULL) {
if (setgid(cf->stable.run_gid) != 0) {
rtpp_log_ewrite(RTPP_LOG_ERR, cf->stable.glog, "can't set current group ID: %d", cf->stable.run_gid);
return -1;
}
}
if (cf->stable.run_uname == NULL)
return 0;
if (setuid(cf->stable.run_uid) != 0) {
rtpp_log_ewrite(RTPP_LOG_ERR, cf->stable.glog, "can't set current user ID: %d", cf->stable.run_uid);
return -1;
}
return 0;
}
void
init_port_table(struct cfg *cf)
{
int i, j;
uint16_t portnum;
/* Generate linear table */
cf->stable.port_table_len = ((cf->stable.port_max - cf->stable.port_min) / 2) + 1;
portnum = cf->stable.port_min;
for (i = 0; i < cf->stable.port_table_len; i += 1) {
cf->stable.port_table[i] = portnum;
portnum += 2;
}
#if !defined(SEQUENTAL_PORTS)
/* Shuffle elements ramdomly */
for (i = 0; i < cf->stable.port_table_len; i += 1) {
j = random() % cf->stable.port_table_len;
portnum = cf->stable.port_table[i];
cf->stable.port_table[i] = cf->stable.port_table[j];
cf->stable.port_table[j] = portnum;
}
#endif
/* Set the last used element to be the last element */
cf->port_table_idx = cf->stable.port_table_len - 1;
}
/*
* Portable strsep(3) implementation, borrowed from FreeBSD. For license
* and other information see:
*
* $FreeBSD: src/lib/libc/string/strsep.c,v 1.6 2007/01/09 00:28:12 imp Exp $
*/
char *
rtpp_strsep(char **stringp, const char *delim)
{
char *s;
const char *spanp;
int c, sc;
char *tok;
if ((s = *stringp) == NULL)
return (NULL);
for (tok = s;;) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}
/*
* Portable daemon(3) implementation, borrowed from FreeBSD. For license
* and other information see:
*
* $FreeBSD: src/lib/libc/gen/daemon.c,v 1.8 2007/01/09 00:27:53 imp Exp $
*/
int
rtpp_daemon(int nochdir, int noclose)
{
struct sigaction osa, sa;
int fd;
pid_t newgrp;
int oerrno;
int osa_ok;
/* A SIGHUP may be thrown when the parent exits below. */
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
osa_ok = sigaction(SIGHUP, &sa, &osa);
switch (fork()) {
case -1:
return (-1);
case 0:
break;
default:
_exit(0);
}
newgrp = setsid();
oerrno = errno;
if (osa_ok != -1)
sigaction(SIGHUP, &osa, NULL);
if (newgrp == -1) {
errno = oerrno;
return (-1);
}
if (!nochdir)
(void)chdir("/");
if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO);
if (fd > 2)
(void)close(fd);
}
return (0);
}
static int8_t hex2char[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
int
url_unquote(uint8_t *buf, int len)
{
int outlen;
uint8_t *cp;
outlen = len;
while (len > 0) {
cp = memchr(buf, '%', len);
if (cp == NULL)
return (outlen);
if (cp - buf + 2 > len)
return (-1);
if (cp[1] > 127 || cp[2] > 127 ||
hex2char[cp[1]] == -1 || hex2char[cp[2]] == -1)
return (-1);
cp[0] = (hex2char[cp[1]] << 4) | hex2char[cp[2]];
len -= cp - buf + 3;
if (len > 0)
memmove(cp + 1, cp + 3, len);
buf = cp + 1;
outlen -= 2;
}
return (outlen);
}
int
pthread_mutex_islocked(pthread_mutex_t *mutex)
{
int rval;
rval = pthread_mutex_trylock(mutex);
if (rval != 0) {
if (rval == EBUSY)
return (1);
return (-1);
}
pthread_mutex_unlock(mutex);
return (0);
}