Use threads for TCP connections.
[tinc] / src / meta.c
index 787ccbd..1e4eb7f 100644 (file)
@@ -52,11 +52,11 @@ bool send_meta(connection_t *c, const char *buffer, int length) {
                }
                
                ifdebug(META) logger(LOG_DEBUG, "Encrypted write %p %p %p %d", c, c->buffer, outbuf, length);
-               bufferevent_write(c->buffer, (void *)outbuf, length);
+               write(c->socket, outbuf, length);
                ifdebug(META) logger(LOG_DEBUG, "Done.");
        } else {
                ifdebug(META) logger(LOG_DEBUG, "Unencrypted write %p %p %p %d", c, c->buffer, buffer, length);
-               bufferevent_write(c->buffer, (void *)buffer, length);
+               write(c->socket, buffer, length);
                ifdebug(META) logger(LOG_DEBUG, "Done.");
        }
 
@@ -76,7 +76,7 @@ void broadcast_meta(connection_t *from, const char *buffer, int length) {
 }
 
 bool receive_meta(connection_t *c) {
-       size_t inlen;
+       int inlen;
        char inbuf[MAXBUFSIZE];
        char *bufp = inbuf, *endp;
 
@@ -92,7 +92,14 @@ bool receive_meta(connection_t *c) {
        inlen = recv(c->socket, inbuf, sizeof inbuf, 0);
 
        if(inlen <= 0) {
-               logger(LOG_ERR, "Receive callback called for %s (%s) but no data to receive: %s", c->name, c->hostname, strerror(errno));
+               if(!inlen || !errno) {
+                       ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
+                                          c->name, c->hostname);
+               } else if(sockwouldblock(sockerrno))
+                       return true;
+               else
+                       logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
+                                  c->name, c->hostname, sockstrerror(sockerrno));
                return false;
        }
 
@@ -110,7 +117,7 @@ bool receive_meta(connection_t *c) {
                        bufp = endp;
                } else {
                        size_t outlen = inlen;
-                       ifdebug(META) logger(LOG_DEBUG, "Received encrypted %zu bytes", inlen);
+                       ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen);
                        evbuffer_expand(c->buffer->input, c->buffer->input->off + inlen);
 
                        if(!cipher_decrypt(&c->incipher, bufp, inlen, c->buffer->input->buffer + c->buffer->input->off, &outlen, false) || inlen != outlen) {
@@ -152,7 +159,5 @@ bool receive_meta(connection_t *c) {
                }
        } while(inlen);
 
-       c->last_ping_time = time(NULL);
-
        return true;
 }