construct multicast group using hash of channel
authorEugene Crosser <evgenii.cherkashin@cloud.ionos.com>
Fri, 15 Mar 2019 15:17:16 +0000 (16:17 +0100)
committerEugene Crosser <evgenii.cherkashin@cloud.ionos.com>
Fri, 15 Mar 2019 15:28:51 +0000 (16:28 +0100)
Signed-off-by: Eugene Crosser <evgenii.cherkashin@profitbricks.com>
src/psmb_socket.c

index 06b90a83f66f28674c0d0a3285bb1f4a2228affb..d18ad57c506327fbfb03078fdc6fd1b1d44ae07c 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -8,6 +9,7 @@
 
 #include <psmb.h>
 #include "psmb_priv.h"
+#include "hash64.h"
 
 static void dummy_log(void *log_priv, int priority, const char *format, ...) {}
 
@@ -31,8 +33,8 @@ psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size),
                .fd = -1,
                .malloc = malloc, .free = free, .realloc = realloc,
                .logf = dummy_log,
-               .prefix = (struct in6_addr){{{  0xff, 0xff, 0x01, 0x05,
-                                               0xb0, 0x55, 0xff, 0xe7,
+               .prefix = (struct in6_addr){{{  0xff, 0x15,  'P',  'S',
+                                                'M',  'B',  '0',  '1',
                                                0x00, 0x00, 0x00, 0x00,
                                                0x00, 0x00, 0x00, 0x00 }}},
                .prefixlen = 64,
@@ -83,6 +85,12 @@ psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port)
 psmb_result_t psmb_set_mgrp(psmb_ctx_t *ctx, struct in6_addr prefix,
        unsigned char prefixlen)
 {
+       if (prefixlen > 128) {
+               LOG(ctx, LOG_ERR, "psmb_set_mgrp() prefixlen %d is too big",
+                               prefixlen);
+               errno = EINVAL;
+               return (psmb_result_t){PSMB_ERROR};
+       }
        if (ctx->fd == -1) {
                ctx->prefix = prefix;
                ctx->prefixlen = prefixlen;
@@ -145,19 +153,37 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx)
        return (psmb_result_t){PSMB_OK};
 }
 
+static struct in6_addr multiaddr(struct in6_addr prefix,
+               unsigned char prefixlen, uint64_t suffix)
+{
+       struct in6_addr result = prefix;
+       unsigned char len = prefixlen > 64 ? prefixlen : 64;
+       uint64_t mask = len == 64 ? ~(uint64_t)0 : ((uint64_t)1 << len) - 1;
+
+       *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) &= ~mask;
+       *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) |= (suffix & mask);
+       return result;
+}
+
 static psmb_result_t psmb_sub_unsub(psmb_ctx_t *ctx, char *channel, int option)
 {
        struct ipv6_mreq mreq = { 0 };
+       char mgrp_str[INET6_ADDRSTRLEN+1];
 
        if (ctx->fd == -1) {
                LOG(ctx, LOG_ERR, "subscribe: psmb is not open");
                errno = EINVAL;
                return (psmb_result_t){PSMB_ERROR};
        }
-       mreq.ipv6mr_multiaddr = ctx->prefix; /* use hash of the channel */
+       mreq.ipv6mr_multiaddr = multiaddr(ctx->prefix, ctx->prefixlen,
+                       hash64(channel, strlen(channel)));
+       (void)inet_ntop(AF_INET6, &mreq.ipv6mr_multiaddr,
+                       mgrp_str, sizeof(mgrp_str));
+       LOG(ctx, LOG_DEBUG, "using multiaddr %s for channel \"%s\"",
+                       mgrp_str, channel);
        mreq.ipv6mr_interface = 0; /* how to use this??? */
        if (setsockopt(ctx->fd, IPPROTO_IPV6, option,
-               (void *)&mreq, sizeof(mreq)) == -1) {
+                       (void *)&mreq, sizeof(mreq)) == -1) {
                int sverr = errno;
                LOG(ctx, LOG_ERR, "add_membership(): %m");
                errno = sverr;