-/*
- setup an outgoing meta (tcp) socket
-*/
-int setup_outgoing_meta_socket(conn_list_t *cl)
-{
- int flags;
- struct sockaddr_in a;
- config_t const *cfg;
-cp
- if(debug_lvl > 0)
- syslog(LOG_INFO, _("Trying to connect to %s"), cl->real_hostname);
-
- if((cfg = get_config_val(upstreamport)) == NULL)
- cl->port = 655;
- else
- cl->port = cfg->data.val;
-
- cl->meta_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(cl->meta_socket == -1)
- {
- syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"),
- cl->real_hostname, cl->port);
- return -1;
- }
-
- a.sin_family = AF_INET;
- a.sin_port = htons(cl->port);
- a.sin_addr.s_addr = htonl(cl->real_ip);
-
- if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
- {
- syslog(LOG_ERR, _("%s port %hd: %m"), cl->real_hostname, cl->port);
- return -1;
- }
-
- flags = fcntl(cl->meta_socket, F_GETFL);
- if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
- cl->real_hostname, cl->port);
- return -1;
- }
-
- if(debug_lvl > 0)
- syslog(LOG_INFO, _("Connected to %s port %hd"),
- cl->real_hostname, cl->port);
-cp
- return 0;
-}
-
-/*
- setup an outgoing connection. It's not
- necessary to also open an udp socket as
- well, because the other host will initiate
- an authentication sequence during which
- we will do just that.
-*/
-int setup_outgoing_connection(char *hostname)
-{
- conn_list_t *ncn;
- struct hostent *h;
-cp
- if(!(h = gethostbyname(hostname)))
- {
- syslog(LOG_ERR, _("Error looking up `%s': %m"), hostname);
- return -1;
- }
-
- ncn = new_conn_list();
- ncn->real_ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
- ncn->real_hostname = hostlookup(htonl(ip));
-
- if(setup_outgoing_meta_socket(ncn) < 0)
- {
- syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
- ncn->real_hostname);
- free_conn_element(ncn);
- return -1;
- }
-
- ncn->status.meta = 1;
- ncn->status.outgoing = 1;
- ncn->next = conn_list;
- conn_list = ncn;
-cp
- return 0;
-}
-
-/*
- set up the local sockets (listen only)
-*/
-int setup_myself(void)
-{
- config_t const *cfg;
-cp
- myself = new_conn_list();
-
- if(!(cfg = get_config_val(myvpnip)))
- {
- syslog(LOG_ERR, _("No value for my VPN IP given"));
- return -1;
- }
-
- myself->vpn_ip = cfg->data.ip->ip;
- myself->vpn_hostname = hostlookup(htonl(myself->vpn_ip));
- myself->real_hostname = hostlookup(htonl(myself->vpn_ip));
- myself->vpn_mask = cfg->data.ip->mask;
- myself->flags = 0;
-
- if(!(cfg = get_config_val(listenport)))
- myself->port = 655;
- else
- myself->port = cfg->data.val;
-
- if(cfg = get_config_val(indirectdata))
- if(cfg->data.val == stupid_true)
- myself->flags |= EXPORTINDIRECTDATA;
-
- if((myself->meta_socket = setup_listen_meta_socket(myself->port)) < 0)
- {
- syslog(LOG_ERR, _("Unable to set up a listening socket"));
- return -1;
- }
-
- if((myself->socket = setup_vpn_in_socket(myself->port)) < 0)
- {
- syslog(LOG_ERR, _("Unable to set up an incoming vpn data socket"));
- close(myself->meta_socket);
- return -1;
- }
-
- myself->status.active = 1;
-
- syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port);
-cp
- return 0;
-}
-
-RETSIGTYPE
-sigalrm_handler(int a)
-{
- config_t const *cfg;
-cp
- cfg = get_next_config_val(upstreamip, upstreamindex++);
-
- while(cfg)
- {
- if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
- {
- signal(SIGALRM, SIG_IGN);
- return;
- }
- cfg = get_next_config_val(upstreamip, upstreamindex++); /* Or else we try the next ConnectTo line */
- }
-
- signal(SIGALRM, sigalrm_handler);
- upstreamindex = 0;
- seconds_till_retry += 5;
- if(seconds_till_retry > MAXTIMEOUT) /* Don't wait more than MAXTIMEOUT seconds. */
- seconds_till_retry = MAXTIMEOUT;
- syslog(LOG_ERR, _("Still failed to connect to other, will retry in %d seconds"),
- seconds_till_retry);
- alarm(seconds_till_retry);
-cp
-}