X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fnet.c;h=ea66f1304a8d1e3dd675f85953ac56e12c130dba;hp=8b340c2b254ca9bf77d0b910da8cea5bdd00ad31;hb=1c007c0627ad5e71b8218fcb086240970e955c87;hpb=468f1d2efcce53937b7f5e0540269ae18f29ebac diff --git a/src/net.c b/src/net.c index 8b340c2b..ea66f130 100644 --- a/src/net.c +++ b/src/net.c @@ -109,27 +109,6 @@ cp 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; -cp - do - { - if((r = write(fd, buf, len)) < 0) - return -1; - len -= r; - buf += r; - done += r; - } while(len > 0); - - return done; -cp -} - int xrecv(conn_list_t *cl, void *packet) { vpn_packet_t vp; @@ -138,7 +117,7 @@ cp do_decrypt((real_packet_t*)packet, &vp, cl->key); add_mac_addresses(&vp); - if((lenin = write_n(tap_fd, &vp, vp.len + 2)) < 0) + 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; @@ -157,13 +136,13 @@ cp if(debug_lvl > 3) syslog(LOG_DEBUG, "packet to queue: %d", s); - e = xmalloc(sizeof(queue_element_t)); + e = xmalloc(sizeof(*e)); e->packet = xmalloc(s); memcpy(e->packet, packet, s); if(!*q) { - *q = xmalloc(sizeof(packet_queue_t)); + *q = xmalloc(sizeof(**q)); (*q)->head = (*q)->tail = NULL; } @@ -794,7 +773,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) @@ -914,7 +893,7 @@ int handle_new_meta_connection(conn_list_t *cl) { conn_list_t *ncn; struct sockaddr client; - int nfd, len = sizeof(struct sockaddr); + int nfd, len = sizeof(client); cp if((nfd = accept(cl->meta_socket, &client, &len)) < 0) { @@ -942,9 +921,10 @@ cp */ int handle_incoming_meta_data(conn_list_t *cl) { - int x, l = sizeof(x), lenin; + int x, l = sizeof(x); unsigned char tmp[1600]; int request; + int lenin = 0; cp if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) { @@ -957,7 +937,7 @@ cp return -1; } - if((lenin = recv(cl->meta_socket, &tmp, sizeof(tmp), 0)) <= 0) + if(read(cl->meta_socket, &tmp, 1) <= 0) { syslog(LOG_ERR, "Receive failed: %m"); return -1; @@ -968,6 +948,35 @@ cp if(debug_lvl > 3) syslog(LOG_DEBUG, "got request %d", request); + /* This is a hack. After an ACK request, multiple ADD_HOSTs can + follow. So if the request is one of these, only read as much + bytes as necessary. (Luckily the ADD_HOST request is of fixed + length) :P -- ivo */ + + if(request != ACK) + { + if(request == ADD_HOST) + { + if((lenin = read(cl->meta_socket, &tmp[1], sizeof(add_host_t) - 1)) <= 0) + { + syslog(LOG_ERR, "Receive failed for ADD_HOST: %m"); + return -1; + } + } + else + { + if((lenin = read(cl->meta_socket, &tmp[1], sizeof(tmp) - 1)) <= 0) + { + if(errno != EAGAIN) /* talk about hacks... */ + { + syslog(LOG_ERR, "Receive failed: %m"); + return -1; + } + } + } + } + + lenin++; if(request_handlers[request] == NULL) syslog(LOG_ERR, "Unknown request %d.", request); else @@ -1061,10 +1070,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])); @@ -1077,7 +1086,7 @@ cp } /* - this is where it al happens... + this is where it all happens... */ void main_loop(void) {