commit 0e12a3c565e8b5abcfb5ea3657b2fbf94f5cc483 Author: David Goulet dgoulet@ev0ke.net Date: Sun Jun 2 14:25:54 2013 -0400
Add utils.c/utils.h with calls from old code
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/common/Makefile.am | 2 +- src/common/config-file.c | 77 ++++------------------------------- src/common/utils.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/common/utils.h | 28 +++++++++++++ 4 files changed, 136 insertions(+), 71 deletions(-)
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index f23e439..5f325a2 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -3,4 +3,4 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src AM_CFLAGS = -fno-strict-aliasing
noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = log.c log.h config-file.c config-file.h +libcommon_la_SOURCES = log.c log.h config-file.c config-file.h utils.c utils.h diff --git a/src/common/config-file.c b/src/common/config-file.c index a085a19..ad7b683 100644 --- a/src/common/config-file.c +++ b/src/common/config-file.c @@ -29,38 +29,11 @@
#include "config-file.h" #include "log.h" +#include "utils.h"
/* Global configuration variables. */ static struct config_server_entry *currentcontext = NULL;
-/* This routines breaks up input lines into tokens */ -/* and places these tokens into the array specified */ -/* by tokens */ -static int tokenize(char *line, int arrsize, char *tokens[]) { - int tokenno = -1; - int finished = 0; - - /* Whitespace is ignored before and after tokens */ - while ((tokenno < (arrsize - 1)) && - (line = line + strspn(line, " \t")) && - (*line != (char) 0) && - (!finished)) { - tokenno++; - tokens[tokenno] = line; - line = line + strcspn(line, " \t"); - *line = (char) 0; - line++; - - /* We ignore everything after a # */ - if (*tokens[tokenno] == '#') { - finished = 1; - tokenno--; - } - } - - return(tokenno + 1); -} - /* Check server entries (and establish defaults) */ static int check_server(struct config_server_entry *server) { @@ -117,15 +90,15 @@ int make_config_network_entry(char *value, struct config_network_entry **ent) { split = buf;
/* Now rip it up */ - ip = strsplit(&separator, &split, "/:"); + ip = utils_strsplit(&separator, &split, "/:"); if (separator == ':') { /* We have a start port */ - start_port = strsplit(&separator, &split, "-/"); + start_port = utils_strsplit(&separator, &split, "-/"); if (separator == '-') /* We have an end port */ - end_port = strsplit(&separator, &split, "/"); + end_port = utils_strsplit(&separator, &split, "/"); } - subnet = strsplit(NULL, &split, " \n"); + subnet = utils_strsplit(NULL, &split, " \n");
if ((ip == NULL) || (subnet == NULL)) { /* Network specification not validly constructed */ @@ -258,7 +231,7 @@ static int handle_reaches(int lineno, char *value) { static int handle_server(struct config_parsed *config, int lineno, char *value) { char *ip;
- ip = strsplit(NULL, &value, " "); + ip = utils_strsplit(NULL, &value, " ");
/* We don't verify this ip/hostname at this stage, */ /* its resolved immediately before use in torsocks.c */ @@ -599,7 +572,7 @@ static int handle_line(struct config_parsed *config, char *line, int lineno) strncpy(savedline, line, CONFIG_MAXLINE - 1); savedline[CONFIG_MAXLINE - 1] = (char) 0; /* Tokenize the input string */ - nowords = tokenize(line, 10, words); + nowords = utils_tokenize_ignore_comments(line, 10, words);
/* Set the spare slots to an empty string to simplify */ /* processing */ @@ -731,42 +704,6 @@ int pick_server(struct config_parsed *config, struct config_server_entry **ent, return(0); }
-/* This function is very much like strsep, it looks in a string for */ -/* a character from a list of characters, when it finds one it */ -/* replaces it with a \0 and returns the start of the string */ -/* (basically spitting out tokens with arbitrary separators). If no */ -/* match is found the remainder of the string is returned and */ -/* the start pointer is set to be NULL. The difference between */ -/* standard strsep and this function is that this one will */ -/* set *separator to the character separator found if it isn't null */ -char *strsplit(char *separator, char **text, const char *search) { - unsigned int len; - char *ret; - - ret = *text; - - if (*text == NULL) { - if (separator) - *separator = '\0'; - return(NULL); - } else { - len = strcspn(*text, search); - if (len == strlen(*text)) { - if (separator) - *separator = '\0'; - *text = NULL; - } else { - *text = *text + len; - if (separator) - *separator = **text; - **text = '\0'; - *text = *text + 1; - } - } - - return(ret); -} - /* * Read and populate the given config parsed data structure. * diff --git a/src/common/utils.c b/src/common/utils.c new file mode 100644 index 0000000..3c537fc --- /dev/null +++ b/src/common/utils.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <string.h> + +#include "macros.h" +#include "utils.h" + +/* + * This routines breaks up input lines into tokens and places these tokens into + * the array specified by tokens + * + * Return the number of token plus one set in the given array. + */ +ATTR_HIDDEN +int utils_tokenize_ignore_comments(char *line, int arrsize, char **tokens) +{ + int tokenno = -1; + int finished = 0; + + /* Whitespace is ignored before and after tokens */ + while ((tokenno < (arrsize - 1)) && + (line = line + strspn(line, " \t")) && + (*line != (char) 0) && + (!finished)) { + tokenno++; + tokens[tokenno] = line; + line = line + strcspn(line, " \t"); + *line = '\0'; + line++; + + /* We ignore everything after a # */ + if (*tokens[tokenno] == '#') { + finished = 1; + tokenno--; + } + } + + return tokenno + 1; +} + +/* + * This function is very much like strsep, it looks in a string for a character + * from a list of characters, when it finds one it replaces it with a \0 and + * returns the start of the string (basically spitting out tokens with + * arbitrary separators). + * + * If no match is found the remainder of the string is returned and the start + * pointer is set to be NULL. The difference between standard strsep and this + * function is that this one will set separator to the character separator + * found if it isn't NULL. + */ +ATTR_HIDDEN +char *utils_strsplit(char *separator, char **text, const char *search) +{ + unsigned int len; + char *ret; + + ret = *text; + + if (*text == NULL) { + if (separator) { + *separator = '\0'; + } + return NULL; + } + + len = strcspn(*text, search); + if (len == strlen(*text)) { + if (separator) { + *separator = '\0'; + } + *text = NULL; + } else { + *text = *text + len; + if (separator) { + *separator = **text; + } + **text = '\0'; + *text = *text + 1; + } + + return ret; +} diff --git a/src/common/utils.h b/src/common/utils.h new file mode 100644 index 0000000..26a5092 --- /dev/null +++ b/src/common/utils.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef TORSOCKS_UTILS_H +#define TORSOCKS_UTILS_H + +#include <netinet/in.h> + +char *utils_strsplit(char *separator, char **text, const char *search); +int utils_tokenize_ignore_comments(char *line, int arrsize, char **tokens); + +#endif /* TORSOCKS_UTILS_H */
tor-commits@lists.torproject.org