- int r;
- real_packet_t rp;
-
- do_encrypt((vpn_packet_t*)packet, &rp, cl->key);
- rp.from = myself->vpn_ip;
-
- if(debug_lvl > 3)
- syslog(LOG_ERR, "Sent %d bytes to %lx", rp.len, cl->vpn_ip);
-
- if((r = send(cl->socket, (char*)&rp, rp.len, 0)) < 0)
- {
- syslog(LOG_ERR, "Error sending data: %m");
- return -1;
- }
-
- total_socket_out += r;
-
- return 0;
-}
-
-/*
- write as many bytes as possible to the tap
- device, possibly in multiple turns.
-*/
-int write_n(int fd, void *buf, size_t len)
-{
- int r, done = 0;
-
- do
- {
- if((r = write(fd, buf, len)) < 0)
- return -1;
- len -= r;
- buf += r;
- done += r;
- } while(len > 0);
-
- return done;
-}
-
-int xrecv(conn_list_t *cl, void *packet)
-{
- vpn_packet_t vp;
- int lenin;
-
- do_decrypt((real_packet_t*)packet, &vp, cl->key);
- add_mac_addresses(&vp);
-
- if((lenin = write_n(tap_fd, &vp, vp.len + 2)) < 0)
- syslog(LOG_ERR, "Can't write to tap device: %m");
- else
- total_tap_out += lenin;
-
- return 0;
-}
-
-/*
- add the given packet of size s to the
- queue q, be it the send or receive queue
-*/
-void add_queue(packet_queue_t **q, void *packet, size_t s)
-{
- queue_element_t *e, *p;
-
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "packet to queue: %d", s);
-
- e = xmalloc(sizeof(queue_element_t));
- e->packet = xmalloc(s);
- memcpy(e->packet, packet, s);
- if(!*q)
- {
- *q = xmalloc(sizeof(packet_queue_t));
- (*q)->head = (*q)->tail = NULL;
- }
-
- e->next = NULL;
-
- if((*q)->tail != NULL)
- (*q)->tail->next = e;
-
- (*q)->tail = e;
-
- if((*q)->head == NULL)
- (*q)->head = e;
-}
-
-/*
- flush a queue by calling function for
- each packet, and removing it when that
- returned a zero exit code
-*/
-void flush_queue(conn_list_t *cl, packet_queue_t *pq,
- int (*function)(conn_list_t*,void*))
-{
- queue_element_t *p, *prev = NULL, *next = NULL;
-
- for(p = pq->head; p != NULL; )
- {
- next = p->next;
-
-cp
- if(!function(cl, p->packet))
- {
- if(prev)
- prev->next = next;
- else
- pq->head = next;
-
-cp
- free(p->packet);
-cp
- free(p);
-cp
- }
- else
- prev = p;
-cp
-
- p = next;
- }
-
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "queue flushed");
-}
-
-/*
- flush the send&recv queues
- void because nothing goes wrong here, packets
- remain in the queue if something goes wrong
-*/
-void flush_queues(conn_list_t *cl)
-{
-cp
- if(cl->sq)
- {
- if(debug_lvl > 1)
- syslog(LOG_DEBUG, "Flushing send queue for " IP_ADDR_S,
- IP_ADDR_V(cl->vpn_ip));
- flush_queue(cl, cl->sq, xsend);
- }
-cp
-
- if(cl->rq)
- {
- if(debug_lvl > 1)
- syslog(LOG_DEBUG, "Flushing receive queue for " IP_ADDR_S,
- IP_ADDR_V(cl->vpn_ip));
- flush_queue(cl, cl->rq, xrecv);
- }
-cp
-}
-
-/*
- send a packet to the given vpn ip.
-*/
-int send_packet(ip_t to, vpn_packet_t *packet)
-{
- conn_list_t *cl;
-
- if((cl = lookup_conn(to)) == NULL)
- {
- if(debug_lvl > 2)
- {
- syslog(LOG_NOTICE, "trying to look up " IP_ADDR_S " in connection list failed.",
- IP_ADDR_V(to));
- }
- for(cl = conn_list; cl != NULL && !cl->status.outgoing; cl = cl->next);
- if(!cl)
- {
- syslog(LOG_ERR, "zxnrbl");
- return -1;
- }
- }
-
-cp
- if(my_key_expiry <= time(NULL))
- regenerate_keys();
-
-cp
- if(!cl->status.dataopen)
- if(setup_vpn_connection(cl) < 0)
- return -1;
-cp
-
- if(!cl->status.validkey)
- {
- add_queue(&(cl->sq), packet, packet->len + 2);
- if(!cl->status.waitingforkey)
- send_key_request(to);
- return 0;
- }
-
-cp
- 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);
-}
-
-int send_broadcast(conn_list_t *cl, vpn_packet_t *packet)
-{
- conn_list_t *p;
-
- for(p = cl; p != NULL; p = p->next)
- if(send_packet(p->real_ip, packet) < 0)
- {
- syslog(LOG_ERR, "Could not send a broadcast packet to %08lx (%08lx): %m",
- p->vpn_ip, p->real_ip);
- break; /* FIXME: should retry later, and send a ping over the metaconnection. */
- }
-
- return 0;
-}
-
-/*
- open the local ethertap device
-*/
-int setup_tap_fd(void)
-{
- int nfd;
- const char *tapfname;
- config_t const *cfg;
-
- 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;
- 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;
-
- 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;
- }
-
- 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;
-
- 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;
- }
-
- 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;
-
- 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);
-
- return 0;