-
Notifications
You must be signed in to change notification settings - Fork 201
/
misc.c
335 lines (305 loc) · 7.36 KB
/
misc.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Layer Two Tunnelling Protocol Daemon
* Copyright (C) 1998 Adtran, Inc.
* Copyright (C) 2002 Jeff McAdams
*
* Mark Spencer
*
* This software is distributed under the terms
* of the GPL, which you should have received
* along with this source.
*
* Miscellaneous but important functions
*
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#if defined(SOLARIS)
# include <varargs.h>
#endif
#include <netinet/in.h>
#include "l2tp.h"
/* prevent deadlock that occurs when a signal handler, which interrupted a
* call to syslog(), attempts to call syslog(). */
static int syslog_nesting = 0;
#define SYSLOG_CALL(code) do { \
if (++syslog_nesting < 2) { \
code; \
} \
--syslog_nesting; \
} while(0)
#define UNUSED(x) (void)(x)
void init_log()
{
static int logopen=0;
if(!logopen) {
SYSLOG_CALL( openlog (BINARY, LOG_PID, LOG_DAEMON) );
logopen=1;
}
}
void l2tp_log (int level, const char *fmt, ...)
{
char buf[2048];
va_list args;
va_start (args, fmt);
vsnprintf (buf, sizeof (buf), fmt, args);
va_end (args);
if(gconfig.syslog) {
init_log();
SYSLOG_CALL( syslog (level, "%s", buf) );
} else {
fprintf(stderr, "xl2tpd[%d]: %s", getpid(), buf);
}
}
void set_error (struct call *c, int error, const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
c->error = error;
c->result = RESULT_ERROR;
c->needclose = -1;
vsnprintf (c->errormsg, sizeof (c->errormsg), fmt, args);
if (c->errormsg[strlen (c->errormsg) - 1] == '\n')
c->errormsg[strlen (c->errormsg) - 1] = 0;
va_end (args);
}
struct buffer *new_buf (int size)
{
struct buffer *b = NULL;
if (!size || size < 0)
return NULL;
b = malloc (sizeof (struct buffer));
if (!b)
return NULL;
b->rstart = malloc (size);
if (!b->rstart)
{
free (b);
return NULL;
}
b->start = b->rstart;
b->rend = b->rstart + size - 1;
b->len = size;
b->maxlen = size;
return b;
}
inline void recycle_buf (struct buffer *b)
{
b->start = b->rstart;
b->len = b->maxlen;
}
#define bufferDumpWIDTH 16
void bufferDump (unsigned char *buf, int buflen)
{
int i = 0, j = 0;
/* we need TWO characters to DISPLAY ONE byte */
char line[2 * bufferDumpWIDTH + 1], *c;
for (i = 0; i < buflen / bufferDumpWIDTH; i++)
{
c = line;
for (j = 0; j < bufferDumpWIDTH; j++)
{
sprintf (c, "%02x", (buf[i * bufferDumpWIDTH + j]) & 0xff);
c++;
c++; /* again two characters to display ONE byte */
}
*c = '\0';
l2tp_log (LOG_WARNING,
"%s: buflen=%d, buffer[%d]: *%s*\n", __FUNCTION__,
buflen, i, line);
}
c = line;
for (j = 0; j < buflen % bufferDumpWIDTH; j++)
{
sprintf (c, "%02x",
buf[(buflen / bufferDumpWIDTH) * bufferDumpWIDTH +
j] & 0xff);
c++;
c++;
}
if (c != line)
{
*c = '\0';
l2tp_log (LOG_WARNING,
"%s: buffer[%d]: *%s*\n", __FUNCTION__, i,
line);
}
}
void do_packet_dump (struct buffer *buf)
{
size_t x;
unsigned char *c = buf->start;
printf ("packet dump: \nHEX: { ");
for (x = 0; x < buf->len; x++)
{
printf ("%.2X ", *c);
c++;
};
printf ("}\nASCII: { ");
c = buf->start;
for (x = 0; x < buf->len; x++)
{
if (*c > 31 && *c < 127)
{
putchar (*c);
}
else
{
putchar (' ');
}
c++;
}
printf ("}\n");
}
void swaps (void *buf_v, int len)
{
#ifdef __alpha
/* Reverse byte order alpha is little endian so lest save a step.
to make things work out easier */
int x;
unsigned char t1;
unsigned char *tmp = (_u16 *) buf_v;
for (x = 0; x < len; x += 2)
{
t1 = tmp[x];
tmp[x] = tmp[x + 1];
tmp[x + 1] = t1;
}
#else
/* Reverse byte order (if proper to do so)
to make things work out easier */
int x;
struct hw { _u16 s; } __attribute__ ((packed)) *p = (struct hw *) buf_v;
for (x = 0; x < len / 2; x++, p++)
p->s = ntohs(p->s);
#endif
}
inline void toss (struct buffer *buf)
{
/*
* Toss a frame and free up the buffer that contained it
*/
free (buf->rstart);
free (buf);
}
inline void safe_copy (char *a, char *b, int size)
{
/* Copies B into A (assuming A holds MAXSTRLEN bytes)
safely */
strncpy (a, b, MIN (size, MAXSTRLEN - 1));
a[MIN (size, MAXSTRLEN - 1)] = '\000';
}
struct ppp_opts *add_opt (struct ppp_opts *option, char *fmt, ...)
{
va_list args;
struct ppp_opts *new, *last;
new = malloc (sizeof (struct ppp_opts));
if (!new)
{
l2tp_log (LOG_WARNING,
"%s : Unable to allocate ppp option memory. Expect a crash\n",
__FUNCTION__);
return option;
}
new->next = NULL;
va_start (args, fmt);
vsnprintf (new->option, sizeof (new->option), fmt, args);
va_end (args);
if (option)
{
last = option;
while (last->next)
last = last->next;
last->next = new;
return option;
}
else
return new;
}
void opt_destroy (struct ppp_opts *option)
{
struct ppp_opts *tmp;
while (option)
{
tmp = option->next;
free (option);
option = tmp;
};
}
int get_egd_entropy(char *buf, int count)
{
UNUSED(buf);
UNUSED(count);
return -1;
}
int get_sys_entropy(unsigned char *buf, int count)
{
/*
* This way of filling buf with rand() generated data is really
* fairly inefficient from a function call point of view...rand()
* returns four bytes of data (on most systems, sizeof(int))
* and we end up only using 1 byte of it (sizeof(char))...ah
* well...it was a *whole* lot easier to code this way...suggestions
* for improvements are, of course, welcome
*/
int counter;
for (counter = 0; counter < count; counter++)
{
buf[counter] = (char)rand();
}
#ifdef DEBUG_ENTROPY
bufferDump (buf, count);
#endif
return count;
}
int get_dev_entropy(unsigned char *buf, int count)
{
int devrandom;
ssize_t entropy_amount;
devrandom = open ("/dev/urandom", O_RDONLY | O_NONBLOCK);
if (devrandom == -1)
{
#ifdef DEBUG_ENTROPY
l2tp_log(LOG_WARNING, "%s: couldn't open /dev/urandom,"
"falling back to rand()\n",
__FUNCTION__);
#endif
return get_sys_entropy(buf, count);
}
entropy_amount = read(devrandom, buf, count);
close(devrandom);
return entropy_amount;
}
int get_entropy (unsigned char *buf, int count)
{
if (rand_source == RAND_SYS)
{
return get_sys_entropy(buf, count);
}
else if (rand_source == RAND_DEV)
{
return get_dev_entropy(buf, count);
}
else if (rand_source == RAND_EGD)
{
l2tp_log(LOG_WARNING,
"%s: EGD Randomness source not yet implemented\n",
__FUNCTION__);
return -1;
}
else
{
l2tp_log(LOG_WARNING,
"%s: Invalid Randomness source specified (%d)\n",
__FUNCTION__, rand_source);
return -1;
}
}