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-2009 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
36 array_mult_safe (const size_t m1, const size_t m2, const size_t extra)
38 const size_t limit = 0xFFFFFFFF;
39 unsigned long long res = (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
40 if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit) || unlikely(res > (unsigned long long)limit))
41 msg (M_FATAL, "attemped allocation of excessively large array");
46 buf_size_error (size_t size)
48 msg (M_FATAL, "fatal buffer size error, size=%lu", (unsigned long)size);
53 alloc_buf_debug (size_t size, const char *file, int line)
55 alloc_buf (size_t size)
59 return alloc_buf_gc_debug (size, NULL, file, line);
61 return alloc_buf_gc (size, NULL);
67 alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line)
69 alloc_buf_gc (size_t size, struct gc_arena *gc)
73 if (!buf_size_valid (size))
74 buf_size_error (size);
75 buf.capacity = (int)size;
79 buf.data = (uint8_t *) gc_malloc_debug (size, false, gc, file, line);
81 buf.data = (uint8_t *) gc_malloc (size, false, gc);
90 clone_buf_debug (const struct buffer* buf, const char *file, int line)
92 clone_buf (const struct buffer* buf)
96 ret.capacity = buf->capacity;
97 ret.offset = buf->offset;
100 ret.data = (uint8_t *) openvpn_dmalloc (file, line, buf->capacity);
102 ret.data = (uint8_t *) malloc (buf->capacity);
104 check_malloc_return (ret.data);
105 memcpy (BPTR (&ret), BPTR (buf), BLEN (buf));
109 #ifdef BUF_INIT_TRACKING
112 buf_init_debug (struct buffer *buf, int offset, const char *file, int line)
114 buf->debug_file = file;
115 buf->debug_line = line;
116 return buf_init_dowork (buf, offset);
120 buf_debug_line (const struct buffer *buf)
122 return buf->debug_line;
126 buf_debug_file (const struct buffer *buf)
128 return buf->debug_file;
133 #define buf_debug_line(buf) 0
134 #define buf_debug_file(buf) "[UNDEF]"
139 buf_clear (struct buffer *buf)
141 if (buf->capacity > 0)
142 memset (buf->data, 0, buf->capacity);
148 buf_assign (struct buffer *dest, const struct buffer *src)
150 if (!buf_init (dest, src->offset))
152 return buf_write (dest, BPTR (src), BLEN (src));
164 free_buf (struct buffer *buf)
172 * Return a buffer for write that is a subset of another buffer
175 buf_sub (struct buffer *buf, int size, bool prepend)
181 data = prepend ? buf_prepend (buf, size) : buf_write_alloc (buf, size);
191 * printf append to a buffer with overflow check
194 buf_printf (struct buffer *buf, const char *format, ...)
197 if (buf_defined (buf))
200 uint8_t *ptr = BEND (buf);
201 int cap = buf_forward_capacity (buf);
206 va_start (arglist, format);
207 stat = vsnprintf ((char *)ptr, cap, format, arglist);
209 *(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
210 buf->len += (int) strlen ((char *)ptr);
211 if (stat >= 0 && stat < cap)
219 * This is necessary due to certain buggy implementations of snprintf,
220 * that don't guarantee null termination for size > 0.
223 int openvpn_snprintf(char *str, size_t size, const char *format, ...)
229 va_start (arglist, format);
230 ret = vsnprintf (str, size, format, arglist);
238 * write a string to the end of a buffer that was
239 * truncated by buf_printf
242 buf_catrunc (struct buffer *buf, const char *str)
244 if (buf_forward_capacity (buf) <= 1)
246 int len = (int) strlen (str) + 1;
247 if (len < buf_forward_capacity_total (buf))
249 strncpynt ((char *)(buf->data + buf->capacity - len), str, len);
255 * convert a multi-line output to one line
258 convert_to_one_line (struct buffer *buf)
260 uint8_t *cp = BPTR(buf);
270 /* NOTE: requires that string be null terminated */
272 buf_write_string_file (const struct buffer *buf, const char *filename, int fd)
274 const int len = strlen ((char *) BPTR (buf));
275 const int size = write (fd, BPTR (buf), len);
277 msg (M_ERR, "Write error on file '%s'", filename);
286 gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line)
288 gc_malloc (size_t size, bool clear, struct gc_arena *a)
296 e = (struct gc_entry *) openvpn_dmalloc (file, line, size + sizeof (struct gc_entry));
298 e = (struct gc_entry *) malloc (size + sizeof (struct gc_entry));
300 check_malloc_return (e);
301 ret = (char *) e + sizeof (struct gc_entry);
302 /*mutex_lock_static (L_GC_MALLOC);*/
305 /*mutex_unlock_static (L_GC_MALLOC);*/
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)
327 /*mutex_lock_static (L_GC_MALLOC);*/
330 /*mutex_unlock_static (L_GC_MALLOC);*/
334 struct gc_entry *next = e->next;
341 * Transfer src arena to dest, resetting src to an empty arena.
344 gc_transfer (struct gc_arena *dest, struct gc_arena *src)
348 struct gc_entry *e = src->list;
351 while (e->next != NULL)
353 e->next = dest->list;
354 dest->list = src->list;
361 * Hex dump -- Output a binary buffer to a hex string and return it.
365 format_hex_ex (const uint8_t *data, int size, int maxoutput,
366 int space_break, const char* separator,
369 struct buffer out = alloc_buf_gc (maxoutput ? maxoutput :
370 ((size * 2) + (size / space_break) * (int) strlen (separator) + 2),
373 for (i = 0; i < size; ++i)
375 if (separator && i && !(i % space_break))
376 buf_printf (&out, "%s", separator);
377 buf_printf (&out, "%02x", data[i]);
379 buf_catrunc (&out, "[more...]");
380 return (char *)out.data;
384 * remove specific trailing character
388 buf_rmtail (struct buffer *buf, uint8_t remove)
390 uint8_t *cp = BLAST(buf);
391 if (cp && *cp == remove)
399 * force a null termination even it requires
400 * truncation of the last char.
403 buf_null_terminate (struct buffer *buf)
405 char *last = (char *) BLAST (buf);
406 if (last && *last == '\0') /* already terminated? */
409 if (!buf_safe (buf, 1)) /* make space for trailing null */
410 buf_inc_len (buf, -1);
412 buf_write_u8 (buf, 0);
416 * Remove trailing \r and \n chars and ensure
420 buf_chomp (struct buffer *buf)
424 char *last = (char *) BLAST (buf);
427 if (char_class (*last, CC_CRLF|CC_NULL))
429 if (!buf_inc_len (buf, -1))
435 buf_null_terminate (buf);
439 skip_leading_whitespace (const char *str)
444 if (!(c == ' ' || c == '\t'))
452 * like buf_null_terminate, but operate on strings
455 string_null_terminate (char *str, int len, int capacity)
457 ASSERT (len >= 0 && len <= capacity && capacity > 0);
460 else if (len == capacity)
461 *(str + len - 1) = '\0';
465 * Remove trailing \r and \n chars.
470 rm_trailing_chars (str, "\r\n");
474 * Remove trailing chars
477 rm_trailing_chars (char *str, const char *what_to_delete)
481 const int len = strlen (str);
485 char *cp = str + (len - 1);
486 if (strchr (what_to_delete, *cp) != NULL)
500 string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line)
502 string_alloc (const char *str, struct gc_arena *gc)
507 const int n = strlen (str) + 1;
511 ret = (char *) gc_malloc_debug (n, false, gc, file, line);
513 ret = (char *) gc_malloc (n, false, gc);
515 memcpy (ret, str, n);
523 * Erase all characters in a string
526 string_clear (char *str)
530 const int len = strlen (str);
532 memset (str, 0, len);
537 * Return the length of a string array
540 string_array_len (const char **array)
552 print_argv (const char **p, struct gc_arena *gc, const unsigned int flags)
554 struct buffer out = alloc_buf_gc (256, gc);
558 const char *cp = *p++;
562 buf_printf (&out, " ");
563 if (flags & PA_BRACKET)
564 buf_printf (&out, "[%s]", cp);
566 buf_printf (&out, "%s", cp);
573 * Allocate a string inside a buffer
577 string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line)
579 string_alloc_buf (const char *str, struct gc_arena *gc)
587 buf_set_read (&buf, (uint8_t*) string_alloc_debug (str, gc, file, line), strlen (str) + 1);
589 buf_set_read (&buf, (uint8_t*) string_alloc (str, gc), strlen (str) + 1);
592 if (buf.len > 0) /* Don't count trailing '\0' as part of length */
603 buf_string_match_head_str (const struct buffer *src, const char *match)
605 const int size = strlen (match);
606 if (size < 0 || size > src->len)
608 return memcmp (BPTR (src), match, size) == 0;
612 buf_string_compare_advance (struct buffer *src, const char *match)
614 if (buf_string_match_head_str (src, match))
616 buf_advance (src, strlen (match));
624 buf_substring_len (const struct buffer *buf, int delim)
627 struct buffer tmp = *buf;
630 while ((c = buf_read_u8 (&tmp)) >= 0)
644 buf_parse (struct buffer *buf, const int delim, char *line, const int size)
654 c = buf_read_u8 (buf);
657 if (c <= 0 || c == delim)
666 return !(eol && !strlen (line));
670 * Print a string which might be NULL
682 * Classify and mutate strings based on character types.
686 char_class (const unsigned char c, const unsigned int flags)
693 if ((flags & CC_NULL) && c == '\0')
696 if ((flags & CC_ALNUM) && isalnum (c))
698 if ((flags & CC_ALPHA) && isalpha (c))
700 if ((flags & CC_ASCII) && isascii (c))
702 if ((flags & CC_CNTRL) && iscntrl (c))
704 if ((flags & CC_DIGIT) && isdigit (c))
706 if ((flags & CC_PRINT) && isprint (c))
708 if ((flags & CC_PUNCT) && ispunct (c))
710 if ((flags & CC_SPACE) && isspace (c))
712 if ((flags & CC_XDIGIT) && isxdigit (c))
715 if ((flags & CC_BLANK) && (c == ' ' || c == '\t'))
717 if ((flags & CC_NEWLINE) && c == '\n')
719 if ((flags & CC_CR) && c == '\r')
722 if ((flags & CC_BACKSLASH) && c == '\\')
724 if ((flags & CC_UNDERBAR) && c == '_')
726 if ((flags & CC_DASH) && c == '-')
728 if ((flags & CC_DOT) && c == '.')
730 if ((flags & CC_COMMA) && c == ',')
732 if ((flags & CC_COLON) && c == ':')
734 if ((flags & CC_SLASH) && c == '/')
736 if ((flags & CC_SINGLE_QUOTE) && c == '\'')
738 if ((flags & CC_DOUBLE_QUOTE) && c == '\"')
740 if ((flags & CC_REVERSE_QUOTE) && c == '`')
742 if ((flags & CC_AT) && c == '@')
744 if ((flags & CC_EQUAL) && c == '=')
751 char_inc_exc (const char c, const unsigned int inclusive, const unsigned int exclusive)
753 return char_class (c, inclusive) && !char_class (c, exclusive);
757 string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive)
763 if (!char_inc_exc (c, inclusive, exclusive))
770 * Modify string in place.
771 * Guaranteed to not increase string length.
774 string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
776 const char *in = str;
786 if (!char_inc_exc (c, inclusive, exclusive))
804 string_mod_const (const char *str,
805 const unsigned int inclusive,
806 const unsigned int exclusive,
812 char *buf = string_alloc (str, gc);
813 string_mod (buf, inclusive, exclusive, replace);
821 string_replace_leading (char *str, const char match, const char replace)
823 ASSERT (match != '\0');
834 #ifdef CHARACTER_CLASS_DEBUG
836 #define CC_INCLUDE (CC_PRINT)
837 #define CC_EXCLUDE (0)
838 #define CC_REPLACE ('.')
841 character_class_debug (void)
845 while (fgets (buf, sizeof (buf), stdin) != NULL)
847 string_mod (buf, CC_INCLUDE, CC_EXCLUDE, CC_REPLACE);
854 #ifdef VERIFY_ALIGNMENT
856 valign4 (const struct buffer *buf, const char *file, const int line)
860 int msglevel = D_ALIGN_DEBUG;
861 const unsigned int u = (unsigned int) BPTR (buf);
863 if (u & (PAYLOAD_ALIGN-1))
864 msglevel = D_ALIGN_ERRORS;
866 msg (msglevel, "%sAlignment at %s/%d ptr=" ptr_format " OLC=%d/%d/%d I=%s/%d",
867 (msglevel == D_ALIGN_ERRORS) ? "ERROR: " : "",
874 buf_debug_file (buf),
875 buf_debug_line (buf));
884 #ifdef ENABLE_BUFFER_LIST
887 buffer_list_new (const int max_size)
889 struct buffer_list *ret;
890 ALLOC_OBJ_CLEAR (ret, struct buffer_list);
891 ret->max_size = max_size;
897 buffer_list_free (struct buffer_list *ol)
899 buffer_list_reset (ol);
904 buffer_list_defined (const struct buffer_list *ol)
906 return ol->head != NULL;
910 buffer_list_reset (struct buffer_list *ol)
912 struct buffer_entry *e = ol->head;
915 struct buffer_entry *next = e->next;
920 ol->head = ol->tail = NULL;
925 buffer_list_push (struct buffer_list *ol, const unsigned char *str)
927 if (!ol->max_size || ol->size < ol->max_size)
929 struct buffer_entry *e;
930 ALLOC_OBJ_CLEAR (e, struct buffer_entry);
943 e->buf = string_alloc_buf ((const char *) str, NULL);
948 const struct buffer *
949 buffer_list_peek (struct buffer_list *ol)
952 return &ol->head->buf;
958 buffer_list_pop (struct buffer_list *ol)
962 struct buffer_entry *e = ol->head->next;
963 free_buf (&ol->head->buf);
973 buffer_list_advance (struct buffer_list *ol, int n)
977 struct buffer *buf = &ol->head->buf;
978 ASSERT (buf_advance (buf, n));
980 buffer_list_pop (ol);
985 buffer_list_file (const char *fn, int max_line_len)
987 FILE *fp = fopen (fn, "r");
988 struct buffer_list *bl = NULL;
992 char *line = (char *) malloc (max_line_len);
995 bl = buffer_list_new (0);
996 while (fgets (line, max_line_len, fp) != NULL)
997 buffer_list_push (bl, (unsigned char *)line);