- 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(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)