- if(my_key_expiry <= time(NULL))
- regenerate_keys();
-
- if(!cl->status.dataopen)
- if(setup_vpn_connection(cl) < 0)
- return -1;
-
- if(!cl->status.validkey)
- {
- add_queue(&(cl->sq), packet, packet->len + 2);
- if(!cl->status.waitingforkey)
- send_key_request(cl->vpn_ip); /* Keys should be sent to the host running the tincd */
- return 0;
- }
-
- if(!cl->status.active)
- {
- add_queue(&(cl->sq), packet, packet->len + 2);
- if(debug_lvl > 1)
- syslog(LOG_INFO, IP_ADDR_S " is not ready, queueing packet.", IP_ADDR_V(cl->vpn_ip));
- return 0; /* We don't want to mess up, do we? */
- }
-
- /* can we send it? can we? can we? huh? */
-cp
- return xsend(cl, packet);
-}
-
-/*
- open the local ethertap device
-*/
-int setup_tap_fd(void)
-{
- int nfd;
- const char *tapfname;
- config_t const *cfg;
-cp
- if((cfg = get_config_val(tapdevice)) == NULL)
- tapfname = "/dev/tap0";
- else
- tapfname = cfg->data.ptr;
-
- if((nfd = open(tapfname, O_RDWR | O_NONBLOCK)) < 0)
- {
- syslog(LOG_ERR, "Could not open %s: %m", tapfname);
- return -1;
- }
-
- tap_fd = nfd;
-cp
- return 0;
-}
-
-/*
- set up the socket that we listen on for incoming
- (tcp) connections
-*/
-int setup_listen_meta_socket(int port)
-{
- int nfd, flags;
- struct sockaddr_in a;
- const int one = 1;
-cp
- if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- {
- syslog(LOG_ERR, "Creating metasocket failed: %m");
- return -1;
- }
-
- if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
- {
- syslog(LOG_ERR, "setsockopt: %m");
- return -1;
- }
-
- flags = fcntl(nfd, F_GETFL);
- if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- syslog(LOG_ERR, "fcntl: %m");
- return -1;
- }
-
- memset(&a, 0, sizeof(a));
- a.sin_family = AF_INET;
- a.sin_port = htons(port);
- a.sin_addr.s_addr = htonl(INADDR_ANY);
-
- if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
- {
- syslog(LOG_ERR, "Can't bind to port %hd/tcp: %m", port);
- return -1;
- }
-
- if(listen(nfd, 3))
- {
- syslog(LOG_ERR, "listen: %m");
- return -1;
- }
-cp
- return nfd;
-}
-
-/*
- setup the socket for incoming encrypted
- data (the udp part)
-*/
-int setup_vpn_in_socket(int port)
-{
- int nfd, flags;
- struct sockaddr_in a;
- const int one = 1;
-cp
- if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
- {
- syslog(LOG_ERR, "Creating socket failed: %m");
- return -1;
- }
-
- if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
- {
- syslog(LOG_ERR, "setsockopt: %m");
- return -1;
- }
-
- flags = fcntl(nfd, F_GETFL);
- if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- syslog(LOG_ERR, "fcntl: %m");
- return -1;
- }
-
- memset(&a, 0, sizeof(a));
- a.sin_family = AF_INET;
- a.sin_port = htons(port);
- a.sin_addr.s_addr = htonl(INADDR_ANY);
-
- if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
- {
- syslog(LOG_ERR, "Can't bind to port %hd/udp: %m", port);
- return -1;
- }
-cp
- return nfd;
-}
-
-/*
- 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((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 failed: %m");
- 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, IP_ADDR_S ":%d: %m", IP_ADDR_V(cl->real_ip), 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: %m");
- return -1;
- }
-
- cl->hostname = hostlookup(htonl(cl->real_ip));
-
- syslog(LOG_INFO, "Connected to %s:%hd" , cl->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(ip_t ip)
-{
- conn_list_t *ncn;
-cp
- ncn = new_conn_list();
- ncn->real_ip = ip;
-
- if(setup_outgoing_meta_socket(ncn) < 0)
- {
- syslog(LOG_ERR, "Could not set up a meta connection.");
- 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_mask = cfg->data.ip->mask;
-
- if(!(cfg = get_config_val(listenport)))
- myself->port = 655;
- else
- myself->port = cfg->data.val;
-
- 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 %d.", myself->port);
-cp
- return 0;
-}
-
-RETSIGTYPE
-sigalrm_handler(int a)
-{
- config_t const *cfg;
-cp
- cfg = get_config_val(upstreamip);
-
- if(!setup_outgoing_connection(cfg->data.ip->ip))
- {
- signal(SIGALRM, SIG_IGN);
- }
- else
- {
- signal(SIGALRM, sigalrm_handler);
- seconds_till_retry += 5;
- if(seconds_till_retry>300) /* Don't wait more than 5 minutes. */
- seconds_till_retry = 300;
- alarm(seconds_till_retry);
- syslog(LOG_ERR, "Still failed to connect to other. Will retry in %d seconds.",
- seconds_till_retry);
- }
-cp
-}
-
-/*
- setup all initial network connections
-*/
-int setup_network_connections(void)
-{
- config_t const *cfg;
-cp
- if((cfg = get_config_val(pingtimeout)) == NULL)
- timeout = 5;
- else
- timeout = cfg->data.val;
-
- if(setup_tap_fd() < 0)
- return -1;
-
- if(setup_myself() < 0)
- return -1;
-
- if((cfg = get_config_val(upstreamip)) == NULL)
- /* No upstream IP given, we're listen only. */
- return 0;