summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-12-28 17:05:32 +0000
committerChristian Pointner <equinox@anytun.org>2008-12-28 17:05:32 +0000
commitb59af50aece804a8c22f7d71e0555f5fe574ac12 (patch)
tree093755c18c13fa17bb0c6fa28da4ced5f1ca1a4a /src
parentsmall cleanup (diff)
int main() is pretty finished now
Diffstat (limited to 'src')
-rw-r--r--src/options.c13
-rw-r--r--src/signal.c6
-rw-r--r--src/tun_helper.h4
-rw-r--r--src/uanytun.c102
4 files changed, 83 insertions, 42 deletions
diff --git a/src/options.c b/src/options.c
index 7dd25c9..d2540b1 100644
--- a/src/options.c
+++ b/src/options.c
@@ -172,6 +172,19 @@ int options_parse(options_t** opt, int argc, char* argv[])
return -1;
}
+ if(!strcmp((*opt)->cipher_, "null") && !strcmp((*opt)->auth_algo_, "null")) {
+ if((*opt)->kd_prf_) free((*opt)->kd_prf_);
+ (*opt)->kd_prf_ = strdup("null");
+ }
+ if((strcmp((*opt)->cipher_, "null") || strcmp((*opt)->auth_algo_, "null")) &&
+ !strcmp((*opt)->kd_prf_, "null")) {
+ if((*opt)->kd_prf_) free((*opt)->kd_prf_);
+ (*opt)->kd_prf_ = strdup("aes-ctr");
+ }
+
+ if(!((*opt)->dev_name_) && !((*opt)->dev_type_))
+ (*opt)->dev_type_ = strdup("tun");
+
return 0;
}
diff --git a/src/signal.c b/src/signal.c
index a34091f..1086969 100644
--- a/src/signal.c
+++ b/src/signal.c
@@ -64,9 +64,9 @@ void handle_signal(int sig)
void handle_signal_exit(int sig)
{
switch(sig) {
- case SIGINT: log_printf(NOTICE, "SIG-Int caught, exiting"); break;
- case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exiting"); break;
- case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exiting"); break;
+ case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); break;
+ case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); break;
+ case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); break;
default: log_printf(NOTICE, "Signal %d caught, ignoring", sig); return;
}
diff --git a/src/tun_helper.h b/src/tun_helper.h
index c403f28..b403891 100644
--- a/src/tun_helper.h
+++ b/src/tun_helper.h
@@ -55,7 +55,9 @@ void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, con
else if(!strncmp(dev_name, "tap", 3))
dev->type_ = TYPE_TAP;
}
-
+
+ dev->local_ = NULL;
+ dev->remote_netmask_ = NULL;
if(ifcfg_lp)
dev->local_ = strdup(ifcfg_lp);
if(ifcfg_rnmp)
diff --git a/src/uanytun.c b/src/uanytun.c
index 53a6219..5ccab3b 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -51,7 +51,7 @@
#include "daemon.h"
#include "sysexec.h"
-void main_loop(tun_device_t* dev, udp_socket_t* sock)
+void main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
{
log_printf(INFO, "entering main loop");
@@ -88,8 +88,7 @@ void main_loop(tun_device_t* dev, udp_socket_t* sock)
/* } */
/* tun_write(dev, plain_packet_get_payload(&plain_packet), plain_packet_get_payload_length(&plain_packet)); */
-
-
+ sleep(1);
}
}
@@ -117,45 +116,72 @@ int main(int argc, char* argv[])
int ret = options_parse(&opt, argc, argv);
if(ret) {
options_print_usage();
+ log_printf(ERR, "error on options_parse, exitting");
exit(ret);
}
- options_print(opt);
-
-// chrootAndDrop("/var/run/", "nobody");
-// daemonize();
-// log_printf(INFO, "running in background now");
-
-/* tun_device_t* dev; */
-/* tun_init(&dev, NULL, "tun", "192.168.23.1", "192.168.23.2"); */
-/* if(!dev) { */
-/* log_printf(ERR, "error on tun_init"); */
-/* exit(-1); */
-/* } */
-
-/* int ret = exec_script("post-up.sh", dev->actual_name_); */
-/* log_printf(NOTICE, "post-up script returned %d", ret); */
-
-/* udp_socket_t* sock; */
-/* udp_init(&sock, NULL, "4444"); */
-/* if(!sock) { */
-/* log_printf(ERR, "error on udp_init"); */
-/* exit(-1); */
-/* } */
-/* char* local_string = udp_get_local_end_string(sock); */
-/* log_printf(INFO, "listening on: %s", local_string); */
-/* free(local_string); */
-
-/* udp_set_remote(sock, "1.2.3.4", "4444"); */
-/* char* remote_string = udp_get_remote_end_string(sock); */
-/* log_printf(INFO, "set remote end to: %s", remote_string); */
-/* free(remote_string); */
-
-/* main_loop(dev, sock); */
-
-/* tun_close(&dev); */
-/* udp_close(&sock); */
+
+ tun_device_t* dev;
+ tun_init(&dev, opt->dev_name_, opt->dev_type_, opt->ifconfig_param_local_, opt->ifconfig_param_remote_netmask_);
+ if(!dev) {
+ log_printf(ERR, "error on tun_init, exitting");
+ exit(-1);
+ }
+ log_printf(NOTICE, "dev of type '%s' opened, actual name is '%s'", tun_get_type_string(dev), dev->actual_name_);
+
+ if(opt->post_up_script_) {
+ int ret = exec_script(opt->post_up_script_, dev->actual_name_);
+ log_printf(NOTICE, "post-up script returned %d", ret);
+ }
+
+
+ udp_socket_t* sock;
+ udp_init(&sock, opt->local_addr_, opt->local_port_);
+ if(!sock) {
+ log_printf(ERR, "error on udp_init, exitting");
+ exit(-1);
+ }
+ char* local_string = udp_get_local_end_string(sock);
+ log_printf(NOTICE, "listening on: %s", local_string);
+ free(local_string);
+
+ if(opt->remote_addr_) {
+ udp_set_remote(sock, opt->remote_addr_, opt->remote_port_);
+ char* remote_string = udp_get_remote_end_string(sock);
+ log_printf(NOTICE, "set remote end to: %s", remote_string);
+ free(remote_string);
+ }
+
+
+ FILE* pid_file = NULL;
+ if(opt->pid_file_) {
+ pid_file = fopen(opt->pid_file_, "w");
+ if(!pid_file) {
+ log_printf(WARNING, "unable to open pid file: %m");
+ }
+ }
+
+ if(opt->chroot_)
+ chrootAndDrop("/var/run/", "nobody");
+ if(opt->daemonize_) {
+ pid_t oldpid = getpid();
+ daemonize();
+ log_printf(INFO, "running in background now (old pid: %d)", oldpid);
+ }
+
+ if(pid_file) {
+ pid_t pid = getpid();
+ fprintf(pid_file, "%d", pid);
+ fclose(pid_file);
+ }
+
+ main_loop(dev, sock, opt);
+
+ tun_close(&dev);
+ udp_close(&sock);
options_clear(&opt);
+ log_printf(NOTICE, "normal shutdown");
+
return 0;
}