- EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
- bufp = outbuf;
- length = outlen;
- } else
- bufp = buffer;
-
- if(write(c->socket, bufp, length) < 0) {
- syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
- c->hostname, strerror(errno));
- return -1;
+ result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
+ &outlen, (unsigned char *)buffer, length);
+ if(!result || outlen < length) {
+ logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s",
+ c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
+ return false;
+ } else if(outlen > length) {
+ logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!");
+ abort();
+ }
+ c->outbuflen += outlen;
+ } else {
+ memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
+ c->outbuflen += length;
+ }
+
+ return true;
+}
+
+bool flush_meta(connection_t *c) {
+ int result;
+
+ ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)",
+ c->outbuflen, c->name, c->hostname);
+
+ while(c->outbuflen) {
+ result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
+ if(result <= 0) {
+ if(!errno || errno == EPIPE) {
+ ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
+ c->name, c->hostname);
+ } else if(errno == EINTR) {
+ continue;
+ } else if(sockwouldblock(sockerrno)) {
+ ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block",
+ c->outbuflen, c->name, c->hostname);
+ return true;
+ } else {
+ logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
+ c->hostname, sockstrerror(sockerrno));
+ }
+
+ return false;
+ }
+
+ c->outbufstart += result;
+ c->outbuflen -= result;