X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fmeta.c;h=4153c447e87288c6b4efc5cd8ff47fa2b3bdfb9c;hb=refs%2Fheads%2F1.0-gnutls;hp=e818b0b4b6b3a48e247e8899c040bff3e2ad504f;hpb=eefa28059ab989c915a7d95fb4ae728abd7ce713;p=tinc diff --git a/src/meta.c b/src/meta.c index e818b0b4..4153c447 100644 --- a/src/meta.c +++ b/src/meta.c @@ -17,26 +17,24 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: meta.c,v 1.1.2.37 2003/07/22 20:55:19 guus Exp $ + $Id: meta.c,v 1.1.2.50 2003/11/17 15:30:17 guus Exp $ */ #include "system.h" -#include +#include #include "avl_tree.h" #include "connection.h" #include "logger.h" +#include "meta.h" #include "net.h" #include "protocol.h" #include "system.h" #include "utils.h" -bool send_meta(connection_t *c, char *buffer, int length) +bool send_meta(connection_t *c, const char *buffer, int length) { - char *bufp; - int outlen; - char outbuf[MAXBUFSIZE]; int result; cp(); @@ -44,30 +42,28 @@ bool send_meta(connection_t *c, char *buffer, int length) ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length, c->name, c->hostname); - if(c->status.encryptout) { - EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length); - bufp = outbuf; - length = outlen; - } else - bufp = buffer; - while(length) { - result = write(c->socket, bufp, length); + result = gnutls_record_send(c->session, buffer, length); + if(result <= 0) { - if(errno == EINTR) + if(!result) { + ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"), + c->name, c->hostname); + } else if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN) continue; - logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name, - c->hostname, strerror(errno)); + else + logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name, + c->hostname, gnutls_strerror(result)); return false; } - bufp += result; + buffer += result; length -= result; } return true; } -void broadcast_meta(connection_t *from, char *buffer, int length) +void broadcast_meta(connection_t *from, const char *buffer, int length) { avl_node_t *node; connection_t *c; @@ -75,7 +71,7 @@ void broadcast_meta(connection_t *from, char *buffer, int length) cp(); for(node = connection_tree->head; node; node = node->next) { - c = (connection_t *) node->data; + c = node->data; if(c != from && c->status.active) send_meta(c, buffer, length); @@ -84,63 +80,52 @@ void broadcast_meta(connection_t *from, char *buffer, int length) bool receive_meta(connection_t *c) { - int x; - socklen_t l = sizeof(x); - int oldlen, i; - int lenin, reqlen; - bool decrypted = false; - char inbuf[MAXBUFSIZE]; + int oldlen, i, result; + int reqlen; cp(); - if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) { - logger(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__, - __LINE__, c->socket, strerror(errno), c->name, c->hostname); - return false; - } - - if(x) { - logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"), - c->name, c->hostname, strerror(x)); - return false; - } - /* Strategy: - Read as much as possible from the TCP socket in one go. - - Decrypt it. - Check if a full request is in the input buffer. - If yes, process request and remove it from the buffer, then check again. - If not, keep stuff in buffer and exit. */ - lenin = read(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen); + if(c->allow_request == ID) { + logger(LOG_DEBUG, _("Continuing handshake...")); + result = gnutls_handshake(c->session); + if(!result) { + logger(LOG_DEBUG, _("Handshake with %s (%s) completed!"), c->name, c->hostname); + c->allow_request = ACK; + return send_ack(c); + } + if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN) + return true; + logger(LOG_DEBUG, _("Handshake with %s (%s) failed: %s"), c->name, c->hostname, gnutls_strerror(result)); + return false; + } - if(lenin <= 0) { - if(lenin == 0) { + result = gnutls_record_recv(c->session, c->buffer + c->buflen, MAXBUFSIZE - c->buflen); + + if(result <= 0) { + if(!result) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"), c->name, c->hostname); - } else if(errno == EINTR) + } else if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN) return true; else logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"), - c->name, c->hostname, strerror(errno)); + c->name, c->hostname, gnutls_strerror(result)); return false; } oldlen = c->buflen; - c->buflen += lenin; - - while(lenin) { - /* Decrypt */ - - if(c->status.decryptin && !decrypted) { - EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin); - memcpy(c->buffer + oldlen, inbuf, lenin); - decrypted = true; - } + c->buflen += result; + while(c->buflen > 0) { /* Are we receiving a TCPpacket? */ if(c->tcplen) { @@ -148,7 +133,6 @@ bool receive_meta(connection_t *c) receive_tcppacket(c, c->buffer, c->tcplen); c->buflen -= c->tcplen; - lenin -= c->tcplen; memmove(c->buffer, c->buffer + c->tcplen, c->buflen); oldlen = 0; c->tcplen = 0; @@ -176,7 +160,6 @@ bool receive_meta(connection_t *c) return false; c->buflen -= reqlen; - lenin -= reqlen; memmove(c->buffer, c->buffer + reqlen, c->buflen); oldlen = 0; continue;