-int xrecv(conn_list_t *cl, void *packet)
-{
- vpn_packet_t vp;
- int lenin;
-cp
- do_decrypt((real_packet_t*)packet, &vp, cl->key);
- add_mac_addresses(&vp);
-
- if(debug_lvl > 3)
- syslog(LOG_ERR, _("Receiving packet of %d bytes from " IP_ADDR_S " (%s)"),
- ((real_packet_t*)packet)->len, IP_ADDR_V(cl->vpn_ip), cl->hostname);
-
- if((lenin = write(tap_fd, &vp, vp.len + sizeof(vp.len))) < 0)
- syslog(LOG_ERR, _("Can't write to tap device: %m"));
- else
- total_tap_out += lenin;
-
- cl->want_ping = 0;
- cl->last_ping_time = time(NULL);
-cp
- 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;
-cp
- e = xmalloc(sizeof(*e));
- e->packet = xmalloc(s);
- memcpy(e->packet, packet, s);
-
- if(!*q)
- {
- *q = xmalloc(sizeof(**q));
- (*q)->head = (*q)->tail = NULL;
- }
-
- e->next = NULL; /* We insert at the tail */
-
- if((*q)->tail) /* Do we have a tail? */
- {
- (*q)->tail->next = e;
- e->prev = (*q)->tail;
- }
- else /* No tail -> no head too */
- {
- (*q)->head = e;
- e->prev = NULL;
- }
-
- (*q)->tail = e;
-cp
-}
-
-/* Remove a queue element */
-void del_queue(packet_queue_t **q, queue_element_t *e)
-{
-cp
- free(e->packet);
-
- if(e->next) /* There is a successor, so we are not tail */
- {
- if(e->prev) /* There is a predecessor, so we are not head */
- {
- e->next->prev = e->prev;
- e->prev->next = e->next;
- }
- else /* We are head */
- {
- e->next->prev = NULL;
- (*q)->head = e->next;
- }
- }
- else /* We are tail (or all alone!) */
- {
- if(e->prev) /* We are not alone :) */
- {
- e->prev->next = NULL;
- (*q)->tail = e->prev;
- }
- else /* Adieu */
- {
- free(*q);
- *q = NULL;
- }
- }
-
- free(e);
-cp
-}
-
-/*
- 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, *next = NULL;
-cp
- for(p = (*pq)->head; p != NULL; )
- {
- next = p->next;
-
- if(!function(cl, p->packet))
- del_queue(pq, p);
-
- p = next;
- }
-
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, _("Queue flushed"));
-cp
-}
-
-/*
- 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 > 3)
- syslog(LOG_DEBUG, _("Flushing send queue for " IP_ADDR_S),
- IP_ADDR_V(cl->vpn_ip));
- flush_queue(cl, &(cl->sq), xsend);
- }
-
- if(cl->rq)
- {
- if(debug_lvl > 3)
- 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;
-cp
- if((cl = lookup_conn(to)) == NULL)
- {
- if(debug_lvl > 3)
- {
- syslog(LOG_NOTICE, _("Trying to look up " IP_ADDR_S " 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 " IP_ADDR_S " via " IP_ADDR_S),
- IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip));
- if((cl = lookup_conn(cl->real_ip)) == NULL)
- {
- if(debug_lvl > 3)
- syslog(LOG_NOTICE, _("Indirect look up " IP_ADDR_S " 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 " IP_ADDR_S),
- IP_ADDR_V(to));
- return -1;
- }
- }
-
- if(my_key_expiry <= time(NULL))
- regenerate_keys();
-
- if(!cl->status.dataopen)
- if(setup_vpn_connection(cl) < 0)