X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fnet.c;h=90bb88f160086525812438a9b93c518cf192cebf;hp=fdc2847e2b3ebb8513ad26638635abf24fd19e8e;hb=643d8712eb2f82bde21f206306cdb6491eee7e08;hpb=3a3356865267ff4c1e4f7d73f6d1486952d641b5 diff --git a/src/net.c b/src/net.c index fdc2847e..90bb88f1 100644 --- a/src/net.c +++ b/src/net.c @@ -152,7 +152,7 @@ cp */ void add_queue(packet_queue_t **q, void *packet, size_t s) { - queue_element_t *e, *p; + queue_element_t *e; cp if(debug_lvl > 3) syslog(LOG_DEBUG, "packet to queue: %d", s); @@ -160,21 +160,64 @@ cp 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; + e->next = NULL; /* We insert at the tail */ - if((*q)->tail != NULL) - (*q)->tail->next = e; + 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 +} - if((*q)->head == NULL) - (*q)->head = e; +/* 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 } @@ -183,28 +226,18 @@ cp each packet, and removing it when that returned a zero exit code */ -void flush_queue(conn_list_t *cl, packet_queue_t *pq, +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; + queue_element_t *p, *next = NULL; cp - for(p = pq->head; p != NULL; ) + for(p = (*pq)->head; p != NULL; ) { next = p->next; if(!function(cl, p->packet)) - { - if(prev) - prev->next = next; - else - pq->head = next; - - free(p->packet); - free(p); - } - else - prev = p; - + del_queue(pq, p); + p = next; } @@ -226,7 +259,7 @@ cp 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); + flush_queue(cl, &(cl->sq), xsend); } if(cl->rq) @@ -234,7 +267,7 @@ cp 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); + flush_queue(cl, &(cl->rq), xrecv); } cp } @@ -273,7 +306,7 @@ cp { add_queue(&(cl->sq), packet, packet->len + 2); if(!cl->status.waitingforkey) - send_key_request(to); + send_key_request(cl->vpn_ip); /* Keys should be sent to the host running the tincd */ return 0; } @@ -761,7 +794,7 @@ cp if(rp.len >= 0) { f = lookup_conn(rp.from); - if(debug_lvl > 2) + if(debug_lvl > 3) syslog(LOG_DEBUG, "packet from " IP_ADDR_S " (len %d)", IP_ADDR_V(rp.from), rp.len); if(!f) @@ -1028,10 +1061,10 @@ cp from = ntohl(*((unsigned long*)(&vp.data[26]))); to = ntohl(*((unsigned long*)(&vp.data[30]))); - if(debug_lvl > 2) + if(debug_lvl > 3) syslog(LOG_DEBUG, "An IP packet (%04x) for " IP_ADDR_S " from " IP_ADDR_S, ether_type, IP_ADDR_V(to), IP_ADDR_V(from)); - if(debug_lvl > 3) + if(debug_lvl > 4) syslog(LOG_DEBUG, MAC_ADDR_S " to " MAC_ADDR_S, MAC_ADDR_V(vp.data[0]), MAC_ADDR_V(vp.data[6]));