- unsigned char tmp[MAXSIZE];
-
- memcpy(&tmp[12], p->data, p->len);
- p->len += 12;
- tmp[0] = tmp[6] = 0xfe;
- tmp[1] = tmp[7] = 0xfd;
- *((ip_t*)(&tmp[2])) = (ip_t)(htonl(myself->vpn_ip));
- *((ip_t*)(&tmp[8])) = *((ip_t*)(&tmp[26]));
- memcpy(p->data, &tmp[0], p->len);
-}
-
-int xsend(conn_list_t *cl, void *packet)
-{
- 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)
- { /* No open outgoing connection has been found. */
- if(debug_lvl > 2)
- syslog(LOG_NOTICE, "There is no remote host I can send this packet to.");
- 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. */
- }