summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2010-12-05 08:15:54 +0000
committerChristian Pointner <equinox@spreadspace.org>2010-12-05 08:15:54 +0000
commit4a6fdccc8260e867fbf4dfd53b029ba085840f51 (patch)
tree8b3319d8613d02e5305d09d835e4c17bcf9bf4bb
parentcheck wether config file was empty (diff)
cleaned parser a bit
better warnings if local-port and config-file are specified git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@30 e61f0598-a718-4e21-a8f0-0aadfa62ad6b
-rw-r--r--src/cfg_parse.y21
-rw-r--r--src/listener.c4
-rw-r--r--src/options.c11
-rw-r--r--src/tcpproxy.c13
4 files changed, 29 insertions, 20 deletions
diff --git a/src/cfg_parse.y b/src/cfg_parse.y
index fb6d97b..a18abd5 100644
--- a/src/cfg_parse.y
+++ b/src/cfg_parse.y
@@ -39,7 +39,7 @@ void yyerror(const char *);
int yylex(void);
int line_cnt = 1;
-options_t* gopt;
+const char* config_file_;
listeners_t* glisteners;
struct listener {
@@ -122,9 +122,9 @@ static void merge_listener_struct(struct listener* dest, struct listener* src)
}
-void yyinit(options_t* opt, listeners_t* listeners)
+void yyinit(const char* config_file, listeners_t* listeners)
{
- gopt = opt;
+ config_file_ = config_file;
glisteners = listeners;
}
@@ -167,8 +167,10 @@ void yyinit(options_t* opt, listeners_t* listeners)
%type <rtype> resolv_type
%type <string> service
%type <string> host_or_addr
-%%
+%error-verbose
+
+%%
cfg:
| cfg listen
;
@@ -180,10 +182,7 @@ listen: listen_head TOK_OPEN listen_body TOK_CLOSE TOK_SEMICOLON
int ret = listener_add(glisteners, $1->la_, $1->lrt_, $1->lp_, $1->ra_, $1->rrt_, $1->rp_, $1->sa_);
clear_listener_struct($1);
if(ret) {
- listener_clear(glisteners);
- options_clear(gopt);
- log_close();
- exit(-1);
+ YYABORT;
}
}
;
@@ -269,10 +268,6 @@ host_or_addr: TOK_NUMBER
void yyerror (const char *string)
{
- log_printf(ERROR, "%s:%d %s\n", gopt->config_file_, line_cnt, string);
- listener_clear(glisteners);
- options_clear(gopt);
- log_close();
- exit(1);
+ log_printf(ERROR, "%s:%d %s\n", config_file_, line_cnt, string);
}
diff --git a/src/listener.c b/src/listener.c
index 493b15d..9f54d26 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -71,6 +71,10 @@ int listener_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const
if(!list)
return -1;
+ if(!lport) { log_printf(ERROR, "no local port specified"); return -1; }
+ if(!raddr) { log_printf(ERROR, "no remote address specified"); return -1; }
+ if(!rport) { log_printf(ERROR, "no remote port specified"); return -1; }
+
// TODO: what if more than one address is returned here?
struct addrinfo* re = tcp_resolve_endpoint(raddr, rport, rrt, 0);
if(!re)
diff --git a/src/options.c b/src/options.c
index bc49a44..b905426 100644
--- a/src/options.c
+++ b/src/options.c
@@ -219,6 +219,11 @@ int options_parse(options_t* opt, int argc, char* argv[])
string_list_add(&opt->log_targets_, "syslog:3,tcpproxy,daemon");
}
+ if(!opt->local_port_ && !opt->config_file_) {
+ opt->config_file_ = strdup(CONFFILE);
+ if(!opt->config_file_) return -2;
+ }
+
return 0;
}
@@ -227,6 +232,10 @@ void options_parse_post(options_t* opt)
if(!opt)
return;
+ if(opt->config_file_ && opt->local_port_) {
+ log_printf(WARNING, "local port and config file specified, will ignore config file");
+ }
+
if(opt->buffer_size_ <= 0) {
log_printf(WARNING, "illegal buffer size %d using default buffer size", opt->buffer_size_);
opt->buffer_size_ = 10 * 1024;
@@ -251,7 +260,7 @@ void options_default(options_t* opt)
opt->rresolv_type_ = ANY;
opt->remote_port_ = NULL;
opt->source_addr_ = NULL;
- opt->config_file_ = strdup(CONFFILE);
+ opt->config_file_ = NULL;
string_list_init(&opt->log_targets_);
opt->buffer_size_ = 10 * 1024;
opt->debug_ = 0;
diff --git a/src/tcpproxy.c b/src/tcpproxy.c
index 57d86b3..3e30338 100644
--- a/src/tcpproxy.c
+++ b/src/tcpproxy.c
@@ -41,7 +41,7 @@
#include "clients.h"
extern FILE *yyin;
-extern void yyinit(options_t* opt, listeners_t* listeners);
+extern void yyinit(const char* config_file, listeners_t* listeners);
extern int yyparse(void);
@@ -169,11 +169,12 @@ int main(int argc, char* argv[])
exit(-1);
}
- yyinit(&opt, &listeners);
- yyparse();
-
- if(!slist_length(&listeners)) {
- log_printf(ERROR, "no listeners defined in config file %s", opt.config_file_);
+ yyinit(opt.config_file_, &listeners);
+ int ret = yyparse();
+ fclose(yyin);
+ if(ret || !slist_length(&listeners)) {
+ if(!ret)
+ log_printf(ERROR, "no listeners defined in config file %s", opt.config_file_);
listener_clear(&listeners);
options_clear(&opt);
log_close();