hammer2 - Messaging layer separation work part 2
authorMatthew Dillon <dillon@apollo.backplane.com>
Thu, 25 Oct 2012 06:26:40 +0000 (23:26 -0700)
committerMatthew Dillon <dillon@apollo.backplane.com>
Thu, 25 Oct 2012 06:26:40 +0000 (23:26 -0700)
* Move most of the low-level message handling from the hammer2 utility
  to a new library called libdmsg.

* Move additional message headers from the hammer2 vfs code to libdmsg,
  cleanup.

* Rename and reorganize a ton of stuff.

25 files changed:
lib/Makefile
lib/libdmsg/Makefile [new file with mode: 0644]
lib/libdmsg/crypto.c [moved from sbin/hammer2/crypto.c with 76% similarity]
lib/libdmsg/debug.c [new file with mode: 0644]
lib/libdmsg/dmsg.h [new file with mode: 0644]
lib/libdmsg/dmsg_local.h [copied from sys/vfs/hammer2/hammer2_mount.h with 70% similarity]
lib/libdmsg/icrc.c [copied from sys/vfs/hammer2/hammer2_icrc.c with 95% similarity]
lib/libdmsg/msg.c [moved from sbin/hammer2/msg.c with 73% similarity]
lib/libdmsg/msg_lnk.c [moved from sbin/hammer2/msg_lnk.c with 86% similarity]
lib/libdmsg/service.c [new file with mode: 0644]
lib/libdmsg/subs.c [copied from sys/vfs/hammer2/hammer2_mount.h with 54% similarity]
sbin/hammer2/Makefile
sbin/hammer2/cmd_debug.c
sbin/hammer2/cmd_rsa.c
sbin/hammer2/cmd_service.c
sbin/hammer2/hammer2.h
sbin/hammer2/main.c
sbin/hammer2/network.h [deleted file]
sbin/hammer2/subs.c
share/mk/bsd.libnames.mk
sys/sys/dmsg.h
sys/vfs/hammer2/hammer2.h
sys/vfs/hammer2/hammer2_disk.h
sys/vfs/hammer2/hammer2_icrc.c
sys/vfs/hammer2/hammer2_mount.h

index 9407d90..fd84b3d 100644 (file)
@@ -47,6 +47,7 @@ SUBDIR=       ${SUBDIR_ORDERED} \
        libdevstat \
        libdl \
        libdm \
+       libdmsg \
        libedit \
        libevtr \
        libexpat \
diff --git a/lib/libdmsg/Makefile b/lib/libdmsg/Makefile
new file mode 100644 (file)
index 0000000..8515fd9
--- /dev/null
@@ -0,0 +1,10 @@
+# libdmsg - hammer2 network cluster messaging library
+#
+LIB=   dmsg
+SRCS+= icrc.c debug.c subs.c crypto.c msg.c msg_lnk.c service.c
+
+CFLAGS+= -pthread
+
+INCS=  dmsg.h
+
+.include <bsd.lib.mk>
similarity index 76%
rename from sbin/hammer2/crypto.c
rename to lib/libdmsg/crypto.c
index 2de957f..de03731 100644 (file)
@@ -34,8 +34,7 @@
  * SUCH DAMAGE.
  */
 
-#include "hammer2.h"
-#include <sys/endian.h>
+#include "dmsg_local.h"
 
 /*
  * Setup crypto for pthreads
 static pthread_mutex_t *crypto_locks;
 int crypto_count;
 
-static int hammer2_crypto_gcm_init(hammer2_ioq_t *, char *, int, char *, int, int);
-static int hammer2_crypto_gcm_encrypt_chunk(hammer2_ioq_t *, char *, char *, int, int *);
-static int hammer2_crypto_gcm_decrypt_chunk(hammer2_ioq_t *, char *, char *, int, int *);
+static int dmsg_crypto_gcm_init(dmsg_ioq_t *, char *, int, char *, int, int);
+static int dmsg_crypto_gcm_encrypt_chunk(dmsg_ioq_t *, char *, char *, int, int *);
+static int dmsg_crypto_gcm_decrypt_chunk(dmsg_ioq_t *, char *, char *, int, int *);
 
 /*
- * NOTE: the order of this table needs to match the HAMMER2_CRYPTO_ALGO_*_IDX
+ * NOTE: the order of this table needs to match the DMSG_CRYPTO_ALGO_*_IDX
  *       defines in network.h.
  */
 static struct crypto_algo crypto_algos[] = {
        {
                .name      = "aes-256-gcm",
-               .keylen    = HAMMER2_CRYPTO_GCM_KEY_SIZE,
-               .taglen    = HAMMER2_CRYPTO_GCM_TAG_SIZE,
-               .init      = hammer2_crypto_gcm_init,
-               .enc_chunk = hammer2_crypto_gcm_encrypt_chunk,
-               .dec_chunk = hammer2_crypto_gcm_decrypt_chunk
+               .keylen    = DMSG_CRYPTO_GCM_KEY_SIZE,
+               .taglen    = DMSG_CRYPTO_GCM_TAG_SIZE,
+               .init      = dmsg_crypto_gcm_init,
+               .enc_chunk = dmsg_crypto_gcm_encrypt_chunk,
+               .dec_chunk = dmsg_crypto_gcm_decrypt_chunk
        },
        { NULL, 0, 0, NULL, NULL, NULL }
 };
 
 static
 unsigned long
-hammer2_crypto_id_callback(void)
+dmsg_crypto_id_callback(void)
 {
        return ((unsigned long)(uintptr_t)pthread_self());
 }
 
 static
 void
-hammer2_crypto_locking_callback(int mode, int type,
+dmsg_crypto_locking_callback(int mode, int type,
                                const char *file __unused, int line __unused)
 {
        assert(type >= 0 && type < crypto_count);
@@ -84,35 +83,35 @@ hammer2_crypto_locking_callback(int mode, int type,
 }
 
 void
-hammer2_crypto_setup(void)
+dmsg_crypto_setup(void)
 {
        crypto_count = CRYPTO_num_locks();
        crypto_locks = calloc(crypto_count, sizeof(crypto_locks[0]));
-       CRYPTO_set_id_callback(hammer2_crypto_id_callback);
-       CRYPTO_set_locking_callback(hammer2_crypto_locking_callback);
+       CRYPTO_set_id_callback(dmsg_crypto_id_callback);
+       CRYPTO_set_locking_callback(dmsg_crypto_locking_callback);
 }
 
 static
 int
-hammer2_crypto_gcm_init(hammer2_ioq_t *ioq, char *key, int klen,
+dmsg_crypto_gcm_init(dmsg_ioq_t *ioq, char *key, int klen,
                        char *iv_fixed, int ivlen, int enc)
 {
        int i, ok;
 
-       if (klen < HAMMER2_CRYPTO_GCM_KEY_SIZE ||
-           ivlen < HAMMER2_CRYPTO_GCM_IV_FIXED_SIZE) {
-               if (DebugOpt)
+       if (klen < DMSG_CRYPTO_GCM_KEY_SIZE ||
+           ivlen < DMSG_CRYPTO_GCM_IV_FIXED_SIZE) {
+               if (DMsgDebugOpt)
                        fprintf(stderr, "Not enough key or iv material\n");
                return -1;
        }
 
        printf("%s key: ", enc ? "Encryption" : "Decryption");
-       for (i = 0; i < HAMMER2_CRYPTO_GCM_KEY_SIZE; ++i)
+       for (i = 0; i < DMSG_CRYPTO_GCM_KEY_SIZE; ++i)
                printf("%02x", (unsigned char)key[i]);
        printf("\n");
 
        printf("%s iv:  ", enc ? "Encryption" : "Decryption");
-       for (i = 0; i < HAMMER2_CRYPTO_GCM_IV_FIXED_SIZE; ++i)
+       for (i = 0; i < DMSG_CRYPTO_GCM_IV_FIXED_SIZE; ++i)
                printf("%02x", (unsigned char)iv_fixed[i]);
        printf(" (fixed part only)\n");
 
@@ -146,12 +145,12 @@ hammer2_crypto_gcm_init(hammer2_ioq_t *ioq, char *key, int klen,
         * traffic.
         */
        ok = EVP_CIPHER_CTX_ctrl(&ioq->ctx, EVP_CTRL_GCM_SET_IVLEN,
-                                HAMMER2_CRYPTO_GCM_IV_SIZE, NULL);
+                                DMSG_CRYPTO_GCM_IV_SIZE, NULL);
        if (!ok)
                goto fail;
 
-       memset(ioq->iv, 0, HAMMER2_CRYPTO_GCM_IV_SIZE);
-       memcpy(ioq->iv, iv_fixed, HAMMER2_CRYPTO_GCM_IV_FIXED_SIZE);
+       memset(ioq->iv, 0, DMSG_CRYPTO_GCM_IV_SIZE);
+       memcpy(ioq->iv, iv_fixed, DMSG_CRYPTO_GCM_IV_FIXED_SIZE);
 
        /*
         * Strictly speaking, padding is irrelevant with a counter mode
@@ -166,7 +165,7 @@ hammer2_crypto_gcm_init(hammer2_ioq_t *ioq, char *key, int klen,
        return 0;
 
 fail:
-       if (DebugOpt)
+       if (DMsgDebugOpt)
                fprintf(stderr, "Error during _gcm_init\n");
        return -1;
 }
@@ -183,7 +182,7 @@ _gcm_iv_increment(char *iv)
         * unique to the session and a 64 bit integer counter.
         */
 
-       uint64_t *c = (uint64_t *)(&iv[HAMMER2_CRYPTO_GCM_IV_FIXED_SIZE]);
+       uint64_t *c = (uint64_t *)(&iv[DMSG_CRYPTO_GCM_IV_FIXED_SIZE]);
 
        /* Increment invocation field integer counter */
        *c = htobe64(be64toh(*c)+1);
@@ -197,7 +196,7 @@ _gcm_iv_increment(char *iv)
 
 static
 int
-hammer2_crypto_gcm_encrypt_chunk(hammer2_ioq_t *ioq, char *ct, char *pt,
+dmsg_crypto_gcm_encrypt_chunk(dmsg_ioq_t *ioq, char *ct, char *pt,
                                 int in_size, int *out_size)
 {
        int ok;
@@ -220,32 +219,32 @@ hammer2_crypto_gcm_encrypt_chunk(hammer2_ioq_t *ioq, char *ct, char *pt,
 
        /* Retrieve auth tag */
        ok = EVP_CIPHER_CTX_ctrl(&ioq->ctx, EVP_CTRL_GCM_GET_TAG,
-                                HAMMER2_CRYPTO_GCM_TAG_SIZE,
+                                DMSG_CRYPTO_GCM_TAG_SIZE,
                                 ct + u_len + f_len);
        if (!ok)
                goto fail;
 
        ok = _gcm_iv_increment(ioq->iv);
        if (!ok) {
-               ioq->error = HAMMER2_IOQ_ERROR_IVWRAP;
+               ioq->error = DMSG_IOQ_ERROR_IVWRAP;
                goto fail_out;
        }
 
-       *out_size = u_len + f_len + HAMMER2_CRYPTO_GCM_TAG_SIZE;
+       *out_size = u_len + f_len + DMSG_CRYPTO_GCM_TAG_SIZE;
 
        return 0;
 
 fail:
-       ioq->error = HAMMER2_IOQ_ERROR_ALGO;
+       ioq->error = DMSG_IOQ_ERROR_ALGO;
 fail_out:
-       if (DebugOpt)
+       if (DMsgDebugOpt)
                fprintf(stderr, "error during encrypt_chunk\n");
        return -1;
 }
 
 static
 int
-hammer2_crypto_gcm_decrypt_chunk(hammer2_ioq_t *ioq, char *ct, char *pt,
+dmsg_crypto_gcm_decrypt_chunk(dmsg_ioq_t *ioq, char *ct, char *pt,
                                 int out_size, int *consume_size)
 {
        int ok;
@@ -256,15 +255,15 @@ hammer2_crypto_gcm_decrypt_chunk(hammer2_ioq_t *ioq, char *ct, char *pt,
        /* Re-initialize with new IV (but without redoing the key schedule) */
        ok = EVP_DecryptInit_ex(&ioq->ctx, NULL, NULL, NULL, ioq->iv);
        if (!ok) {
-               ioq->error = HAMMER2_IOQ_ERROR_ALGO;
+               ioq->error = DMSG_IOQ_ERROR_ALGO;
                goto fail_out;
        }
 
        ok = EVP_CIPHER_CTX_ctrl(&ioq->ctx, EVP_CTRL_GCM_SET_TAG,
-                                HAMMER2_CRYPTO_GCM_TAG_SIZE,
+                                DMSG_CRYPTO_GCM_TAG_SIZE,
                                 ct + out_size);
        if (!ok) {
-               ioq->error = HAMMER2_IOQ_ERROR_ALGO;
+               ioq->error = DMSG_IOQ_ERROR_ALGO;
                goto fail_out;
        }
 
@@ -278,18 +277,18 @@ hammer2_crypto_gcm_decrypt_chunk(hammer2_ioq_t *ioq, char *ct, char *pt,
 
        ok = _gcm_iv_increment(ioq->iv);
        if (!ok) {
-               ioq->error = HAMMER2_IOQ_ERROR_IVWRAP;
+               ioq->error = DMSG_IOQ_ERROR_IVWRAP;
                goto fail_out;
        }
 
-       *consume_size = u_len + f_len + HAMMER2_CRYPTO_GCM_TAG_SIZE;
+       *consume_size = u_len + f_len + DMSG_CRYPTO_GCM_TAG_SIZE;
 
        return 0;
 
 fail:
-       ioq->error = HAMMER2_IOQ_ERROR_MACFAIL;
+       ioq->error = DMSG_IOQ_ERROR_MACFAIL;
 fail_out:
-       if (DebugOpt)
+       if (DMsgDebugOpt)
                fprintf(stderr, "error during decrypt_chunk (likely authentication error)\n");
        return -1;
 }
@@ -339,14 +338,14 @@ typedef union {
 } sockaddr_any_t;
 
 void
-hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
+dmsg_crypto_negotiate(dmsg_iocom_t *iocom)
 {
        sockaddr_any_t sa;
        socklen_t salen = sizeof(sa);
        char peername[128];
        char realname[128];
-       hammer2_handshake_t handtx;
-       hammer2_handshake_t handrx;
+       dmsg_handshake_t handtx;
+       dmsg_handshake_t handrx;
        char buf1[sizeof(handtx)];
        char buf2[sizeof(handtx)];
        char *ptr;
@@ -365,21 +364,21 @@ hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
         * Get the peer IP address for the connection as a string.
         */
        if (getpeername(iocom->sock_fd, &sa.sa, &salen) < 0) {
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOPEER;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_NOPEER;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "accept: getpeername() failed\n");
                goto done;
        }
        if (getnameinfo(&sa.sa, salen, peername, sizeof(peername),
                        NULL, 0, NI_NUMERICHOST) < 0) {
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOPEER;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_NOPEER;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "accept: cannot decode sockaddr\n");
                goto done;
        }
-       if (DebugOpt) {
+       if (DMsgDebugOpt) {
                if (realhostname_sa(realname, sizeof(realname),
                                    &sa.sa, salen) == HOSTNAME_FOUND) {
                        fprintf(stderr, "accept from %s (%s)\n",
@@ -395,19 +394,19 @@ hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
         * If the link is not to be encrypted (<ip>.none located) we shortcut
         * the handshake entirely.  No buffers are exchanged.
         */
-       asprintf(&path, "%s/%s.pub", HAMMER2_PATH_REMOTE, peername);
+       asprintf(&path, "%s/%s.pub", DMSG_PATH_REMOTE, peername);
        if ((fp = fopen(path, "r")) == NULL) {
                free(path);
                asprintf(&path, "%s/%s.none",
-                        HAMMER2_PATH_REMOTE, peername);
+                        DMSG_PATH_REMOTE, peername);
                if (stat(path, &st) < 0) {
-                       iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NORKEY;
-                       iocom->flags |= HAMMER2_IOCOMF_EOF;
-                       if (DebugOpt)
+                       iocom->ioq_rx.error = DMSG_IOQ_ERROR_NORKEY;
+                       iocom->flags |= DMSG_IOCOMF_EOF;
+                       if (DMsgDebugOpt)
                                fprintf(stderr, "auth failure: unknown host\n");
                        goto done;
                }
-               if (DebugOpt)
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth succeeded, unencrypted link\n");
                goto done;
        }
@@ -415,9 +414,9 @@ hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
                keys[0] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
                fclose(fp);
                if (keys[0] == NULL) {
-                       iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
-                       iocom->flags |= HAMMER2_IOCOMF_EOF;
-                       if (DebugOpt)
+                       iocom->ioq_rx.error = DMSG_IOQ_ERROR_KEYFMT;
+                       iocom->flags |= DMSG_IOCOMF_EOF;
+                       if (DMsgDebugOpt)
                                fprintf(stderr,
                                        "auth failure: bad key format\n");
                        goto done;
@@ -428,37 +427,37 @@ hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
         * Get our public and private keys
         */
        free(path);
-       asprintf(&path, HAMMER2_DEFAULT_DIR "/rsa.pub");
+       asprintf(&path, DMSG_DEFAULT_DIR "/rsa.pub");
        if ((fp = fopen(path, "r")) == NULL) {
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOLKEY;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_NOLKEY;
+               iocom->flags |= DMSG_IOCOMF_EOF;
                goto done;
        }
        keys[1] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
        fclose(fp);
        if (keys[1] == NULL) {
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_KEYFMT;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: bad host key format\n");
                goto done;
        }
 
        free(path);
-       asprintf(&path, HAMMER2_DEFAULT_DIR "/rsa.prv");
+       asprintf(&path, DMSG_DEFAULT_DIR "/rsa.prv");
        if ((fp = fopen(path, "r")) == NULL) {
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOLKEY;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_NOLKEY;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: bad host key format\n");
                goto done;
        }
        keys[2] = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
        fclose(fp);
        if (keys[2] == NULL) {
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_KEYFMT;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: bad host key format\n");
                goto done;
        }
@@ -473,9 +472,9 @@ hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
                if (blksize != (size_t)RSA_size(keys[1]) ||
                    blksize != (size_t)RSA_size(keys[2]) ||
                    sizeof(handtx) % blksize != 0) {
-                       iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
-                       iocom->flags |= HAMMER2_IOCOMF_EOF;
-                       if (DebugOpt)
+                       iocom->ioq_rx.error = DMSG_IOQ_ERROR_KEYFMT;
+                       iocom->flags |= DMSG_IOCOMF_EOF;
+                       if (DMsgDebugOpt)
                                fprintf(stderr, "auth failure: "
                                                "key size mismatch\n");
                        goto done;
@@ -500,9 +499,9 @@ hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
 urandfail:
                if (fd >= 0)
                        close(fd);
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_BADURANDOM;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_BADURANDOM;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: bad rng\n");
                goto done;
        }
@@ -554,14 +553,14 @@ urandfail:
                                if (RSA_private_encrypt(blksize, ptr, buf1,
                                            keys[2], RSA_NO_PADDING) < 0) {
                                        iocom->ioq_rx.error =
-                                               HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
+                                               DMSG_IOQ_ERROR_KEYXCHGFAIL;
                                }
                        } while (buf1[0] & 0xC0);
 
                        if (RSA_public_encrypt(blksize, buf1, buf2,
                                            keys[0], RSA_NO_PADDING) < 0) {
                                iocom->ioq_rx.error =
-                                       HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
+                                       DMSG_IOQ_ERROR_KEYXCHGFAIL;
                        }
                }
                if (write(iocom->sock_fd, buf2, blksize) != (ssize_t)blksize) {
@@ -569,8 +568,8 @@ urandfail:
                }
        }
        if (iocom->ioq_rx.error) {
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: key exchange failure "
                                        "during encryption\n");
                goto done;
@@ -591,16 +590,16 @@ urandfail:
                        if (RSA_private_decrypt(blksize, ptr, buf1,
                                           keys[2], RSA_NO_PADDING) < 0)
                                iocom->ioq_rx.error =
-                                               HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
+                                               DMSG_IOQ_ERROR_KEYXCHGFAIL;
                        if (RSA_public_decrypt(blksize, buf1, ptr,
                                           keys[0], RSA_NO_PADDING) < 0)
                                iocom->ioq_rx.error =
-                                               HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
+                                               DMSG_IOQ_ERROR_KEYXCHGFAIL;
                }
        }
        if (iocom->ioq_rx.error) {
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: key exchange failure "
                                        "during decryption\n");
                goto done;
@@ -612,9 +611,9 @@ urandfail:
         */
        if (i != sizeof(handrx)) {
 keyxchgfail:
-               iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
-               if (DebugOpt)
+               iocom->ioq_rx.error = DMSG_IOQ_ERROR_KEYXCHGFAIL;
+               iocom->flags |= DMSG_IOCOMF_EOF;
+               if (DMsgDebugOpt)
                        fprintf(stderr, "auth failure: key exchange failure\n");
                goto done;
        }
@@ -637,25 +636,25 @@ keyxchgfail:
         * Use separate session keys and session fixed IVs for receive and
         * transmit.
         */
-       error = crypto_algos[HAMMER2_CRYPTO_ALGO].init(&iocom->ioq_rx, handrx.sess,
-           crypto_algos[HAMMER2_CRYPTO_ALGO].keylen,
-           handrx.sess + crypto_algos[HAMMER2_CRYPTO_ALGO].keylen,
-           sizeof(handrx.sess) - crypto_algos[HAMMER2_CRYPTO_ALGO].keylen,
+       error = crypto_algos[DMSG_CRYPTO_ALGO].init(&iocom->ioq_rx, handrx.sess,
+           crypto_algos[DMSG_CRYPTO_ALGO].keylen,
+           handrx.sess + crypto_algos[DMSG_CRYPTO_ALGO].keylen,
+           sizeof(handrx.sess) - crypto_algos[DMSG_CRYPTO_ALGO].keylen,
            0 /* decryption */);
        if (error)
                goto keyxchgfail;
 
-       error = crypto_algos[HAMMER2_CRYPTO_ALGO].init(&iocom->ioq_tx, handtx.sess,
-           crypto_algos[HAMMER2_CRYPTO_ALGO].keylen,
-           handtx.sess + crypto_algos[HAMMER2_CRYPTO_ALGO].keylen,
-           sizeof(handtx.sess) - crypto_algos[HAMMER2_CRYPTO_ALGO].keylen,
+       error = crypto_algos[DMSG_CRYPTO_ALGO].init(&iocom->ioq_tx, handtx.sess,
+           crypto_algos[DMSG_CRYPTO_ALGO].keylen,
+           handtx.sess + crypto_algos[DMSG_CRYPTO_ALGO].keylen,
+           sizeof(handtx.sess) - crypto_algos[DMSG_CRYPTO_ALGO].keylen,
            1 /* encryption */);
        if (error)
                goto keyxchgfail;
 
-       iocom->flags |= HAMMER2_IOCOMF_CRYPTED;
+       iocom->flags |= DMSG_IOCOMF_CRYPTED;
 
-       if (DebugOpt)
+       if (DMsgDebugOpt)
                fprintf(stderr, "auth success: %s\n", handrx.quickmsg);
 done:
        if (path)
@@ -672,7 +671,7 @@ done:
  * Decrypt pending data in the ioq's fifo.  The data is decrypted in-place.
  */
 void
-hammer2_crypto_decrypt(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
+dmsg_crypto_decrypt(dmsg_iocom_t *iocom __unused, dmsg_ioq_t *ioq)
 {
        int p_len;
        int used;
@@ -688,15 +687,15 @@ hammer2_crypto_decrypt(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
        if (p_len == 0)
                return;
 
-       while (p_len >= crypto_algos[HAMMER2_CRYPTO_ALGO].taglen +
-           HAMMER2_CRYPTO_CHUNK_SIZE) {
+       while (p_len >= crypto_algos[DMSG_CRYPTO_ALGO].taglen +
+           DMSG_CRYPTO_CHUNK_SIZE) {
                bcopy(ioq->buf + ioq->fifo_cdn, buf,
-                     crypto_algos[HAMMER2_CRYPTO_ALGO].taglen +
-                     HAMMER2_CRYPTO_CHUNK_SIZE);
-               error = crypto_algos[HAMMER2_CRYPTO_ALGO].dec_chunk(
+                     crypto_algos[DMSG_CRYPTO_ALGO].taglen +
+                     DMSG_CRYPTO_CHUNK_SIZE);
+               error = crypto_algos[DMSG_CRYPTO_ALGO].dec_chunk(
                    ioq, buf,
                    ioq->buf + ioq->fifo_cdx,
-                   HAMMER2_CRYPTO_CHUNK_SIZE,
+                   DMSG_CRYPTO_CHUNK_SIZE,
                    &used);
 #ifdef CRYPTO_DEBUG
                printf("dec: p_len: %d, used: %d, fifo_cdn: %ju, fifo_cdx: %ju\n",
@@ -704,7 +703,7 @@ hammer2_crypto_decrypt(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
 #endif
                p_len -= used;
                ioq->fifo_cdn += used;
-               ioq->fifo_cdx += HAMMER2_CRYPTO_CHUNK_SIZE;
+               ioq->fifo_cdx += DMSG_CRYPTO_CHUNK_SIZE;
 #ifdef CRYPTO_DEBUG
                printf("dec: p_len: %d, used: %d, fifo_cdn: %ju, fifo_cdx: %ju\n",
                       p_len, used, ioq->fifo_cdn, ioq->fifo_cdx);
@@ -717,8 +716,8 @@ hammer2_crypto_decrypt(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
  * The FIFO may contain more data.
  */
 int
-hammer2_crypto_encrypt(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq,
-                      struct iovec *iov, int n, size_t *nactp)
+dmsg_crypto_encrypt(dmsg_iocom_t *iocom __unused, dmsg_ioq_t *ioq,
+                   struct iovec *iov, int n, size_t *nactp)
 {
        int p_len, used, ct_used;
        int i;
@@ -733,22 +732,22 @@ hammer2_crypto_encrypt(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq,
                p_len = iov[i].iov_len;
                assert((p_len & DMSG_ALIGNMASK) == 0);
 
-               while (p_len >= HAMMER2_CRYPTO_CHUNK_SIZE &&
-                   nmax >= HAMMER2_CRYPTO_CHUNK_SIZE +
-                   (size_t)crypto_algos[HAMMER2_CRYPTO_ALGO].taglen) {
-                       error = crypto_algos[HAMMER2_CRYPTO_ALGO].enc_chunk(
+               while (p_len >= DMSG_CRYPTO_CHUNK_SIZE &&
+                   nmax >= DMSG_CRYPTO_CHUNK_SIZE +
+                   (size_t)crypto_algos[DMSG_CRYPTO_ALGO].taglen) {
+                       error = crypto_algos[DMSG_CRYPTO_ALGO].enc_chunk(
                            ioq,
                            ioq->buf + ioq->fifo_cdx,
                            (char *)iov[i].iov_base + used,
-                           HAMMER2_CRYPTO_CHUNK_SIZE, &ct_used);
+                           DMSG_CRYPTO_CHUNK_SIZE, &ct_used);
 #ifdef CRYPTO_DEBUG
                        printf("nactp: %ju, p_len: %d, ct_used: %d, used: %d, nmax: %ju\n",
                               *nactp, p_len, ct_used, used, nmax);
 #endif
 
-                       *nactp += (size_t)HAMMER2_CRYPTO_CHUNK_SIZE;    /* plaintext count */
-                       used += HAMMER2_CRYPTO_CHUNK_SIZE;
-                       p_len -= HAMMER2_CRYPTO_CHUNK_SIZE;
+                       *nactp += (size_t)DMSG_CRYPTO_CHUNK_SIZE;       /* plaintext count */
+                       used += DMSG_CRYPTO_CHUNK_SIZE;
+                       p_len -= DMSG_CRYPTO_CHUNK_SIZE;
 
                        ioq->fifo_cdx += (size_t)ct_used;       /* crypted count */
                        ioq->fifo_cdn += (size_t)ct_used;       /* crypted count */
diff --git a/lib/libdmsg/debug.c b/lib/libdmsg/debug.c
new file mode 100644 (file)
index 0000000..9c69ef0
--- /dev/null
@@ -0,0 +1,260 @@
+/*
+ * Copyright (c) 2012 The DragonFly Project.  All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Matthew Dillon <dillon@dragonflybsd.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "dmsg_local.h"
+
+const char *
+dmsg_basecmd_str(uint32_t cmd)
+{
+       static char buf[64];
+       char protobuf[32];
+       char cmdbuf[32];
+       const char *protostr;
+       const char *cmdstr;
+
+       switch(cmd & DMSGF_PROTOS) {
+       case DMSG_PROTO_LNK:
+               protostr = "LNK_";
+               break;
+       case DMSG_PROTO_DBG:
+               protostr = "DBG_";
+               break;
+       case DMSG_PROTO_DOM:
+               protostr = "DOM_";
+               break;
+       case DMSG_PROTO_CAC:
+               protostr = "CAC_";
+               break;
+       case DMSG_PROTO_QRM:
+               protostr = "QRM_";
+               break;
+       case DMSG_PROTO_BLK:
+               protostr = "BLK_";
+               break;
+       case DMSG_PROTO_VOP:
+               protostr = "VOP_";
+               break;
+       default:
+               snprintf(protobuf, sizeof(protobuf), "%x_",
+                       (cmd & DMSGF_PROTOS) >> 20);
+               protostr = protobuf;
+               break;
+       }
+
+       switch(cmd & (DMSGF_PROTOS |
+                     DMSGF_CMDS |
+                     DMSGF_SIZE)) {
+       case DMSG_LNK_PAD:
+               cmdstr = "PAD";
+               break;
+       case DMSG_LNK_PING:
+               cmdstr = "PING";
+               break;
+       case DMSG_LNK_AUTH:
+               cmdstr = "AUTH";
+               break;
+       case DMSG_LNK_CONN:
+               cmdstr = "CONN";
+               break;
+       case DMSG_LNK_SPAN:
+               cmdstr = "SPAN";
+               break;
+       case DMSG_LNK_VOLCONF:
+               cmdstr = "VOLCONF";
+               break;
+       case DMSG_LNK_ERROR:
+               if (cmd & DMSGF_DELETE)
+                       cmdstr = "RETURN";
+               else
+                       cmdstr = "RESULT";
+               break;
+       case DMSG_DBG_SHELL:
+               cmdstr = "SHELL";
+               break;
+       default:
+               snprintf(cmdbuf, sizeof(cmdbuf),
+                        "%06x", (cmd & (DMSGF_PROTOS |
+                                        DMSGF_CMDS |
+                                        DMSGF_SIZE)));
+               cmdstr = cmdbuf;
+               break;
+       }
+       snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
+       return (buf);
+}
+
+const char *
+dmsg_msg_str(dmsg_msg_t *msg)
+{
+       dmsg_state_t *state;
+       static char buf[256];
+       char errbuf[16];
+       char statebuf[64];
+       char flagbuf[64];
+       const char *statestr;
+       const char *errstr;
+       uint32_t basecmd;
+       int i;
+
+       /*
+        * Parse the state
+        */
+       if ((state = msg->state) != NULL) {
+               basecmd = (state->rxcmd & DMSGF_REPLY) ?
+                         state->txcmd : state->rxcmd;
+               snprintf(statebuf, sizeof(statebuf),
+                        " %s=%s,L=%s%s,R=%s%s",
+                        ((state->txcmd & DMSGF_REPLY) ?
+                               "rcvcmd" : "sndcmd"),
+                        dmsg_basecmd_str(basecmd),
+                        ((state->txcmd & DMSGF_CREATE) ? "C" : ""),
+                        ((state->txcmd & DMSGF_DELETE) ? "D" : ""),
+                        ((state->rxcmd & DMSGF_CREATE) ? "C" : ""),
+                        ((state->rxcmd & DMSGF_DELETE) ? "D" : "")
+               );
+               statestr = statebuf;
+       } else {
+               statestr = "";
+       }
+
+       /*
+        * Parse the error
+        */
+       switch(msg->any.head.error) {
+       case 0:
+               errstr = "";
+               break;
+       case DMSG_IOQ_ERROR_SYNC:
+               errstr = "err=IOQ:NOSYNC";
+               break;
+       case DMSG_IOQ_ERROR_EOF:
+               errstr = "err=IOQ:STREAMEOF";
+               break;
+       case DMSG_IOQ_ERROR_SOCK:
+               errstr = "err=IOQ:SOCKERR";
+               break;
+       case DMSG_IOQ_ERROR_FIELD:
+               errstr = "err=IOQ:BADFIELD";
+               break;
+       case DMSG_IOQ_ERROR_HCRC:
+               errstr = "err=IOQ:BADHCRC";
+               break;
+       case DMSG_IOQ_ERROR_XCRC:
+               errstr = "err=IOQ:BADXCRC";
+               break;
+       case DMSG_IOQ_ERROR_ACRC:
+               errstr = "err=IOQ:BADACRC";
+               break;
+       case DMSG_IOQ_ERROR_STATE:
+               errstr = "err=IOQ:BADSTATE";
+               break;
+       case DMSG_IOQ_ERROR_NOPEER:
+               errstr = "err=IOQ:PEERCONFIG";
+               break;
+       case DMSG_IOQ_ERROR_NORKEY:
+               errstr = "err=IOQ:BADRKEY";
+               break;
+       case DMSG_IOQ_ERROR_NOLKEY:
+               errstr = "err=IOQ:BADLKEY";
+               break;
+       case DMSG_IOQ_ERROR_KEYXCHGFAIL:
+               errstr = "err=IOQ:BADKEYXCHG";
+               break;
+       case DMSG_IOQ_ERROR_KEYFMT:
+               errstr = "err=IOQ:BADFMT";
+               break;
+       case DMSG_IOQ_ERROR_BADURANDOM:
+               errstr = "err=IOQ:BADRANDOM";
+               break;
+       case DMSG_IOQ_ERROR_MSGSEQ:
+               errstr = "err=IOQ:BADSEQ";
+               break;
+       case DMSG_IOQ_ERROR_EALREADY:
+               errstr = "err=IOQ:DUPMSG";
+               break;
+       case DMSG_IOQ_ERROR_TRANS:
+               errstr = "err=IOQ:BADTRANS";
+               break;
+       case DMSG_IOQ_ERROR_IVWRAP:
+               errstr = "err=IOQ:IVWRAP";
+               break;
+       case DMSG_IOQ_ERROR_MACFAIL:
+               errstr = "err=IOQ:MACFAIL";
+               break;
+       case DMSG_IOQ_ERROR_ALGO:
+               errstr = "err=IOQ:ALGOFAIL";
+               break;
+       case DMSG_ERR_NOSUPP:
+               errstr = "err=NOSUPPORT";
+               break;
+       default:
+               snprintf(errbuf, sizeof(errbuf),
+                        " err=%d", msg->any.head.error);
+               errstr = errbuf;
+               break;
+       }
+
+       /*
+        * Message flags
+        */
+       i = 0;
+       if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
+                                DMSGF_ABORT | DMSGF_REPLY)) {
+               flagbuf[i++] = '|';
+               if (msg->any.head.cmd & DMSGF_CREATE)
+                       flagbuf[i++] = 'C';
+               if (msg->any.head.cmd & DMSGF_DELETE)
+                       flagbuf[i++] = 'D';
+               if (msg->any.head.cmd & DMSGF_REPLY)
+                       flagbuf[i++] = 'R';
+               if (msg->any.head.cmd & DMSGF_ABORT)
+                       flagbuf[i++] = 'A';
+       }
+       flagbuf[i] = 0;
+
+       /*
+        * Generate the buf
+        */
+       snprintf(buf, sizeof(buf),
+               "msg=%s%s %s id=%08x src=%08x tgt=%08x %s",
+                dmsg_basecmd_str(msg->any.head.cmd),
+                flagbuf,
+                errstr,
+                (uint32_t)(intmax_t)msg->any.head.msgid,   /* for brevity */
+                (uint32_t)(intmax_t)msg->any.head.source,  /* for brevity */
+                (uint32_t)(intmax_t)msg->any.head.target,  /* for brevity */
+                statestr);
+
+       return(buf);
+}
diff --git a/lib/libdmsg/dmsg.h b/lib/libdmsg/dmsg.h
new file mode 100644 (file)
index 0000000..c7be4f0
--- /dev/null
@@ -0,0 +1,412 @@
+/*
+ * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Matthew Dillon <dillon@dragonflybsd.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/tree.h>
+#include <sys/dmsg.h>
+
+#include <pthread.h>
+
+#if 0
+#include <openssl/rsa.h>       /* public/private key functions */
+#include <openssl/pem.h>       /* public/private key file load */
+#endif
+#include <openssl/err.h>
+#include <openssl/evp.h>       /* aes_256_cbc functions */
+
+#define DMSG_DEFAULT_DIR       "/etc/hammer2"
+#define DMSG_PATH_REMOTE       DMSG_DEFAULT_DIR "/remote"
+
+#define DMSG_LISTEN_PORT       987
+
+/***************************************************************************
+ *                             CRYPTO HANDSHAKE                           *
+ ***************************************************************************
+ *
+ * The initial public-key exchange is implementing by transmitting a
+ * 512-byte buffer to the other side in a symmetrical fashion.  This
+ * buffer contains the following:
+ *
+ * (1) A random session key.  512 bits is specified.  We use aes_256_cbc()
+ *     and initialize the key with the first 256 bits and the iv[] with
+ *     the second.  Note that the transmitted and received session
+ *     keys are XOR'd together to create the session key used for
+ *     communications (so even if the verifier is compromised the session
+ *     will still be gobbly gook if the public key has not been completely
+ *     broken).
+ *
+ * (2) A verifier to determine that the decode was successful.  It encodes
+ *     an XOR of each group of 4 bytes from the session key.
+ *
+ * (3) Additional configuration and additional random data.
+ *
+ *     - The hammer2 message header magic for endian detect
+ *
+ *     - The hammer2 protocol version.  The two sides agree on the
+ *      smaller of the two.
+ *
+ *     - All unused fields (junk*) are filled with random data.
+ *
+ * This structure must be exactly 512 bytes and expects to use 256-byte
+ * RSA keys.
+ */
+struct dmsg_handshake {
+       char pad1[8];           /* 000 */
+       uint16_t magic;         /* 008 DMSG_HDR_MAGIC for endian detect */
+       uint16_t version;       /* 00A hammer2 protocol version */
+       uint32_t flags;         /* 00C protocol extension flags */
+       uint8_t sess[64];       /* 010 512-bit session key */
+       uint8_t verf[16];       /* 050 verifier = ~sess */
+       char quickmsg[32];      /* 060 reason for connecting */
+       char junk080[128];      /* 080-0FF */
+       char pad2[8];           /* 100-107 */
+       char junk100[256-8];    /* 108-1FF */
+};
+
+typedef struct dmsg_handshake dmsg_handshake_t;
+
+
+#define DMSG_CRYPTO_CHUNK_SIZE         DMSG_ALIGN
+#define DMSG_MAX_IV_SIZE               32
+
+#define DMSG_CRYPTO_GCM_IV_FIXED_SIZE  4
+#define DMSG_CRYPTO_GCM_IV_SIZE                12
+#define DMSG_CRYPTO_GCM_KEY_SIZE       32
+#define DMSG_CRYPTO_GCM_TAG_SIZE       16
+
+#define DMSG_CRYPTO_ALGO_GCM_IDX       0
+
+#define DMSG_CRYPTO_ALGO               DMSG_CRYPTO_ALGO_GCM_IDX
+
+/*
+ * master service thread info
+ */
+struct dmsg_master_service_info {
+       int     fd;
+       int     detachme;
+};
+
+typedef struct dmsg_master_service_info dmsg_master_service_info_t;
+
+/***************************************************************************
+ *                             LOW LEVEL MESSAGING                        *
+ ***************************************************************************
+ *
+ * dmsg_msg - A standalone copy of a message, typically referenced by
+ *              or embedded in other structures, or used with I/O queues.
+ *
+ * These structures are strictly temporary, so they do not have to be
+ * particularly optimized for size.  All possible message headers are
+ * directly embedded (any), and the message may contain a reference
+ * to allocated auxillary data.  The structure is recycled quite often
+ * by a connection.
+ *
+ * This structure is typically not used for storing persistent message
+ * state (see dmsg_persist for that).
+ */
+struct dmsg_iocom;
+struct dmsg_persist;
+struct dmsg_state;
+struct dmsg_router;
+struct dmsg_msg;
+
+TAILQ_HEAD(dmsg_state_queue, dmsg_state);
+TAILQ_HEAD(dmsg_msg_queue, dmsg_msg);
+RB_HEAD(dmsg_state_tree, dmsg_state);
+RB_HEAD(dmsg_router_tree, dmsg_router);
+
+struct h2span_link;
+struct h2span_relay;
+struct h2span_conn;
+
+struct dmsg_state {
+       RB_ENTRY(dmsg_state) rbnode;            /* indexed by msgid */
+       struct dmsg_iocom *iocom;
+       struct dmsg_router *router;             /* if routed */
+       uint32_t        txcmd;                  /* mostly for CMDF flags */
+       uint32_t        rxcmd;                  /* mostly for CMDF flags */
+       uint64_t        msgid;                  /* {spanid,msgid} uniq */
+       int             flags;
+       int             error;
+       struct dmsg_msg *msg;
+       void (*func)(struct dmsg_msg *);
+       union {
+               void *any;
+               struct h2span_link *link;
+               struct h2span_conn *conn;
+               struct h2span_relay *relay;
+       } any;
+};
+
+#define DMSG_STATE_INSERTED    0x0001
+#define DMSG_STATE_DYNAMIC     0x0002
+#define DMSG_STATE_NODEID      0x0004          /* manages a node id */
+
+struct dmsg_msg {
+       TAILQ_ENTRY(dmsg_msg) qentry;
+       struct dmsg_router *router;
+       struct dmsg_state *state;
+       size_t          hdr_size;
+       size_t          aux_size;
+       char            *aux_data;
+       dmsg_any_t      any;
+};
+
+typedef struct dmsg_state dmsg_state_t;
+typedef struct dmsg_msg dmsg_msg_t;
+typedef struct dmsg_msg_queue dmsg_msg_queue_t;
+
+int dmsg_state_cmp(dmsg_state_t *state1, dmsg_state_t *state2);
+RB_PROTOTYPE(dmsg_state_tree, dmsg_state, rbnode, dmsg_state_cmp);
+
+/*
+ * dmsg_ioq - An embedded component of dmsg_conn, holds state
+ * for the buffering and parsing of incoming and outgoing messages.
+ *
+ * cdx - beg  - processed buffer data, encrypted or decrypted
+ * end - cdn  - unprocessed buffer data not yet encrypted or decrypted
+ */
+struct dmsg_ioq {
+       enum { DMSG_MSGQ_STATE_HEADER1,
+              DMSG_MSGQ_STATE_HEADER2,
+              DMSG_MSGQ_STATE_AUXDATA1,
+              DMSG_MSGQ_STATE_AUXDATA2,
+              DMSG_MSGQ_STATE_ERROR } state;
+       size_t          fifo_beg;               /* buffered data */
+       size_t          fifo_cdx;               /* cdx-beg processed */
+       size_t          fifo_cdn;               /* end-cdn unprocessed */
+       size_t          fifo_end;
+       size_t          hbytes;                 /* header size */
+       size_t          abytes;                 /* aux_data size */
+       int             error;
+       int             seq;                    /* salt sequencer */
+       int             msgcount;
+       EVP_CIPHER_CTX  ctx;
+       char            iv[DMSG_MAX_IV_SIZE]; /* encrypt or decrypt iv[] */
+       dmsg_msg_t      *msg;
+       dmsg_msg_queue_t msgq;
+       char            buf[DMSG_BUF_SIZE];     /* staging buffer */
+};
+
+typedef struct dmsg_ioq dmsg_ioq_t;
+
+#define DMSG_IOQ_ERROR_SYNC            1       /* bad magic / out of sync */
+#define DMSG_IOQ_ERROR_EOF             2       /* unexpected EOF */
+#define DMSG_IOQ_ERROR_SOCK            3       /* read() error on socket */
+#define DMSG_IOQ_ERROR_FIELD           4       /* invalid field */
+#define DMSG_IOQ_ERROR_HCRC            5       /* core header crc bad */
+#define DMSG_IOQ_ERROR_XCRC            6       /* ext header crc bad */
+#define DMSG_IOQ_ERROR_ACRC            7       /* aux data crc bad */
+#define DMSG_IOQ_ERROR_STATE           8       /* bad state */
+#define DMSG_IOQ_ERROR_NOPEER          9       /* bad socket peer */
+#define DMSG_IOQ_ERROR_NORKEY          10      /* no remote keyfile found */
+#define DMSG_IOQ_ERROR_NOLKEY          11      /* no local keyfile found */
+#define DMSG_IOQ_ERROR_KEYXCHGFAIL     12      /* key exchange failed */
+#define DMSG_IOQ_ERROR_KEYFMT          13      /* key file format problem */
+#define DMSG_IOQ_ERROR_BADURANDOM      14      /* /dev/urandom is bad */
+#define DMSG_IOQ_ERROR_MSGSEQ          15      /* message sequence error */
+#define DMSG_IOQ_ERROR_EALREADY                16      /* ignore this message */
+#define DMSG_IOQ_ERROR_TRANS           17      /* state transaction issue */
+#define DMSG_IOQ_ERROR_IVWRAP          18      /* IVs exhaused */
+#define DMSG_IOQ_ERROR_MACFAIL         19      /* MAC of encr alg failed */
+#define DMSG_IOQ_ERROR_ALGO            20      /* Misc. encr alg error */
+
+#define DMSG_IOQ_MAXIOVEC    16
+
+/*
+ * dmsg_router - governs the routing of a message.  Passed into
+ *                 dmsg_msg_write.
+ *
+ * The router is either connected to an iocom (socket) directly, or
+ * connected to a SPAN transaction (h2span_link structure for outgoing)
+ * or to a SPAN transaction (h2span_relay structure for incoming).
+ */
+struct dmsg_router {
+       RB_ENTRY(dmsg_router) rbnode;   /* indexed by target */
+       struct dmsg_iocom *iocom;
+       struct h2span_link   *link;             /* may be NULL */
+       struct h2span_relay  *relay;            /* may be NULL */
+       void    (*signal_callback)(struct dmsg_router *);
+       void    (*rcvmsg_callback)(struct dmsg_msg *);
+       void    (*altmsg_callback)(struct dmsg_iocom *);
+       void    (*dbgmsg_callback)(dmsg_msg_t *msg);
+       struct dmsg_state_tree staterd_tree; /* active messages */
+       struct dmsg_state_tree statewr_tree; /* active messages */
+       dmsg_msg_queue_t txmsgq;                /* tx msgq from remote */
+       uint64_t        target;                 /* for routing */
+       int             flags;
+       int             refs;                   /* refs prevent destruction */
+};
+
+#define DMSG_ROUTER_CONNECTED          0x0001  /* on global RB tree */
+#define DMSG_ROUTER_DELETED            0x0002  /* parent structure destroyed */
+
+typedef struct dmsg_router dmsg_router_t;
+
+int dmsg_router_cmp(dmsg_router_t *router1, dmsg_router_t *router2);
+RB_PROTOTYPE(dmsg_router_tree, dmsg_router, rbnode, dmsg_router_cmp);
+
+/*
+ * dmsg_iocom - governs a messaging stream connection
+ */
+struct dmsg_iocom {
+       dmsg_ioq_t      ioq_rx;
+       dmsg_ioq_t      ioq_tx;
+       dmsg_msg_queue_t freeq;         /* free msgs hdr only */
+       dmsg_msg_queue_t freeq_aux;             /* free msgs w/aux_data */
+       int     sock_fd;                        /* comm socket or pipe */
+       int     alt_fd;                         /* thread signal, tty, etc */
+       int     wakeupfds[2];                   /* pipe wakes up iocom thread */
+       int     flags;
+       int     rxmisc;
+       int     txmisc;
+       struct dmsg_router *router;
+       pthread_mutex_t mtx;                    /* mutex for state*tree/rmsgq */
+};
+
+typedef struct dmsg_iocom dmsg_iocom_t;
+
+#define DMSG_IOCOMF_EOF                0x00000001      /* EOF or ERROR on desc */
+#define DMSG_IOCOMF_RREQ       0x00000002      /* request read-data event */
+#define DMSG_IOCOMF_WREQ       0x00000004      /* request write-avail event */
+#define DMSG_IOCOMF_RWORK      0x00000008      /* immediate work pending */
+#define DMSG_IOCOMF_WWORK      0x00000010      /* immediate work pending */
+#define DMSG_IOCOMF_PWORK      0x00000020      /* immediate work pending */
+#define DMSG_IOCOMF_ARWORK     0x00000040      /* immediate work pending */
+#define DMSG_IOCOMF_AWWORK     0x00000080      /* immediate work pending */
+#define DMSG_IOCOMF_SWORK      0x00000100      /* immediate work pending */
+#define DMSG_IOCOMF_CRYPTED    0x00000200      /* encrypt enabled */
+
+/*
+ * Crypto algorithm table and related typedefs.
+ */
+
+typedef int (*algo_init_fn)(dmsg_ioq_t *, char *, int, char *, int, int);
+typedef int (*algo_enc_fn)(dmsg_ioq_t *, char *, char *, int, int *);
+typedef int (*algo_dec_fn)(dmsg_ioq_t *, char *, char *, int, int *);
+
+struct crypto_algo {
+       const char      *name;
+       int             keylen;
+       int             taglen;
+       algo_init_fn    init;
+       algo_enc_fn     enc_chunk;
+       algo_dec_fn     dec_chunk;
+};
+
+/*
+ * icrc
+ */
+uint32_t dmsg_icrc32(const void *buf, size_t size);
+uint32_t dmsg_icrc32c(const void *buf, size_t size, uint32_t crc);
+
+/*
+ * debug
+ */
+const char *dmsg_basecmd_str(uint32_t cmd);
+const char *dmsg_msg_str(dmsg_msg_t *msg);
+
+/*
+ * subs
+ */
+void *dmsg_alloc(size_t bytes);
+void dmsg_free(void *ptr);
+const char *dmsg_uuid_to_str(uuid_t *uuid, char **strp);
+int dmsg_connect(const char *hostname);
+
+/*
+ * Msg support functions
+ */
+void dmsg_bswap_head(dmsg_hdr_t *head);
+void dmsg_ioq_init(dmsg_iocom_t *iocom, dmsg_ioq_t *ioq);
+void dmsg_ioq_done(dmsg_iocom_t *iocom, dmsg_ioq_t *ioq);
+void dmsg_iocom_init(dmsg_iocom_t *iocom, int sock_fd, int alt_fd,
+                       void (*state_func)(dmsg_router_t *),
+                       void (*rcvmsg_func)(dmsg_msg_t *),
+                       void (*altmsg_func)(dmsg_iocom_t *));
+void dmsg_router_restate(dmsg_router_t *router,
+                       void (*state_func)(dmsg_router_t *),
+                       void (*rcvmsg_func)(dmsg_msg_t *),
+                       void (*altmsg_func)(dmsg_iocom_t *));
+void dmsg_router_signal(dmsg_router_t *router);
+void dmsg_iocom_done(dmsg_iocom_t *iocom);
+dmsg_msg_t *dmsg_msg_alloc(dmsg_router_t *router,
+                       size_t aux_size, uint32_t cmd,
+                       void (*func)(dmsg_msg_t *), void *data);
+void dmsg_msg_reply(dmsg_msg_t *msg, uint32_t error);
+void dmsg_msg_result(dmsg_msg_t *msg, uint32_t error);
+void dmsg_state_reply(dmsg_state_t *state, uint32_t error);
+
+void dmsg_msg_free(dmsg_msg_t *msg);
+
+void dmsg_iocom_core(dmsg_iocom_t *iocom);
+dmsg_msg_t *dmsg_ioq_read(dmsg_iocom_t *iocom);
+void dmsg_msg_write(dmsg_msg_t *msg);
+
+void dmsg_iocom_drain(dmsg_iocom_t *iocom);
+void dmsg_iocom_flush1(dmsg_iocom_t *iocom);
+void dmsg_iocom_flush2(dmsg_iocom_t *iocom);
+
+void dmsg_state_cleanuprx(dmsg_iocom_t *iocom, dmsg_msg_t *msg);
+void dmsg_state_free(dmsg_state_t *state);
+
+dmsg_router_t *dmsg_router_alloc(void);
+void dmsg_router_connect(dmsg_router_t *router);
+void dmsg_router_disconnect(dmsg_router_t **routerp);
+
+/*
+ * Msg protocol functions
+ */
+void dmsg_msg_lnk_signal(dmsg_router_t *router);
+void dmsg_msg_lnk(dmsg_msg_t *msg);
+void dmsg_msg_dbg(dmsg_msg_t *msg);
+void dmsg_shell_tree(dmsg_router_t *router, char *cmdbuf __unused);
+
+/*
+ * Crypto functions
+ */
+void dmsg_crypto_setup(void);
+void dmsg_crypto_negotiate(dmsg_iocom_t *iocom);
+void dmsg_crypto_decrypt(dmsg_iocom_t *iocom, dmsg_ioq_t *ioq);
+int dmsg_crypto_encrypt(dmsg_iocom_t *iocom, dmsg_ioq_t *ioq,
+                       struct iovec *iov, int n, size_t *nactp);
+
+/*
+ * Service daemon functions
+ */
+void *dmsg_master_service(void *data);
+void dmsg_router_printf(dmsg_router_t *router, const char *ctl, ...);
+
+extern int DMsgDebugOpt;
similarity index 70%
copy from sys/vfs/hammer2/hammer2_mount.h
copy to lib/libdmsg/dmsg_local.h
index d10ae8b..a987532 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
  *
  * This code is derived from software contributed to The DragonFly Project
- * by Matthew Dillon <dillon@backplane.com>
+ * by Matthew Dillon <dillon@dragonflybsd.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SUCH DAMAGE.
  */
 
-#ifndef _VFS_HAMMER2_MOUNT_H_
-#define _VFS_HAMMER2_MOUNT_H_
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/tree.h>
+#include <sys/endian.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/dmsg.h>
+#include <sys/poll.h>
+#include <sys/uio.h>
 
-/*
- * This structure is passed from userland to the kernel during the mount
- * system call.
- *
- * The volume name is formatted as '/dev/ad0s1a@LABEL', where the label is
- * the mount point under the super-root.
- */
-struct hammer2_mount_info {
-       const char      *volume;
-       int             hflags;         /* extended hammer mount flags */
-       int             cluster_fd;     /* cluster management pipe/socket */
-       char            reserved1[112];
-};
+#include <netinet/in.h>
+#include <arpa/inet.h>
 
-#define HMNT2_NOAUTOSNAP       0x00000001
+#include <assert.h>
+#include <pthread.h>
+#include <libutil.h>
 
-#define HMNT2_USERFLAGS                (HMNT2_NOAUTOSNAP)
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <uuid.h>
 
-#define HAMMER2_LISTEN_PORT    987
+#include <openssl/rsa.h>       /* public/private key functions */
+#include <openssl/pem.h>       /* public/private key file load */
+#include <openssl/err.h>
+#include <openssl/evp.h>       /* aes_256_cbc functions */
 
-#endif
+#include "dmsg.h"
similarity index 95%
copy from sys/vfs/hammer2/hammer2_icrc.c
copy to lib/libdmsg/icrc.c
index d18a866..1f3e357 100644 (file)
  | $Id: isc_subr.c 560 2009-05-07 07:37:49Z danny $
  */
 
-#include <sys/types.h>
-#include <sys/uuid.h>
-
-#include "hammer2_disk.h"
+#include "dmsg_local.h"
 
 /*****************************************************************/
 /*                                                               */
@@ -54,7 +51,7 @@
 /*                                                               */
 /*****************************************************************/
 
-static uint32_t crc32Table[256] = {
+static const uint32_t dmsg_crc32Table[256] = {
     0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L,
     0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL,
     0x8AD958CFL, 0x78B2DBCCL, 0x6BE22838L, 0x9989AB3BL,
@@ -122,26 +119,26 @@ static uint32_t crc32Table[256] = {
 };
 
 uint32_t
-hammer2_icrc32(const void *buf, size_t size)
+dmsg_icrc32(const void *buf, size_t size)
 {
      const uint8_t *p = buf;
      uint32_t crc = 0;
 
      crc = crc ^ 0xffffffff;
      while (size--)
-         crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
+         crc = dmsg_crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
      crc = crc ^ 0xffffffff;
      return crc;
 }
 
 uint32_t
-hammer2_icrc32c(const void *buf, size_t size, uint32_t crc)
+dmsg_icrc32c(const void *buf, size_t size, uint32_t crc)
 {
      const uint8_t *p = buf;
 
      crc = crc ^ 0xffffffff;
      while (size--)
-         crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
+         crc = dmsg_crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
      crc = crc ^ 0xffffffff;
      return crc;
 }
similarity index 73%
rename from sbin/hammer2/msg.c
rename to lib/libdmsg/msg.c
index c5fbb9e..60e9089 100644 (file)
  * SUCH DAMAGE.
  */
 
-#include "hammer2.h"
+#include "dmsg_local.h"
 
-static int hammer2_state_msgrx(hammer2_msg_t *msg);
-static void hammer2_state_cleanuptx(hammer2_msg_t *msg);
+int DMsgDebugOpt;
+
+static int dmsg_state_msgrx(dmsg_msg_t *msg);
+static void dmsg_state_cleanuptx(dmsg_msg_t *msg);
 
 /*
  * ROUTER TREE - Represents available routes for message routing, indexed
@@ -44,7 +46,7 @@ static void hammer2_state_cleanuptx(hammer2_msg_t *msg);
  *              either an iocom, h2span_link, or h2span_relay (see msg_lnk.c).
  */
 int
-hammer2_router_cmp(hammer2_router_t *router1, hammer2_router_t *router2)
+dmsg_router_cmp(dmsg_router_t *router1, dmsg_router_t *router2)
 {
        if (router1->target < router2->target)
                return(-1);
@@ -53,11 +55,11 @@ hammer2_router_cmp(hammer2_router_t *router1, hammer2_router_t *router2)
        return(0);
 }
 
-RB_GENERATE(hammer2_router_tree, hammer2_router, rbnode, hammer2_router_cmp);
+RB_GENERATE(dmsg_router_tree, dmsg_router, rbnode, dmsg_router_cmp);
 
 static pthread_mutex_t router_mtx;
-static struct hammer2_router_tree router_ltree = RB_INITIALIZER(router_ltree);
-static struct hammer2_router_tree router_rtree = RB_INITIALIZER(router_rtree);
+static struct dmsg_router_tree router_ltree = RB_INITIALIZER(router_ltree);
+static struct dmsg_router_tree router_rtree = RB_INITIALIZER(router_rtree);
 
 /*
  * STATE TREE - Represents open transactions which are indexed by their
@@ -67,7 +69,7 @@ static struct hammer2_router_tree router_rtree = RB_INITIALIZER(router_rtree);
  *             for relayed messages.
  */
 int
-hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
+dmsg_state_cmp(dmsg_state_t *state1, dmsg_state_t *state2)
 {
 #if 0
        if (state1->router < state2->router)
@@ -82,16 +84,16 @@ hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
        return(0);
 }
 
-RB_GENERATE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
+RB_GENERATE(dmsg_state_tree, dmsg_state, rbnode, dmsg_state_cmp);
 
 /*
  * Initialize a low-level ioq
  */
 void
-hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
+dmsg_ioq_init(dmsg_iocom_t *iocom __unused, dmsg_ioq_t *ioq)
 {
        bzero(ioq, sizeof(*ioq));
-       ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
+       ioq->state = DMSG_MSGQ_STATE_HEADER1;
        TAILQ_INIT(&ioq->msgq);
 }
 
@@ -101,18 +103,18 @@ hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
  * caller holds iocom->mtx.
  */
 void
-hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
+dmsg_ioq_done(dmsg_iocom_t *iocom __unused, dmsg_ioq_t *ioq)
 {
-       hammer2_msg_t *msg;
+       dmsg_msg_t *msg;
 
        while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
                assert(0);      /* shouldn't happen */
                TAILQ_REMOVE(&ioq->msgq, msg, qentry);
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        }
        if ((msg = ioq->msg) != NULL) {
                ioq->msg = NULL;
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        }
 }
 
@@ -120,23 +122,23 @@ hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
  * Initialize a low-level communications channel.
  *
  * NOTE: The signal_func() is called at least once from the loop and can be
- *      re-armed via hammer2_iocom_restate().
+ *      re-armed via dmsg_iocom_restate().
  */
 void
-hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
-                  void (*signal_func)(hammer2_router_t *),
-                  void (*rcvmsg_func)(hammer2_msg_t *),
-                  void (*altmsg_func)(hammer2_iocom_t *))
+dmsg_iocom_init(dmsg_iocom_t *iocom, int sock_fd, int alt_fd,
+                  void (*signal_func)(dmsg_router_t *),
+                  void (*rcvmsg_func)(dmsg_msg_t *),
+                  void (*altmsg_func)(dmsg_iocom_t *))
 {
        struct stat st;
 
        bzero(iocom, sizeof(*iocom));
 
-       iocom->router = hammer2_router_alloc();
+       iocom->router = dmsg_router_alloc();
        iocom->router->signal_callback = signal_func;
        iocom->router->rcvmsg_callback = rcvmsg_func;
        iocom->router->altmsg_callback = altmsg_func;
-       /* we do not call hammer2_router_connect() for iocom routers */
+       /* we do not call dmsg_router_connect() for iocom routers */
 
        pthread_mutex_init(&iocom->mtx, NULL);
        RB_INIT(&iocom->router->staterd_tree);
@@ -147,11 +149,11 @@ hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
        iocom->router->iocom = iocom;
        iocom->sock_fd = sock_fd;
        iocom->alt_fd = alt_fd;
-       iocom->flags = HAMMER2_IOCOMF_RREQ;
+       iocom->flags = DMSG_IOCOMF_RREQ;
        if (signal_func)
-               iocom->flags |= HAMMER2_IOCOMF_SWORK;
-       hammer2_ioq_init(iocom, &iocom->ioq_rx);
-       hammer2_ioq_init(iocom, &iocom->ioq_tx);
+               iocom->flags |= DMSG_IOCOMF_SWORK;
+       dmsg_ioq_init(iocom, &iocom->ioq_rx);
+       dmsg_ioq_init(iocom, &iocom->ioq_tx);
        if (pipe(iocom->wakeupfds) < 0)
                assert(0);
        fcntl(iocom->wakeupfds[0], F_SETFL, O_NONBLOCK);
@@ -166,7 +168,7 @@ hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
        if (fstat(sock_fd, &st) < 0)
                assert(0);
        if (S_ISSOCK(st.st_mode))
-               hammer2_crypto_negotiate(iocom);
+               dmsg_crypto_negotiate(iocom);
 
        /*
         * Make sure our fds are set to non-blocking for the iocom core.
@@ -187,25 +189,25 @@ hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
  * the recevmsg_func and the sendmsg_func is called at least once.
  */
 void
-hammer2_router_restate(hammer2_router_t *router,
-                  void (*signal_func)(hammer2_router_t *),
-                  void (*rcvmsg_func)(hammer2_msg_t *msg),
-                  void (*altmsg_func)(hammer2_iocom_t *))
+dmsg_router_restate(dmsg_router_t *router,
+                  void (*signal_func)(dmsg_router_t *),
+                  void (*rcvmsg_func)(dmsg_msg_t *msg),
+                  void (*altmsg_func)(dmsg_iocom_t *))
 {
        router->signal_callback = signal_func;
        router->rcvmsg_callback = rcvmsg_func;
        router->altmsg_callback = altmsg_func;
        if (signal_func)
-               router->iocom->flags |= HAMMER2_IOCOMF_SWORK;
+               router->iocom->flags |= DMSG_IOCOMF_SWORK;
        else
-               router->iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
+               router->iocom->flags &= ~DMSG_IOCOMF_SWORK;
 }
 
 void
-hammer2_router_signal(hammer2_router_t *router)
+dmsg_router_signal(dmsg_router_t *router)
 {
        if (router->signal_callback)
-               router->iocom->flags |= HAMMER2_IOCOMF_SWORK;
+               router->iocom->flags |= DMSG_IOCOMF_SWORK;
 }
 
 /*
@@ -215,9 +217,9 @@ hammer2_router_signal(hammer2_router_t *router)
  * from all possible references to it.
  */
 void
-hammer2_iocom_done(hammer2_iocom_t *iocom)
+dmsg_iocom_done(dmsg_iocom_t *iocom)
 {
-       hammer2_msg_t *msg;
+       dmsg_msg_t *msg;
 
        if (iocom->sock_fd >= 0) {
                close(iocom->sock_fd);
@@ -227,8 +229,8 @@ hammer2_iocom_done(hammer2_iocom_t *iocom)
                close(iocom->alt_fd);
                iocom->alt_fd = -1;
        }
-       hammer2_ioq_done(iocom, &iocom->ioq_rx);
-       hammer2_ioq_done(iocom, &iocom->ioq_tx);
+       dmsg_ioq_done(iocom, &iocom->ioq_rx);
+       dmsg_ioq_done(iocom, &iocom->ioq_tx);
        if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
                TAILQ_REMOVE(&iocom->freeq, msg, qentry);
                free(msg);
@@ -253,13 +255,13 @@ hammer2_iocom_done(hammer2_iocom_t *iocom)
 /*
  * Allocate a new one-way message.
  */
-hammer2_msg_t *
-hammer2_msg_alloc(hammer2_router_t *router, size_t aux_size, uint32_t cmd,
-                 void (*func)(hammer2_msg_t *), void *data)
+dmsg_msg_t *
+dmsg_msg_alloc(dmsg_router_t *router, size_t aux_size, uint32_t cmd,
+                 void (*func)(dmsg_msg_t *), void *data)
 {
-       hammer2_state_t *state = NULL;
-       hammer2_iocom_t *iocom = router->iocom;
-       hammer2_msg_t *msg;
+       dmsg_state_t *state = NULL;
+       dmsg_iocom_t *iocom = router->iocom;
+       dmsg_msg_t *msg;
        int hbytes;
 
        pthread_mutex_lock(&iocom->mtx);
@@ -276,13 +278,13 @@ hammer2_msg_alloc(hammer2_router_t *router, size_t aux_size, uint32_t cmd,
                /*
                 * Create state when CREATE is set without REPLY.
                 *
-                * NOTE: CREATE in txcmd handled by hammer2_msg_write()
-                * NOTE: DELETE in txcmd handled by hammer2_state_cleanuptx()
+                * NOTE: CREATE in txcmd handled by dmsg_msg_write()
+                * NOTE: DELETE in txcmd handled by dmsg_state_cleanuptx()
                 */
                state = malloc(sizeof(*state));
                bzero(state, sizeof(*state));
                state->iocom = iocom;
-               state->flags = HAMMER2_STATE_DYNAMIC;
+               state->flags = DMSG_STATE_DYNAMIC;
                state->msgid = (uint64_t)(uintptr_t)state;
                state->router = router;
                state->txcmd = cmd & ~(DMSGF_CREATE | DMSGF_DELETE);
@@ -290,11 +292,11 @@ hammer2_msg_alloc(hammer2_router_t *router, size_t aux_size, uint32_t cmd,
                state->func = func;
                state->any.any = data;
                pthread_mutex_lock(&iocom->mtx);
-               RB_INSERT(hammer2_state_tree,
+               RB_INSERT(dmsg_state_tree,
                          &iocom->router->statewr_tree,
                          state);
                pthread_mutex_unlock(&iocom->mtx);
-               state->flags |= HAMMER2_STATE_INSERTED;
+               state->flags |= DMSG_STATE_INSERTED;
        }
        pthread_mutex_unlock(&iocom->mtx);
        if (msg == NULL) {
@@ -337,9 +339,9 @@ hammer2_msg_alloc(hammer2_router_t *router, size_t aux_size, uint32_t cmd,
  */
 static
 void
-hammer2_msg_free_locked(hammer2_msg_t *msg)
+dmsg_msg_free_locked(dmsg_msg_t *msg)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
+       dmsg_iocom_t *iocom = msg->router->iocom;
 
        msg->state = NULL;
        if (msg->aux_data)
@@ -349,12 +351,12 @@ hammer2_msg_free_locked(hammer2_msg_t *msg)
 }
 
 void
-hammer2_msg_free(hammer2_msg_t *msg)
+dmsg_msg_free(dmsg_msg_t *msg)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
+       dmsg_iocom_t *iocom = msg->router->iocom;
 
        pthread_mutex_lock(&iocom->mtx);
-       hammer2_msg_free_locked(msg);
+       dmsg_msg_free_locked(msg);
        pthread_mutex_unlock(&iocom->mtx);
 }
 
@@ -364,24 +366,24 @@ hammer2_msg_free(hammer2_msg_t *msg)
  * Thread localized, iocom->mtx not held.
  */
 void
-hammer2_iocom_core(hammer2_iocom_t *iocom)
+dmsg_iocom_core(dmsg_iocom_t *iocom)
 {
        struct pollfd fds[3];
        char dummybuf[256];
-       hammer2_msg_t *msg;
+       dmsg_msg_t *msg;
        int timeout;
        int count;
        int wi; /* wakeup pipe */
        int si; /* socket */
        int ai; /* alt bulk path socket */
 
-       while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
-               if ((iocom->flags & (HAMMER2_IOCOMF_RWORK |
-                                    HAMMER2_IOCOMF_WWORK |
-                                    HAMMER2_IOCOMF_PWORK |
-                                    HAMMER2_IOCOMF_SWORK |
-                                    HAMMER2_IOCOMF_ARWORK |
-                                    HAMMER2_IOCOMF_AWWORK)) == 0) {
+       while ((iocom->flags & DMSG_IOCOMF_EOF) == 0) {
+               if ((iocom->flags & (DMSG_IOCOMF_RWORK |
+                                    DMSG_IOCOMF_WWORK |
+                                    DMSG_IOCOMF_PWORK |
+                                    DMSG_IOCOMF_SWORK |
+                                    DMSG_IOCOMF_ARWORK |
+                                    DMSG_IOCOMF_AWWORK)) == 0) {
                        /*
                         * Only poll if no immediate work is pending.
                         * Otherwise we are just wasting our time calling
@@ -407,16 +409,16 @@ hammer2_iocom_core(hammer2_iocom_t *iocom)
                         * Check the socket input/output direction as
                         * requested
                         */
-                       if (iocom->flags & (HAMMER2_IOCOMF_RREQ |
-                                           HAMMER2_IOCOMF_WREQ)) {
+                       if (iocom->flags & (DMSG_IOCOMF_RREQ |
+                                           DMSG_IOCOMF_WREQ)) {
                                si = count++;
                                fds[si].fd = iocom->sock_fd;
                                fds[si].events = 0;
                                fds[si].revents = 0;
 
-                               if (iocom->flags & HAMMER2_IOCOMF_RREQ)
+                               if (iocom->flags & DMSG_IOCOMF_RREQ)
                                        fds[si].events |= POLLIN;
-                               if (iocom->flags & HAMMER2_IOCOMF_WREQ)
+                               if (iocom->flags & DMSG_IOCOMF_WREQ)
                                        fds[si].events |= POLLOUT;
                        }
 
@@ -432,24 +434,24 @@ hammer2_iocom_core(hammer2_iocom_t *iocom)
                        poll(fds, count, timeout);
 
                        if (wi >= 0 && (fds[wi].revents & POLLIN))
-                               iocom->flags |= HAMMER2_IOCOMF_PWORK;
+                               iocom->flags |= DMSG_IOCOMF_PWORK;
                        if (si >= 0 && (fds[si].revents & POLLIN))
-                               iocom->flags |= HAMMER2_IOCOMF_RWORK;
+                               iocom->flags |= DMSG_IOCOMF_RWORK;
                        if (si >= 0 && (fds[si].revents & POLLOUT))
-                               iocom->flags |= HAMMER2_IOCOMF_WWORK;
+                               iocom->flags |= DMSG_IOCOMF_WWORK;
                        if (wi >= 0 && (fds[wi].revents & POLLOUT))
-                               iocom->flags |= HAMMER2_IOCOMF_WWORK;
+                               iocom->flags |= DMSG_IOCOMF_WWORK;
                        if (ai >= 0 && (fds[ai].revents & POLLIN))
-                               iocom->flags |= HAMMER2_IOCOMF_ARWORK;
+                               iocom->flags |= DMSG_IOCOMF_ARWORK;
                } else {
                        /*
                         * Always check the pipe
                         */
-                       iocom->flags |= HAMMER2_IOCOMF_PWORK;
+                       iocom->flags |= DMSG_IOCOMF_PWORK;
                }
 
-               if (iocom->flags & HAMMER2_IOCOMF_SWORK) {
-                       iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
+               if (iocom->flags & DMSG_IOCOMF_SWORK) {
+                       iocom->flags &= ~DMSG_IOCOMF_SWORK;
                        iocom->router->signal_callback(iocom->router);
                }
 
@@ -458,40 +460,40 @@ hammer2_iocom_core(hammer2_iocom_t *iocom)
                 * with a write to the wakeupfds[] pipe.  We have to clear
                 * the pipe with a dummy read.
                 */
-               if (iocom->flags & HAMMER2_IOCOMF_PWORK) {
-                       iocom->flags &= ~HAMMER2_IOCOMF_PWORK;
+               if (iocom->flags & DMSG_IOCOMF_PWORK) {
+                       iocom->flags &= ~DMSG_IOCOMF_PWORK;
                        read(iocom->wakeupfds[0], dummybuf, sizeof(dummybuf));
-                       iocom->flags |= HAMMER2_IOCOMF_RWORK;
-                       iocom->flags |= HAMMER2_IOCOMF_WWORK;
+                       iocom->flags |= DMSG_IOCOMF_RWORK;
+                       iocom->flags |= DMSG_IOCOMF_WWORK;
                        if (TAILQ_FIRST(&iocom->router->txmsgq))
-                               hammer2_iocom_flush1(iocom);
+                               dmsg_iocom_flush1(iocom);
                }
 
                /*
                 * Message write sequencing
                 */
-               if (iocom->flags & HAMMER2_IOCOMF_WWORK)
-                       hammer2_iocom_flush1(iocom);
+               if (iocom->flags & DMSG_IOCOMF_WWORK)
+                       dmsg_iocom_flush1(iocom);
 
                /*
                 * Message read sequencing.  Run this after the write
                 * sequencing in case the write sequencing allowed another
                 * auto-DELETE to occur on the read side.
                 */
-               if (iocom->flags & HAMMER2_IOCOMF_RWORK) {
-                       while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
-                              (msg = hammer2_ioq_read(iocom)) != NULL) {
-                               if (DebugOpt) {
+               if (iocom->flags & DMSG_IOCOMF_RWORK) {
+                       while ((iocom->flags & DMSG_IOCOMF_EOF) == 0 &&
+                              (msg = dmsg_ioq_read(iocom)) != NULL) {
+                               if (DMsgDebugOpt) {
                                        fprintf(stderr, "receive %s\n",
-                                               hammer2_msg_str(msg));
+                                               dmsg_msg_str(msg));
                                }
                                iocom->router->rcvmsg_callback(msg);
-                               hammer2_state_cleanuprx(iocom, msg);
+                               dmsg_state_cleanuprx(iocom, msg);
                        }
                }
 
-               if (iocom->flags & HAMMER2_IOCOMF_ARWORK) {
-                       iocom->flags &= ~HAMMER2_IOCOMF_ARWORK;
+               if (iocom->flags & DMSG_IOCOMF_ARWORK) {
+                       iocom->flags &= ~DMSG_IOCOMF_ARWORK;
                        iocom->router->altmsg_callback(iocom);
                }
        }
@@ -506,7 +508,7 @@ hammer2_iocom_core(hammer2_iocom_t *iocom)
  */
 static
 size_t
-hammer2_ioq_makeroom(hammer2_ioq_t *ioq, size_t needed)
+dmsg_ioq_makeroom(dmsg_ioq_t *ioq, size_t needed)
 {
        size_t bytes;
        size_t nmax;
@@ -545,12 +547,12 @@ hammer2_ioq_makeroom(hammer2_ioq_t *ioq, size_t needed)
  *
  * Thread localized, iocom->mtx not held.
  */
-hammer2_msg_t *
-hammer2_ioq_read(hammer2_iocom_t *iocom)
+dmsg_msg_t *
+dmsg_ioq_read(dmsg_iocom_t *iocom)
 {
-       hammer2_ioq_t *ioq = &iocom->ioq_rx;
-       hammer2_msg_t *msg;
-       hammer2_state_t *state;
+       dmsg_ioq_t *ioq = &iocom->ioq_rx;
+       dmsg_msg_t *msg;
+       dmsg_state_t *state;
        dmsg_hdr_t *head;
        ssize_t n;
        size_t bytes;
@@ -559,7 +561,7 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
        int error;
 
 again:
-       iocom->flags &= ~(HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_RWORK);
+       iocom->flags &= ~(DMSG_IOCOMF_RREQ | DMSG_IOCOMF_RWORK);
 
        /*
         * If a message is already pending we can just remove and
@@ -586,27 +588,27 @@ again:
        msg = ioq->msg;
 
        switch(ioq->state) {
-       case HAMMER2_MSGQ_STATE_HEADER1:
+       case DMSG_MSGQ_STATE_HEADER1:
                /*
                 * Load the primary header, fail on any non-trivial read
                 * error or on EOF.  Since the primary header is the same
                 * size is the message alignment it will never straddle
                 * the end of the buffer.
                 */
-               nmax = hammer2_ioq_makeroom(ioq, sizeof(msg->any.head));
+               nmax = dmsg_ioq_makeroom(ioq, sizeof(msg->any.head));
                if (bytes < sizeof(msg->any.head)) {
                        n = read(iocom->sock_fd,
                                 ioq->buf + ioq->fifo_end,
                                 nmax);
                        if (n <= 0) {
                                if (n == 0) {
-                                       ioq->error = HAMMER2_IOQ_ERROR_EOF;
+                                       ioq->error = DMSG_IOQ_ERROR_EOF;
                                        break;
                                }
                                if (errno != EINTR &&
                                    errno != EINPROGRESS &&
                                    errno != EAGAIN) {
-                                       ioq->error = HAMMER2_IOQ_ERROR_SOCK;
+                                       ioq->error = DMSG_IOQ_ERROR_SOCK;
                                        break;
                                }
                                n = 0;
@@ -625,8 +627,8 @@ again:
                 *           do not fix it up until we get the entire
                 *           extended header.
                 */
-               if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
-                       hammer2_crypto_decrypt(iocom, ioq);
+               if (iocom->flags & DMSG_IOCOMF_CRYPTED) {
+                       dmsg_crypto_decrypt(iocom, ioq);
                } else {
                        ioq->fifo_cdx = ioq->fifo_end;
                        ioq->fifo_cdn = ioq->fifo_end;
@@ -650,7 +652,7 @@ again:
                head = (void *)(ioq->buf + ioq->fifo_beg);
                if (head->magic != DMSG_HDR_MAGIC &&
                    head->magic != DMSG_HDR_MAGIC_REV) {
-                       ioq->error = HAMMER2_IOQ_ERROR_SYNC;
+                       ioq->error = DMSG_IOQ_ERROR_SYNC;
                        break;
                }
 
@@ -670,14 +672,14 @@ again:
                if (ioq->hbytes < sizeof(msg->any.head) ||
                    ioq->hbytes > sizeof(msg->any) ||
                    ioq->abytes > DMSG_AUX_MAX) {
-                       ioq->error = HAMMER2_IOQ_ERROR_FIELD;
+                       ioq->error = DMSG_IOQ_ERROR_FIELD;
                        break;
                }
 
                /*
                 * Allocate the message, the next state will fill it in.
                 */
-               msg = hammer2_msg_alloc(iocom->router, ioq->abytes, 0,
+               msg = dmsg_msg_alloc(iocom->router, ioq->abytes, 0,
                                        NULL, NULL);
                ioq->msg = msg;
 
@@ -689,10 +691,10 @@ again:
                 *
                 * Make sure there is enough room for bloated encrypt data.
                 */
-               nmax = hammer2_ioq_makeroom(ioq, ioq->hbytes);
-               ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
+               nmax = dmsg_ioq_makeroom(ioq, ioq->hbytes);
+               ioq->state = DMSG_MSGQ_STATE_HEADER2;
                /* fall through */
-       case HAMMER2_MSGQ_STATE_HEADER2:
+       case DMSG_MSGQ_STATE_HEADER2:
                /*
                 * Fill out the extended header.
                 */
@@ -703,13 +705,13 @@ again:
                                 nmax);
                        if (n <= 0) {
                                if (n == 0) {
-                                       ioq->error = HAMMER2_IOQ_ERROR_EOF;
+                                       ioq->error = DMSG_IOQ_ERROR_EOF;
                                        break;
                                }
                                if (errno != EINTR &&
                                    errno != EINPROGRESS &&
                                    errno != EAGAIN) {
-                                       ioq->error = HAMMER2_IOQ_ERROR_SOCK;
+                                       ioq->error = DMSG_IOQ_ERROR_SOCK;
                                        break;
                                }
                                n = 0;
@@ -719,8 +721,8 @@ again:
                        nmax -= (size_t)n;
                }
 
-               if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
-                       hammer2_crypto_decrypt(iocom, ioq);
+               if (iocom->flags & DMSG_IOCOMF_CRYPTED) {
+                       dmsg_crypto_decrypt(iocom, ioq);
                } else {
                        ioq->fifo_cdx = ioq->fifo_end;
                        ioq->fifo_cdn = ioq->fifo_end;
@@ -751,18 +753,18 @@ again:
                else
                        xcrc32 = head->hdr_crc;
                head->hdr_crc = 0;
-               if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
-                       ioq->error = HAMMER2_IOQ_ERROR_XCRC;
+               if (dmsg_icrc32(head, ioq->hbytes) != xcrc32) {
+                       ioq->error = DMSG_IOQ_ERROR_XCRC;
                        fprintf(stderr, "BAD-XCRC(%08x,%08x) %s\n",
-                               xcrc32, hammer2_icrc32(head, ioq->hbytes),
-                               hammer2_msg_str(msg));
+                               xcrc32, dmsg_icrc32(head, ioq->hbytes),
+                               dmsg_msg_str(msg));
                        assert(0);
                        break;
                }
                head->hdr_crc = xcrc32;
 
                if (head->magic == DMSG_HDR_MAGIC_REV) {
-                       hammer2_bswap_head(head);
+                       dmsg_bswap_head(head);
                }
 
                /*
@@ -785,9 +787,9 @@ again:
                 */
                ioq->fifo_beg += ioq->hbytes;
                bytes -= ioq->hbytes;
-               ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
+               ioq->state = DMSG_MSGQ_STATE_AUXDATA1;
                /* fall through */
-       case HAMMER2_MSGQ_STATE_AUXDATA1:
+       case DMSG_MSGQ_STATE_AUXDATA1:
                /*
                 * Copy the partial or complete payload from remaining
                 * bytes in the FIFO in order to optimize the makeroom call
@@ -817,14 +819,14 @@ again:
                } else {
                        msg->aux_size = 0;
                }
-               ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
+               ioq->state = DMSG_MSGQ_STATE_AUXDATA2;
                /* fall through */
-       case HAMMER2_MSGQ_STATE_AUXDATA2:
+       case DMSG_MSGQ_STATE_AUXDATA2:
                /*
                 * Make sure there is enough room for more data.
                 */
                assert(msg);
-               nmax = hammer2_ioq_makeroom(ioq, ioq->abytes - msg->aux_size);
+               nmax = dmsg_ioq_makeroom(ioq, ioq->abytes - msg->aux_size);
 
                /*
                 * Read and decrypt more of the payload.
@@ -836,13 +838,13 @@ again:
                                 nmax);
                        if (n <= 0) {
                                if (n == 0) {
-                                       ioq->error = HAMMER2_IOQ_ERROR_EOF;
+                                       ioq->error = DMSG_IOQ_ERROR_EOF;
                                        break;
                                }
                                if (errno != EINTR &&
                                    errno != EINPROGRESS &&
                                    errno != EAGAIN) {
-                                       ioq->error = HAMMER2_IOQ_ERROR_SOCK;
+                                       ioq->error = DMSG_IOQ_ERROR_SOCK;
                                        break;
                                }
                                n = 0;
@@ -852,8 +854,8 @@ again:
                        nmax -= (size_t)n;
                }
 
-               if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
-                       hammer2_crypto_decrypt(iocom, ioq);
+               if (iocom->flags & DMSG_IOCOMF_CRYPTED) {
+                       dmsg_crypto_decrypt(iocom, ioq);
                } else {
                        ioq->fifo_cdx = ioq->fifo_end;
                        ioq->fifo_cdn = ioq->fifo_end;
@@ -884,13 +886,13 @@ again:
                /*
                 * Check aux_crc, then we are done.
                 */
-               xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
+               xcrc32 = dmsg_icrc32(msg->aux_data, msg->aux_size);
                if (xcrc32 != msg->any.head.aux_crc) {
-                       ioq->error = HAMMER2_IOQ_ERROR_ACRC;
+                       ioq->error = DMSG_IOQ_ERROR_ACRC;
                        break;
                }
                break;
-       case HAMMER2_MSGQ_STATE_ERROR:
+       case DMSG_MSGQ_STATE_ERROR:
                /*
                 * Continued calls to drain recorded transactions (returning
                 * a LNK_ERROR for each one), before we return the final
@@ -913,7 +915,7 @@ again:
         */
        if (msg && ioq->error == 0) {
                if ((msg->any.head.salt & 255) != (ioq->seq & 255)) {
-                       ioq->error = HAMMER2_IOQ_ERROR_MSGSEQ;
+                       ioq->error = DMSG_IOQ_ERROR_MSGSEQ;
                } else {
                        ++ioq->seq;
                }
@@ -923,10 +925,10 @@ again:
         * Process transactional state for the message.
         */
        if (msg && ioq->error == 0) {
-               error = hammer2_state_msgrx(msg);
+               error = dmsg_state_msgrx(msg);
                if (error) {
-                       if (error == HAMMER2_IOQ_ERROR_EALREADY) {
-                               hammer2_msg_free(msg);
+                       if (error == DMSG_IOQ_ERROR_EALREADY) {
+                               dmsg_msg_free(msg);
                                goto again;
                        }
                        ioq->error = error;
@@ -952,14 +954,14 @@ skip:
                 */
                assert(ioq->msg == msg);
                if (msg) {
-                       hammer2_msg_free(msg);
+                       dmsg_msg_free(msg);
                        ioq->msg = NULL;
                }
 
                /*
                 * No more I/O read processing
                 */
-               ioq->state = HAMMER2_MSGQ_STATE_ERROR;
+               ioq->state = DMSG_MSGQ_STATE_ERROR;
 
                /*
                 * Simulate a remote LNK_ERROR DELETE msg for any open
@@ -967,21 +969,21 @@ skip:
                 * LNK_ERROR (that the session can detect) when no
                 * transactions remain.
                 */
-               msg = hammer2_msg_alloc(iocom->router, 0, 0, NULL, NULL);
+               msg = dmsg_msg_alloc(iocom->router, 0, 0, NULL, NULL);
                bzero(&msg->any.head, sizeof(msg->any.head));
                msg->any.head.magic = DMSG_HDR_MAGIC;
                msg->any.head.cmd = DMSG_LNK_ERROR;
                msg->any.head.error = ioq->error;
 
                pthread_mutex_lock(&iocom->mtx);
-               hammer2_iocom_drain(iocom);
+               dmsg_iocom_drain(iocom);
                if ((state = RB_ROOT(&iocom->router->staterd_tree)) != NULL) {
                        /*
                         * Active remote transactions are still present.
                         * Simulate the other end sending us a DELETE.
                         */
                        if (state->rxcmd & DMSGF_DELETE) {
-                               hammer2_msg_free(msg);
+                               dmsg_msg_free(msg);
                                msg = NULL;
                        } else {
                                /*state->txcmd |= DMSGF_DELETE;*/
@@ -998,7 +1000,7 @@ skip:
                         * Simulate the other end sending us a DELETE.
                         */
                        if (state->rxcmd & DMSGF_DELETE) {
-                               hammer2_msg_free(msg);
+                               dmsg_msg_free(msg);
                                msg = NULL;
                        } else {
                                msg->state = state;
@@ -1018,7 +1020,7 @@ skip:
                         * Generate a final LNK_ERROR and flag EOF.
                         */
                        msg->state = NULL;
-                       iocom->flags |= HAMMER2_IOCOMF_EOF;
+                       iocom->flags |= DMSG_IOCOMF_EOF;
                        fprintf(stderr, "EOF ON SOCKET %d\n", iocom->sock_fd);
                }
                pthread_mutex_unlock(&iocom->mtx);
@@ -1034,7 +1036,7 @@ skip:
                 * re-set RWORK.
                 */
                if (msg)
-                       iocom->flags |= HAMMER2_IOCOMF_RWORK;
+                       iocom->flags |= DMSG_IOCOMF_RWORK;
        } else if (msg == NULL) {
                /*
                 * Insufficient data received to finish building the message,
@@ -1043,7 +1045,7 @@ skip:
                 * Leave ioq->msg intact.
                 * Leave the FIFO intact.
                 */
-               iocom->flags |= HAMMER2_IOCOMF_RREQ;
+               iocom->flags |= DMSG_IOCOMF_RREQ;
        } else {
                /*
                 * Return msg.
@@ -1057,15 +1059,15 @@ skip:
                 */
                if (ioq->fifo_beg == ioq->fifo_cdx &&
                    ioq->fifo_cdn == ioq->fifo_end) {
-                       iocom->flags |= HAMMER2_IOCOMF_RREQ;
+                       iocom->flags |= DMSG_IOCOMF_RREQ;
                        ioq->fifo_cdx = 0;
                        ioq->fifo_cdn = 0;
                        ioq->fifo_beg = 0;
                        ioq->fifo_end = 0;
                } else {
-                       iocom->flags |= HAMMER2_IOCOMF_RWORK;
+                       iocom->flags |= DMSG_IOCOMF_RWORK;
                }
-               ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
+               ioq->state = DMSG_MSGQ_STATE_HEADER1;
                ioq->msg = NULL;
        }
        return (msg);
@@ -1082,15 +1084,15 @@ skip:
  * Caller must hold iocom->mtx.
  */
 void
-hammer2_iocom_flush1(hammer2_iocom_t *iocom)
+dmsg_iocom_flush1(dmsg_iocom_t *iocom)
 {
-       hammer2_ioq_t *ioq = &iocom->ioq_tx;
-       hammer2_msg_t *msg;
+       dmsg_ioq_t *ioq = &iocom->ioq_tx;
+       dmsg_msg_t *msg;
        uint32_t xcrc32;
        int hbytes;
-       hammer2_msg_queue_t tmpq;
+       dmsg_msg_queue_t tmpq;
 
-       iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
+       iocom->flags &= ~(DMSG_IOCOMF_WREQ | DMSG_IOCOMF_WWORK);
        TAILQ_INIT(&tmpq);
        pthread_mutex_lock(&iocom->mtx);
        while ((msg = TAILQ_FIRST(&iocom->router->txmsgq)) != NULL) {
@@ -1126,7 +1128,7 @@ hammer2_iocom_flush1(hammer2_iocom_t *iocom)
                 */
                if (msg->aux_size && msg->any.head.aux_crc == 0) {
                        assert((msg->aux_size & DMSG_ALIGNMASK) == 0);
-                       xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
+                       xcrc32 = dmsg_icrc32(msg->aux_data, msg->aux_size);
                        msg->any.head.aux_crc = xcrc32;
                }
                msg->any.head.aux_bytes = msg->aux_size / DMSG_ALIGN;
@@ -1135,7 +1137,7 @@ hammer2_iocom_flush1(hammer2_iocom_t *iocom)
                hbytes = (msg->any.head.cmd & DMSGF_SIZE) *
                         DMSG_ALIGN;
                msg->any.head.hdr_crc = 0;
-               msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
+               msg->any.head.hdr_crc = dmsg_icrc32(&msg->any.head, hbytes);
 
                /*
                 * Enqueue the message (the flush codes handles stream
@@ -1144,19 +1146,19 @@ hammer2_iocom_flush1(hammer2_iocom_t *iocom)
                TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
                ++ioq->msgcount;
        }
-       hammer2_iocom_flush2(iocom);
+       dmsg_iocom_flush2(iocom);
 }
 
 /*
  * Thread localized, iocom->mtx not held by caller.
  */
 void
-hammer2_iocom_flush2(hammer2_iocom_t *iocom)
+dmsg_iocom_flush2(dmsg_iocom_t *iocom)
 {
-       hammer2_ioq_t *ioq = &iocom->ioq_tx;
-       hammer2_msg_t *msg;
+       dmsg_ioq_t *ioq = &iocom->ioq_tx;
+       dmsg_msg_t *msg;
        ssize_t n;
-       struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
+       struct iovec iov[DMSG_IOQ_MAXIOVEC];
        size_t nact;
        size_t hbytes;
        size_t abytes;
@@ -1165,7 +1167,7 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
        int iovcnt;
 
        if (ioq->error) {
-               hammer2_iocom_drain(iocom);
+               dmsg_iocom_drain(iocom);
                return;
        }
 
@@ -1192,7 +1194,7 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
                        iov[iovcnt].iov_len = hbytes - hoff;
                        nact += hbytes - hoff;
                        ++iovcnt;
-                       if (iovcnt == HAMMER2_IOQ_MAXIOVEC)
+                       if (iovcnt == DMSG_IOQ_MAXIOVEC)
                                break;
                }
                if (aoff < abytes) {
@@ -1201,7 +1203,7 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
                        iov[iovcnt].iov_len = abytes - aoff;
                        nact += abytes - aoff;
                        ++iovcnt;
-                       if (iovcnt == HAMMER2_IOQ_MAXIOVEC)
+                       if (iovcnt == DMSG_IOQ_MAXIOVEC)
                                break;
                }
                hoff = 0;
@@ -1226,7 +1228,7 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
         * NOTE: The return value from the writev() is the post-encrypted
         *       byte count, not the plaintext count.
         */
-       if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
+       if (iocom->flags & DMSG_IOCOMF_CRYPTED) {
                /*
                 * Make sure the FIFO has a reasonable amount of space
                 * left (if not completely full).
@@ -1241,7 +1243,7 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
                        ioq->fifo_beg = 0;
                }
 
-               iovcnt = hammer2_crypto_encrypt(iocom, ioq, iov, iovcnt, &nact);
+               iovcnt = dmsg_crypto_encrypt(iocom, ioq, iov, iovcnt, &nact);
                n = writev(iocom->sock_fd, iov, iovcnt);
                if (n > 0) {
                        ioq->fifo_beg += n;
@@ -1291,7 +1293,7 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
                ioq->hbytes = 0;
                ioq->abytes = 0;
 
-               hammer2_state_cleanuptx(msg);
+               dmsg_state_cleanuptx(msg);
        }
        assert(nact == 0);
 
@@ -1305,19 +1307,19 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
                        /*
                         * Fatal write error
                         */
-                       ioq->error = HAMMER2_IOQ_ERROR_SOCK;
-                       hammer2_iocom_drain(iocom);
+                       ioq->error = DMSG_IOQ_ERROR_SOCK;
+                       dmsg_iocom_drain(iocom);
                } else {
                        /*
                         * Wait for socket buffer space
                         */
-                       iocom->flags |= HAMMER2_IOCOMF_WREQ;
+                       iocom->flags |= DMSG_IOCOMF_WREQ;
                }
        } else {
-               iocom->flags |= HAMMER2_IOCOMF_WREQ;
+               iocom->flags |= DMSG_IOCOMF_WREQ;
        }
        if (ioq->error) {
-               hammer2_iocom_drain(iocom);
+               dmsg_iocom_drain(iocom);
        }
 }
 
@@ -1330,19 +1332,19 @@ hammer2_iocom_flush2(hammer2_iocom_t *iocom)
  * Thread localized, iocom->mtx not held by caller.
  */
 void
-hammer2_iocom_drain(hammer2_iocom_t *iocom)
+dmsg_iocom_drain(dmsg_iocom_t *iocom)
 {
-       hammer2_ioq_t *ioq = &iocom->ioq_tx;
-       hammer2_msg_t *msg;
+       dmsg_ioq_t *ioq = &iocom->ioq_tx;
+       dmsg_msg_t *msg;
 
-       iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
+       iocom->flags &= ~(DMSG_IOCOMF_WREQ | DMSG_IOCOMF_WWORK);
        ioq->hbytes = 0;
        ioq->abytes = 0;
 
        while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
                TAILQ_REMOVE(&ioq->msgq, msg, qentry);
                --ioq->msgcount;
-               hammer2_state_cleanuptx(msg);
+               dmsg_state_cleanuptx(msg);
        }
 }
 
@@ -1350,10 +1352,10 @@ hammer2_iocom_drain(hammer2_iocom_t *iocom)
  * Write a message to an iocom, with additional state processing.
  */
 void
-hammer2_msg_write(hammer2_msg_t *msg)
+dmsg_msg_write(dmsg_msg_t *msg)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
-       hammer2_state_t *state;
+       dmsg_iocom_t *iocom = msg->router->iocom;
+       dmsg_state_t *state;
        char dummy;
 
        /*
@@ -1368,7 +1370,7 @@ hammer2_msg_write(hammer2_msg_t *msg)
                 *
                 * state->txcmd is adjusted to hold the final message cmd,
                 * and we also be sure to set the CREATE bit here.  We did
-                * not set it in hammer2_msg_alloc() because that would have
+                * not set it in dmsg_msg_alloc() because that would have
                 * not been serialized (state could have gotten ripped out
                 * from under the message prior to it being transmitted).
                 */
@@ -1411,11 +1413,11 @@ hammer2_msg_write(hammer2_msg_t *msg)
  * The reply contains no extended data.
  */
 void
-hammer2_msg_reply(hammer2_msg_t *msg, uint32_t error)
+dmsg_msg_reply(dmsg_msg_t *msg, uint32_t error)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
-       hammer2_state_t *state = msg->state;
-       hammer2_msg_t *nmsg;
+       dmsg_iocom_t *iocom = msg->router->iocom;
+       dmsg_state_t *state = msg->state;
+       dmsg_msg_t *nmsg;
        uint32_t cmd;
 
 
@@ -1449,28 +1451,28 @@ hammer2_msg_reply(hammer2_msg_t *msg, uint32_t error)
         * We cannot pass MSGF_CREATE to msg_alloc() because that may
         * allocate new state.  We have our state already.
         */
-       nmsg = hammer2_msg_alloc(iocom->router, 0, cmd, NULL, NULL);
+       nmsg = dmsg_msg_alloc(iocom->router, 0, cmd, NULL, NULL);
        if (state) {
                if ((state->txcmd & DMSGF_CREATE) == 0)
                        nmsg->any.head.cmd |= DMSGF_CREATE;
        }
        nmsg->any.head.error = error;
        nmsg->state = state;
-       hammer2_msg_write(nmsg);
+       dmsg_msg_write(nmsg);
 }
 
 /*
- * Similar to hammer2_msg_reply() but leave the transaction open.  That is,
+ * Similar to dmsg_msg_reply() but leave the transaction open.  That is,
  * we are generating a streaming reply or an intermediate acknowledgement
  * of some sort as part of the higher level protocol, with more to come
  * later.
  */
 void
-hammer2_msg_result(hammer2_msg_t *msg, uint32_t error)
+dmsg_msg_result(dmsg_msg_t *msg, uint32_t error)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
-       hammer2_state_t *state = msg->state;
-       hammer2_msg_t *nmsg;
+       dmsg_iocom_t *iocom = msg->router->iocom;
+       dmsg_state_t *state = msg->state;
+       dmsg_msg_t *nmsg;
        uint32_t cmd;
 
 
@@ -1499,23 +1501,23 @@ hammer2_msg_result(hammer2_msg_t *msg, uint32_t error)
                        cmd |= DMSGF_REPLY;
        }
 
-       nmsg = hammer2_msg_alloc(iocom->router, 0, cmd, NULL, NULL);
+       nmsg = dmsg_msg_alloc(iocom->router, 0, cmd, NULL, NULL);
        if (state) {
                if ((state->txcmd & DMSGF_CREATE) == 0)
                        nmsg->any.head.cmd |= DMSGF_CREATE;
        }
        nmsg->any.head.error = error;
        nmsg->state = state;
-       hammer2_msg_write(nmsg);
+       dmsg_msg_write(nmsg);
 }
 
 /*
  * Terminate a transaction given a state structure by issuing a DELETE.
  */
 void
-hammer2_state_reply(hammer2_state_t *state, uint32_t error)
+dmsg_state_reply(dmsg_state_t *state, uint32_t error)
 {
-       hammer2_msg_t *nmsg;
+       dmsg_msg_t *nmsg;
        uint32_t cmd = DMSG_LNK_ERROR | DMSGF_DELETE;
 
        /*
@@ -1531,14 +1533,14 @@ hammer2_state_reply(hammer2_state_t *state, uint32_t error)
        if (state->txcmd & DMSGF_REPLY)
                cmd |= DMSGF_REPLY;
 
-       nmsg = hammer2_msg_alloc(state->iocom->router, 0, cmd, NULL, NULL);
+       nmsg = dmsg_msg_alloc(state->iocom->router, 0, cmd, NULL, NULL);
        if (state) {
                if ((state->txcmd & DMSGF_CREATE) == 0)
                        nmsg->any.head.cmd |= DMSGF_CREATE;
        }
        nmsg->any.head.error = error;
        nmsg->state = state;
-       hammer2_msg_write(nmsg);
+       dmsg_msg_write(nmsg);
 }
 
 /************************************************************************
@@ -1615,11 +1617,11 @@ hammer2_state_reply(hammer2_state_t *state, uint32_t error)
  * will typically just contain status updates.
  */
 static int
-hammer2_state_msgrx(hammer2_msg_t *msg)
+dmsg_state_msgrx(dmsg_msg_t *msg)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
-       hammer2_state_t *state;
-       hammer2_state_t dummy;
+       dmsg_iocom_t *iocom = msg->router->iocom;
+       dmsg_state_t *state;
+       dmsg_state_t dummy;
        int error;
 
        /*
@@ -1631,10 +1633,10 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
        dummy.msgid = msg->any.head.msgid;
        pthread_mutex_lock(&iocom->mtx);
        if (msg->any.head.cmd & DMSGF_REPLY) {
-               state = RB_FIND(hammer2_state_tree,
+               state = RB_FIND(dmsg_state_tree,
                                &iocom->router->statewr_tree, &dummy);
        } else {
-               state = RB_FIND(hammer2_state_tree,
+               state = RB_FIND(dmsg_state_tree,
                                &iocom->router->staterd_tree, &dummy);
        }
        msg->state = state;
@@ -1661,28 +1663,28 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                 */
                if (state) {
                        fprintf(stderr, "duplicate-trans %s\n",
-                               hammer2_msg_str(msg));
-                       error = HAMMER2_IOQ_ERROR_TRANS;
+                               dmsg_msg_str(msg));
+                       error = DMSG_IOQ_ERROR_TRANS;
                        assert(0);
                        break;
                }
                state = malloc(sizeof(*state));
                bzero(state, sizeof(*state));
                state->iocom = iocom;
-               state->flags = HAMMER2_STATE_DYNAMIC;
+               state->flags = DMSG_STATE_DYNAMIC;
                state->msg = msg;
                state->txcmd = DMSGF_REPLY;
                state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
-               state->flags |= HAMMER2_STATE_INSERTED;
+               state->flags |= DMSG_STATE_INSERTED;
                state->msgid = msg->any.head.msgid;
                state->router = msg->router;
                msg->state = state;
                pthread_mutex_lock(&iocom->mtx);
-               RB_INSERT(hammer2_state_tree,
+               RB_INSERT(dmsg_state_tree,
                          &iocom->router->staterd_tree, state);
                pthread_mutex_unlock(&iocom->mtx);
                error = 0;
-               if (DebugOpt) {
+               if (DMsgDebugOpt) {
                        fprintf(stderr, "create state %p id=%08x on iocom staterd %p\n",
                                state, (uint32_t)state->msgid, iocom);
                }
@@ -1694,11 +1696,11 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                 */
                if (state == NULL) {
                        if (msg->any.head.cmd & DMSGF_ABORT) {
-                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               error = DMSG_IOQ_ERROR_EALREADY;
                        } else {
                                fprintf(stderr, "missing-state %s\n",
-                                       hammer2_msg_str(msg));
-                               error = HAMMER2_IOQ_ERROR_TRANS;
+                                       dmsg_msg_str(msg));
+                               error = DMSG_IOQ_ERROR_TRANS;
                        assert(0);
                        }
                        break;
@@ -1710,11 +1712,11 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                 */
                if ((state->rxcmd & DMSGF_CREATE) == 0) {
                        if (msg->any.head.cmd & DMSGF_ABORT) {
-                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               error = DMSG_IOQ_ERROR_EALREADY;
                        } else {
                                fprintf(stderr, "reused-state %s\n",
-                                       hammer2_msg_str(msg));
-                               error = HAMMER2_IOQ_ERROR_TRANS;
+                                       dmsg_msg_str(msg));
+                               error = DMSG_IOQ_ERROR_TRANS;
                        assert(0);
                        }
                        break;
@@ -1729,7 +1731,7 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                if (msg->any.head.cmd & DMSGF_ABORT) {
                        if (state == NULL ||
                            (state->rxcmd & DMSGF_CREATE) == 0) {
-                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               error = DMSG_IOQ_ERROR_EALREADY;
                                break;
                        }
                }
@@ -1743,8 +1745,8 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                 */
                if (state == NULL) {
                        fprintf(stderr, "no-state(r) %s\n",
-                               hammer2_msg_str(msg));
-                       error = HAMMER2_IOQ_ERROR_TRANS;
+                               dmsg_msg_str(msg));
+                       error = DMSG_IOQ_ERROR_TRANS;
                        assert(0);
                        break;
                }
@@ -1760,11 +1762,11 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                 */
                if (state == NULL) {
                        if (msg->any.head.cmd & DMSGF_ABORT) {
-                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               error = DMSG_IOQ_ERROR_EALREADY;
                        } else {
                                fprintf(stderr, "no-state(r,d) %s\n",
-                                       hammer2_msg_str(msg));
-                               error = HAMMER2_IOQ_ERROR_TRANS;
+                                       dmsg_msg_str(msg));
+                               error = DMSG_IOQ_ERROR_TRANS;
                        assert(0);
                        }
                        break;
@@ -1777,11 +1779,11 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                 */
                if ((state->rxcmd & DMSGF_CREATE) == 0) {
                        if (msg->any.head.cmd & DMSGF_ABORT) {
-                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               error = DMSG_IOQ_ERROR_EALREADY;
                        } else {
                                fprintf(stderr, "reused-state(r,d) %s\n",
-                                       hammer2_msg_str(msg));
-                               error = HAMMER2_IOQ_ERROR_TRANS;
+                                       dmsg_msg_str(msg));
+                               error = DMSG_IOQ_ERROR_TRANS;
                        assert(0);
                        }
                        break;
@@ -1795,7 +1797,7 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
                if (msg->any.head.cmd & DMSGF_ABORT) {
                        if (state == NULL ||
                            (state->rxcmd & DMSGF_CREATE) == 0) {
-                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               error = DMSG_IOQ_ERROR_EALREADY;
                                break;
                        }
                }
@@ -1806,16 +1808,16 @@ hammer2_state_msgrx(hammer2_msg_t *msg)
 }
 
 void
-hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
+dmsg_state_cleanuprx(dmsg_iocom_t *iocom, dmsg_msg_t *msg)
 {
-       hammer2_state_t *state;
+       dmsg_state_t *state;
 
        if ((state = msg->state) == NULL) {
                /*
                 * Free a non-transactional message, there is no state
                 * to worry about.
                 */
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        } else if (msg->any.head.cmd & DMSGF_DELETE) {
                /*
                 * Message terminating transaction, destroy the related
@@ -1827,65 +1829,65 @@ hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
                if (state->txcmd & DMSGF_DELETE) {
                        if (state->msg == msg)
                                state->msg = NULL;
-                       assert(state->flags & HAMMER2_STATE_INSERTED);
+                       assert(state->flags & DMSG_STATE_INSERTED);
                        if (state->rxcmd & DMSGF_REPLY) {
                                assert(msg->any.head.cmd & DMSGF_REPLY);
-                               RB_REMOVE(hammer2_state_tree,
+                               RB_REMOVE(dmsg_state_tree,
                                          &iocom->router->statewr_tree, state);
                        } else {
                                assert((msg->any.head.cmd & DMSGF_REPLY) == 0);
-                               RB_REMOVE(hammer2_state_tree,
+                               RB_REMOVE(dmsg_state_tree,
                                          &iocom->router->staterd_tree, state);
                        }
-                       state->flags &= ~HAMMER2_STATE_INSERTED;
-                       hammer2_state_free(state);
+                       state->flags &= ~DMSG_STATE_INSERTED;
+                       dmsg_state_free(state);
                } else {
                        ;
                }
                pthread_mutex_unlock(&iocom->mtx);
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        } else if (state->msg != msg) {
                /*
                 * Message not terminating transaction, leave state intact
                 * and free message if it isn't the CREATE message.
                 */
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        }
 }
 
 static void
-hammer2_state_cleanuptx(hammer2_msg_t *msg)
+dmsg_state_cleanuptx(dmsg_msg_t *msg)
 {
-       hammer2_iocom_t *iocom = msg->router->iocom;
-       hammer2_state_t *state;
+       dmsg_iocom_t *iocom = msg->router->iocom;
+       dmsg_state_t *state;
 
        if ((state = msg->state) == NULL) {
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        } else if (msg->any.head.cmd & DMSGF_DELETE) {
                pthread_mutex_lock(&iocom->mtx);
                state->txcmd |= DMSGF_DELETE;
                if (state->rxcmd & DMSGF_DELETE) {
                        if (state->msg == msg)
                                state->msg = NULL;
-                       assert(state->flags & HAMMER2_STATE_INSERTED);
+                       assert(state->flags & DMSG_STATE_INSERTED);
                        if (state->txcmd & DMSGF_REPLY) {
                                assert(msg->any.head.cmd & DMSGF_REPLY);
-                               RB_REMOVE(hammer2_state_tree,
+                               RB_REMOVE(dmsg_state_tree,
                                          &iocom->router->staterd_tree, state);
                        } else {
                                assert((msg->any.head.cmd & DMSGF_REPLY) == 0);
-                               RB_REMOVE(hammer2_state_tree,
+                               RB_REMOVE(dmsg_state_tree,
                                          &iocom->router->statewr_tree, state);
                        }
-                       state->flags &= ~HAMMER2_STATE_INSERTED;
-                       hammer2_state_free(state);
+                       state->flags &= ~DMSG_STATE_INSERTED;
+                       dmsg_state_free(state);
                } else {
                        ;
                }
                pthread_mutex_unlock(&iocom->mtx);
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        } else if (state->msg != msg) {
-               hammer2_msg_free(msg);
+               dmsg_msg_free(msg);
        }
 }
 
@@ -1893,13 +1895,13 @@ hammer2_state_cleanuptx(hammer2_msg_t *msg)
  * Called with iocom locked
  */
 void
-hammer2_state_free(hammer2_state_t *state)
+dmsg_state_free(dmsg_state_t *state)
 {
-       hammer2_iocom_t *iocom = state->iocom;
-       hammer2_msg_t *msg;
+       dmsg_iocom_t *iocom = state->iocom;
+       dmsg_msg_t *msg;
        char dummy;
 
-       if (DebugOpt) {
+       if (DMsgDebugOpt) {
                fprintf(stderr, "terminate state %p id=%08x\n",
                        state, (uint32_t)state->msgid);
        }
@@ -1907,7 +1909,7 @@ hammer2_state_free(hammer2_state_t *state)
        msg = state->msg;
        state->msg = NULL;
        if (msg)
-               hammer2_msg_free_locked(msg);
+               dmsg_msg_free_locked(msg);
        free(state);
 
        /*
@@ -1948,49 +1950,49 @@ hammer2_state_free(hammer2_state_t *state)
  * SPANs and RELAYs), but the routes are distinct from the perspective
  * of the router.
  */
-hammer2_router_t *
-hammer2_router_alloc(void)
+dmsg_router_t *
+dmsg_router_alloc(void)
 {
-       hammer2_router_t *router;
+       dmsg_router_t *router;
 
-       router = hammer2_alloc(sizeof(*router));
+       router = dmsg_alloc(sizeof(*router));
        TAILQ_INIT(&router->txmsgq);
        return (router);
 }
 
 void
-hammer2_router_connect(hammer2_router_t *router)
+dmsg_router_connect(dmsg_router_t *router)
 {
-       hammer2_router_t *tmp;
+       dmsg_router_t *tmp;
 
        assert(router->link || router->relay);
-       assert((router->flags & HAMMER2_ROUTER_CONNECTED) == 0);
+       assert((router->flags & DMSG_ROUTER_CONNECTED) == 0);
 
        pthread_mutex_lock(&router_mtx);
        if (router->link)
-               tmp = RB_INSERT(hammer2_router_tree, &router_ltree, router);
+               tmp = RB_INSERT(dmsg_router_tree, &router_ltree, router);
        else
-               tmp = RB_INSERT(hammer2_router_tree, &router_rtree, router);
+               tmp = RB_INSERT(dmsg_router_tree, &router_rtree, router);
        assert(tmp == NULL);
-       router->flags |= HAMMER2_ROUTER_CONNECTED;
+       router->flags |= DMSG_ROUTER_CONNECTED;
        pthread_mutex_unlock(&router_mtx);
 }
 
 void
-hammer2_router_disconnect(hammer2_router_t **routerp)
+dmsg_router_disconnect(dmsg_router_t **routerp)
 {
-       hammer2_router_t *router;
+       dmsg_router_t *router;
 
        router = *routerp;
        assert(router->link || router->relay);
-       assert(router->flags & HAMMER2_ROUTER_CONNECTED);
+       assert(router->flags & DMSG_ROUTER_CONNECTED);
 
        pthread_mutex_lock(&router_mtx);
        if (router->link)
-               RB_REMOVE(hammer2_router_tree, &router_ltree, router);
+               RB_REMOVE(dmsg_router_tree, &router_ltree, router);
        else
-               RB_REMOVE(hammer2_router_tree, &router_rtree, router);
-       router->flags &= ~HAMMER2_ROUTER_CONNECTED;
+               RB_REMOVE(dmsg_router_tree, &router_rtree, router);
+       router->flags &= ~DMSG_ROUTER_CONNECTED;
        *routerp = NULL;
        pthread_mutex_unlock(&router_mtx);
 }
@@ -1999,237 +2001,32 @@ hammer2_router_disconnect(hammer2_router_t **routerp)
 /*
  * XXX
  */
-hammer2_router_t *
-hammer2_route_msg(hammer2_msg_t *msg)
+dmsg_router_t *
+dmsg_route_msg(dmsg_msg_t *msg)
 {
 }
 #endif
 
-/************************************************************************
- *                             DEBUGGING                               *
- ************************************************************************/
-
-const char *
-hammer2_basecmd_str(uint32_t cmd)
-{
-       static char buf[64];
-       char protobuf[32];
-       char cmdbuf[32];
-       const char *protostr;
-       const char *cmdstr;
-
-       switch(cmd & DMSGF_PROTOS) {
-       case DMSG_PROTO_LNK:
-               protostr = "LNK_";
-               break;
-       case DMSG_PROTO_DBG:
-               protostr = "DBG_";
-               break;
-       case DMSG_PROTO_DOM:
-               protostr = "DOM_";
-               break;
-       case DMSG_PROTO_CAC:
-               protostr = "CAC_";
-               break;
-       case DMSG_PROTO_QRM:
-               protostr = "QRM_";
-               break;
-       case DMSG_PROTO_BLK:
-               protostr = "BLK_";
-               break;
-       case DMSG_PROTO_VOP:
-               protostr = "VOP_";
-               break;
-       default:
-               snprintf(protobuf, sizeof(protobuf), "%x_",
-                       (cmd & DMSGF_PROTOS) >> 20);
-               protostr = protobuf;
-               break;
-       }
-
-       switch(cmd & (DMSGF_PROTOS |
-                     DMSGF_CMDS |
-                     DMSGF_SIZE)) {
-       case DMSG_LNK_PAD:
-               cmdstr = "PAD";
-               break;
-       case DMSG_LNK_PING:
-               cmdstr = "PING";
-               break;
-       case DMSG_LNK_AUTH:
-               cmdstr = "AUTH";
-               break;
-       case DMSG_LNK_CONN:
-               cmdstr = "CONN";
-               break;
-       case DMSG_LNK_SPAN:
-               cmdstr = "SPAN";
-               break;
-       case DMSG_LNK_VOLCONF:
-               cmdstr = "VOLCONF";
-               break;
-       case DMSG_LNK_ERROR:
-               if (cmd & DMSGF_DELETE)
-                       cmdstr = "RETURN";
-               else
-                       cmdstr = "RESULT";
-               break;
-       case DMSG_DBG_SHELL:
-               cmdstr = "SHELL";
-               break;
-       default:
-               snprintf(cmdbuf, sizeof(cmdbuf),
-                        "%06x", (cmd & (DMSGF_PROTOS |
-                                        DMSGF_CMDS |
-                                        DMSGF_SIZE)));
-               cmdstr = cmdbuf;
-               break;
-       }
-       snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
-       return (buf);
-}
-
-const char *
-hammer2_msg_str(hammer2_msg_t *msg)
+/*
+ * This swaps endian for a hammer2_msg_hdr.  Note that the extended
+ * header is not adjusted, just the core header.
+ */
+void
+dmsg_bswap_head(dmsg_hdr_t *head)
 {
-       hammer2_state_t *state;
-       static char buf[256];
-       char errbuf[16];
-       char statebuf[64];
-       char flagbuf[64];
-       const char *statestr;
-       const char *errstr;
-       uint32_t basecmd;
-       int i;
-
-       /*
-        * Parse the state
-        */
-       if ((state = msg->state) != NULL) {
-               basecmd = (state->rxcmd & DMSGF_REPLY) ?
-                         state->txcmd : state->rxcmd;
-               snprintf(statebuf, sizeof(statebuf),
-                        " %s=%s,L=%s%s,R=%s%s",
-                        ((state->txcmd & DMSGF_REPLY) ?
-                               "rcvcmd" : "sndcmd"),
-                        hammer2_basecmd_str(basecmd),
-                        ((state->txcmd & DMSGF_CREATE) ? "C" : ""),
-                        ((state->txcmd & DMSGF_DELETE) ? "D" : ""),
-                        ((state->rxcmd & DMSGF_CREATE) ? "C" : ""),
-                        ((state->rxcmd & DMSGF_DELETE) ? "D" : "")
-               );
-               statestr = statebuf;
-       } else {
-               statestr = "";
-       }
-
-       /*
-        * Parse the error
-        */
-       switch(msg->any.head.error) {
-       case 0:
-               errstr = "";
-               break;
-       case HAMMER2_IOQ_ERROR_SYNC:
-               errstr = "err=IOQ:NOSYNC";
-               break;
-       case HAMMER2_IOQ_ERROR_EOF:
-               errstr = "err=IOQ:STREAMEOF";
-               break;
-       case HAMMER2_IOQ_ERROR_SOCK:
-               errstr = "err=IOQ:SOCKERR";
-               break;
-       case HAMMER2_IOQ_ERROR_FIELD:
-               errstr = "err=IOQ:BADFIELD";
-               break;
-       case HAMMER2_IOQ_ERROR_HCRC:
-               errstr = "err=IOQ:BADHCRC";
-               break;
-       case HAMMER2_IOQ_ERROR_XCRC:
-               errstr = "err=IOQ:BADXCRC";
-               break;
-       case HAMMER2_IOQ_ERROR_ACRC:
-               errstr = "err=IOQ:BADACRC";
-               break;
-       case HAMMER2_IOQ_ERROR_STATE:
-               errstr = "err=IOQ:BADSTATE";
-               break;
-       case HAMMER2_IOQ_ERROR_NOPEER:
-               errstr = "err=IOQ:PEERCONFIG";
-               break;
-       case HAMMER2_IOQ_ERROR_NORKEY:
-               errstr = "err=IOQ:BADRKEY";
-               break;
-       case HAMMER2_IOQ_ERROR_NOLKEY:
-               errstr = "err=IOQ:BADLKEY";
-               break;
-       case HAMMER2_IOQ_ERROR_KEYXCHGFAIL:
-               errstr = "err=IOQ:BADKEYXCHG";
-               break;
-       case HAMMER2_IOQ_ERROR_KEYFMT:
-               errstr = "err=IOQ:BADFMT";
-               break;
-       case HAMMER2_IOQ_ERROR_BADURANDOM:
-               errstr = "err=IOQ:BADRANDOM";
-               break;
-       case HAMMER2_IOQ_ERROR_MSGSEQ:
-               errstr = "err=IOQ:BADSEQ";
-               break;
-       case HAMMER2_IOQ_ERROR_EALREADY:
-               errstr = "err=IOQ:DUPMSG";
-               break;
-       case HAMMER2_IOQ_ERROR_TRANS:
-               errstr = "err=IOQ:BADTRANS";
-               break;
-       case HAMMER2_IOQ_ERROR_IVWRAP:
-               errstr = "err=IOQ:IVWRAP";
-               break;
-       case HAMMER2_IOQ_ERROR_MACFAIL:
-               errstr = "err=IOQ:MACFAIL";
-               break;
-       case HAMMER2_IOQ_ERROR_ALGO:
-               errstr = "err=IOQ:ALGOFAIL";
-               break;
-       case DMSG_ERR_NOSUPP:
-               errstr = "err=NOSUPPORT";
-               break;
-       default:
-               snprintf(errbuf, sizeof(errbuf),
-                        " err=%d", msg->any.head.error);
-               errstr = errbuf;
-               break;
-       }
-
-       /*
-        * Message flags
-        */
-       i = 0;
-       if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
-                                DMSGF_ABORT | DMSGF_REPLY)) {
-               flagbuf[i++] = '|';
-               if (msg->any.head.cmd & DMSGF_CREATE)
-                       flagbuf[i++] = 'C';
-               if (msg->any.head.cmd & DMSGF_DELETE)
-                       flagbuf[i++] = 'D';
-               if (msg->any.head.cmd & DMSGF_REPLY)
-                       flagbuf[i++] = 'R';
-               if (msg->any.head.cmd & DMSGF_ABORT)
-                       flagbuf[i++] = 'A';
-       }
-       flagbuf[i] = 0;
-
-       /*
-        * Generate the buf
-        */
-       snprintf(buf, sizeof(buf),
-               "msg=%s%s %s id=%08x src=%08x tgt=%08x %s",
-                hammer2_basecmd_str(msg->any.head.cmd),
-                flagbuf,
-                errstr,
-                (uint32_t)(intmax_t)msg->any.head.msgid,   /* for brevity */
-                (uint32_t)(intmax_t)msg->any.head.source,  /* for brevity */
-                (uint32_t)(intmax_t)msg->any.head.target,  /* for brevity */
-                statestr);
-
-       return(buf);
+       head->magic     = bswap16(head->magic);
+       head->reserved02 = bswap16(head->reserved02);
+       head->salt      = bswap32(head->salt);
+
+       head->msgid     = bswap64(head->msgid);
+       head->source    = bswap64(head->source);
+       head->target    = bswap64(head->target);
+
+       head->cmd       = bswap32(head->cmd);
+       head->aux_crc   = bswap32(head->aux_crc);
+       head->aux_bytes = bswap32(head->aux_bytes);
+       head->error     = bswap32(head->error);
+       head->aux_descr = bswap64(head->aux_descr);
+       head->reserved38= bswap32(head->reserved38);
+       head->hdr_crc   = bswap32(head->hdr_crc);
 }
similarity index 86%
rename from sbin/hammer2/msg_lnk.c
rename to lib/libdmsg/msg_lnk.c
index 1d59488..6a48c6b 100644 (file)
  * core master nodes and satellite slaves and cache nodes.
  */
 
-#include "hammer2.h"
+#include "dmsg_local.h"
 
 /*
  * Maximum spanning tree distance.  This has the practical effect of
  * stopping tail-chasing closed loops when a feeder span is lost.
  */
-#define HAMMER2_SPAN_MAXDIST   16
+#define DMSG_SPAN_MAXDIST      16
 
 /*
  * RED-BLACK TREE DEFINITIONS
@@ -208,10 +208,10 @@ struct h2span_media {
                pthread_cond_t          cond;
                int                     ctl;
                int                     fd;
-               hammer2_iocom_t         iocom;
+               dmsg_iocom_t            iocom;
                pthread_t               iocom_thread;
                enum { H2MC_STOPPED, H2MC_CONNECT, H2MC_RUNNING } state;
-       } config[HAMMER2_COPYID_COUNT];
+       } config[DMSG_COPYID_COUNT];
 };
 
 typedef struct h2span_media_config h2span_media_config_t;
@@ -228,7 +228,7 @@ struct h2span_conn {
        TAILQ_ENTRY(h2span_conn) entry;
        struct h2span_relay_tree tree;
        struct h2span_media *media;
-       hammer2_state_t *state;
+       dmsg_state_t *state;
 };
 
 /*
@@ -252,11 +252,11 @@ struct h2span_node {
 
 struct h2span_link {
        RB_ENTRY(h2span_link) rbnode;
-       hammer2_state_t *state;         /* state<->link */
+       dmsg_state_t    *state;         /* state<->link */
        struct h2span_node *node;       /* related node */
        int32_t dist;
        struct h2span_relay_queue relayq; /* relay out */
-       struct hammer2_router *router;  /* route out this link */
+       struct dmsg_router *router;     /* route out this link */
 };
 
 /*
@@ -282,9 +282,9 @@ struct h2span_relay {
        RB_ENTRY(h2span_relay) rbnode;  /* from h2span_conn */
        TAILQ_ENTRY(h2span_relay) entry; /* from link */
        struct h2span_conn *conn;
-       hammer2_state_t *state;         /* transmitted LNK_SPAN */
+       dmsg_state_t    *state;         /* transmitted LNK_SPAN */
        struct h2span_link *link;       /* LNK_SPAN being relayed */
-       struct hammer2_router   *router;/* route out this relay */
+       struct dmsg_router      *router;/* route out this relay */
 };
 
 
@@ -399,53 +399,53 @@ static struct h2span_cluster_tree cluster_tree = RB_INITIALIZER(cluster_tree);
 static struct h2span_conn_queue connq = TAILQ_HEAD_INITIALIZER(connq);
 static struct h2span_media_queue mediaq = TAILQ_HEAD_INITIALIZER(mediaq);
 
-static void hammer2_lnk_span(hammer2_msg_t *msg);
-static void hammer2_lnk_conn(hammer2_msg_t *msg);
-static void hammer2_lnk_relay(hammer2_msg_t *msg);
-static void hammer2_relay_scan(h2span_conn_t *conn, h2span_node_t *node);
-static void hammer2_relay_delete(h2span_relay_t *relay);
+static void dmsg_lnk_span(dmsg_msg_t *msg);
+static void dmsg_lnk_conn(dmsg_msg_t *msg);
+static void dmsg_lnk_relay(dmsg_msg_t *msg);
+static void dmsg_relay_scan(h2span_conn_t *conn, h2span_node_t *node);
+static void dmsg_relay_delete(h2span_relay_t *relay);
 
-static void *hammer2_volconf_thread(void *info);
-static void hammer2_volconf_stop(h2span_media_config_t *conf);
-static void hammer2_volconf_start(h2span_media_config_t *conf,
+static void *dmsg_volconf_thread(void *info);
+static void dmsg_volconf_stop(h2span_media_config_t *conf);
+static void dmsg_volconf_start(h2span_media_config_t *conf,
                                const char *hostname);
 
 void
-hammer2_msg_lnk_signal(hammer2_router_t *router __unused)
+dmsg_msg_lnk_signal(dmsg_router_t *router __unused)
 {
        pthread_mutex_lock(&cluster_mtx);
-       hammer2_relay_scan(NULL, NULL);
+       dmsg_relay_scan(NULL, NULL);
        pthread_mutex_unlock(&cluster_mtx);
 }
 
 /*
- * Receive a HAMMER2_MSG_PROTO_LNK message.  This only called for
+ * Receive a DMSG_PROTO_LNK message.  This only called for
  * one-way and opening-transactions since state->func will be assigned
  * in all other cases.
  */
 void
-hammer2_msg_lnk(hammer2_msg_t *msg)
+dmsg_msg_lnk(dmsg_msg_t *msg)
 {
        switch(msg->any.head.cmd & DMSGF_BASECMDMASK) {
        case DMSG_LNK_CONN:
-               hammer2_lnk_conn(msg);
+               dmsg_lnk_conn(msg);
                break;
        case DMSG_LNK_SPAN:
-               hammer2_lnk_span(msg);
+               dmsg_lnk_span(msg);
                break;
        default:
                fprintf(stderr,
                        "MSG_PROTO_LNK: Unknown msg %08x\n", msg->any.head.cmd);
-               hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
+               dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
                /* state invalid after reply */
                break;
        }
 }
 
 void
-hammer2_lnk_conn(hammer2_msg_t *msg)
+dmsg_lnk_conn(dmsg_msg_t *msg)
 {
-       hammer2_state_t *state = msg->state;
+       dmsg_state_t *state = msg->state;
        h2span_media_t *media;
        h2span_media_config_t *conf;
        h2span_conn_t *conn;
@@ -465,16 +465,16 @@ hammer2_lnk_conn(hammer2_msg_t *msg)
                 */
                fprintf(stderr, "LNK_CONN(%08x): %s/%s\n",
                        (uint32_t)msg->any.head.msgid,
-                       hammer2_uuid_to_str(&msg->any.lnk_conn.pfs_clid,
+                       dmsg_uuid_to_str(&msg->any.lnk_conn.pfs_clid,
                                            &alloc),
                        msg->any.lnk_conn.label);
                free(alloc);
 
-               conn = hammer2_alloc(sizeof(*conn));
+               conn = dmsg_alloc(sizeof(*conn));
 
                RB_INIT(&conn->tree);
                conn->state = state;
-               state->func = hammer2_lnk_conn;
+               state->func = dmsg_lnk_conn;
                state->any.conn = conn;
                TAILQ_INSERT_TAIL(&connq, conn, entry);
 
@@ -488,7 +488,7 @@ hammer2_lnk_conn(hammer2_msg_t *msg)
                        }
                }
                if (media == NULL) {
-                       media = hammer2_alloc(sizeof(*media));
+                       media = dmsg_alloc(sizeof(*media));
                        media->mediaid = msg->any.lnk_conn.mediaid;
                        TAILQ_INSERT_TAIL(&mediaq, media, entry);
                }
@@ -496,8 +496,8 @@ hammer2_lnk_conn(hammer2_msg_t *msg)
                ++media->refs;
 
                if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
-                       hammer2_msg_result(msg, 0);
-                       hammer2_router_signal(msg->router);
+                       dmsg_msg_result(msg, 0);
+                       dmsg_router_signal(msg->router);
                        break;
                }
                /* FALL THROUGH */
@@ -520,7 +520,7 @@ deleteconn:
                media = conn->media;
                if (--media->refs == 0) {
                        fprintf(stderr, "Shutting down media spans\n");
-                       for (i = 0; i < HAMMER2_COPYID_COUNT; ++i) {
+                       for (i = 0; i < DMSG_COPYID_COUNT; ++i) {
                                conf = &media->config[i];
 
                                if (conf->thread == NULL)
@@ -528,7 +528,7 @@ deleteconn:
                                conf->ctl = H2CONFCTL_STOP;
                                pthread_cond_signal(&conf->cond);
                        }
-                       for (i = 0; i < HAMMER2_COPYID_COUNT; ++i) {
+                       for (i = 0; i < DMSG_COPYID_COUNT; ++i) {
                                conf = &media->config[i];
 
                                if (conf->thread == NULL)
@@ -541,7 +541,7 @@ deleteconn:
                        }
                        fprintf(stderr, "Media shutdown complete\n");
                        TAILQ_REMOVE(&mediaq, media, entry);
-                       hammer2_free(media);
+                       dmsg_free(media);
                }
 
                /*
@@ -549,7 +549,7 @@ deleteconn:
                 * relay transaction.
                 */
                while ((relay = RB_ROOT(&conn->tree)) != NULL) {
-                       hammer2_relay_delete(relay);
+                       dmsg_relay_delete(relay);
                }
 
                /*
@@ -559,9 +559,9 @@ deleteconn:
                conn->state = NULL;
                msg->state->any.conn = NULL;
                TAILQ_REMOVE(&connq, conn, entry);
-               hammer2_free(conn);
+               dmsg_free(conn);
 
-               hammer2_msg_reply(msg, 0);
+               dmsg_msg_reply(msg, 0);
                /* state invalid after reply */
                break;
        case DMSG_LNK_VOLCONF:
@@ -571,7 +571,7 @@ deleteconn:
                 */
                fprintf(stderr, "RECEIVED VOLCONF\n");
                if (msg->any.lnk_volconf.index < 0 ||
-                   msg->any.lnk_volconf.index >= HAMMER2_COPYID_COUNT) {
+                   msg->any.lnk_volconf.index >= DMSG_COPYID_COUNT) {
                        fprintf(stderr, "VOLCONF: ILLEGAL INDEX %d\n",
                                msg->any.lnk_volconf.index);
                        break;
@@ -594,7 +594,7 @@ deleteconn:
                        fprintf(stderr, "VOLCONF THREAD STARTED\n");
                        pthread_cond_init(&conf->cond, NULL);
                        pthread_create(&conf->thread, NULL,
-                                      hammer2_volconf_thread, (void *)conf);
+                                      dmsg_volconf_thread, (void *)conf);
                }
                pthread_cond_signal(&conf->cond);
                break;
@@ -604,16 +604,16 @@ deleteconn:
                 */
                if (msg->any.head.cmd & DMSGF_DELETE)
                        goto deleteconn;
-               hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
+               dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
                break;
        }
        pthread_mutex_unlock(&cluster_mtx);
 }
 
 void
-hammer2_lnk_span(hammer2_msg_t *msg)
+dmsg_lnk_span(dmsg_msg_t *msg)
 {
-       hammer2_state_t *state = msg->state;
+       dmsg_state_t *state = msg->state;
        h2span_cluster_t dummy_cls;
        h2span_node_t dummy_node;
        h2span_cluster_t *cls;
@@ -631,7 +631,7 @@ hammer2_lnk_span(hammer2_msg_t *msg)
         */
        if (msg->any.head.cmd & DMSGF_CREATE) {
                assert(state->func == NULL);
-               state->func = hammer2_lnk_span;
+               state->func = dmsg_lnk_span;
 
                msg->any.lnk_span.label[sizeof(msg->any.lnk_span.label)-1] = 0;
 
@@ -641,7 +641,7 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                dummy_cls.pfs_clid = msg->any.lnk_span.pfs_clid;
                cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls);
                if (cls == NULL) {
-                       cls = hammer2_alloc(sizeof(*cls));
+                       cls = dmsg_alloc(sizeof(*cls));
                        cls->pfs_clid = msg->any.lnk_span.pfs_clid;
                        RB_INIT(&cls->tree);
                        RB_INSERT(h2span_cluster_tree, &cluster_tree, cls);
@@ -653,7 +653,7 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                dummy_node.pfs_fsid = msg->any.lnk_span.pfs_fsid;
                node = RB_FIND(h2span_node_tree, &cls->tree, &dummy_node);
                if (node == NULL) {
-                       node = hammer2_alloc(sizeof(*node));
+                       node = dmsg_alloc(sizeof(*node));
                        node->pfs_fsid = msg->any.lnk_span.pfs_fsid;
                        node->cls = cls;
                        RB_INIT(&node->tree);
@@ -666,7 +666,7 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                 * Create the link
                 */
                assert(state->any.link == NULL);
-               slink = hammer2_alloc(sizeof(*slink));
+               slink = dmsg_alloc(sizeof(*slink));
                TAILQ_INIT(&slink->relayq);
                slink->node = node;
                slink->dist = msg->any.lnk_span.dist;
@@ -680,26 +680,26 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                 * the SPAN link it is embedded in, allowing messages to
                 * be routed via &slink->router.
                 */
-               slink->router = hammer2_router_alloc();
+               slink->router = dmsg_router_alloc();
                slink->router->iocom = state->iocom;
                slink->router->link = slink;
                slink->router->target = state->msgid;
-               hammer2_router_connect(slink->router);
+               dmsg_router_connect(slink->router);
 
                RB_INSERT(h2span_link_tree, &node->tree, slink);
 
                fprintf(stderr, "LNK_SPAN(thr %p): %p %s/%s dist=%d\n",
                        msg->router->iocom,
                        slink,
-                       hammer2_uuid_to_str(&msg->any.lnk_span.pfs_clid,
+                       dmsg_uuid_to_str(&msg->any.lnk_span.pfs_clid,
                                            &alloc),
                        msg->any.lnk_span.label,
                        msg->any.lnk_span.dist);
                free(alloc);
 #if 0
-               hammer2_relay_scan(NULL, node);
+               dmsg_relay_scan(NULL, node);
 #endif
-               hammer2_router_signal(msg->router);
+               dmsg_router_signal(msg->router);
        }
 
        /*
@@ -714,7 +714,7 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                fprintf(stderr, "LNK_DELE(thr %p): %p %s/%s dist=%d\n",
                        msg->router->iocom,
                        slink,
-                       hammer2_uuid_to_str(&cls->pfs_clid, &alloc),
+                       dmsg_uuid_to_str(&cls->pfs_clid, &alloc),
                        state->msg->any.lnk_span.label,
                        state->msg->any.lnk_span.dist);
                free(alloc);
@@ -722,14 +722,14 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                /*
                 * Remove the router from consideration
                 */
-               hammer2_router_disconnect(&slink->router);
+               dmsg_router_disconnect(&slink->router);
 
                /*
                 * Clean out all relays.  This requires terminating each
                 * relay transaction.
                 */
                while ((relay = TAILQ_FIRST(&slink->relayq)) != NULL) {
-                       hammer2_relay_delete(relay);
+                       dmsg_relay_delete(relay);
                }
 
                /*
@@ -741,21 +741,21 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                        if (RB_EMPTY(&cls->tree) && cls->refs == 0) {
                                RB_REMOVE(h2span_cluster_tree,
                                          &cluster_tree, cls);
-                               hammer2_free(cls);
+                               dmsg_free(cls);
                        }
                        node->cls = NULL;
-                       hammer2_free(node);
+                       dmsg_free(node);
                        node = NULL;
                }
                state->any.link = NULL;
                slink->state = NULL;
                slink->node = NULL;
-               hammer2_free(slink);
+               dmsg_free(slink);
 
                /*
                 * We have to terminate the transaction
                 */
-               hammer2_state_reply(state, 0);
+               dmsg_state_reply(state, 0);
                /* state invalid after reply */
 
                /*
@@ -765,10 +765,10 @@ hammer2_lnk_span(hammer2_msg_t *msg)
                 */
 #if 0
                if (node)
-                       hammer2_relay_scan(NULL, node);
+                       dmsg_relay_scan(NULL, node);
 #endif
                if (node)
-                       hammer2_router_signal(msg->router);
+                       dmsg_router_signal(msg->router);
        }
 
        pthread_mutex_unlock(&cluster_mtx);
@@ -781,9 +781,9 @@ hammer2_lnk_span(hammer2_msg_t *msg)
  * XXX MPRACE on state structure
  */
 static void
-hammer2_lnk_relay(hammer2_msg_t *msg)
+dmsg_lnk_relay(dmsg_msg_t *msg)
 {
-       hammer2_state_t *state = msg->state;
+       dmsg_state_t *state = msg->state;
        h2span_relay_t *relay;
 
        assert(msg->any.head.cmd & DMSGF_REPLY);
@@ -791,9 +791,9 @@ hammer2_lnk_relay(hammer2_msg_t *msg)
        if (msg->any.head.cmd & DMSGF_DELETE) {
                pthread_mutex_lock(&cluster_mtx);
                if ((relay = state->any.relay) != NULL) {
-                       hammer2_relay_delete(relay);
+                       dmsg_relay_delete(relay);
                } else {
-                       hammer2_state_reply(state, 0);
+                       dmsg_state_reply(state, 0);
                }
                pthread_mutex_unlock(&cluster_mtx);
        }
@@ -804,11 +804,11 @@ hammer2_lnk_relay(hammer2_msg_t *msg)
  *
  * Called with cluster_mtx held.
  */
-static void hammer2_relay_scan_specific(h2span_node_t *node,
+static void dmsg_relay_scan_specific(h2span_node_t *node,
                                        h2span_conn_t *conn);
 
 static void
-hammer2_relay_scan(h2span_conn_t *conn, h2span_node_t *node)
+dmsg_relay_scan(h2span_conn_t *conn, h2span_node_t *node)
 {
        h2span_cluster_t *cls;
 
@@ -817,7 +817,7 @@ hammer2_relay_scan(h2span_conn_t *conn, h2span_node_t *node)
                 * Iterate specific node
                 */
                TAILQ_FOREACH(conn, &connq, entry)
-                       hammer2_relay_scan_specific(node, conn);
+                       dmsg_relay_scan_specific(node, conn);
        } else {
                /*
                 * Full iteration.
@@ -835,10 +835,10 @@ hammer2_relay_scan(h2span_conn_t *conn, h2span_node_t *node)
                                 * with each connection's relays.
                                 */
                                if (conn) {
-                                       hammer2_relay_scan_specific(node, conn);
+                                       dmsg_relay_scan_specific(node, conn);
                                } else {
                                        TAILQ_FOREACH(conn, &connq, entry) {
-                                           hammer2_relay_scan_specific(node,
+                                           dmsg_relay_scan_specific(node,
                                                                        conn);
                                        }
                                        assert(conn == NULL);
@@ -854,7 +854,7 @@ hammer2_relay_scan(h2span_conn_t *conn, h2span_node_t *node)
  * Iterate links and adjust relays to match.  We only propagate the top link
  * for now (XXX we want to propagate the top two).
  *
- * The hammer2_relay_scan_cmp() function locates the first relay element
+ * The dmsg_relay_scan_cmp() function locates the first relay element
  * for any given node.  The relay elements will be sub-sorted by dist.
  */
 struct relay_scan_info {
@@ -863,7 +863,7 @@ struct relay_scan_info {
 };
 
 static int
-hammer2_relay_scan_cmp(h2span_relay_t *relay, void *arg)
+dmsg_relay_scan_cmp(h2span_relay_t *relay, void *arg)
 {
        struct relay_scan_info *info = arg;
 
@@ -875,7 +875,7 @@ hammer2_relay_scan_cmp(h2span_relay_t *relay, void *arg)
 }
 
 static int
-hammer2_relay_scan_callback(h2span_relay_t *relay, void *arg)
+dmsg_relay_scan_callback(h2span_relay_t *relay, void *arg)
 {
        struct relay_scan_info *info = arg;
 
@@ -884,14 +884,14 @@ hammer2_relay_scan_callback(h2span_relay_t *relay, void *arg)
 }
 
 static void
-hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
+dmsg_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
 {
        struct relay_scan_info info;
        h2span_relay_t *relay;
        h2span_relay_t *next_relay;
        h2span_link_t *slink;
        dmsg_lnk_conn_t *lconn;
-       hammer2_msg_t *msg;
+       dmsg_msg_t *msg;
        int count = 2;
        uint8_t peer_type;
 
@@ -903,13 +903,13 @@ hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
         * relay will be NULL if there were none.
         */
        RB_SCAN(h2span_relay_tree, &conn->tree,
-               hammer2_relay_scan_cmp, hammer2_relay_scan_callback, &info);
+               dmsg_relay_scan_cmp, dmsg_relay_scan_callback, &info);
        relay = info.relay;
        info.relay = NULL;
        if (relay)
                assert(relay->link->node == node);
 
-       if (DebugOpt > 8)
+       if (DMsgDebugOpt > 8)
                fprintf(stderr, "relay scan for connection %p\n", conn);
 
        /*
@@ -943,7 +943,7 @@ hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
                 * The spanning tree can cause closed loops so we have
                 * to limit slink->dist.
                 */
-               if (slink->dist > HAMMER2_SPAN_MAXDIST)
+               if (slink->dist > DMSG_SPAN_MAXDIST)
                        break;
 
                /*
@@ -974,7 +974,7 @@ hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
                 * particular cluster it represents).
                 */
                if (peer_type == lconn->peer_type &&
-                   peer_type == HAMMER2_PEER_HAMMER2) {
+                   peer_type == DMSG_PEER_HAMMER2) {
                        if (!uuid_is_nil(&slink->node->cls->pfs_clid, NULL) &&
                            uuid_compare(&slink->node->cls->pfs_clid,
                                         &lconn->pfs_clid, NULL) != 0) {
@@ -988,16 +988,16 @@ hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
                assert(relay == NULL ||
                       relay->link->node != slink->node ||
                       relay->link->dist >= slink->dist);
-               relay = hammer2_alloc(sizeof(*relay));
+               relay = dmsg_alloc(sizeof(*relay));
                relay->conn = conn;
                relay->link = slink;
 
-               msg = hammer2_msg_alloc(conn->state->iocom->router, 0,
+               msg = dmsg_msg_alloc(conn->state->iocom->router, 0,
                                        DMSG_LNK_SPAN |
                                        DMSGF_CREATE,
-                                       hammer2_lnk_relay, relay);
+                                       dmsg_lnk_relay, relay);
                relay->state = msg->state;
-               relay->router = hammer2_router_alloc();
+               relay->router = dmsg_router_alloc();
                relay->router->iocom = relay->state->iocom;
                relay->router->relay = relay;
                relay->router->target = relay->state->msgid;
@@ -1005,12 +1005,12 @@ hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
                msg->any.lnk_span = slink->state->msg->any.lnk_span;
                msg->any.lnk_span.dist = slink->dist + 1;
 
-               hammer2_router_connect(relay->router);
+               dmsg_router_connect(relay->router);
 
                RB_INSERT(h2span_relay_tree, &conn->tree, relay);
                TAILQ_INSERT_TAIL(&slink->relayq, relay, entry);
 
-               hammer2_msg_write(msg);
+               dmsg_msg_write(msg);
 
                fprintf(stderr,
                        "RELAY SPAN %p RELAY %p ON CLS=%p NODE=%p DIST=%d "
@@ -1036,14 +1036,14 @@ hammer2_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
         */
        while (relay && relay->link->node == node) {
                next_relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
-               hammer2_relay_delete(relay);
+               dmsg_relay_delete(relay);
                relay = next_relay;
        }
 }
 
 static
 void
-hammer2_relay_delete(h2span_relay_t *relay)
+dmsg_relay_delete(h2span_relay_t *relay)
 {
        fprintf(stderr,
                "RELAY DELETE %p RELAY %p ON CLS=%p NODE=%p DIST=%d FD %d STATE %p\n",
@@ -1053,24 +1053,24 @@ hammer2_relay_delete(h2span_relay_t *relay)
                relay->link->dist,
                relay->conn->state->iocom->sock_fd, relay->state);
 
-       hammer2_router_disconnect(&relay->router);
+       dmsg_router_disconnect(&relay->router);
 
        RB_REMOVE(h2span_relay_tree, &relay->conn->tree, relay);
        TAILQ_REMOVE(&relay->link->relayq, relay, entry);
 
        if (relay->state) {
                relay->state->any.relay = NULL;
-               hammer2_state_reply(relay->state, 0);
+               dmsg_state_reply(relay->state, 0);
                /* state invalid after reply */
                relay->state = NULL;
        }
        relay->conn = NULL;
        relay->link = NULL;
-       hammer2_free(relay);
+       dmsg_free(relay);
 }
 
 static void *
-hammer2_volconf_thread(void *info)
+dmsg_volconf_thread(void *info)
 {
        h2span_media_config_t *conf = info;
 
@@ -1090,18 +1090,18 @@ hammer2_volconf_thread(void *info)
                         */
 
                        pthread_mutex_unlock(&cluster_mtx);
-                       hammer2_volconf_stop(conf);
+                       dmsg_volconf_stop(conf);
                        conf->copy_run = conf->copy_pend;
                        if (conf->copy_run.copyid != 0 &&
                            strncmp(conf->copy_run.path, "span:", 5) == 0) {
-                               hammer2_volconf_start(conf,
+                               dmsg_volconf_start(conf,
                                                      conf->copy_run.path + 5);
                        }
                        pthread_mutex_lock(&cluster_mtx);
                        fprintf(stderr, "VOLCONF UPDATE DONE state %d\n", conf->state);
                }
                if (conf->state == H2MC_CONNECT) {
-                       hammer2_volconf_start(conf, conf->copy_run.path + 5);
+                       dmsg_volconf_start(conf, conf->copy_run.path + 5);
                        pthread_mutex_unlock(&cluster_mtx);
                        sleep(5);
                        pthread_mutex_lock(&cluster_mtx);
@@ -1110,13 +1110,13 @@ hammer2_volconf_thread(void *info)
                }
        }
        pthread_mutex_unlock(&cluster_mtx);
-       hammer2_volconf_stop(conf);
+       dmsg_volconf_stop(conf);
        return(NULL);
 }
 
 static
 void
-hammer2_volconf_stop(h2span_media_config_t *conf)
+dmsg_volconf_stop(h2span_media_config_t *conf)
 {
        switch(conf->state) {
        case H2MC_STOPPED:
@@ -1134,14 +1134,14 @@ hammer2_volconf_stop(h2span_media_config_t *conf)
 
 static
 void
-hammer2_volconf_start(h2span_media_config_t *conf, const char *hostname)
+dmsg_volconf_start(h2span_media_config_t *conf, const char *hostname)
 {
-       hammer2_master_service_info_t *info;
+       dmsg_master_service_info_t *info;
 
        switch(conf->state) {
        case H2MC_STOPPED:
        case H2MC_CONNECT:
-               conf->fd = hammer2_connect(hostname);
+               conf->fd = dmsg_connect(hostname);
                if (conf->fd < 0) {
                        fprintf(stderr, "Unable to connect to %s\n", hostname);
                        conf->state = H2MC_CONNECT;
@@ -1152,7 +1152,7 @@ hammer2_volconf_start(h2span_media_config_t *conf, const char *hostname)
                        info->detachme = 0;
                        conf->state = H2MC_RUNNING;
                        pthread_create(&conf->iocom_thread, NULL,
-                                      master_service, info);
+                                      dmsg_master_service, info);
                }
                break;
        case H2MC_RUNNING:
@@ -1166,7 +1166,7 @@ hammer2_volconf_start(h2span_media_config_t *conf, const char *hostname)
  *
  * Basically the idea here is to provide a stable data structure which
  * can be localized to the caller for higher level protocols to work with.
- * Depends on the context, these hammer2_handle's can be pooled by use-case
+ * Depends on the context, these dmsg_handle's can be pooled by use-case
  * and remain persistent through a client (or mount point's) life.
  */
 
@@ -1179,7 +1179,7 @@ hammer2_volconf_start(h2span_media_config_t *conf, const char *hostname)
  * pointer to it.
  */
 h2span_cluster_t *
-hammer2_cluster_get(uuid_t *pfs_clid)
+dmsg_cluster_get(uuid_t *pfs_clid)
 {
        h2span_cluster_t dummy_cls;
        h2span_cluster_t *cls;
@@ -1194,7 +1194,7 @@ hammer2_cluster_get(uuid_t *pfs_clid)
 }
 
 void
-hammer2_cluster_put(h2span_cluster_t *cls)
+dmsg_cluster_put(h2span_cluster_t *cls)
 {
        pthread_mutex_lock(&cluster_mtx);
        assert(cls->refs > 0);
@@ -1202,7 +1202,7 @@ hammer2_cluster_put(h2span_cluster_t *cls)
        if (RB_EMPTY(&cls->tree) && cls->refs == 0) {
                RB_REMOVE(h2span_cluster_tree,
                          &cluster_tree, cls);
-               hammer2_free(cls);
+               dmsg_free(cls);
        }
        pthread_mutex_unlock(&cluster_mtx);
 }
@@ -1210,11 +1210,11 @@ hammer2_cluster_put(h2span_cluster_t *cls)
 /*
  * Obtain a stable handle to a specific cluster node given its uuid.
  * This handle does NOT lock in the route to the node and is typically
- * used as part of the hammer2_handle_*() API to obtain a set of
+ * used as part of the dmsg_handle_*() API to obtain a set of
  * stable nodes.
  */
 h2span_node_t *
-hammer2_node_get(h2span_cluster_t *cls, uuid_t *pfs_fsid)
+dmsg_node_get(h2span_cluster_t *cls, uuid_t *pfs_fsid)
 {
 }
 
@@ -1226,8 +1226,8 @@ hammer2_node_get(h2span_cluster_t *cls, uuid_t *pfs_fsid)
  * Messages can be transacted via this structure while held.  If the route
  * is lost messages will return failure.
  */
-hammer2_router_t *
-hammer2_router_get(uuid_t *pfs_clid, uuid_t *pfs_fsid)
+dmsg_router_t *
+dmsg_router_get(uuid_t *pfs_clid, uuid_t *pfs_fsid)
 {
 }
 
@@ -1235,19 +1235,16 @@ hammer2_router_get(uuid_t *pfs_clid, uuid_t *pfs_fsid)
  * Release previously acquired router.
  */
 void
-hammer2_router_put(hammer2_router_t *router)
+dmsg_router_put(dmsg_router_t *router)
 {
 }
 #endif
 
-/************************************************************************
- *                             DEBUGGER                                *
- ************************************************************************/
 /*
  * Dumps the spanning tree
  */
 void
-shell_tree(hammer2_router_t *router, char *cmdbuf __unused)
+dmsg_shell_tree(dmsg_router_t *router, char *cmdbuf __unused)
 {
        h2span_cluster_t *cls;
        h2span_node_t *node;
@@ -1256,16 +1253,17 @@ shell_tree(hammer2_router_t *router, char *cmdbuf __unused)
 
        pthread_mutex_lock(&cluster_mtx);
        RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) {
-               router_printf(router, "Cluster %s\n",
-                            hammer2_uuid_to_str(&cls->pfs_clid, &uustr));
+               dmsg_router_printf(router, "Cluster %s\n",
+                                  dmsg_uuid_to_str(&cls->pfs_clid, &uustr));
                RB_FOREACH(node, h2span_node_tree, &cls->tree) {
-                       router_printf(router, "    Node %s (%s)\n",
-                                hammer2_uuid_to_str(&node->pfs_fsid, &uustr),
-                                node->label);
+                       dmsg_router_printf(router, "    Node %s (%s)\n",
+                               dmsg_uuid_to_str(&node->pfs_fsid, &uustr),
+                               node->label);
                        RB_FOREACH(slink, h2span_link_tree, &node->tree) {
-                               router_printf(router, "\tLink dist=%d via %d\n",
-                                            slink->dist,
-                                            slink->state->iocom->sock_fd);
+                               dmsg_router_printf(router,
+                                           "\tLink dist=%d via %d\n",
+                                           slink->dist,
+                                           slink->state->iocom->sock_fd);
                        }
                }
        }
diff --git a/lib/libdmsg/service.c b/lib/libdmsg/service.c
new file mode 100644 (file)
index 0000000..95b34a0
--- /dev/null
@@ -0,0 +1,237 @@
+/*
+ * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Matthew Dillon <dillon@dragonflybsd.org>
+ * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "dmsg_local.h"
+
+static void master_auth_signal(dmsg_router_t *router);
+static void master_auth_rxmsg(dmsg_msg_t *msg);
+static void master_link_signal(dmsg_router_t *router);
+static void master_link_rxmsg(dmsg_msg_t *msg);
+
+/*
+ * Service an accepted connection (runs as a pthread)
+ *
+ * (also called from a couple of other places)
+ */
+void *
+dmsg_master_service(void *data)
+{
+       dmsg_master_service_info_t *info = data;
+       dmsg_iocom_t iocom;
+
+       if (info->detachme)
+               pthread_detach(pthread_self());
+
+       dmsg_iocom_init(&iocom, info->fd, -1,
+                          master_auth_signal,
+                          master_auth_rxmsg,
+                          NULL);
+       dmsg_iocom_core(&iocom);
+
+       fprintf(stderr,
+               "iocom on fd %d terminated error rx=%d, tx=%d\n",
+               info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
+       close(info->fd);
+       info->fd = -1;  /* safety */
+       free(info);
+
+       return (NULL);
+}
+
+/************************************************************************
+ *                         AUTHENTICATION                              *
+ ************************************************************************
+ *
+ * Callback via dmsg_iocom_core().
+ *
+ * Additional messaging-based authentication must occur before normal
+ * message operation.  The connection has already been encrypted at
+ * this point.
+ */
+static void master_auth_conn_rx(dmsg_msg_t *msg);
+
+static
+void
+master_auth_signal(dmsg_router_t *router)
+{
+       dmsg_msg_t *msg;
+
+       /*
+        * Transmit LNK_CONN, enabling the SPAN protocol if both sides
+        * agree.
+        *
+        * XXX put additional authentication states here?
+        */
+       msg = dmsg_msg_alloc(router, 0, DMSG_LNK_CONN | DMSGF_CREATE,
+                            master_auth_conn_rx, NULL);
+       msg->any.lnk_conn.peer_mask = (uint64_t)-1;
+       msg->any.lnk_conn.peer_type = DMSG_PEER_CLUSTER;
+
+       dmsg_msg_write(msg);
+
+       dmsg_router_restate(router,
+                           master_link_signal,
+                           master_link_rxmsg,
+                           NULL);
+}
+
+static
+void
+master_auth_conn_rx(dmsg_msg_t *msg)
+{
+       if (msg->any.head.cmd & DMSGF_DELETE)
+               dmsg_msg_reply(msg, 0);
+}
+
+static
+void
+master_auth_rxmsg(dmsg_msg_t *msg __unused)
+{
+}
+
+/************************************************************************
+ *                     POST-AUTHENTICATION SERVICE MSGS                *
+ ************************************************************************
+ *
+ * Callback via dmsg_iocom_core().
+ */
+static
+void
+master_link_signal(dmsg_router_t *router)
+{
+       dmsg_msg_lnk_signal(router);
+}
+
+static
+void
+master_link_rxmsg(dmsg_msg_t *msg)
+{
+       dmsg_state_t *state;
+       uint32_t cmd;
+
+       /*
+        * If the message state has a function established we just
+        * call the function, otherwise we call the appropriate
+        * link-level protocol related to the original command and
+        * let it sort it out.
+        *
+        * Non-transactional one-off messages, on the otherhand,
+        * might have REPLY set.
+        */
+       state = msg->state;
+       cmd = state ? state->msg->any.head.cmd : msg->any.head.cmd;
+
+       fprintf(stderr, "service-receive: %s\n", dmsg_msg_str(msg));
+
+       if (state && state->func) {
+               assert(state->func != NULL);
+               state->func(msg);
+       } else {
+               switch(cmd & DMSGF_PROTOS) {
+               case DMSG_PROTO_LNK:
+                       dmsg_msg_lnk(msg);
+                       break;
+               case DMSG_PROTO_DBG:
+                       dmsg_msg_dbg(msg);
+                       break;
+               default:
+                       dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
+                       break;
+               }
+       }
+}
+
+/*
+ * This is called from the master node to process a received debug
+ * shell command.  We process the command, outputting the results,
+ * then finish up by outputting another prompt.
+ */
+void
+dmsg_msg_dbg(dmsg_msg_t *msg)
+{
+       switch(msg->any.head.cmd & DMSGF_CMDSWMASK) {
+       case DMSG_DBG_SHELL:
+               /*
+                * This is a command which we must process.
+                * When we are finished we generate a final reply.
+                */
+               if (msg->aux_data)
+                       msg->aux_data[msg->aux_size - 1] = 0;
+               msg->router->dbgmsg_callback(msg);
+               dmsg_msg_reply(msg, 0);
+               break;
+       case DMSG_DBG_SHELL | DMSGF_REPLY:
+               /*
+                * A reply just prints out the string.  No newline is added
+                * (it is expected to be embedded if desired).
+                */
+               if (msg->aux_data)
+                       msg->aux_data[msg->aux_size - 1] = 0;
+               if (msg->aux_data)
+                       write(2, msg->aux_data, strlen(msg->aux_data));
+               break;
+       default:
+               dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
+               break;
+       }
+}
+
+/*
+ * Returns text debug output to the original defined by (msg).  (msg) is
+ * not modified and stays intact.  We use a one-way message with REPLY set
+ * to distinguish between a debug command and debug terminal output.
+ *
+ * To prevent loops router_printf() can filter the message (cmd) related
+ * to the router_printf().  We filter out DBG messages.
+ */
+void
+dmsg_router_printf(dmsg_router_t *router, const char *ctl, ...)
+{
+       dmsg_msg_t *rmsg;
+       va_list va;
+       char buf[1024];
+       size_t len;
+
+       va_start(va, ctl);
+       vsnprintf(buf, sizeof(buf), ctl, va);
+       va_end(va);
+       len = strlen(buf) + 1;
+
+       rmsg = dmsg_msg_alloc(router, len, DMSG_DBG_SHELL | DMSGF_REPLY,
+                                NULL, NULL);
+       bcopy(buf, rmsg->aux_data, len);
+
+       dmsg_msg_write(rmsg);
+}
similarity index 54%
copy from sys/vfs/hammer2/hammer2_mount.h
copy to lib/libdmsg/subs.c
index d10ae8b..fc744d4 100644 (file)
@@ -2,7 +2,8 @@
  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
  *
  * This code is derived from software contributed to The DragonFly Project
- * by Matthew Dillon <dillon@backplane.com>
+ * by Matthew Dillon <dillon@dragonflybsd.org>
+ * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SUCH DAMAGE.
  */
 
-#ifndef _VFS_HAMMER2_MOUNT_H_
-#define _VFS_HAMMER2_MOUNT_H_
+#include "dmsg_local.h"
 
 /*
- * This structure is passed from userland to the kernel during the mount
- * system call.
- *
- * The volume name is formatted as '/dev/ad0s1a@LABEL', where the label is
- * the mount point under the super-root.
+ * Allocation wrappers give us shims for possible future use
  */
-struct hammer2_mount_info {
-       const char      *volume;
-       int             hflags;         /* extended hammer mount flags */
-       int             cluster_fd;     /* cluster management pipe/socket */
-       char            reserved1[112];
-};
+void *
+dmsg_alloc(size_t bytes)
+{
+       void *ptr;
+
+       ptr = malloc(bytes);
+       assert(ptr);
+       bzero(ptr, bytes);
+       return (ptr);
+}
+
+void
+dmsg_free(void *ptr)
+{
+       free(ptr);
+}
+
+const char *
+dmsg_uuid_to_str(uuid_t *uuid, char **strp)
+{
+       uint32_t status;
+       if (*strp) {
+               free(*strp);
+               *strp = NULL;
+       }
+       uuid_to_string(uuid, strp, &status);
+       return (*strp);
+}
 
-#define HMNT2_NOAUTOSNAP       0x00000001
+int
+dmsg_connect(const char *hostname)
+{
+       struct sockaddr_in lsin;
+       struct hostent *hen;
+       int fd;
 
-#define HMNT2_USERFLAGS                (HMNT2_NOAUTOSNAP)
+       /*
+        * Acquire socket and set options
+        */
+       if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+               fprintf(stderr, "cmd_debug: socket(): %s\n",
+                       strerror(errno));
+               return -1;
+       }
 
-#define HAMMER2_LISTEN_PORT    987
+       /*
+        * Connect to the target
+        */
+       bzero(&lsin, sizeof(lsin));
+       lsin.sin_family = AF_INET;
+       lsin.sin_addr.s_addr = 0;
+       lsin.sin_port = htons(DMSG_LISTEN_PORT);
 
-#endif
+       if (hostname) {
+               hen = gethostbyname2(hostname, AF_INET);
+               if (hen == NULL) {
+                       if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
+                               fprintf(stderr,
+                                       "Cannot resolve %s\n", hostname);
+                               return -1;
+                       }
+               } else {
+                       bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
+               }
+       }
+       if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
+               close(fd);
+               fprintf(stderr, "debug: connect failed: %s\n",
+                       strerror(errno));
+               return -1;
+       }
+       return (fd);
+}
index 3de282d..6935c5f 100644 (file)
@@ -1,17 +1,16 @@
 PROG=  hammer2
-SRCS=  main.c subs.c icrc.c crypto.c
+SRCS=  main.c subs.c
 SRCS+= cmd_remote.c cmd_snapshot.c cmd_pfs.c
 SRCS+= cmd_service.c cmd_leaf.c cmd_debug.c
 SRCS+= cmd_rsa.c cmd_stat.c
-SRCS+= msg.c msg_lnk.c
 #MAN=  hammer2.8
 NOMAN= TRUE
 DEBUG_FLAGS=-g
 
 CFLAGS+= -I${.CURDIR}/../../sys
 CFLAGS+= -pthread
-LDADD= -lm -lutil -lmd -lcrypto
-DPADD= ${LIBM} ${LIBUTIL} ${LIBMD} ${LIBCRYPTO}
+LDADD= -ldmsg -lm -lutil -lmd -lcrypto
+DPADD= ${LIBDMSG} ${LIBM} ${LIBUTIL} ${LIBMD} ${LIBCRYPTO}
 
 #.PATH: ${.CURDIR}/../../sys/libkern
 #SRCS+= crc32.c
index bc3f16f..51f9555 100644 (file)
@@ -37,9 +37,9 @@
 
 #define SHOW_TAB       2
 
-static void shell_rcvmsg(hammer2_msg_t *msg);
-static void shell_ttymsg(hammer2_iocom_t *iocom);
-static void hammer2_shell_parse(hammer2_msg_t *msg);
+static void shell_rcvmsg(dmsg_msg_t *msg);
+static void shell_ttymsg(dmsg_iocom_t *iocom);
+static void hammer2_shell_parse(dmsg_msg_t *msg);
 
 /************************************************************************
  *                                 SHELL                               *
@@ -48,39 +48,40 @@ static void hammer2_shell_parse(hammer2_msg_t *msg);
 int
 cmd_shell(const char *hostname)
 {
-       struct hammer2_iocom iocom;
-       hammer2_msg_t *msg;
+       struct dmsg_iocom iocom;
+       dmsg_msg_t *msg;
        int fd;
 
        /*
         * Connect to the target
         */
-       fd = hammer2_connect(hostname);
+       fd = dmsg_connect(hostname);
        if (fd < 0)
                return 1;
 
        /*
         * Run the session.  The remote end transmits our prompt.
         */
-       hammer2_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
+       dmsg_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
+       iocom.router->dbgmsg_callback = hammer2_shell_parse;
        fcntl(0, F_SETFL, O_NONBLOCK);
        printf("debug: connected\n");
 
-       msg = hammer2_msg_alloc(iocom.router, 0, DMSG_DBG_SHELL, NULL, NULL);
-       hammer2_msg_write(msg);
-       hammer2_iocom_core(&iocom);
+       msg = dmsg_msg_alloc(iocom.router, 0, DMSG_DBG_SHELL, NULL, NULL);
+       dmsg_msg_write(msg);
+       dmsg_iocom_core(&iocom);
        fprintf(stderr, "debug: disconnected\n");
        close(fd);
        return 0;
 }
 
 /*
- * Callback from hammer2_iocom_core() when messages might be present
+ * Callback from dmsg_iocom_core() when messages might be present
  * on the socket.
  */
 static
 void
-shell_rcvmsg(hammer2_msg_t *msg)
+shell_rcvmsg(dmsg_msg_t *msg)
 {
        switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
        case DMSG_LNK_ERROR:
@@ -92,7 +93,7 @@ shell_rcvmsg(hammer2_msg_t *msg)
                 */
                if (msg->any.head.error) {
                        fprintf(stderr, "Stream failure: %s\n",
-                               hammer2_msg_str(msg));
+                               dmsg_msg_str(msg));
                } else {
                        write(1, "debug> ", 7);
                }
@@ -105,7 +106,7 @@ shell_rcvmsg(hammer2_msg_t *msg)
                 * We send the commands, not accept them.
                 * (one-way message, not transactional)
                 */
-               hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
+               dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
                break;
        case DMSG_DBG_SHELL | DMSGF_REPLY:
                /*
@@ -119,7 +120,7 @@ shell_rcvmsg(hammer2_msg_t *msg)
                break;
        case DMSG_LNK_CONN | DMSGF_CREATE:
                fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
-               hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
+               dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
                break;
        case DMSG_LNK_CONN | DMSGF_DELETE:
                break;
@@ -128,20 +129,20 @@ shell_rcvmsg(hammer2_msg_t *msg)
                 * Ignore any unknown messages, Terminate any unknown
                 * transactions with an error.
                 */
-               fprintf(stderr, "Unknown message: %s\n", hammer2_msg_str(msg));
+               fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
                if (msg->any.head.cmd & DMSGF_CREATE)
-                       hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
+                       dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
                if (msg->any.head.cmd & DMSGF_DELETE)
-                       hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
+                       dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
                break;
        }
 }
 
 static
 void
-shell_ttymsg(hammer2_iocom_t *iocom)
+shell_ttymsg(dmsg_iocom_t *iocom)
 {
-       hammer2_msg_t *msg;
+       dmsg_msg_t *msg;
        char buf[256];
        size_t len;
 
@@ -150,63 +151,27 @@ shell_ttymsg(hammer2_iocom_t *iocom)
                if (len && buf[len - 1] == '\n')
                        buf[--len] = 0;
                ++len;
-               msg = hammer2_msg_alloc(iocom->router, len, DMSG_DBG_SHELL,
-                                       NULL, NULL);
+               msg = dmsg_msg_alloc(iocom->router, len, DMSG_DBG_SHELL,
+                                    NULL, NULL);
                bcopy(buf, msg->aux_data, len);
-               hammer2_msg_write(msg);
+               dmsg_msg_write(msg);
        } else if (feof(stdin)) {
                /*
                 * Set EOF flag without setting any error code for normal
                 * EOF.
                 */
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
+               iocom->flags |= DMSG_IOCOMF_EOF;
        } else {
                clearerr(stdin);
        }
 }
 
-/*
- * This is called from the master node to process a received debug
- * shell command.  We process the command, outputting the results,
- * then finish up by outputting another prompt.
- */
-void
-hammer2_msg_dbg(hammer2_msg_t *msg)
-{
-       switch(msg->any.head.cmd & DMSGF_CMDSWMASK) {
-       case DMSG_DBG_SHELL:
-               /*
-                * This is a command which we must process.
-                * When we are finished we generate a final reply.
-                */
-               if (msg->aux_data)
-                       msg->aux_data[msg->aux_size - 1] = 0;
-               hammer2_shell_parse(msg);
-               hammer2_msg_reply(msg, 0);
-               break;
-       case DMSG_DBG_SHELL | DMSGF_REPLY:
-               /*
-                * A reply just prints out the string.  No newline is added
-                * (it is expected to be embedded if desired).
-                */
-               if (msg->aux_data)
-                       msg->aux_data[msg->aux_size - 1] = 0;
-               if (msg->aux_data)
-                       write(2, msg->aux_data, strlen(msg->aux_data));
-               break;
-       default:
-               hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
-               break;
-       }
-}
-
-static void shell_span(hammer2_router_t *router, char *cmdbuf);
-/*static void shell_tree(hammer2_router_t *router, char *cmdbuf);*/
+static void shell_span(dmsg_router_t *router, char *cmdbuf);
 
 static void
-hammer2_shell_parse(hammer2_msg_t *msg)
+hammer2_shell_parse(dmsg_msg_t *msg)
 {
-       hammer2_router_t *router = msg->router;
+       dmsg_router_t *router = msg->router;
        char *cmdbuf = msg->aux_data;
        char *cmd = strsep(&cmdbuf, " \t");
 
@@ -215,18 +180,18 @@ hammer2_shell_parse(hammer2_msg_t *msg)
        } else if (strcmp(cmd, "span") == 0) {
                shell_span(router, cmdbuf);
        } else if (strcmp(cmd, "tree") == 0) {
-               shell_tree(router, cmdbuf);
+               dmsg_shell_tree(router, cmdbuf); /* dump spanning tree */
        } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
-               router_printf(router, "help            Command help\n");
-               router_printf(router, "span <host>     Span to target host\n");
-               router_printf(router, "tree            Dump spanning tree\n");
+               dmsg_router_printf(router, "help            Command help\n");
+               dmsg_router_printf(router, "span <host>     Span to target host\n");
+               dmsg_router_printf(router, "tree            Dump spanning tree\n");
        } else {
-               router_printf(router, "Unrecognized command: %s\n", cmd);
+               dmsg_router_printf(router, "Unrecognized command: %s\n", cmd);
        }
 }
 
 static void
-shell_span(hammer2_router_t *router, char *cmdbuf)
+shell_span(dmsg_router_t *router, char *cmdbuf)
 {
        const char *hostname = strsep(&cmdbuf, " \t");
        pthread_t thread;
@@ -238,50 +203,22 @@ shell_span(hammer2_router_t *router, char *cmdbuf)
        if (hostname == NULL) {
                fd = -1;
        } else {
-               fd = hammer2_connect(hostname);
+               fd = dmsg_connect(hostname);
        }
 
        /*
         * Start master service
         */
        if (fd < 0) {
-               router_printf(router, "Connection to %s failed\n", hostname);
+               dmsg_router_printf(router, "Connection to %s failed\n", hostname);
        } else {
-               router_printf(router, "Connected to %s\n", hostname);
+               dmsg_router_printf(router, "Connected to %s\n", hostname);
                pthread_create(&thread, NULL,
-                              master_service, (void *)(intptr_t)fd);
+                              dmsg_master_service, (void *)(intptr_t)fd);
                /*pthread_join(thread, &res);*/
        }
 }
 
-/*
- * Returns text debug output to the original defined by (msg).  (msg) is
- * not modified and stays intact.  We use a one-way message with REPLY set
- * to distinguish between a debug command and debug terminal output.
- *
- * To prevent loops router_printf() can filter the message (cmd) related
- * to the router_printf().  We filter out DBG messages.
- */
-void
-router_printf(hammer2_router_t *router, const char *ctl, ...)
-{
-       hammer2_msg_t *rmsg;
-       va_list va;
-       char buf[1024];
-       size_t len;
-
-       va_start(va, ctl);
-       vsnprintf(buf, sizeof(buf), ctl, va);
-       va_end(va);
-       len = strlen(buf) + 1;
-
-       rmsg = hammer2_msg_alloc(router, len, DMSG_DBG_SHELL | DMSGF_REPLY,
-                                NULL, NULL);
-       bcopy(buf, rmsg->aux_data, len);
-
-       hammer2_msg_write(rmsg);
-}
-
 /************************************************************************
  *                             DEBUGSPAN                               *
  ************************************************************************
@@ -299,12 +236,13 @@ cmd_debugspan(const char *hostname)
        /*
         * Connect to the target
         */
-       fd = hammer2_connect(hostname);
+       fd = dmsg_connect(hostname);
        if (fd < 0)
                return 1;
 
        printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
-       pthread_create(&thread, NULL, master_service, (void *)(intptr_t)fd);
+       pthread_create(&thread, NULL,
+                      dmsg_master_service, (void *)(intptr_t)fd);
        pthread_join(thread, &res);
        return(0);
 }
index 75d3893..e99c5ea 100644 (file)
 
 #include "hammer2.h"
 
+#include <openssl/rsa.h>       /* public/private key functions */
+#include <openssl/pem.h>       /* public/private key file load */
+#include <openssl/err.h>
+
 /*
  * Should be run as root.  Creates /etc/hammer2/rsa.{pub,prv} using
  * an openssl command.
index f387b2e..10b8223 100644 (file)
 
 static void *service_thread(void *data);
 static void *udev_thread(void *data);
-static void master_auth_signal(hammer2_router_t *router);
-static void master_auth_rxmsg(hammer2_msg_t *msg);
-static void master_link_signal(hammer2_router_t *router);
-static void master_link_rxmsg(hammer2_msg_t *msg);
 static void master_reconnect(const char *mntpt);
 static void udev_check_disks(void);
 
@@ -87,7 +83,7 @@ cmd_service(void)
        bzero(&lsin, sizeof(lsin));
        lsin.sin_family = AF_INET;
        lsin.sin_addr.s_addr = INADDR_ANY;
-       lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
+       lsin.sin_port = htons(DMSG_LISTEN_PORT);
        if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
                close(lfd);
                if (QuietOpt == 0) {
@@ -125,7 +121,7 @@ service_thread(void *data)
        struct sockaddr_in asin;
        socklen_t alen;
        pthread_t thread;
-       hammer2_master_service_info_t *info;
+       dmsg_master_service_info_t *info;
        int lfd = (int)(intptr_t)data;
        int fd;
        int i;
@@ -173,7 +169,7 @@ service_thread(void *data)
                bzero(info, sizeof(*info));
                info->fd = fd;
                info->detachme = 1;
-               pthread_create(&thread, NULL, master_service, info);
+               pthread_create(&thread, NULL, dmsg_master_service, info);
        }
        return (NULL);
 }
@@ -262,7 +258,7 @@ void
 master_reconnect(const char *mntpt)
 {
        struct hammer2_ioc_recluster recls;
-       hammer2_master_service_info_t *info;
+       dmsg_master_service_info_t *info;
        pthread_t thread;
        int fd;
        int pipefds[2];
@@ -293,139 +289,5 @@ master_reconnect(const char *mntpt)
        bzero(info, sizeof(*info));
        info->fd = pipefds[1];
        info->detachme = 1;
-       pthread_create(&thread, NULL, master_service, info);
-}
-
-/*
- * Service an accepted connection (runs as a pthread)
- *
- * (also called from a couple of other places)
- */
-void *
-master_service(void *data)
-{
-       hammer2_master_service_info_t *info = data;
-       hammer2_iocom_t iocom;
-
-       if (info->detachme)
-               pthread_detach(pthread_self());
-
-       hammer2_iocom_init(&iocom, info->fd, -1,
-                          master_auth_signal,
-                          master_auth_rxmsg,
-                          NULL);
-       hammer2_iocom_core(&iocom);
-
-       fprintf(stderr,
-               "iocom on fd %d terminated error rx=%d, tx=%d\n",
-               info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
-       close(info->fd);
-       info->fd = -1;  /* safety */
-       free(info);
-
-       return (NULL);
-}
-
-/************************************************************************
- *                         AUTHENTICATION                              *
- ************************************************************************
- *
- * Callback via hammer2_iocom_core().
- *
- * Additional messaging-based authentication must occur before normal
- * message operation.  The connection has already been encrypted at
- * this point.
- */
-static void master_auth_conn_rx(hammer2_msg_t *msg);
-
-static
-void
-master_auth_signal(hammer2_router_t *router)
-{
-       hammer2_msg_t *msg;
-
-       /*
-        * Transmit LNK_CONN, enabling the SPAN protocol if both sides
-        * agree.
-        *
-        * XXX put additional authentication states here?
-        */
-       msg = hammer2_msg_alloc(router, 0, DMSG_LNK_CONN |
-                                          DMSGF_CREATE,
-                               master_auth_conn_rx, NULL);
-       msg->any.lnk_conn.peer_mask = (uint64_t)-1;
-       msg->any.lnk_conn.peer_type = HAMMER2_PEER_CLUSTER;
-
-       hammer2_msg_write(msg);
-
-       hammer2_router_restate(router,
-                             master_link_signal,
-                             master_link_rxmsg,
-                             NULL);
-}
-
-static
-void
-master_auth_conn_rx(hammer2_msg_t *msg)
-{
-       if (msg->any.head.cmd & DMSGF_DELETE)
-               hammer2_msg_reply(msg, 0);
-}
-
-static
-void
-master_auth_rxmsg(hammer2_msg_t *msg __unused)
-{
-}
-
-/************************************************************************
- *                     POST-AUTHENTICATION SERVICE MSGS                *
- ************************************************************************
- *
- * Callback via hammer2_iocom_core().
- */
-static
-void
-master_link_signal(hammer2_router_t *router)
-{
-       hammer2_msg_lnk_signal(router);
-}
-
-static
-void
-master_link_rxmsg(hammer2_msg_t *msg)
-{
-       hammer2_state_t *state;
-       uint32_t cmd;
-
-       /*
-        * If the message state has a function established we just
-        * call the function, otherwise we call the appropriate
-        * link-level protocol related to the original command and
-        * let it sort it out.
-        *
-        * Non-transactional one-off messages, on the otherhand,
-        * might have REPLY set.
-        */
-       state = msg->state;
-       cmd = state ? state->msg->any.head.cmd : msg->any.head.cmd;
-
-       fprintf(stderr, "service-receive: %s\n", hammer2_msg_str(msg));
-
-       if (state && state->func) {
-               assert(state->func != NULL);
-               state->func(msg);
-       } else {
-               switch(cmd & DMSGF_PROTOS) {
-               case DMSG_PROTO_LNK:
-                       hammer2_msg_lnk(msg);
-                       break;
-               case DMSG_PROTO_DBG:
-                       hammer2_msg_dbg(msg);
-                       break;
-               default:
-                       hammer2_msg_reply(msg, DMSG_ERR_NOSUPP);
-                       break;
-               }
-       }
+       pthread_create(&thread, NULL, dmsg_master_service, info);
 }
index 399f53f..543abc6 100644 (file)
@@ -47,7 +47,7 @@
 #include <sys/endian.h>
 #include <sys/sysctl.h>
 #include <sys/udev.h>
-#include <sys/dmsg.h>
+#include <dmsg.h>
 
 #include <netinet/in.h>
 #include <netinet/tcp.h>
@@ -76,8 +76,6 @@
 
 #include <libutil.h>
 
-#include "network.h"
-
 #define HAMMER2_DEFAULT_DIR    "/etc/hammer2"
 #define HAMMER2_PATH_REMOTE    HAMMER2_DEFAULT_DIR "/remote"
 #ifndef UDEV_DEVICE_PATH
@@ -92,13 +90,6 @@ struct hammer2_idmap {
 
 typedef struct hammer2_idmap hammer2_idmap_t;
 
-struct hammer2_master_service_info {
-       int     fd;
-       int     detachme;
-};
-
-typedef struct hammer2_master_service_info hammer2_master_service_info_t;
-
 extern int DebugOpt;
 extern int VerboseOpt;
 extern int QuietOpt;
@@ -130,62 +121,6 @@ int cmd_rsainit(const char *dir_path);
 int cmd_rsaenc(const char **keys, int nkeys);
 int cmd_rsadec(const char **keys, int nkeys);
 
-/*
- * Msg support functions
- */
-void hammer2_bswap_head(dmsg_hdr_t *head);
-void hammer2_ioq_init(hammer2_iocom_t *iocom, hammer2_ioq_t *ioq);
-void hammer2_ioq_done(hammer2_iocom_t *iocom, hammer2_ioq_t *ioq);
-void hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
-                       void (*state_func)(hammer2_router_t *),
-                       void (*rcvmsg_func)(hammer2_msg_t *),
-                       void (*altmsg_func)(hammer2_iocom_t *));
-void hammer2_router_restate(hammer2_router_t *router,
-                       void (*state_func)(hammer2_router_t *),
-                       void (*rcvmsg_func)(hammer2_msg_t *),
-                       void (*altmsg_func)(hammer2_iocom_t *));
-void hammer2_router_signal(hammer2_router_t *router);
-void hammer2_iocom_done(hammer2_iocom_t *iocom);
-hammer2_msg_t *hammer2_msg_alloc(hammer2_router_t *router,
-                       size_t aux_size, uint32_t cmd,
-                       void (*func)(hammer2_msg_t *), void *data);
-void hammer2_msg_reply(hammer2_msg_t *msg, uint32_t error);
-void hammer2_msg_result(hammer2_msg_t *msg, uint32_t error);
-void hammer2_state_reply(hammer2_state_t *state, uint32_t error);
-
-void hammer2_msg_free(hammer2_msg_t *msg);
-
-void hammer2_iocom_core(hammer2_iocom_t *iocom);
-hammer2_msg_t *hammer2_ioq_read(hammer2_iocom_t *iocom);
-void hammer2_msg_write(hammer2_msg_t *msg);
-
-void hammer2_iocom_drain(hammer2_iocom_t *iocom);
-void hammer2_iocom_flush1(hammer2_iocom_t *iocom);
-void hammer2_iocom_flush2(hammer2_iocom_t *iocom);
-
-void hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
-void hammer2_state_free(hammer2_state_t *state);
-
-hammer2_router_t *hammer2_router_alloc(void);
-void hammer2_router_connect(hammer2_router_t *router);
-void hammer2_router_disconnect(hammer2_router_t **routerp);
-
-/*
- * Msg protocol functions
- */
-void hammer2_msg_lnk_signal(hammer2_router_t *router);
-void hammer2_msg_lnk(hammer2_msg_t *msg);
-void hammer2_msg_dbg(hammer2_msg_t *msg);
-
-/*
- * Crypto functions
- */
-void hammer2_crypto_setup(void);
-void hammer2_crypto_negotiate(hammer2_iocom_t *iocom);
-void hammer2_crypto_decrypt(hammer2_iocom_t *iocom, hammer2_ioq_t *ioq);
-int hammer2_crypto_encrypt(hammer2_iocom_t *iocom, hammer2_ioq_t *ioq,
-                       struct iovec *iov, int n, size_t *nactp);
-
 /*
  * Misc functions
  */
@@ -194,15 +129,6 @@ const char *hammer2_uuid_to_str(uuid_t *uuid, char **strp);
 const char *hammer2_iptype_to_str(uint8_t type);
 const char *hammer2_pfstype_to_str(uint8_t type);
 const char *sizetostr(hammer2_off_t size);
-const char * hammer2_basecmd_str(uint32_t cmd);
-const char *hammer2_msg_str(hammer2_msg_t *msg);
-int hammer2_connect(const char *hostname);
-
-void shell_tree(hammer2_router_t *router, char *cmdbuf);
-
-void *master_service(void *data);
 
-void hammer2_msg_debug(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
-void router_printf(hammer2_router_t *router, const char *ctl, ...);
-void *hammer2_alloc(size_t bytes);
-void hammer2_free(void *ptr);
+uint32_t hammer2_icrc32(const void *buf, size_t size);
+uint32_t hammer2_icrc32c(const void *buf, size_t size, uint32_t crc);
index 1172c8d..e97a5b9 100644 (file)
@@ -55,7 +55,7 @@ main(int ac, char **av)
 
        srandomdev();
        signal(SIGPIPE, SIG_IGN);
-       hammer2_crypto_setup();
+       dmsg_crypto_setup();
 
        /*
         * Core options
diff --git a/sbin/hammer2/network.h b/sbin/hammer2/network.h
deleted file mode 100644 (file)
index 2f3bbda..0000000
+++ /dev/null
@@ -1,306 +0,0 @@
-/*
- * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
- *
- * This code is derived from software contributed to The DragonFly Project
- * by Matthew Dillon <dillon@dragonflybsd.org>
- * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name of The DragonFly Project nor the names of its
- *    contributors may be used to endorse or promote products derived
- *    from this software without specific, prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
- * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <openssl/rsa.h>       /* public/private key functions */
-#include <openssl/pem.h>       /* public/private key file load */
-#include <openssl/err.h>
-#include <openssl/evp.h>       /* aes_256_cbc functions */
-
-/***************************************************************************
- *                             CRYPTO HANDSHAKE                           *
- ***************************************************************************
- *
- * The initial public-key exchange is implementing by transmitting a
- * 512-byte buffer to the other side in a symmetrical fashion.  This
- * buffer contains the following:
- *
- * (1) A random session key.  512 bits is specified.  We use aes_256_cbc()
- *     and initialize the key with the first 256 bits and the iv[] with
- *     the second.  Note that the transmitted and received session
- *     keys are XOR'd together to create the session key used for
- *     communications (so even if the verifier is compromised the session
- *     will still be gobbly gook if the public key has not been completely
- *     broken).
- *
- * (2) A verifier to determine that the decode was successful.  It encodes
- *     an XOR of each group of 4 bytes from the session key.
- *
- * (3) Additional configuration and additional random data.
- *
- *     - The hammer2 message header magic for endian detect
- *
- *     - The hammer2 protocol version.  The two sides agree on the
- *      smaller of the two.
- *
- *     - All unused fields (junk*) are filled with random data.
- *
- * This structure must be exactly 512 bytes and expects to use 256-byte
- * RSA keys.
- */
-struct hammer2_handshake {
-       char pad1[8];           /* 000 */
-       uint16_t magic;         /* 008 DMSG_HDR_MAGIC for endian detect */
-       uint16_t version;       /* 00A hammer2 protocol version */
-       uint32_t flags;         /* 00C protocol extension flags */
-       uint8_t sess[64];       /* 010 512-bit session key */
-       uint8_t verf[16];       /* 050 verifier = ~sess */
-       char quickmsg[32];      /* 060 reason for connecting */
-       char junk080[128];      /* 080-0FF */
-       char pad2[8];           /* 100-107 */
-       char junk100[256-8];    /* 108-1FF */
-};
-
-typedef struct hammer2_handshake hammer2_handshake_t;
-
-
-#define HAMMER2_CRYPTO_CHUNK_SIZE              DMSG_ALIGN
-#define HAMMER2_MAX_IV_SIZE                    32
-
-#define HAMMER2_CRYPTO_GCM_IV_FIXED_SIZE       4
-#define HAMMER2_CRYPTO_GCM_IV_SIZE             12
-#define HAMMER2_CRYPTO_GCM_KEY_SIZE            32
-#define HAMMER2_CRYPTO_GCM_TAG_SIZE            16
-
-#define HAMMER2_CRYPTO_ALGO_GCM_IDX            0
-
-#define HAMMER2_CRYPTO_ALGO                    HAMMER2_CRYPTO_ALGO_GCM_IDX
-
-
-
-
-/***************************************************************************
- *                             LOW LEVEL MESSAGING                        *
- ***************************************************************************
- *
- * hammer2_msg - A standalone copy of a message, typically referenced by
- *              or embedded in other structures, or used with I/O queues.
- *
- * These structures are strictly temporary, so they do not have to be
- * particularly optimized for size.  All possible message headers are
- * directly embedded (any), and the message may contain a reference
- * to allocated auxillary data.  The structure is recycled quite often
- * by a connection.
- *
- * This structure is typically not used for storing persistent message
- * state (see hammer2_persist for that).
- */
-struct hammer2_iocom;
-struct hammer2_persist;
-struct hammer2_state;
-struct hammer2_router;
-struct hammer2_msg;
-
-TAILQ_HEAD(hammer2_state_queue, hammer2_state);
-TAILQ_HEAD(hammer2_msg_queue, hammer2_msg);
-RB_HEAD(hammer2_state_tree, hammer2_state);
-RB_HEAD(hammer2_router_tree, hammer2_router);
-
-struct h2span_link;
-struct h2span_relay;
-struct h2span_conn;
-
-struct hammer2_state {
-       RB_ENTRY(hammer2_state) rbnode;         /* indexed by msgid */
-       struct hammer2_iocom *iocom;
-       struct hammer2_router *router;          /* if routed */
-       uint32_t        txcmd;                  /* mostly for CMDF flags */
-       uint32_t        rxcmd;                  /* mostly for CMDF flags */
-       uint64_t        msgid;                  /* {spanid,msgid} uniq */
-       int             flags;
-       int             error;
-       struct hammer2_msg *msg;
-       void (*func)(struct hammer2_msg *);
-       union {
-               void *any;
-               struct h2span_link *link;
-               struct h2span_conn *conn;
-               struct h2span_relay *relay;
-       } any;
-};
-
-#define HAMMER2_STATE_INSERTED 0x0001
-#define HAMMER2_STATE_DYNAMIC  0x0002
-#define HAMMER2_STATE_NODEID   0x0004          /* manages a node id */
-
-struct hammer2_msg {
-       TAILQ_ENTRY(hammer2_msg) qentry;
-       struct hammer2_router *router;
-       struct hammer2_state *state;
-       size_t          hdr_size;
-       size_t          aux_size;
-       char            *aux_data;
-       dmsg_any_t      any;
-};
-
-typedef struct hammer2_state hammer2_state_t;
-typedef struct hammer2_msg hammer2_msg_t;
-typedef struct hammer2_msg_queue hammer2_msg_queue_t;
-
-int hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2);
-RB_PROTOTYPE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
-
-/*
- * hammer2_ioq - An embedded component of hammer2_conn, holds state
- * for the buffering and parsing of incoming and outgoing messages.
- *
- * cdx - beg  - processed buffer data, encrypted or decrypted
- * end - cdn  - unprocessed buffer data not yet encrypted or decrypted
- */
-struct hammer2_ioq {
-       enum { HAMMER2_MSGQ_STATE_HEADER1,
-              HAMMER2_MSGQ_STATE_HEADER2,
-              HAMMER2_MSGQ_STATE_AUXDATA1,
-              HAMMER2_MSGQ_STATE_AUXDATA2,
-              HAMMER2_MSGQ_STATE_ERROR } state;
-       size_t          fifo_beg;               /* buffered data */
-       size_t          fifo_cdx;               /* cdx-beg processed */
-       size_t          fifo_cdn;               /* end-cdn unprocessed */
-       size_t          fifo_end;
-       size_t          hbytes;                 /* header size */
-       size_t          abytes;                 /* aux_data size */
-       int             error;
-       int             seq;                    /* salt sequencer */
-       int             msgcount;
-       EVP_CIPHER_CTX  ctx;
-       char            iv[HAMMER2_MAX_IV_SIZE]; /* encrypt or decrypt iv[] */
-       hammer2_msg_t   *msg;
-       hammer2_msg_queue_t msgq;
-       char            buf[DMSG_BUF_SIZE];     /* staging buffer */
-};
-
-typedef struct hammer2_ioq hammer2_ioq_t;
-
-#define HAMMER2_IOQ_ERROR_SYNC         1       /* bad magic / out of sync */
-#define HAMMER2_IOQ_ERROR_EOF          2       /* unexpected EOF */
-#define HAMMER2_IOQ_ERROR_SOCK         3       /* read() error on socket */
-#define HAMMER2_IOQ_ERROR_FIELD                4       /* invalid field */
-#define HAMMER2_IOQ_ERROR_HCRC         5       /* core header crc bad */
-#define HAMMER2_IOQ_ERROR_XCRC         6       /* ext header crc bad */
-#define HAMMER2_IOQ_ERROR_ACRC         7       /* aux data crc bad */
-#define HAMMER2_IOQ_ERROR_STATE                8       /* bad state */
-#define HAMMER2_IOQ_ERROR_NOPEER       9       /* bad socket peer */
-#define HAMMER2_IOQ_ERROR_NORKEY       10      /* no remote keyfile found */
-#define HAMMER2_IOQ_ERROR_NOLKEY       11      /* no local keyfile found */
-#define HAMMER2_IOQ_ERROR_KEYXCHGFAIL  12      /* key exchange failed */
-#define HAMMER2_IOQ_ERROR_KEYFMT       13      /* key file format problem */
-#define HAMMER2_IOQ_ERROR_BADURANDOM   14      /* /dev/urandom is bad */
-#define HAMMER2_IOQ_ERROR_MSGSEQ       15      /* message sequence error */
-#define HAMMER2_IOQ_ERROR_EALREADY     16      /* ignore this message */
-#define HAMMER2_IOQ_ERROR_TRANS                17      /* state transaction issue */
-#define HAMMER2_IOQ_ERROR_IVWRAP       18      /* IVs exhaused */
-#define HAMMER2_IOQ_ERROR_MACFAIL      19      /* MAC of encryption algorithm failed */
-#define HAMMER2_IOQ_ERROR_ALGO         20      /* Misc. encryption algorithm error */
-
-#define HAMMER2_IOQ_MAXIOVEC    16
-
-/*
- * hammer2_router - governs the routing of a message.  Passed into
- *                 hammer2_msg_write.
- *
- * The router is either connected to an iocom (socket) directly, or
- * connected to a SPAN transaction (h2span_link structure for outgoing)
- * or to a SPAN transaction (h2span_relay structure for incoming).
- */
-struct hammer2_router {
-       RB_ENTRY(hammer2_router) rbnode;        /* indexed by target */
-       struct hammer2_iocom *iocom;
-       struct h2span_link   *link;             /* may be NULL */
-       struct h2span_relay  *relay;            /* may be NULL */
-       void    (*signal_callback)(struct hammer2_router *);
-       void    (*rcvmsg_callback)(struct hammer2_msg *);
-       void    (*altmsg_callback)(struct hammer2_iocom *);
-       struct hammer2_state_tree staterd_tree; /* active messages */
-       struct hammer2_state_tree statewr_tree; /* active messages */
-       hammer2_msg_queue_t txmsgq;             /* tx msgq from remote */
-       uint64_t        target;                 /* for routing */
-       int             flags;
-       int             refs;                   /* refs prevent destruction */
-};
-
-#define HAMMER2_ROUTER_CONNECTED       0x0001  /* on global RB tree */
-#define HAMMER2_ROUTER_DELETED         0x0002  /* parent structure destroyed */
-
-typedef struct hammer2_router hammer2_router_t;
-
-int hammer2_router_cmp(hammer2_router_t *router1, hammer2_router_t *router2);
-RB_PROTOTYPE(hammer2_router_tree, hammer2_router, rbnode, hammer2_router_cmp);
-
-/*
- * hammer2_iocom - governs a messaging stream connection
- */
-struct hammer2_iocom {
-       hammer2_ioq_t   ioq_rx;
-       hammer2_ioq_t   ioq_tx;
-       hammer2_msg_queue_t freeq;              /* free msgs hdr only */
-       hammer2_msg_queue_t freeq_aux;          /* free msgs w/aux_data */
-       int     sock_fd;                        /* comm socket or pipe */
-       int     alt_fd;                         /* thread signal, tty, etc */
-       int     wakeupfds[2];                   /* pipe wakes up iocom thread */
-       int     flags;
-       int     rxmisc;
-       int     txmisc;
-       struct hammer2_router *router;
-       pthread_mutex_t mtx;                    /* mutex for state*tree/rmsgq */
-};
-
-typedef struct hammer2_iocom hammer2_iocom_t;
-
-#define HAMMER2_IOCOMF_EOF     0x00000001      /* EOF or ERROR on desc */
-#define HAMMER2_IOCOMF_RREQ    0x00000002      /* request read-data event */
-#define HAMMER2_IOCOMF_WREQ    0x00000004      /* request write-avail event */
-#define HAMMER2_IOCOMF_RWORK   0x00000008      /* immediate work pending */
-#define HAMMER2_IOCOMF_WWORK   0x00000010      /* immediate work pending */
-#define HAMMER2_IOCOMF_PWORK   0x00000020      /* immediate work pending */
-#define HAMMER2_IOCOMF_ARWORK  0x00000040      /* immediate work pending */
-#define HAMMER2_IOCOMF_AWWORK  0x00000080      /* immediate work pending */
-#define HAMMER2_IOCOMF_SWORK   0x00000100      /* immediate work pending */
-#define HAMMER2_IOCOMF_CRYPTED 0x00000200      /* encrypt enabled */
-
-/*
- * Crypto algorithm table and related typedefs.
- */
-
-typedef int (*algo_init_fn)(hammer2_ioq_t *, char *, int, char *, int, int);
-typedef int (*algo_enc_fn)(hammer2_ioq_t *, char *, char *, int, int *);
-typedef int (*algo_dec_fn)(hammer2_ioq_t *, char *, char *, int, int *);
-
-struct crypto_algo {
-       const char      *name;
-       int             keylen;
-       int             taglen;
-       algo_init_fn    init;
-       algo_enc_fn     enc_chunk;
-       algo_dec_fn     dec_chunk;
-};
index a75e3d6..e4f2b6d 100644 (file)
@@ -139,30 +139,6 @@ hammer2_demon(void *(*func)(void *), void *arg)
        _exit(2);       /* NOT REACHED */
 }
 
-/*
- * This swaps endian for a hammer2_msg_hdr.  Note that the extended
- * header is not adjusted, just the core header.
- */
-void
-hammer2_bswap_head(dmsg_hdr_t *head)
-{
-       head->magic     = bswap16(head->magic);
-       head->reserved02 = bswap16(head->reserved02);
-       head->salt      = bswap32(head->salt);
-
-       head->msgid     = bswap64(head->msgid);
-       head->source    = bswap64(head->source);
-       head->target    = bswap64(head->target);
-
-       head->cmd       = bswap32(head->cmd);
-       head->aux_crc   = bswap32(head->aux_crc);
-       head->aux_bytes = bswap32(head->aux_bytes);
-       head->error     = bswap32(head->error);
-       head->aux_descr = bswap64(head->aux_descr);
-       head->reserved38= bswap32(head->reserved38);
-       head->hdr_crc   = bswap32(head->hdr_crc);
-}
-
 const char *
 hammer2_time64_to_str(uint64_t htime64, char **strp)
 {
@@ -269,6 +245,7 @@ sizetostr(hammer2_off_t size)
        return(buf);
 }
 
+#if 0
 /*
  * Allocation wrappers give us shims for possible future use
  */
@@ -289,47 +266,4 @@ hammer2_free(void *ptr)
        free(ptr);
 }
 
-int
-hammer2_connect(const char *hostname)
-{
-       struct sockaddr_in lsin;
-       struct hostent *hen;
-       int fd;
-
-       /*
-        * Acquire socket and set options
-        */
-       if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-               fprintf(stderr, "cmd_debug: socket(): %s\n",
-                       strerror(errno));
-               return -1;
-       }
-
-       /*
-        * Connect to the target
-        */
-       bzero(&lsin, sizeof(lsin));
-       lsin.sin_family = AF_INET;
-       lsin.sin_addr.s_addr = 0;
-       lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
-
-       if (hostname) {
-               hen = gethostbyname2(hostname, AF_INET);
-               if (hen == NULL) {
-                       if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
-                               fprintf(stderr,
-                                       "Cannot resolve %s\n", hostname);
-                               return -1;
-                       }
-               } else {
-                       bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
-               }
-       }
-       if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
-               close(fd);
-               fprintf(stderr, "debug: connect failed: %s\n",
-                       strerror(errno));
-               return -1;
-       }
-       return (fd);
-}
+#endif
index c3dff0c..ac7e35f 100644 (file)
@@ -31,6 +31,7 @@ LIBDEVMAPPER?=        ${DESTDIR}${LIBDIR}/libdevmapper.a
 LIBDEVSTAT?=   ${DESTDIR}${LIBDIR}/libdevstat.a
 LIBDIALOG?=    ${DESTDIR}${LIBDIR}/libdialog.a
 LIBDM?=                ${DESTDIR}${LIBDIR}/libdm.a
+LIBDMSG?=      ${DESTDIR}${LIBDIR}/libdmsg.a
 LIBEDIT?=      ${DESTDIR}${LIBDIR}/libedit.a
 LIBEVTR?=      ${DESTDIR}${LIBDIR}/libevtr.a
 LIBFETCH?=     ${DESTDIR}${LIBDIR}/libfetch.a
index f283183..e763a51 100644 (file)
@@ -396,7 +396,7 @@ struct dmsg_lnk_auth {
  * left empty (zero-fill) if not supported by a particular peer.
  *
  * DMSG_PEER_CLUSTER           filter: none
- * DMSG_PEER_DISK              filter: label
+ * DMSG_PEER_BLOCK             filter: label
  * DMSG_PEER_HAMMER2           filter: pfs_clid if not empty, and label
  */
 struct dmsg_lnk_conn {
@@ -417,6 +417,11 @@ struct dmsg_lnk_conn {
 
 typedef struct dmsg_lnk_conn dmsg_lnk_conn_t;
 
+#define DMSG_PEER_NONE         0
+#define DMSG_PEER_CLUSTER      1       /* a cluster controller */
+#define DMSG_PEER_BLOCK                2       /* block devices */
+#define DMSG_PEER_HAMMER2      3       /* hammer2-mounted volumes */
+
 /*
  * LNK_SPAN - Relay a SPAN (transaction, left open)
  *
@@ -531,6 +536,8 @@ typedef struct dmsg_vol_data dmsg_vol_data_t;
 #define DMSG_VOLF_CONN_EF      0x40    /* media errors flagged */
 #define DMSG_VOLF_CONN_PRI     0x0F    /* select priority 0-15 (15=best) */
 
+#define DMSG_COPYID_COUNT      256     /* WARNING! embedded in hammer2 vol */
+
 struct dmsg_lnk_volconf {
        dmsg_hdr_t              head;
        dmsg_vol_data_t         copy;   /* copy spec */
index b9cb410..383e542 100644 (file)
@@ -447,6 +447,9 @@ extern long hammer2_ioa_volu_write;
 /*
  * hammer2_subr.c
  */
+uint32_t hammer2_icrc32(const void *buf, size_t size);
+uint32_t hammer2_icrc32c(const void *buf, size_t size, uint32_t crc);
+
 void hammer2_inode_lock_ex(hammer2_inode_t *ip);
 void hammer2_inode_unlock_ex(hammer2_inode_t *ip);
 void hammer2_inode_lock_sh(hammer2_inode_t *ip);
index 4ff3921..6e8e049 100644 (file)
@@ -514,10 +514,12 @@ typedef struct hammer2_inode_data hammer2_inode_data_t;
  * PEER types identify connections and help cluster controller filter
  * out unwanted SPANs.
  */
-#define HAMMER2_PEER_NONE              0
-#define HAMMER2_PEER_CLUSTER           1       /* a cluster controller */
-#define HAMMER2_PEER_BLOCK             2       /* block devices */
-#define HAMMER2_PEER_HAMMER2           3       /* hammer2-mounted volumes */
+#define HAMMER2_PEER_NONE              DMSG_PEER_NONE
+#define HAMMER2_PEER_CLUSTER           DMSG_PEER_CLUSTER
+#define HAMMER2_PEER_BLOCK             DMSG_PEER_BLOCK
+#define HAMMER2_PEER_HAMMER2           DMSG_PEER_HAMMER2
+
+#define HAMMER2_COPYID_COUNT           DMSG_COPYID_COUNT
 
 /*
  * PFS types identify a PFS on media and in LNK_SPAN messages.
@@ -653,8 +655,6 @@ typedef struct hammer2_allocref hammer2_allocref_t;
 #define HAMMER2_VOLUME_ID_HBO  0x48414d3205172011LLU
 #define HAMMER2_VOLUME_ID_ABO  0x11201705324d4148LLU
 
-#define HAMMER2_COPYID_COUNT   256
-
 struct hammer2_volume_data {
        /*
         * sector #0 - 512 bytes
@@ -800,11 +800,4 @@ union hammer2_media_data {
 
 typedef union hammer2_media_data hammer2_media_data_t;
 
-/*
- * Prototypes for user & kernel functions.  Kernel-only prototypes are
- * elsewhere.
- */
-uint32_t hammer2_icrc32(const void *buf, size_t size);
-uint32_t hammer2_icrc32c(const void *buf, size_t size, uint32_t crc);
-
 #endif
index d18a866..b0fdd39 100644 (file)
  | $Id: isc_subr.c 560 2009-05-07 07:37:49Z danny $
  */
 
-#include <sys/types.h>
-#include <sys/uuid.h>
-
-#include "hammer2_disk.h"
+#include "hammer2.h"
 
 /*****************************************************************/
 /*                                                               */
index d10ae8b..23a1692 100644 (file)
@@ -53,6 +53,4 @@ struct hammer2_mount_info {
 
 #define HMNT2_USERFLAGS                (HMNT2_NOAUTOSNAP)
 
-#define HAMMER2_LISTEN_PORT    987
-
 #endif