-/*
- send a packet to the given vpn ip.
-*/
-int send_packet(ip_t to, vpn_packet_t *packet)
-{
- conn_list_t *cl;
-cp
- if((cl = lookup_conn(to)) == NULL)
- {
- if(debug_lvl > 3)
- {
- syslog(LOG_NOTICE, _("Trying to look up %d.%d.%d.%d in connection list failed!"),
- IP_ADDR_V(to));
- }
-
- /* Is this really necessary? If we can't find "to", then neither should any uplink. (GS) */
-
- return -1;
-
- for(cl = conn_list; cl != NULL && !cl->status.outgoing; cl = cl->next);
- if(!cl)
- { /* No open outgoing connection has been found. */
- if(debug_lvl > 3)
- syslog(LOG_NOTICE, _("There is no remote host I can send this packet to!"));
- return -1;
- }
- }
-
- /* If we ourselves have indirectdata flag set, we should send only to our uplink! */
-
- if(myself->flags & EXPORTINDIRECTDATA)
- {
- for(cl = conn_list; cl != NULL && !cl->status.outgoing; cl = cl->next);
- if(!cl)
- { /* No open outgoing connection has been found. */
- if(debug_lvl > 3)
- syslog(LOG_NOTICE, _("There is no remote host I can send this packet to!"));
- return -1;
- }
- }
- else
-
- /* If indirectdata flag is set for the destination we just looked up,
- * then real_ip is actually the vpn_ip of the gateway tincd
- * it is behind.
- */
-
- if(cl->flags & INDIRECTDATA)
- {
- if(debug_lvl > 3)
- syslog(LOG_NOTICE, _("Indirect packet to %s via %s"),
- cl->vpn_hostname, cl->real_hostname);
- if((cl = lookup_conn(cl->real_ip)) == NULL)
- {
- if(debug_lvl > 3)
- syslog(LOG_NOTICE, _("Indirect look up %d.%d.%d.%d in connection list failed!"),
- IP_ADDR_V(to));
-
- /* Gateway tincd dead? Should we kill it? (GS) */
-
- return -1;
- }
- if(cl->flags & INDIRECTDATA) /* This should not happen */
- {
- if(debug_lvl > 3)
- syslog(LOG_NOTICE, _("Double indirection for %d.%d.%d.%d"),
- IP_ADDR_V(to));
- return -1;
- }
- }
-
- if(my_key_expiry <= time(NULL))
- regenerate_keys();
-
- if(!cl->status.dataopen)
- if(setup_vpn_connection(cl) < 0)
- {
- syslog(LOG_ERR, _("Could not open UDP connection to %s (%s)"), cl->vpn_hostname, cl->real_hostname);
- return -1;
- }
-
- if(!cl->status.validkey)
- {
- if(debug_lvl > 3)
- syslog(LOG_INFO, _("%s (%s) has no valid key, queueing packet"), cl->vpn_hostname, cl->real_hostname);
- 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)
- {
- if(debug_lvl > 3)
- syslog(LOG_INFO, _("%s (%s) is not ready, queueing packet"), cl->vpn_hostname, cl->real_hostname);
- add_queue(&(cl->sq), packet, packet->len + 2);
- 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;
-}