X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fnet.c;h=b6007b4871a4781341ea51ad1038df735640e797;hp=ceda90b837ecd5e1b2651a235fa49d844acbf3c1;hb=e20e143f1e99bdc0a7d92e97da1bd0dc40e8a83b;hpb=dad90e82d3c7af95820b1c04903bed7074e2b175 diff --git a/src/net.c b/src/net.c index ceda90b8..b6007b48 100644 --- a/src/net.c +++ b/src/net.c @@ -1,6 +1,6 @@ /* net.c -- most of the network code - Copyright (C) 1998,99 Ivo Timmermans + Copyright (C) 1998,1999,2000 Ivo Timmermans This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -51,8 +51,6 @@ int total_tap_out = 0; int total_socket_in = 0; int total_socket_out = 0; -time_t last_ping_time = 0; - /* The global list of existing connections */ conn_list_t *conn_list = NULL; conn_list_t *myself = NULL; @@ -93,41 +91,24 @@ int xsend(conn_list_t *cl, void *packet) real_packet_t rp; cp do_encrypt((vpn_packet_t*)packet, &rp, cl->key); - rp.from = myself->vpn_ip; + rp.from = htonl(myself->vpn_ip); + rp.data.len = htons(rp.data.len); + rp.len = htons(rp.len); if(debug_lvl > 3) - syslog(LOG_ERR, "Sent %d bytes to %lx", rp.len, cl->vpn_ip); + syslog(LOG_ERR, "Sent %d bytes to %lx", ntohs(rp.len), cl->vpn_ip); - if((r = send(cl->socket, (char*)&rp, rp.len, 0)) < 0) + if((r = send(cl->socket, (char*)&rp, ntohs(rp.len), 0)) < 0) { syslog(LOG_ERR, "Error sending data: %m"); return -1; } total_socket_out += r; -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; + cl->want_ping = 1; cp + return 0; } int xrecv(conn_list_t *cl, void *packet) @@ -138,10 +119,13 @@ 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; + + cl->want_ping = 0; + cl->last_ping_time = time(NULL); cp return 0; } @@ -157,13 +141,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; } @@ -306,7 +290,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; } @@ -323,21 +307,6 @@ cp return xsend(cl, packet); } -int send_broadcast(conn_list_t *cl, vpn_packet_t *packet) -{ - conn_list_t *p; -cp - 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. */ - } -cp - return 0; -} - /* open the local ethertap device */ @@ -580,7 +549,7 @@ int setup_network_connections(void) config_t const *cfg; cp if((cfg = get_config_val(pingtimeout)) == NULL) - timeout = 10; + timeout = 5; else timeout = cfg->data.val; @@ -725,7 +694,10 @@ cp p->real_ip = ntohl(ci.sin_addr.s_addr); p->meta_socket = sfd; p->status.meta = 1; - + p->buflen = 0; + p->last_ping_time = time(NULL); + p->want_ping = 0; + syslog(LOG_NOTICE, "Connection from %s:%d", p->hostname, htons(ci.sin_port)); if(send_basic_info(p) < 0) @@ -791,10 +763,15 @@ cp return -1; } total_socket_in += lenin; + + rp.data.len = ntohs(rp.data.len); + rp.len = ntohs(rp.len); + rp.from = ntohl(rp.from); + 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) @@ -854,52 +831,41 @@ cp } /* - send out a ping request to all active - connections -*/ -int send_broadcast_ping(void) -{ - conn_list_t *p; -cp - for(p = conn_list; p != NULL; p = p->next) - { - if(p->status.remove) - continue; - if(p->status.active && p->status.meta) - { - if(send_ping(p)) - terminate_connection(p); - else - { - p->status.pinged = 1; - p->status.got_pong = 0; - } - } - } - - last_ping_time = time(NULL); -cp - return 0; -} - -/* - end all connections that did not respond - to the ping probe in time + Check if the other end is active. + If we have sent packets, but didn't receive any, + then possibly the other end is dead. We send a + PING request over the meta connection. If the other + end does not reply in time, we consider them dead + and close the connection. */ int check_dead_connections(void) { conn_list_t *p; + time_t now; cp + now = time(NULL); for(p = conn_list; p != NULL; p = p->next) { if(p->status.remove) continue; - if(p->status.active && p->status.meta && p->status.pinged && !p->status.got_pong) + if(p->status.active && p->status.meta) { - syslog(LOG_INFO, "%s (" IP_ADDR_S ") didn't respond to ping", - p->hostname, IP_ADDR_V(p->vpn_ip)); - p->status.timeout = 1; - terminate_connection(p); + if(p->last_ping_time + timeout < now) + if(p->status.pinged && !p->status.got_pong) + { + syslog(LOG_INFO, "%s (" IP_ADDR_S ") didn't respond to ping", + p->hostname, IP_ADDR_V(p->vpn_ip)); + p->status.timeout = 1; + terminate_connection(p); + } + else if(p->want_ping) + { + send_ping(p); + p->last_ping_time = now; + p->status.pinged = 1; + p->status.get_pong = 0; + } + } } } cp @@ -914,7 +880,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 +908,9 @@ cp */ int handle_incoming_meta_data(conn_list_t *cl) { - int x, l = sizeof(x), lenin; - unsigned char tmp[1600]; - int request; + int x, l = sizeof(x); + int request, oldlen, i; + int lenin = 0; cp if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) { @@ -957,22 +923,69 @@ cp return -1; } - if((lenin = recv(cl->meta_socket, &tmp, sizeof(tmp), 0)) <= 0) + if(cl->buflen >= MAXBUFSIZE) + { + syslog(LOG_ERR, "Metadata read buffer full! Discarding contents."); + cl->buflen = 0; + } + + lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen); + + if(lenin<=0) { - syslog(LOG_ERR, "Receive failed: %m"); + syslog(LOG_ERR, "Metadata socket read error: %m"); return -1; } - request = (int)(tmp[0]); + oldlen = cl->buflen; + cl->buflen += lenin; - if(debug_lvl > 3) - syslog(LOG_DEBUG, "got request %d", request); + for(;;) + { + cl->reqlen = 0; - if(request_handlers[request] == NULL) - syslog(LOG_ERR, "Unknown request %d.", request); - else - if(request_handlers[request](cl, tmp, lenin) < 0) - return -1; + for(i = oldlen; i < cl->buflen; i++) + { + if(cl->buffer[i] == '\n') + { + cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */ + cl->reqlen = i + 1; + break; + } + } + + if(cl->reqlen) + { + if(sscanf(cl->buffer, "%d", &request) == 1) + { + if(request_handlers[request] == NULL) + { + syslog(LOG_ERR, "Unknown request: %s", cl->buffer); + return 0; + } + + if(debug_lvl > 3) + syslog(LOG_DEBUG, "Got request: %s", cl->buffer); + + request_handlers[request](cl); + } + else + { + syslog(LOG_ERR, "Bogus data received: %s", cl->buffer); + } + + cl->buflen -= cl->reqlen; + memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen); + oldlen = 0; + } + else + { + break; + } + } + + cl->last_ping_time = time(NULL); + cl->want_ping = 0; cp return 0; } @@ -991,7 +1004,7 @@ cp if(p->status.remove) continue; - if(p->status.active) + if(p->status.dataopen) if(FD_ISSET(p->socket, f)) { /* @@ -1061,10 +1074,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,15 +1090,16 @@ cp } /* - this is where it al happens... + this is where it all happens... */ void main_loop(void) { fd_set fset; struct timeval tv; int r; + time_t last_ping_check; cp - last_ping_time = time(NULL); + last_ping_check = time(NULL); for(;;) { @@ -1103,11 +1117,11 @@ cp return; } - if(r == 0 || last_ping_time + timeout < time(NULL)) - /* Timeout... hm... something might be wrong. */ + if(last_ping_check + timeout < time(NULL)) + /* Let's check if everybody is still alive */ { check_dead_connections(); - send_broadcast_ping(); + last_ping_check = time(NULL); continue; }