+ a.sin_port = htons(0);
+ a.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ if(bind(c->socket, (struct sockaddr *)&a, sizeof(struct sockaddr)))
+ {
+ close(c->socket);
+ syslog(LOG_ERR, _("System call `%s' failed: %m"), "bind");
+ return -1;
+ }
+
+ */
+
+ /* Optimize TCP settings?
+
+ option = 1;
+ setsockopt(c->socket, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
+#ifdef HAVE_LINUX
+ setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
+
+ option = IPTOS_LOWDELAY;
+ setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
+#endif
+
+ */
+
+ /* Connect */
+
+ if(connect(c->socket, c->address->ai_addr, c->address->ai_addrlen) == -1)
+ {
+ close(c->socket);
+ syslog(LOG_ERR, _("%s port %s: %m"), c->hostname, c->port);
+ return -1;
+ }
+
+ flags = fcntl(c->socket, F_GETFL);
+
+ if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
+ {
+ close(c->socket);
+ syslog(LOG_ERR, _("fcntl for %s port %s: %m"),
+ c->hostname, c->port);
+ return -1;
+ }
+
+ if(debug_lvl >= DEBUG_CONNECTIONS)
+ syslog(LOG_INFO, _("Connected to %s port %s"),
+ c->hostname, c->port);
+cp
+ return 0;
+}
+
+int setup_outgoing_connection(char *name)
+{
+ connection_t *c;
+ node_t *n;
+ struct addrinfo *ai, *aitop, hints;
+ int r, ipv6preferred;
+
+cp
+ n = lookup_node(name);
+
+ if(n)
+ if(n->connection)
+ {
+ if(debug_lvl >= DEBUG_CONNECTIONS)
+ syslog(LOG_INFO, _("Already connected to %s"), name);
+ return 0;
+ }
+
+ c = new_connection();
+ c->name = xstrdup(name);
+
+ init_configuration(&c->config_tree);
+ read_connection_config(c);
+
+ if(!get_config_string(lookup_config(c->config_tree, "Address"), &c->hostname))
+ {
+ syslog(LOG_ERR, _("No address specified for %s"), c->name);
+ free_connection(c);
+ return -1;
+ }
+
+ if(!get_config_string(lookup_config(c->config_tree, "Port"), &c->port))
+ {
+ syslog(LOG_ERR, _("No port specified for %s"), c->name);
+ free_connection(c);
+ return -1;
+ }
+
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_family = AF_INET;
+ if(get_config_bool(lookup_config(c->config_tree, "IPv6Preferred"), &ipv6preferred))
+ {
+ if(ipv6preferred)
+ hints.ai_family = PF_UNSPEC;
+ }
+
+ if((r = getaddrinfo(c->hostname, c->port, &hints, &aitop)) != 0)
+ {
+ syslog(LOG_ERR, _("Looking up %s failed: %s\n"),
+ c->hostname, gai_strerror(r));
+ return -1;
+ }
+
+ for(ai = aitop; ai != NULL; ai = ai->ai_next)
+ {
+ if(setup_outgoing_socket(c) < 0)
+ continue;
+ }
+
+ if(ai == NULL)
+ {
+ /* No connection alternative succeeded */
+ free_connection(c);
+ return -1;
+ }
+
+ c->status.outgoing = 1;
+ c->last_ping_time = time(NULL);
+
+ connection_add(c);
+
+ send_id(c);
+cp
+ return 0;
+}
+
+int read_rsa_public_key(connection_t *c)
+{
+ FILE *fp;
+ char *fname;
+ char *key;
+cp
+ if(!c->rsa_key)
+ c->rsa_key = RSA_new();
+
+ /* First, check for simple PublicKey statement */
+
+ if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
+ {
+ BN_hex2bn(&c->rsa_key->n, key);
+ BN_hex2bn(&c->rsa_key->e, "FFFF");
+ return 0;
+ }
+
+ /* Else, check for PublicKeyFile statement and read it */
+
+ if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
+ {
+ if(is_safe_path(fname))
+ {
+ if((fp = fopen(fname, "r")) == NULL)
+ {
+ syslog(LOG_ERR, _("Error reading RSA public key file `%s': %m"),
+ fname);
+ return -1;
+ }
+ c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
+ fclose(fp);
+ if(!c->rsa_key)
+ {
+ syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
+ fname);
+ return -1;
+ }
+ return 0;
+ }
+ else
+ return -1;
+ }
+
+ /* Else, check if a harnessed public key is in the config file */