diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/listener.c | 8 | ||||
-rw-r--r-- | src/listener.h | 2 | ||||
-rw-r--r-- | src/options.c | 68 | ||||
-rw-r--r-- | src/options.h | 4 | ||||
-rw-r--r-- | src/tcp.c | 5 | ||||
-rw-r--r-- | src/tcpproxy.c | 2 |
6 files changed, 66 insertions, 23 deletions
diff --git a/src/listener.c b/src/listener.c index bea19e4..a9cfa1e 100644 --- a/src/listener.c +++ b/src/listener.c @@ -66,26 +66,26 @@ void listener_clear(listeners_t* list) slist_clear(list); } -int listener_add(listeners_t* list, const char* laddr, const char* lport, const char* raddr, const char* rport, const char* saddr) +int listener_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const char* lport, const char* raddr, resolv_type_t rrt, const char* rport, const char* saddr, resolv_type_t srt) { if(!list) return -1; // TODO: what if more than one address is returned here? - struct addrinfo* re = tcp_resolve_endpoint(raddr, rport, ANY); + struct addrinfo* re = tcp_resolve_endpoint(raddr, rport, rrt); if(!re) return -1; struct addrinfo* se = NULL; if(saddr) { - se = tcp_resolve_endpoint(saddr, NULL, ANY); + se = tcp_resolve_endpoint(saddr, NULL, srt); if(!se) { freeaddrinfo(re); return -1; } } - struct addrinfo* le = tcp_resolve_endpoint(laddr, lport, ANY); + struct addrinfo* le = tcp_resolve_endpoint(laddr, lport, lrt); if(!le) { freeaddrinfo(re); if(se) diff --git a/src/listener.h b/src/listener.h index 11d5d24..fc3feed 100644 --- a/src/listener.h +++ b/src/listener.h @@ -47,7 +47,7 @@ typedef slist_t listeners_t; int listener_init(listeners_t* list); void listener_clear(listeners_t* list); -int listener_add(listeners_t* list, const char* laddr, const char* lport, const char* raddr, const char* rport, const char* saddr); +int listener_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const char* lport, const char* raddr, resolv_type_t rrt, const char* rport, const char* saddr, resolv_type_t srt); void listener_remove(listeners_t* list, int fd); listener_t* listener_find(listeners_t* list, int fd); void listener_print(listeners_t* list); diff --git a/src/options.c b/src/options.c index 30361d4..4e630e5 100644 --- a/src/options.c +++ b/src/options.c @@ -30,6 +30,7 @@ #include "options.h" #include "log.h" +#include "tcp.h" #include <stdlib.h> #include <stdio.h> @@ -115,6 +116,23 @@ i++; \ } +#define PARSE_RESOLV_TYPE(SHORT, LONG, VALUE) \ + else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ + { \ + if(argc < 1 || argv[i+1][0] == '-') \ + return i; \ + if(!strcmp(argv[i+1], "4") || \ + !strcmp(argv[i+1], "ipv4")) \ + VALUE = IPV4_ONLY; \ + else if(!strcmp(argv[i+1], "6") || \ + !strcmp(argv[i+1], "ipv6")) \ + VALUE = IPV6_ONLY; \ + else \ + return i+1; \ + argc--; \ + i++; \ + } + int options_parse_hex_string(const char* hex, buffer_t* buffer) { if(!hex || !buffer) @@ -180,10 +198,13 @@ int options_parse(options_t* opt, int argc, char* argv[]) PARSE_STRING_LIST("-L","--log", opt->log_targets_) PARSE_BOOL_PARAM("-U", "--debug", opt->debug_) PARSE_STRING_PARAM("-l","--local-addr", opt->local_addr_) + PARSE_RESOLV_TYPE("-t","--local-resolv", opt->lresolv_type_) PARSE_STRING_PARAM("-p","--local-port", opt->local_port_) PARSE_STRING_PARAM("-r","--remote-addr", opt->remote_addr_) + PARSE_RESOLV_TYPE("-R","--remote-resolv", opt->rresolv_type_) PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_) PARSE_STRING_PARAM("-s","--source-addr", opt->source_addr_) + PARSE_RESOLV_TYPE("-S","--source-resolv", opt->sresolv_type_) PARSE_STRING_PARAM("-c","--config", opt->config_file_) PARSE_INT_PARAM("-b","--buffer-size", opt->buffer_size_) else @@ -225,10 +246,13 @@ void options_default(options_t* opt) opt->chroot_dir_ = NULL; opt->pid_file_ = NULL; opt->local_addr_ = NULL; + opt->lresolv_type_ = ANY; opt->local_port_ = NULL; opt->remote_addr_ = NULL; + opt->rresolv_type_ = ANY; opt->remote_port_ = NULL; opt->source_addr_ = NULL; + opt->sresolv_type_ = ANY; opt->config_file_ = strdup(CONFFILE); string_list_init(&opt->log_targets_); opt->buffer_size_ = 10 * 1024; @@ -268,23 +292,26 @@ void options_clear(options_t* opt) void options_print_usage() { printf("USAGE:\n"); - printf("tcpproxy [-h|--help] prints this...\n"); - printf(" [-v|--version] print version info and exit\n"); - printf(" [-D|--nodaemonize] don't run in background\n"); - printf(" [-u|--username] <username> change to this user\n"); - printf(" [-g|--groupname] <groupname> change to this group\n"); - printf(" [-C|--chroot] <path> chroot to this directory\n"); - printf(" [-P|--write-pid] <path> write pid to this file\n"); + printf("tcpproxy [-h|--help] prints this...\n"); + printf(" [-v|--version] print version info and exit\n"); + printf(" [-D|--nodaemonize] don't run in background\n"); + printf(" [-u|--username] <username> change to this user\n"); + printf(" [-g|--groupname] <groupname> change to this group\n"); + printf(" [-C|--chroot] <path> chroot to this directory\n"); + printf(" [-P|--write-pid] <path> write pid to this file\n"); printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n"); - printf(" add a log target, can be invoked several times\n"); - printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n"); - printf(" [-l|--local-addr] <host> local address to listen on\n"); - printf(" [-p|--local-port] <service> local port to listen on\n"); - printf(" [-r|--remote-addr] <host> remote address to connect to\n"); - printf(" [-o|--remote-port] <service> remote port to connect to\n"); - printf(" [-s|--source-addr] <host> source address to connect from\n"); - printf(" [-b|--buffer-size] <size> size of transmit buffers\n"); - printf(" [-c|--config] <file> configuration file\n"); + printf(" add a log target, can be invoked several times\n"); + printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n"); + printf(" [-l|--local-addr] <host> local address to listen on\n"); + printf(" [-t|--local-resolv] (ipv4|4|ipv6|6) set IPv4 or IPv6 only resolving for local address\n"); + printf(" [-p|--local-port] <service> local port to listen on\n"); + printf(" [-r|--remote-addr] <host> remote address to connect to\n"); + printf(" [-R|--remote-resolv] (ipv4|4|ipv6|6) set IPv4 or IPv6 only resolving for local address\n"); + printf(" [-o|--remote-port] <service> remote port to connect to\n"); + printf(" [-s|--source-addr] <host> source address to connect from\n"); + printf(" [-S|--source-resolv] (ipv4|4|ipv6|6) set IPv4 or IPv6 only resolving for local address\n"); + printf(" [-b|--buffer-size] <size> size of transmit buffers\n"); + printf(" [-c|--config] <file> configuration file\n"); } void options_print_version() @@ -307,10 +334,19 @@ void options_print(options_t* opt) printf("log_targets: \n"); string_list_print(&opt->log_targets_, " '", "'\n"); printf("local_addr: '%s'\n", opt->local_addr_); + if(opt->lresolv_type_ == IPV4_ONLY) printf("lresolv_type: IPv4\n"); + else if(opt->lresolv_type_ == IPV6_ONLY) printf("lresolv_type: IPv6\n"); + else printf("lresolv_type: Both\n"); printf("local_port: '%s'\n", opt->local_port_); printf("remote_addr: '%s'\n", opt->remote_addr_); + if(opt->rresolv_type_ == IPV4_ONLY) printf("rresolv_type: IPv4\n"); + else if(opt->rresolv_type_ == IPV6_ONLY) printf("rresolv_type: IPv6\n"); + else printf("rresolv_type: Both\n"); printf("remote_port: '%s'\n", opt->remote_port_); printf("source_addr: '%s'\n", opt->source_addr_); + if(opt->sresolv_type_ == IPV4_ONLY) printf("sresolv_type: IPv4\n"); + else if(opt->sresolv_type_ == IPV6_ONLY) printf("sresolv_type: IPv6\n"); + else printf("sresolv_type: Both\n"); printf("buffer-size: %d\n", opt->buffer_size_); printf("config_file: '%s'\n", opt->config_file_); printf("debug: %s\n", !opt->debug_ ? "false" : "true"); diff --git a/src/options.h b/src/options.h index c48412d..085544c 100644 --- a/src/options.h +++ b/src/options.h @@ -30,6 +30,7 @@ #include "string_list.h" #include "datatypes.h" +#include "tcp.h" struct options_struct { char* progname_; @@ -40,10 +41,13 @@ struct options_struct { char* pid_file_; string_list_t log_targets_; char* local_addr_; + resolv_type_t lresolv_type_; char* local_port_; char* remote_addr_; + resolv_type_t rresolv_type_; char* remote_port_; char* source_addr_; + resolv_type_t sresolv_type_; char* config_file_; int32_t buffer_size_; int debug_; @@ -75,7 +75,10 @@ struct addrinfo* tcp_resolve_endpoint(const char* addr, const char* port, resolv int errcode = getaddrinfo(addr, port, &hints, &res); if (errcode != 0) { - log_printf(ERROR, "Error resolving address (%s:%s): %s", (addr) ? addr : "*", (port) ? port : "0", gai_strerror(errcode)); + char* type = ""; + if(rt == IPV4_ONLY) type = "IPv4 "; + else if(rt == IPV6_ONLY) type = "IPv6 "; + log_printf(ERROR, "Error resolving %saddress (%s:%s): %s", type, (addr) ? addr : "*", (port) ? port : "0", gai_strerror(errcode)); return NULL; } if(!res) { diff --git a/src/tcpproxy.c b/src/tcpproxy.c index d708770..5423036 100644 --- a/src/tcpproxy.c +++ b/src/tcpproxy.c @@ -145,7 +145,7 @@ int main(int argc, char* argv[]) log_close(); exit(-1); } - ret = listener_add(&listeners, opt.local_addr_, opt.local_port_, opt.remote_addr_, opt.remote_port_, opt.source_addr_); + ret = listener_add(&listeners, opt.local_addr_, opt.lresolv_type_, opt.local_port_, opt.remote_addr_, opt.rresolv_type_, opt.remote_port_, opt.source_addr_, opt.sresolv_type_); if(ret) { listener_clear(&listeners); options_clear(&opt); |