2
* OpenVPN -- An application to securely tunnel IP networks
3
* over a single UDP port, with support for SSL/TLS-based
4
* session authentication and key exchange,
5
* packet encryption, packet authentication, and
8
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License version 2
12
* as published by the Free Software Foundation.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program (see the file COPYING included with this
21
* distribution); if not, write to the Free Software Foundation, Inc.,
22
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
array_mult_safe (const size_t m1, const size_t m2, const size_t extra)
37
const size_t limit = 0xFFFFFFFF;
38
unsigned long long res = (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
39
if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit) || unlikely(res > (unsigned long long)limit))
40
msg (M_FATAL, "attemped allocation of excessively large array");
45
buf_size_error (const size_t size)
47
msg (M_FATAL, "fatal buffer size error, size=%lu", (unsigned long)size);
52
alloc_buf_debug (size_t size, const char *file, int line)
54
alloc_buf (size_t size)
58
return alloc_buf_gc_debug (size, NULL, file, line);
60
return alloc_buf_gc (size, NULL);
66
alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line)
68
alloc_buf_gc (size_t size, struct gc_arena *gc)
72
if (!buf_size_valid (size))
73
buf_size_error (size);
74
buf.capacity = (int)size;
78
buf.data = (uint8_t *) gc_malloc_debug (size, false, gc, file, line);
80
buf.data = (uint8_t *) gc_malloc (size, false, gc);
89
clone_buf_debug (const struct buffer* buf, const char *file, int line)
91
clone_buf (const struct buffer* buf)
95
ret.capacity = buf->capacity;
96
ret.offset = buf->offset;
99
ret.data = (uint8_t *) openvpn_dmalloc (file, line, buf->capacity);
101
ret.data = (uint8_t *) malloc (buf->capacity);
103
check_malloc_return (ret.data);
104
memcpy (BPTR (&ret), BPTR (buf), BLEN (buf));
108
#ifdef BUF_INIT_TRACKING
111
buf_init_debug (struct buffer *buf, int offset, const char *file, int line)
113
buf->debug_file = file;
114
buf->debug_line = line;
115
return buf_init_dowork (buf, offset);
119
buf_debug_line (const struct buffer *buf)
121
return buf->debug_line;
125
buf_debug_file (const struct buffer *buf)
127
return buf->debug_file;
132
#define buf_debug_line(buf) 0
133
#define buf_debug_file(buf) "[UNDEF]"
138
buf_clear (struct buffer *buf)
140
if (buf->capacity > 0)
141
memset (buf->data, 0, buf->capacity);
147
buf_assign (struct buffer *dest, const struct buffer *src)
149
if (!buf_init (dest, src->offset))
151
return buf_write (dest, BPTR (src), BLEN (src));
163
free_buf (struct buffer *buf)
171
* Return a buffer for write that is a subset of another buffer
174
buf_sub (struct buffer *buf, int size, bool prepend)
180
data = prepend ? buf_prepend (buf, size) : buf_write_alloc (buf, size);
190
* printf append to a buffer with overflow check
193
buf_printf (struct buffer *buf, const char *format, ...)
196
if (buf_defined (buf))
199
uint8_t *ptr = BEND (buf);
200
int cap = buf_forward_capacity (buf);
205
va_start (arglist, format);
206
stat = vsnprintf ((char *)ptr, cap, format, arglist);
208
*(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
209
buf->len += (int) strlen ((char *)ptr);
210
if (stat >= 0 && stat < cap)
218
* This is necessary due to certain buggy implementations of snprintf,
219
* that don't guarantee null termination for size > 0.
221
* This function is duplicated into service-win32/openvpnserv.c
222
* Any modifications here should be done to the other place as well.
225
int openvpn_snprintf(char *str, size_t size, const char *format, ...)
231
va_start (arglist, format);
232
ret = vsnprintf (str, size, format, arglist);
240
* write a string to the end of a buffer that was
241
* truncated by buf_printf
244
buf_catrunc (struct buffer *buf, const char *str)
246
if (buf_forward_capacity (buf) <= 1)
248
int len = (int) strlen (str) + 1;
249
if (len < buf_forward_capacity_total (buf))
251
strncpynt ((char *)(buf->data + buf->capacity - len), str, len);
257
* convert a multi-line output to one line
260
convert_to_one_line (struct buffer *buf)
262
uint8_t *cp = BPTR(buf);
272
/* NOTE: requires that string be null terminated */
274
buf_write_string_file (const struct buffer *buf, const char *filename, int fd)
276
const int len = strlen ((char *) BPTR (buf));
277
const int size = write (fd, BPTR (buf), len);
279
msg (M_ERR, "Write error on file '%s'", filename);
288
gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line)
290
gc_malloc (size_t size, bool clear, struct gc_arena *a)
298
e = (struct gc_entry *) openvpn_dmalloc (file, line, size + sizeof (struct gc_entry));
300
e = (struct gc_entry *) malloc (size + sizeof (struct gc_entry));
302
check_malloc_return (e);
303
ret = (char *) e + sizeof (struct gc_entry);
310
ret = openvpn_dmalloc (file, line, size);
314
check_malloc_return (ret);
316
#ifndef ZERO_BUFFER_ON_ALLOC
319
memset (ret, 0, size);
324
x_gc_free (struct gc_arena *a)
332
struct gc_entry *next = e->next;
339
* Transfer src arena to dest, resetting src to an empty arena.
342
gc_transfer (struct gc_arena *dest, struct gc_arena *src)
346
struct gc_entry *e = src->list;
349
while (e->next != NULL)
351
e->next = dest->list;
352
dest->list = src->list;
359
* Hex dump -- Output a binary buffer to a hex string and return it.
363
format_hex_ex (const uint8_t *data, int size, int maxoutput,
364
int space_break, const char* separator,
367
struct buffer out = alloc_buf_gc (maxoutput ? maxoutput :
368
((size * 2) + (size / space_break) * (int) strlen (separator) + 2),
371
for (i = 0; i < size; ++i)
373
if (separator && i && !(i % space_break))
374
buf_printf (&out, "%s", separator);
375
buf_printf (&out, "%02x", data[i]);
377
buf_catrunc (&out, "[more...]");
378
return (char *)out.data;
382
* remove specific trailing character
386
buf_rmtail (struct buffer *buf, uint8_t remove)
388
uint8_t *cp = BLAST(buf);
389
if (cp && *cp == remove)
397
* force a null termination even it requires
398
* truncation of the last char.
401
buf_null_terminate (struct buffer *buf)
403
char *last = (char *) BLAST (buf);
404
if (last && *last == '\0') /* already terminated? */
407
if (!buf_safe (buf, 1)) /* make space for trailing null */
408
buf_inc_len (buf, -1);
410
buf_write_u8 (buf, 0);
414
* Remove trailing \r and \n chars and ensure
418
buf_chomp (struct buffer *buf)
422
char *last = (char *) BLAST (buf);
425
if (char_class (*last, CC_CRLF|CC_NULL))
427
if (!buf_inc_len (buf, -1))
433
buf_null_terminate (buf);
437
skip_leading_whitespace (const char *str)
442
if (!(c == ' ' || c == '\t'))
450
* like buf_null_terminate, but operate on strings
453
string_null_terminate (char *str, int len, int capacity)
455
ASSERT (len >= 0 && len <= capacity && capacity > 0);
458
else if (len == capacity)
459
*(str + len - 1) = '\0';
463
* Remove trailing \r and \n chars.
468
rm_trailing_chars (str, "\r\n");
472
* Remove trailing chars
475
rm_trailing_chars (char *str, const char *what_to_delete)
479
const int len = strlen (str);
483
char *cp = str + (len - 1);
484
if (strchr (what_to_delete, *cp) != NULL)
498
string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line)
500
string_alloc (const char *str, struct gc_arena *gc)
505
const int n = strlen (str) + 1;
509
ret = (char *) gc_malloc_debug (n, false, gc, file, line);
511
ret = (char *) gc_malloc (n, false, gc);
513
memcpy (ret, str, n);
521
* Erase all characters in a string
524
string_clear (char *str)
528
const int len = strlen (str);
530
memset (str, 0, len);
535
* Return the length of a string array
538
string_array_len (const char **array)
550
print_argv (const char **p, struct gc_arena *gc, const unsigned int flags)
552
struct buffer out = alloc_buf_gc (256, gc);
556
const char *cp = *p++;
560
buf_printf (&out, " ");
561
if (flags & PA_BRACKET)
562
buf_printf (&out, "[%s]", cp);
564
buf_printf (&out, "%s", cp);
571
* Allocate a string inside a buffer
575
string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line)
577
string_alloc_buf (const char *str, struct gc_arena *gc)
585
buf_set_read (&buf, (uint8_t*) string_alloc_debug (str, gc, file, line), strlen (str) + 1);
587
buf_set_read (&buf, (uint8_t*) string_alloc (str, gc), strlen (str) + 1);
590
if (buf.len > 0) /* Don't count trailing '\0' as part of length */
601
buf_string_match_head_str (const struct buffer *src, const char *match)
603
const int size = strlen (match);
604
if (size < 0 || size > src->len)
606
return memcmp (BPTR (src), match, size) == 0;
610
buf_string_compare_advance (struct buffer *src, const char *match)
612
if (buf_string_match_head_str (src, match))
614
buf_advance (src, strlen (match));
622
buf_substring_len (const struct buffer *buf, int delim)
625
struct buffer tmp = *buf;
628
while ((c = buf_read_u8 (&tmp)) >= 0)
642
buf_parse (struct buffer *buf, const int delim, char *line, const int size)
652
c = buf_read_u8 (buf);
655
if (c <= 0 || c == delim)
664
return !(eol && !strlen (line));
668
* Print a string which might be NULL
680
* Classify and mutate strings based on character types.
684
char_class (const unsigned char c, const unsigned int flags)
691
if ((flags & CC_NULL) && c == '\0')
694
if ((flags & CC_ALNUM) && isalnum (c))
696
if ((flags & CC_ALPHA) && isalpha (c))
698
if ((flags & CC_ASCII) && isascii (c))
700
if ((flags & CC_CNTRL) && iscntrl (c))
702
if ((flags & CC_DIGIT) && isdigit (c))
704
if ((flags & CC_PRINT) && isprint (c))
706
if ((flags & CC_PUNCT) && ispunct (c))
708
if ((flags & CC_SPACE) && isspace (c))
710
if ((flags & CC_XDIGIT) && isxdigit (c))
713
if ((flags & CC_BLANK) && (c == ' ' || c == '\t'))
715
if ((flags & CC_NEWLINE) && c == '\n')
717
if ((flags & CC_CR) && c == '\r')
720
if ((flags & CC_BACKSLASH) && c == '\\')
722
if ((flags & CC_UNDERBAR) && c == '_')
724
if ((flags & CC_DASH) && c == '-')
726
if ((flags & CC_DOT) && c == '.')
728
if ((flags & CC_COMMA) && c == ',')
730
if ((flags & CC_COLON) && c == ':')
732
if ((flags & CC_SLASH) && c == '/')
734
if ((flags & CC_SINGLE_QUOTE) && c == '\'')
736
if ((flags & CC_DOUBLE_QUOTE) && c == '\"')
738
if ((flags & CC_REVERSE_QUOTE) && c == '`')
740
if ((flags & CC_AT) && c == '@')
742
if ((flags & CC_EQUAL) && c == '=')
749
char_inc_exc (const char c, const unsigned int inclusive, const unsigned int exclusive)
751
return char_class (c, inclusive) && !char_class (c, exclusive);
755
string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive)
761
if (!char_inc_exc (c, inclusive, exclusive))
768
* Modify string in place.
769
* Guaranteed to not increase string length.
772
string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
774
const char *in = str;
784
if (!char_inc_exc (c, inclusive, exclusive))
802
string_mod_const (const char *str,
803
const unsigned int inclusive,
804
const unsigned int exclusive,
810
char *buf = string_alloc (str, gc);
811
string_mod (buf, inclusive, exclusive, replace);
819
string_replace_leading (char *str, const char match, const char replace)
821
ASSERT (match != '\0');
832
#ifdef CHARACTER_CLASS_DEBUG
834
#define CC_INCLUDE (CC_PRINT)
835
#define CC_EXCLUDE (0)
836
#define CC_REPLACE ('.')
839
character_class_debug (void)
843
while (fgets (buf, sizeof (buf), stdin) != NULL)
845
string_mod (buf, CC_INCLUDE, CC_EXCLUDE, CC_REPLACE);
852
#ifdef VERIFY_ALIGNMENT
854
valign4 (const struct buffer *buf, const char *file, const int line)
858
int msglevel = D_ALIGN_DEBUG;
859
const unsigned int u = (unsigned int) BPTR (buf);
861
if (u & (PAYLOAD_ALIGN-1))
862
msglevel = D_ALIGN_ERRORS;
864
msg (msglevel, "%sAlignment at %s/%d ptr=" ptr_format " OLC=%d/%d/%d I=%s/%d",
865
(msglevel == D_ALIGN_ERRORS) ? "ERROR: " : "",
872
buf_debug_file (buf),
873
buf_debug_line (buf));
882
#ifdef ENABLE_BUFFER_LIST
885
buffer_list_new (const int max_size)
887
struct buffer_list *ret;
888
ALLOC_OBJ_CLEAR (ret, struct buffer_list);
889
ret->max_size = max_size;
895
buffer_list_free (struct buffer_list *ol)
899
buffer_list_reset (ol);
905
buffer_list_defined (const struct buffer_list *ol)
907
return ol->head != NULL;
911
buffer_list_reset (struct buffer_list *ol)
913
struct buffer_entry *e = ol->head;
916
struct buffer_entry *next = e->next;
921
ol->head = ol->tail = NULL;
926
buffer_list_push (struct buffer_list *ol, const unsigned char *str)
930
const size_t len = strlen ((const char *)str);
931
struct buffer_entry *e = buffer_list_push_data (ol, str, len+1);
933
e->buf.len = len; /* Don't count trailing '\0' as part of length */
937
struct buffer_entry *
938
buffer_list_push_data (struct buffer_list *ol, const uint8_t *data, size_t size)
940
struct buffer_entry *e = NULL;
941
if (data && (!ol->max_size || ol->size < ol->max_size))
943
ALLOC_OBJ_CLEAR (e, struct buffer_entry);
956
e->buf = alloc_buf (size);
957
memcpy (e->buf.data, data, size);
958
e->buf.len = (int)size;
965
buffer_list_peek (struct buffer_list *ol)
968
return &ol->head->buf;
974
buffer_list_aggregate (struct buffer_list *bl, const size_t max)
978
struct buffer_entry *more = bl->head;
981
for (count = 0; more && size <= max; ++count)
983
size += BLEN(&more->buf);
990
struct buffer_entry *e = bl->head, *f;
992
ALLOC_OBJ_CLEAR (f, struct buffer_entry);
993
f->buf.data = malloc (size);
994
check_malloc_return (f->buf.data);
995
f->buf.capacity = size;
996
for (i = 0; e && i < count; ++i)
998
struct buffer_entry *next = e->next;
999
buf_copy (&f->buf, &e->buf);
1013
buffer_list_pop (struct buffer_list *ol)
1017
struct buffer_entry *e = ol->head->next;
1018
free_buf (&ol->head->buf);
1028
buffer_list_advance (struct buffer_list *ol, int n)
1032
struct buffer *buf = &ol->head->buf;
1033
ASSERT (buf_advance (buf, n));
1035
buffer_list_pop (ol);
1039
struct buffer_list *
1040
buffer_list_file (const char *fn, int max_line_len)
1042
FILE *fp = fopen (fn, "r");
1043
struct buffer_list *bl = NULL;
1047
char *line = (char *) malloc (max_line_len);
1050
bl = buffer_list_new (0);
1051
while (fgets (line, max_line_len, fp) != NULL)
1052
buffer_list_push (bl, (unsigned char *)line);