From 668f72e224aac1e0dbecf4fa3baba567016f0f22 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 26 Nov 2010 16:11:46 +0000 Subject: added generic single linked list git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@6 e61f0598-a718-4e21-a8f0-0aadfa62ad6b --- src/Makefile | 1 + src/slist.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/slist.h | 50 +++++++++++++++++++++++ src/string_list.c | 57 +++++--------------------- src/string_list.h | 13 ++---- src/tcpproxy.c | 12 +++--- 6 files changed, 187 insertions(+), 63 deletions(-) create mode 100644 src/slist.c create mode 100644 src/slist.h (limited to 'src') diff --git a/src/Makefile b/src/Makefile index ee2c7f1..6e70e6b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -33,6 +33,7 @@ EXECUTABLE := tcpproxy C_OBJS := log.o \ options.o \ + slist.o \ string_list.o \ sig_handler.o \ tcpproxy.o diff --git a/src/slist.c b/src/slist.c new file mode 100644 index 0000000..58528bd --- /dev/null +++ b/src/slist.c @@ -0,0 +1,117 @@ +/* + * tcpproxy + * + * tcpproxy is a simple tcp connection proxy which combines the + * features of rinetd and 6tunnel. tcpproxy supports IPv4 and + * IPv6 and also supports connections from IPv6 to IPv4 + * endpoints and vice versa. + * + * + * Copyright (C) 2010-2011 Christian Pointner + * + * This file is part of tcpproxy. + * + * tcpproxy is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * tcpproxy 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 tcpproxy. If not, see . + */ + +#include +#include + +#include "datatypes.h" + +#include "slist.h" + +slist_element_t* slist_get_last(slist_element_t* first) +{ + if(!first) + return NULL; + + while(first->next_) + first = first->next_; + + return first; +} + +int slist_init(slist_t* lst, void (*delete_element)(void*)) +{ + if(!lst || !delete_element) + return -1; + + lst->delete_element = delete_element; + lst->first_ = NULL; + + return 0; +} + +slist_element_t* slist_add(slist_t* lst, void* data) +{ + if(!lst || !data) + return NULL; + + slist_element_t* new_element = malloc(sizeof(slist_element_t)); + if(!new_element) + return NULL; + + new_element->data_ = data; + new_element->next_ = NULL; + + if(!lst->first_) + lst->first_ = new_element; + else + slist_get_last(lst->first_)->next_ = new_element; + + return new_element; +} + +void slist_remove(slist_t* lst, void* data) +{ + if(!lst || !lst->first_) + return; + + slist_element_t* tmp = lst->first_->next_; + slist_element_t* prev = lst->first_; + if(lst->first_->data_ == data) { + lst->first_ = tmp; + lst->delete_element(prev->data_); + free(prev); + } + else { + while(tmp) { + if(tmp->data_ == data) { + prev->next_ = tmp->next_; + lst->delete_element(tmp->data_); + free(tmp); + return; + } + prev = tmp; + tmp = tmp->next_; + } + } +} + +void slist_clear(slist_t* lst) +{ + if(!lst || !lst->first_) + return; + + do { + slist_element_t* deletee = lst->first_; + lst->first_ = lst->first_->next_; + lst->delete_element(deletee->data_); + free(deletee); + } + while(lst->first_); + + lst->first_ = NULL; +} diff --git a/src/slist.h b/src/slist.h new file mode 100644 index 0000000..4812192 --- /dev/null +++ b/src/slist.h @@ -0,0 +1,50 @@ +/* + * tcpproxy + * + * tcpproxy is a simple tcp connection proxy which combines the + * features of rinetd and 6tunnel. tcpproxy supports IPv4 and + * IPv6 and also supports connections from IPv6 to IPv4 + * endpoints and vice versa. + * + * + * Copyright (C) 2010-2011 Christian Pointner + * + * This file is part of tcpproxy. + * + * tcpproxy is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * tcpproxy 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 tcpproxy. If not, see . + */ + +#ifndef TCPPROXY_slist_h_INCLUDED +#define TCPPROXY_slist_h_INCLUDED + +struct slist_element_struct { + void* data_; + struct slist_element_struct* next_; +}; +typedef struct slist_element_struct slist_element_t; + +slist_element_t* slist_get_last(slist_element_t* first); + +struct slist_struct { + void (*delete_element)(void* element); + slist_element_t* first_; +}; +typedef struct slist_struct slist_t; + +int slist_init(slist_t* lst, void (*delete_element)(void*)); +slist_element_t* slist_add(slist_t* lst, void* data); +void slist_remove(slist_t* lst, void* data); +void slist_clear(slist_t* lst); + +#endif diff --git a/src/string_list.c b/src/string_list.c index 3c7316d..192ff19 100644 --- a/src/string_list.c +++ b/src/string_list.c @@ -30,65 +30,26 @@ #include #include "string_list.h" +#include "slist.h" -void string_list_init(string_list_t* list) +int string_list_init(string_list_t* list) { - if(!list) - return; - - list->first_ = NULL; + return slist_init(list, &free); } void string_list_clear(string_list_t* list) { - if(!list) - return; - - while(list->first_) { - string_list_element_t* tmp; - tmp = list->first_; - list->first_ = tmp->next_; - if(tmp->string_) - free(tmp->string_); - free(tmp); - } + slist_clear(list); } int string_list_add(string_list_t* list, const char* string) { if(!list) return -1; + + if(slist_add(list, strdup(string)) == NULL) + return -2; - if(!list->first_) { - list->first_ = malloc(sizeof(string_list_element_t)); - if(!list->first_) - return -2; - - list->first_->next_ = 0; - list->first_->string_ = strdup(string); - if(!list->first_->string_) { - free(list->first_); - list->first_ = NULL; - return -2; - } - } - else { - string_list_element_t* tmp = list->first_; - while(tmp->next_) - tmp = tmp->next_; - - tmp->next_ = malloc(sizeof(string_list_element_t)); - if(!tmp->next_) - return -2; - - tmp->next_->next_ = 0; - tmp->next_->string_ = strdup(string); - if(!tmp->next_->string_) { - free(tmp->next_); - tmp->next_ = NULL; - return -2; - } - } return 0; } @@ -97,9 +58,9 @@ void string_list_print(string_list_t* list, const char* head, const char* tail) if(!list) return; - string_list_element_t* tmp = list->first_; + slist_element_t* tmp = list->first_; while(tmp) { - printf("%s%s%s", head, tmp->string_, tail); + printf("%s%s%s", head, (char*)(tmp->data_), tail); tmp = tmp->next_; } } diff --git a/src/string_list.h b/src/string_list.h index a9095db..e748e43 100644 --- a/src/string_list.h +++ b/src/string_list.h @@ -28,18 +28,11 @@ #ifndef TCPPROXY_string_list_h_INCLUDED #define TCPPROXY_string_list_h_INCLUDED -struct string_list_element_struct { - char* string_; - struct string_list_element_struct* next_; -}; -typedef struct string_list_element_struct string_list_element_t; +#include "slist.h" -struct string_list_struct { - string_list_element_t* first_; -}; -typedef struct string_list_struct string_list_t; +typedef slist_t string_list_t; -void string_list_init(string_list_t* list); +int string_list_init(string_list_t* list); void string_list_clear(string_list_t* list); int string_list_add(string_list_t* list, const char* string); diff --git a/src/tcpproxy.c b/src/tcpproxy.c index 68add0f..b708f31 100644 --- a/src/tcpproxy.c +++ b/src/tcpproxy.c @@ -100,15 +100,15 @@ int main(int argc, char* argv[]) log_close(); exit(ret); } - string_list_element_t* tmp = opt.log_targets_.first_; + slist_element_t* tmp = opt.log_targets_.first_; while(tmp) { - ret = log_add_target(tmp->string_); + ret = log_add_target(tmp->data_); if(ret) { switch(ret) { case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break; - case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break; - case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break; - default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break; + case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", (char*)(tmp->data_)); break; + case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", (char*)(tmp->data_)); break; + default: fprintf(stderr, "syntax error near: '%s', exitting\n", (char*)(tmp->data_)); break; } options_clear(&opt); @@ -121,6 +121,8 @@ int main(int argc, char* argv[]) log_printf(NOTICE, "just started..."); options_parse_post(&opt); + options_print(&opt); + priv_info_t priv; if(opt.username_) if(priv_init(&priv, opt.username_, opt.groupname_)) { -- cgit v1.2.3