-int setup_myself(void)
-{
- config_t *cfg;
- subnet_t *subnet;
- char *name, *hostname, *mode, *afname, *cipher, *digest;
- struct addrinfo hint, *ai, *aip;
- int choice, err;
-cp
- myself = new_node();
- myself->connection = new_connection();
- init_configuration(&myself->connection->config_tree);
-
- asprintf(&myself->hostname, _("MYSELF"));
- asprintf(&myself->connection->hostname, _("MYSELF"));
-
- myself->connection->options = 0;
- myself->connection->protocol_version = PROT_CURRENT;
-
- if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
- {
- syslog(LOG_ERR, _("Name for tinc daemon required!"));
- return -1;
- }
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Invalid name for myself!"));
- free(name);
- return -1;
- }
-
- myself->name = name;
- myself->connection->name = xstrdup(name);
-
-cp
- if(read_rsa_private_key())
- return -1;
-
- if(read_connection_config(myself->connection))
- {
- syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
- return -1;
- }
-
- if(read_rsa_public_key(myself->connection))
- return -1;
-cp
-
- if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
- asprintf(&myport, "655");
-
-/* Read in all the subnets specified in the host configuration file */
-
- cfg = lookup_config(myself->connection->config_tree, "Subnet");
-
- while(cfg)
- {
- if(!get_config_subnet(cfg, &subnet))
- return -1;
-
- subnet_add(myself, subnet);
-
- cfg = lookup_config_next(myself->connection->config_tree, cfg);
- }
-
-cp
- /* Check some options */
-
- if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
- if(choice)
- myself->options |= OPTION_INDIRECT;
-
- if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
- if(choice)
- myself->options |= OPTION_TCPONLY;
-
- if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
- if(choice)
- myself->options |= OPTION_INDIRECT;
-
- if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
- if(choice)
- myself->options |= OPTION_TCPONLY;
-
- if(myself->options & OPTION_TCPONLY)
- myself->options |= OPTION_INDIRECT;
-
- if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
- {
- if(!strcasecmp(mode, "router"))
- routing_mode = RMODE_ROUTER;
- else if (!strcasecmp(mode, "switch"))
- routing_mode = RMODE_SWITCH;
- else if (!strcasecmp(mode, "hub"))
- routing_mode = RMODE_HUB;
- else
- {
- syslog(LOG_ERR, _("Invalid routing mode!"));
- return -1;
- }
- free(mode);
- }
- else
- routing_mode = RMODE_ROUTER;
-
- get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
+static bool setup_myself(void) {
+ config_t *cfg;
+ subnet_t *subnet;
+ char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
+ char *fname = NULL;
+ char *address = NULL;
+ char *proxy = NULL;
+ char *space;
+ char *envp[5] = {0};
+ struct addrinfo *ai, *aip, hint = {0};
+ bool choice;
+ int i, err;
+ int replaywin_int;
+ bool port_specified = false;
+
+ myself = new_node();
+ myself->connection = new_connection();
+
+ myself->hostname = xstrdup("MYSELF");
+ myself->connection->hostname = xstrdup("MYSELF");
+
+ myself->connection->options = 0;
+ myself->connection->protocol_version = PROT_CURRENT;
+
+ if(!(name = get_name())) {
+ logger(LOG_ERR, "Name for tinc daemon required!");
+ return false;
+ }
+
+ /* Read tinc.conf and our own host config file */
+
+ myself->name = name;
+ myself->connection->name = xstrdup(name);
+ xasprintf(&fname, "%s/hosts/%s", confbase, name);
+ read_config_options(config_tree, name);
+ read_config_file(config_tree, fname);
+ free(fname);
+
+ if(!read_rsa_private_key()) {
+ return false;
+ }
+
+ if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) {
+ myport = xstrdup("655");
+ } else {
+ port_specified = true;
+ }
+
+ /* Ensure myport is numeric */
+
+ if(!atoi(myport)) {
+ struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
+ sockaddr_t sa;
+
+ if(!ai || !ai->ai_addr) {
+ return false;
+ }
+
+ free(myport);
+ memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
+ sockaddr2str(&sa, NULL, &myport);
+ }
+
+ if(get_config_string(lookup_config(config_tree, "Proxy"), &proxy)) {
+ if((space = strchr(proxy, ' '))) {
+ *space++ = 0;
+ }
+
+ if(!strcasecmp(proxy, "none")) {
+ proxytype = PROXY_NONE;
+ } else if(!strcasecmp(proxy, "socks4")) {
+ proxytype = PROXY_SOCKS4;
+ } else if(!strcasecmp(proxy, "socks4a")) {
+ proxytype = PROXY_SOCKS4A;
+ } else if(!strcasecmp(proxy, "socks5")) {
+ proxytype = PROXY_SOCKS5;
+ } else if(!strcasecmp(proxy, "http")) {
+ proxytype = PROXY_HTTP;
+ } else if(!strcasecmp(proxy, "exec")) {
+ proxytype = PROXY_EXEC;
+ } else {
+ logger(LOG_ERR, "Unknown proxy type %s!", proxy);
+ free(proxy);
+ return false;
+ }
+
+ switch(proxytype) {
+ case PROXY_NONE:
+ default:
+ break;
+
+ case PROXY_EXEC:
+ if(!space || !*space) {
+ logger(LOG_ERR, "Argument expected for proxy type exec!");
+ free(proxy);
+ return false;
+ }
+
+ proxyhost = xstrdup(space);
+ break;
+
+ case PROXY_SOCKS4:
+ case PROXY_SOCKS4A:
+ case PROXY_SOCKS5:
+ case PROXY_HTTP:
+ proxyhost = space;
+
+ if(space && (space = strchr(space, ' '))) {
+ *space++ = 0, proxyport = space;
+ }
+
+ if(space && (space = strchr(space, ' '))) {
+ *space++ = 0, proxyuser = space;
+ }
+
+ if(space && (space = strchr(space, ' '))) {
+ *space++ = 0, proxypass = space;
+ }
+
+ if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
+ logger(LOG_ERR, "Host and port argument expected for proxy!");
+ free(proxy);
+ return false;
+ }
+
+ proxyhost = xstrdup(proxyhost);
+ proxyport = xstrdup(proxyport);
+
+ if(proxyuser && *proxyuser) {
+ proxyuser = xstrdup(proxyuser);
+ }
+
+ if(proxypass && *proxypass) {
+ proxypass = xstrdup(proxypass);
+ }
+
+ break;
+ }
+
+ free(proxy);
+ }
+
+ /* Read in all the subnets specified in the host configuration file */
+
+ cfg = lookup_config(config_tree, "Subnet");
+
+ while(cfg) {
+ if(!get_config_subnet(cfg, &subnet)) {
+ return false;
+ }
+
+ subnet_add(myself, subnet);
+
+ cfg = lookup_config_next(config_tree, cfg);
+ }
+
+ /* Check some options */
+
+ if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) {
+ myself->options |= OPTION_INDIRECT;
+ }
+
+ if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) {
+ myself->options |= OPTION_TCPONLY;
+ }
+
+ if(myself->options & OPTION_TCPONLY) {
+ myself->options |= OPTION_INDIRECT;
+ }
+
+ get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
+ get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
+ get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
+ get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
+ strictsubnets |= tunnelserver;
+
+ if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
+ if(!strcasecmp(mode, "router")) {
+ routing_mode = RMODE_ROUTER;
+ } else if(!strcasecmp(mode, "switch")) {
+ routing_mode = RMODE_SWITCH;
+ } else if(!strcasecmp(mode, "hub")) {
+ routing_mode = RMODE_HUB;
+ } else {
+ logger(LOG_ERR, "Invalid routing mode!");
+ free(mode);
+ return false;
+ }
+
+ free(mode);
+ }
+
+ if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
+ if(!strcasecmp(mode, "off")) {
+ forwarding_mode = FMODE_OFF;
+ } else if(!strcasecmp(mode, "internal")) {
+ forwarding_mode = FMODE_INTERNAL;
+ } else if(!strcasecmp(mode, "kernel")) {
+ forwarding_mode = FMODE_KERNEL;
+ } else {
+ logger(LOG_ERR, "Invalid forwarding mode!");
+ free(mode);
+ return false;
+ }
+
+ free(mode);
+ }
+
+ choice = !(myself->options & OPTION_TCPONLY);
+ get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
+
+ if(choice) {
+ myself->options |= OPTION_PMTU_DISCOVERY;
+ }
+
+ choice = true;
+ get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
+
+ if(choice) {
+ myself->options |= OPTION_CLAMP_MSS;
+ }
+
+ get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
+ get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
+
+ if(get_config_string(lookup_config(config_tree, "Broadcast"), &mode)) {
+ if(!strcasecmp(mode, "no")) {
+ broadcast_mode = BMODE_NONE;
+ } else if(!strcasecmp(mode, "yes") || !strcasecmp(mode, "mst")) {
+ broadcast_mode = BMODE_MST;
+ } else if(!strcasecmp(mode, "direct")) {
+ broadcast_mode = BMODE_DIRECT;
+ } else {
+ logger(LOG_ERR, "Invalid broadcast mode!");
+ free(mode);
+ return false;
+ }
+
+ free(mode);
+ }
+